Reputation: 53097
I am playing around in Parsec with an unfinished parser for a Haskell-like language.
It seems to be working correctly, although I am not happy with the error message.
"foo (bar"
expecting letter or digit, operand or ")"
How can I get it to only print expecting operand or ")"
? I have tried adding <?>
but cannot get it to work.
Here is my code:
separator = skipMany1 space
<?> ""
identifier :: Parser String
identifier = (:) <$> letter <*> many alphaNum
<?> "identifier"
number :: Parser String
number = many1 digit
<?> "numeric literal"
primitiveExpr :: Parser String
primitiveExpr = (identifier
<|> number)
<?> "primitive expression"
expr :: Parser ()
expr = do identifier
spaces <?> ""
sepBy operand separator
return ()
parenExpr :: Parser String
parenExpr = do char '('
expr
char ')'
return "foo"
<?> "parenthesized expression"
operand = parenExpr <|> primitiveExpr
<?> "operand"
Upvotes: 4
Views: 385
Reputation: 53097
I figured out how to get the desired behavior. It was caused by alphaNum
:
identifier = (:) <$> letter <*> (many alphaNum <?> "")
<?> "identifier"
Since "bar"
could continue to be parsed as an identifier.
Upvotes: 3