Search code examples
haskellparsec

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.

  • Input:"foo (bar"
  • Error: 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"

Solution

  • 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.