Gros deplacement vers Parser.Advanced dans l'espoir de débusquer les problèmes

pull/1/head
Jean-Christophe Jameux 3 years ago
parent 4410ab1d31
commit ac4a5db2c9
  1. 248
      src/GenerateurH5P.elm

@ -12,7 +12,7 @@ import Html exposing (Attribute, Html, button, div, iframe, input, p, section, t
import Json.Decode as D import Json.Decode as D
import Json.Encode as E import Json.Encode as E
import List as L import List as L
import Parser as P exposing (..) import Parser.Advanced as P exposing (..)
import Random import Random
import Random.Extra import Random.Extra
import Random.List import Random.List
@ -71,9 +71,9 @@ update msg model =
StructureDuContenu nouvelleStructure -> StructureDuContenu nouvelleStructure ->
let let
f strCtn = f strCtn =
case P.run h5pParser strCtn of case P.run h5psParser strCtn of
Ok ctn -> Ok ctn ->
h5pEncode 2 ctn S.join "\n\n" <| L.map (h5pEncode 2) ctn
Err erreurs -> Err erreurs ->
deadEndsToStringBis erreurs deadEndsToStringBis erreurs
@ -88,9 +88,9 @@ update msg model =
GenererContenu -> GenererContenu ->
let let
f strCtn = f strCtn =
case P.run h5pParser strCtn of case P.run h5psParser strCtn of
Ok ctn -> Ok ctn ->
h5pEncode 0 ctn S.join "\n\n" <| L.map (h5pEncode 0) ctn
Err erreurs -> Err erreurs ->
deadEndsToStringBis erreurs deadEndsToStringBis erreurs
@ -1462,29 +1462,114 @@ withContent cntnt record =
-} -}
h5psParser = type alias H5Parser a =
sequence Parser Context Problem a
{ start = ""
, separator = ""
, end = "" type Context
, spaces = blankLines = Preamble
, item = h5pParser | BranchingScenarioContext
, trailing = Optional | BranchingQuestionContext
} | BranchingQuestionAlternativeContext
| CoursePresentationContext
| TrueFalseContext
h5pParser : Parser (H5P bSC cPC) h5psParser =
h5pParser =
oneOf oneOf
[ backtrackable <| branchingScenarioParser 1 [ succeed identity
, backtrackable <| coursePresentationParser 1 |. preambleParser
, trueFalseParser 1 |= loop [] h5psParserHelp
, problem NoContent
] ]
preambleParser =
inContext Preamble <|
whileNoStarOnFirstColumn
whileNoStarOnFirstColumn =
succeed identity
|. chompWhile ((/=) '*')
|= getCol
|> andThen
(\col ->
if col > 1 then
succeed ()
|. symbol (Token "*" EndOfFile)
|. whileNoStarOnFirstColumn
else
oneOf
[ succeed ()
|. end NoContent
-- Pourquoi ça ne marche pas ???
|. problem NoContent
, succeed ()
]
)
h5psParserHelp h5ps =
countStars
|> andThen
(\profondeur ->
if profondeur == 0 then
h5ps
|> L.reverse
|> Done
|> succeed
else
succeed (\h5p -> Loop <| h5p :: h5ps)
|= h5pParser profondeur
)
-- h5pParser : Int -> H5Parser (H5P bSC cPC)
h5pParser profondeur =
succeed identity
|. espaces
|= variable
{ start = Char.isUpper
, inner = Char.isAlphaNum
, reserved = Set.fromList []
, expecting = ExpectingH5PcontentType
}
|. espaces
|> andThen
(\contentType ->
case contentType of
"BranchingScenario" ->
inContext BranchingScenarioContext <|
branchingScenarioParser profondeur
"CoursePresentation" ->
inContext BranchingScenarioContext <|
coursePresentationParser profondeur
"TrueFalse" ->
inContext BranchingScenarioContext <|
trueFalseParser profondeur
_ ->
problem <| UnknownH5PcontentType contentType
)
countStars : H5Parser Int
countStars =
succeed S.length
|= getChompedString (chompWhile ((==) '*'))
stars profondeur = stars profondeur =
succeed () succeed ()
|. symbol (S.repeat profondeur "*") |. symbol (Token (S.repeat profondeur "*") GenericProblem)
|. espaces |. espaces
@ -1523,7 +1608,7 @@ blankLines =
== '\u{000D}' == '\u{000D}'
) )
|. oneOf |. oneOf
[ end [ end GenericProblem
, getPosition , getPosition
|> andThen |> andThen
(\( row, col ) -> (\( row, col ) ->
@ -1531,7 +1616,7 @@ blankLines =
succeed () succeed ()
else else
problem problemBis
("N'y aurait-il pas des espaces en trop au début de la ligne ?" ("N'y aurait-il pas des espaces en trop au début de la ligne ?"
++ String.fromInt row ++ String.fromInt row
) )
@ -1543,6 +1628,14 @@ espaces =
chompWhile <| \x -> x == ' ' || x == '\t' chompWhile <| \x -> x == ' ' || x == '\t'
keywordBis x =
keyword <| Token x GenericProblem
problemBis x =
problem <| Problem x
{- {-
@ -1571,9 +1664,6 @@ branchingScenarioParser profondeur =
} }
in in
succeed f succeed f
|. stars profondeur
|. keyword "BranchingScenario"
|. espaces
|= titleParser |= titleParser
|. blankLines |. blankLines
|= loop ( [], 0 ) (branchingScenarioParserHelp (profondeur + 1)) |= loop ( [], 0 ) (branchingScenarioParserHelp (profondeur + 1))
@ -1615,7 +1705,7 @@ contentParser profondeur contentId =
, trueFalseParser profondeur , trueFalseParser profondeur
] ]
--, problem "Oups" --, problemBis "Oups"
] ]
@ -1655,7 +1745,7 @@ branchingQuestionParser profondeur contentId =
in in
succeed f succeed f
|. stars profondeur |. stars profondeur
|. keyword "BranchingQuestion" |. keywordBis "BranchingQuestion"
|. espaces |. espaces
|= questionParser |= questionParser
|. blankLines |. blankLines
@ -1689,10 +1779,10 @@ branchingQuestionAlternativeParser profondeur ( branchList, contentId ) =
(\xx -> (\xx ->
case xx of case xx of
[] -> [] ->
problem "Un embranchement doit avoir des branches !" problemBis "Un embranchement doit avoir des branches !"
x :: [] -> x :: [] ->
problem "Un embranchement doit avoir au moins deux branches !" problemBis "Un embranchement doit avoir au moins deux branches !"
x :: y :: zz -> x :: y :: zz ->
succeed () succeed ()
@ -1733,9 +1823,10 @@ branchingQuestionAlternativeParserHelp profondeur ( branchList, contentId ) =
coursePresentationParser profondeur = coursePresentationParser profondeur =
succeed (CoursePresentationH5P nouveauCoursePresentation) inContext CoursePresentationContext <|
|. stars profondeur succeed (CoursePresentationH5P nouveauCoursePresentation)
|. keyword "CoursePresentation" |. stars profondeur
|. keywordBis "CoursePresentation"
@ -1758,7 +1849,7 @@ coursePresentationParser profondeur =
trueFalseParser profondeur = trueFalseParser profondeur =
succeed (TrueFalseH5P nouveauTrueFalse) succeed (TrueFalseH5P nouveauTrueFalse)
|. stars profondeur |. stars profondeur
|. keyword "TrueFalse" |. keywordBis "TrueFalse"
@ -1778,23 +1869,90 @@ trueFalseParser profondeur =
-} -}
type Problem
= NoContent
| BadKeyword String
| Problem String
| GenericProblem
| EndOfFile
| ExpectingH5PcontentType
| UnknownH5PcontentType String
deadEndsToStringBis errs = deadEndsToStringBis errs =
errs errs
|> List.map voirErreur |> L.map voirErreur
|> String.concat |> S.join "\n\n"
|> (++) "Il y a des problèmes aux endroits suivants :\n" |> (++) "J'ai rencontré les problèmes suivants :\n\n"
voirErreur err = voirErreur err =
"Ligne : " "Problème : "
++ showProblem err.problem
++ "Ligne : "
++ String.fromInt err.row ++ String.fromInt err.row
++ " | Colonne : " ++ " | Colonne : "
++ String.fromInt err.col ++ String.fromInt err.col
++ "\n" ++ showContext (L.map .context err.contextStack)
++ (case err.problem of ++ "\n\n-----------------------------------------\n"
Problem p ->
p
showProblem prob =
_ -> case prob of
"" Problem p ->
) p ++ "\n"
NoContent ->
"""Je n'ai aucun contenu à produire !
Y a-t-il autre chose qu'un préambule à analyser ?
Est-ce que vos * ne seraient pas trop indentées ?
"""
EndOfFile ->
"Fin de fichier\n"
ExpectingH5PcontentType ->
"""Je m'attends à trouver l'un des mots clefs suivants :
BranchingScenario
CoursePresentation
TrueFalse
"""
UnknownH5PcontentType x ->
"Contenu H5P inconnu : " ++ x ++ "\n"
_ ->
"Problème inconnu\n"
showContext ccc =
case ccc of
[] ->
""
_ ->
"\nContexte :\n" ++ showContextHelp ccc
showContextHelp ccc =
case ccc of
[] ->
"\n"
Preamble :: cc ->
"Préambule"
BranchingScenarioContext :: cc ->
"BranchingScenario > " ++ showContextHelp cc
BranchingQuestionContext :: cc ->
"BranchingQuestion > " ++ showContextHelp cc
BranchingQuestionAlternativeContext :: cc ->
"Alternative > " ++ showContextHelp cc
CoursePresentationContext :: cc ->
"CoursePresentation > " ++ showContextHelp cc
TrueFalseContext :: cc ->
"TrueFalse"

Loading…
Cancel
Save