From fff20406d719e4309e7358599dfa4766ba203f1f Mon Sep 17 00:00:00 2001 From: Jean-Christophe Jameux Date: Mon, 1 Nov 2021 18:39:11 +0100 Subject: [PATCH] =?UTF-8?q?Parser=20jouet=20totalement=20op=C3=A9rationnel?= =?UTF-8?q?=20!!!!!!!!!!!!!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- elm-stuff/0.19.1/ParserTest.elmi | Bin 2133 -> 2101 bytes elm-stuff/0.19.1/ParserTest.elmo | Bin 5294 -> 6655 bytes elm-stuff/0.19.1/d.dat | Bin 2144 -> 2144 bytes src/ParserTest.elm | 244 ++++++------------------------- 4 files changed, 46 insertions(+), 198 deletions(-) diff --git a/elm-stuff/0.19.1/ParserTest.elmi b/elm-stuff/0.19.1/ParserTest.elmi index 050a764ab69244dff2afaea6f3b7cd99f6e97cd4..f7801ee95494fce19085ec1e701c3558698f2fcf 100644 GIT binary patch delta 222 zcmcaAuvK7!7$e_A@tYG>RVIEkoqUEd3`ofdr=%vPxaOr4hvWyB6lLb6J7pF#Kma3S z;=~W4oJ>$I(_{lS>B$9b(#$}K$?KUVHZNh4W@h6m%g-!wocP#z@>(_tX#p^=D5)s5 wI3Tg8I2FmP$=6tQu^R?*4p6f^*eKVcqSVqNgcBHW+CTXy+Y?5{$(kHG0Mx-Z_y7O^ delta 297 zcmdlga8+P}7$f&Y@tceclM~rZCw@?o;ABqC$z>}@EGkYdVhaFMEWstI1t58_nyfH_ zYBnb_Ni&P{mE~s^ITj@qrGkxNfP%?CSao$-F>PUYNli>~%}ar)V`PG|m?sM`NvpCK z6y+!7q~^_j$hhH-M}005smP-_4H diff --git a/elm-stuff/0.19.1/ParserTest.elmo b/elm-stuff/0.19.1/ParserTest.elmo index 8e28bedf7dbef3efed12bef71cdb9368db208c17..5fedd6a193d8f2615e5326d7dd91b151e7354a17 100644 GIT binary patch delta 1348 zcmaJ=&ubGw6yDj%Y?5t~Hci^prqz`Oq{ULT;Gv-XMGghgw(-ZMAluEfE+ji~CTSG} z?-kVY#G@b{EC^nTcf~)T7f)X7|KXudb~ee@CS6$O&3iN7``-87e4RX*eY=V>`)f~N z0hN$`_zqn~D89co3P*iWNML8uu=ysZBCVN%HN>L7Nowjtq#2YkyybeZ7|BAJ*zvSx zY_f(^p}r@t7hr+(1C1QOYkZ;U%Ms?$ds{7#L}2KUB(pHD&!Q1nBiR`B%ESl$jCZ3% zVZui=48O@Z%#$=x(ImM6d3{3ukKn1X?dl9 zl{Is)6ZbyzTSQ0QCk3$c)0KFlV@ z^z=ZJnd+=xx9~MArLHuG;AM->rwzX0S=N+Vx)~u~FY}cD)TWJV$uG2-4(yQY924H8 zZu+Bp9F5544$2zOme(x2(EkC?_+qHSwN$7nO1GH9sx>C)dLXP0?PkKCj0vyIOYlCG zjo45U(b&VTW2p!u5|vXa z$4zM3U?QA5C`i-rIndX`>a-BM!>Fh(bH%>yB!`@H%-OCyGP#trURcYl!aV<0xRdI6^|>XrL?gnu6jU6AyP_&30-#JTv5#Q!ZG7ciDhD=@tsbB++Y1BGMno?^eH7pvb6Nj+5 zcDjniTI2X)N7@|Rn1xid7p(65L+_Qe5QTAP9Vp{Z z!S|^+9$yT>wT!fLQivnqGgr>W5`5BY=_x)+XG(J*89n7M+`+x6=g z(1IYvNv8a*J?UMG9htb^A5xTX?NF_^r^QtNGVEU(ILhSVSx?RedeN5^n#4RRBoj(J Ico`i32K9NyROIDm1r`Su0Qj;H A1ONa4 delta 53 zcmaDL@IYXKKI4v!2ENRK4GX5c&)fcDO#}l32u_~J6f*e{vnl5msK}1Z3M>vR0ROEJ A7XSbN diff --git a/src/ParserTest.elm b/src/ParserTest.elm index fe47b8b..394741d 100644 --- a/src/ParserTest.elm +++ b/src/ParserTest.elm @@ -1,11 +1,5 @@ module ParserTest exposing (..) --- Press buttons to increment and decrement a counter. --- --- Read how it works: --- https://guide.elm-lang.org/architecture/buttons.html --- - import Parser exposing (..) import Browser import Html exposing (Html, button, div, text) @@ -62,8 +56,7 @@ view model = div [] [ text texte ] -texte = voirArbreParse <| run arbre "*\n *" -{-- +texte = voirArbresParses <| run (withIndent -1 arbres) -- permet d'avoir... """ * * @@ -76,12 +69,11 @@ texte = voirArbreParse <| run arbre "*\n *" * * * - * +* <- cet arbre * * * - """ ---} +""" unArbre = Arbre @@ -91,33 +83,20 @@ unArbre = ] ] -voirArbreParse arbreParsePotentiel = - case arbreParsePotentiel of - Err erreurs -> deadEndsToString erreurs - Ok arbreParse -> voirArbre arbreParse - -{-- -parser = - succeed identity - |. mangeDesEspaces 2 - |= ( - chompUntil "\n" - |> getChompedString - ) - -mangeDesEspaces ind = - token <| String.repeat ind " " - - -mangeDesEspacesBis = - let - ind = - case getIndent of - Ok indent -> indent - Err _ -> 0 - in - token <| String.repeat ind " " ---} +voirArbresParses arbresParsesPotentiels = + case arbresParsesPotentiels of + Err erreurs -> deadEndsToStringBis erreurs + Ok arbresParses -> voirArbres arbresParses + +deadEndsToStringBis errs = + errs + |> List.map voirErreur + |> String.concat + |> (++) "Il y a des problèmes aux endroits suivants :\n" + +voirErreur err = + "Ligne : " ++ String.fromInt err.row + ++ " | Colonne : " ++ String.fromInt err.col type Arbre = Arbre (List Arbre) @@ -126,62 +105,8 @@ voirArbre arbr = Arbre [] -> "[]" Arbre arbrs -> "[" ++ String.concat (List.map voirArbre arbrs) ++ "]" -{-- -parserArbre = parserArbreInd 0 - -parserArbreInd ind = - succeed Arbre - |. token <| String.repeat ind " " - | - |. symbol "*" - |= oneOF [fin, sousArbre] - |. token "\n" - |. spaces - |= - -type Ligne = - LigneVide - | Ligne Indentation - -type alias Indentation = - { indentation : Int - , ligne : String - } - -parserArborescence chaine = - chaine - |> String.lines - |> List.map profondeur - |> construireArbre - -construireArbre : List Ligne -> Result Arbre -construireArbre lgns = - case lgns of - [] -> Ok <| Arbre [] - LigneVide :: lgnss -> Ok <| construireArbre lgnss - Ligne ind :: lgnss -> - construireArbreBis ind.indentation ( Arbre [] ) lgnss - -construireArbreBis prfdr arbrs lgns = - case - -profondeur : String -> Ligne -profondeur ligne = - Indentation 0 ligne - |> profondeurBis - -profondeurBis ind = - if String.isEmpty ind.ligne then - LigneVide - else if String.startsWith " " then - { ind - | indentation = ind.indentation + 1 - , ligne = String.dropLeft 1 ind.ligne - } - else - ind ---} - +voirArbres = + List.map voirArbre >> String.concat {-| Ce parser change l'indentation courante, cré un arbre puis y intègre ses branches grâce à une boucle @@ -193,115 +118,38 @@ arbre = flip withIndent <| succeed Arbre |. symbol "*" - |= loop [] arbres -- (Etat ind []) remplacé par [] + |= arbres in getCol |> andThen suite flip f a b = f b a - -type alias Etat = - { indentation : Int - , arborescence : List Arbre - } - -arbres arbrs = +arbres = let - suite col_ind = - if Tuple.first col_ind > Tuple.second col_ind then -- if col > ind - succeed ( \arbr -> Loop (arbr :: arbrs) ) - |= lazy (\_ -> arbre) - else - succeed () - |> map (\_ -> Done (List.reverse arbrs)) + sousArbres arbrs = + let + boucle = + succeed ( \arbr -> Loop (arbr :: arbrs) ) + |= lazy (\_ -> arbre) + fin = + map (\_ -> Done (List.reverse arbrs)) + suite col_ind = + oneOf + [ succeed () + |. end + |> fin + , if Tuple.first col_ind > Tuple.second col_ind then -- if col > ind + boucle + else + succeed () + |> fin + ] + in + succeed Tuple.pair + |. spaces + |= getCol + |= getIndent + |> andThen suite in - succeed Tuple.pair - |. spaces - |= getCol - |= getIndent - |> andThen suite - {-- - oneOf - [ succeed ( \arbr -> Loop { etat | arborescence = arbr :: etat.arborescence } ) - |= arbresBis - |. spaces - , succeed () - |> map (\_ -> Done (List.reverse etat.arborescence)) - ] - --} - -{-- -arbresBis typeInd = - case typeInd of - MemeIndentation -> - succeed ( \arbr -> Loop { etat | arborescence = arbr :: etat.arborescence } ) - |. symbol "*" - |. spaces - |= regarderIndentation - -type Indentation = - MemeIndentation - | MoinsIndentee - | PlusIndentee - -regarderIndentation : Parser Indentation -regarderIndentation = - succeed pair - |= getCol - |= getIndent - |> andThen regarderIndentationBis - -regarderIndentationBis col_ind = - let - col = first col_ind - ind = second col_ind - if col == ind then MemeIndentation - else if col < ind then MoinsIndentee - else PlusIndentee - -arbresBis : Parser Arbre -arbresBis = - memeIndentation - |> andThen arbresTer - -arbresTer : Bool -> Parser Arbre -arbresTer mmInd = - if mmInd then - succeed Arbre - else - problem "expecting more spaces" - -plusIndentee : Parser Bool -plusIndentee = - succeed (>=) - |= getCol - |= getIndent - -moinsIndentee : Parser Bool -moinsIndentee = - succeed (<=) - |= getCol - |= getIndent - -parser = - succeed (x -> Arbre ) - |= oneOf - [ getChompedString <| chompUntil "\n" - , succeed "" - ] - |. token "\n" - |. spaces - |= changerIndentation parser - -parserBis = - getChompedString <| chompUntil "\n" - |. token "\n" - |. spaces - |= changerIndentation parser - - -changerIndentation prsr - succeed (col -> withIndent col prsr) - |= getCol ---} + loop [] sousArbres \ No newline at end of file