attoparsec: succeeding on part of the input instead of failing

221 views Asked by At

I have an attoparsec parser, and tests for it, what annoys me is that if I comment part of the parser and run the tests, the parser doesn't return Left "parse error at line ..." but instead I get Right [].

Note that I'm using parseOnly to make it clear that there'll be no more input.

Otherwise it's nice to get the partially parsed input, it can definitely be useful and I'm glad to have it. However I'd like to be informed that the whole input was not consumed. Maybe to get a character offset of the last consumed letter, or if that's what it takes, at least an option to be returned Left.

If it's relevant, the parser can be found there.

If I comment for instance the line:

            <|> PlainText <$> choice (string <$> ["[", "]", "*", "`"])

And run the tests, I get for instance:

  1) notes parsing tests parses notes properly
       simple test
       expected: Right [NormalLine [PlainText "one line* # hello world"]]
        but got: Right []

This is from that test.

1

There are 1 answers

1
Petr On BEST ANSWER

Depending on if consuming the whole input should be the property of parseNoteDocument or just the tests, I'd extend one or the other with endOfInput or atEnd.

I'd suggest to define a proper Parser for your documents, like

parseNoteDocument' :: Text -> Parsec NoteDocument
parseNoteDocument' = many parseLine

and then define parseNoteDocument in terms of it. Then you can use parseNoteDocument' in the tests by defining a helper that parses a given piece of text using

parseNoteDocument' <* endOfInput

to ensure that the whole input is consumed.