I'm using Parsec to parse some expressions (see this question for more context), and most relevant part of my code is:
statement :: Parser Stmt
statement = assignStmt <|> simpleStmt
assignStmt :: Parser Stmt
assignStmt =
do var <- identifier
reservedOp "="
expr <- expression
return $ Assign var expr
simpleStmt :: Parser Stmt
simpleStmt =
do expr <- expression
return $ Simple expr
In action:
boobla> foo = 100 + ~100
167
boobla> foo
parser error: (line 1, column 4):
unexpected end of input
expecting letter or digit or "="
Second expression should have evaluated to 167
, value of foo
.
I thought that when Parsec would try to extract token reservedOp "="
, it should have failed because there is no such token in the string, then it was to try second function simpleStmt
and succeed with it. But it works differently: it expects more input and just throws this exception.
What should I use to make assignStmt
fail if there is no more characters in the string (or in current line). foo = 10
should be parsed with assignStmt
and foo
should be parsed with simpleStmt
.
try assignStmt <|> simpleStmt
. Look up the documentation fortry
. - MathematicalOrchid