I'm taking a course on compilers at my university. I'm choosing to do the project using Haskell + Parsec. The lexer and parser are required to be separate. I'm using Parsec to convert a string into a list of tokens, which will then be passed onto another Parsec parser that converts a list of tokens into an AST.
The problem is that the lexer is supposed to continue attempting to lex, even in the case of an error. To try to do this, I introduced a token representing an "unexpected token" to my Token data type, and I tried to sprinkle my code with <|> unexpected in order to produce this token in case of an error. This is a lot of boilerplate, and also can be difficult to reason about where to place these.
My preferred solution would be to somehow have Parsec automatically do this: If there's ever a ParseError, produce an unexpected token at that position, and continue parsing one position later. How would I do this?
Here is a snippet of some of the code I have now: http://lpaste.net/8144414997276000256 For some reason I can still get a parse error, even though the Unexpected token should be catching the non-handled cases.