I need to give a failure message to a given position in parsec.
I tried by setting the position before giving an unexpected error message, but it didn't work:
runParser ( do pos0 <- getPosition
id <- many1 alphaNum
if (id == reverse id) then return id
else setPosition pos0 >> unexpected id
eof )
() "" "abccbb"
Gives back
Left (line 1, column 7):
unexpected end of input
expecting letter or digit
While the correct response is:
unexpected abccbb
expecting letter or digit
It can be produced (with a wrong position), by omitting setPosition pos0 >>
from the code.
My workaround is to do the parsing, save the correct and the actual error position in the user state of parsec, and correct the error position, but I would like a better solution.
As it was asked by AndrewC, it is part of giving error messages with more information to our users. For example, in some places we want special identifiers, but if it was encoded in the parser, parsec would given an error message like "expected a g, got an r, position is in the middle of an identifier". The correct message would be, "identifier expected in the special format, but got 'abccbb', position is before the identifier". If there is a better approach that can be used to give error messages like this, it would be a correct answer to our question. But I 'm also curious about why parsec behaves like that, and why cannot I raise a custom error message , pointing to the position I want to.
if all alphaNum xs then...
. – AndrewC