Pubby
Pubby

Reputation: 53097

How can I remove "expecting letter or digit" from this Parsec error?

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.

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

Answers (1)

Pubby
Pubby

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

Related Questions