I’m trying to learn parser library and stuck. It’s not working for single number (for example “3”), but I think it should because of
expression : Parser Expression
expression =
oneOf
[ addExpr
, intExpr
]
after addExpr failed it should run intExpr. What do I do wrong? Here is whole code
module TokenParser exposing (..)
import Parser exposing (..)
-- EXPRESSIONS
type AST
= Statement Expression
type Expression
= IntValue Int
| AddExpr Expression Expression
newLines =
chompWhile (\c -> c == '\n')
addExpr : Parser Expression
addExpr =
succeed
AddExpr
|= intExpr
|. symbol "+"
|= intExpr
intExpr =
succeed IntValue
|= int
expression : Parser Expression
expression =
oneOf
[ addExpr
, intExpr
]
statement : Parser AST
statement =
map Statement expression
statements : Parser (List AST)
statements =
loop [] statementsHelp
statementsHelp : List AST -> Parser (Step (List AST) (List AST))
statementsHelp revStmts =
oneOf
[ succeed (\stmt -> Loop (stmt :: revStmts))
|= statement
|. newLines
, succeed ()
|. end
|> map (\_ -> Done (List.reverse revStmts))
]
parse : String -> Result (List DeadEnd) (List AST)
parse string =
run statements string