In Parsec, fixing a broken token before collecting the parser

Background

I am using a combination of Alex and Parsec to analyze an indentation sensitive language. The line that is indented higher and then above it is either the data for that line, or the continuation of commands on that line. Alex has no way of knowing for sure, so he just returns the token Block{...}

.

The parser knows when a block of data is expected and passes the data to the Abstract Syntax Tree for use by the interpreter.

The problem arises when the parser encounters a token Block

when it needs something else. I have a function that converts a block marker to a list Tokens

called lex_block

, but I can't seem to apply it at the right place in the input stream.

Question:

Given a parser combinator that doesn't work on some token in the input stream, is there a way to change the token that caused the crash and continue parsing the new input stream, or at least try again?

I would like to write a function retry

like this:

> retry :: (s -> Maybe s) -> ParsecT s u m a -> ParsecT s u m a

      

retry f p

acts like a parser p

, except that if p

not, the function is f

used to modify the input stream at the point of p

failure and p

retry. When it f

returns Nothing

, the failure is transmitted.

I tried to write this with getInput

, setInput

and try

, but I can't figure out how to get the input stream at the point where the parser was combined, not the point at which it was started. I think that if I could find a way to find out where the failure occurred on the input stream, I could make this work.

+3


source to share





All Articles