Get left input Parsec

I was wondering if there is a way to get the remaining input from Parsec after it stops parsing, or if it was a successful or unsuccessful syntax, maybe this signature:

parseRemaining :: Stream s Identity t => Parsec s () a -> SourceName -> s -> (s, Either ParseError a) 

Where we get instead of Either ParseError a , we additionally get the remaining Stream s

+5
source share
3 answers

Take a look at Megaparsec - the modern Parsec fork, starting with version 4.2.0, allows user state at the beginning of parsing and retrieving the state of the parser at the end (it does not matter if the parser succeeds or fails). This allows you to partially parse input, resume parsing, indicate a non-standard source text position, etc. runParser' as well as runParserT' .


Disclosure: I am one of the authors of Megaparsec.

0
source

You can use getInput , which is a parser that returns the remaining input.

+1
source

I dug a bit in the interior of Parsec, and I could not find an easy way to recover the state information (which contains the stream).

A special solution will be to check the error you received and then use its location information to determine where the parsing has stopped. (Of course, this will only work if your thread supports search.)

0
source

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


All Articles