How to remove the "pending letter or number" from this Parsec error?

I play Parsec with an incomplete parser for a language like Haskell.

It seems to work correctly, although I am not happy with the error message.

  • Input: "foo (bar"
  • Error: expecting letter or digit, operand or ")"

How can I make it print only expecting operand or ")" ? I tried adding <?> , But can't 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" 
+4
source share
1 answer

I figured out how to achieve the desired behavior. This was caused by alphaNum :

 identifier = (:) <$> letter <*> (many alphaNum <?> "") <?> "identifier" 

Since "bar" can continue to be parsed as an identifier.

+3
source

Source: https://habr.com/ru/post/1394528/


All Articles