6

I was wondering if there's a way to get te remaining input from Parsec after it stops parsing, either if it was a successful or failed parse, maybe this signature:

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

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

chamini2
  • 2,820
  • 2
  • 24
  • 37
  • 1
    Consider using attoparsec, it is much faster and supports partial matches. – arrowd Sep 28 '15 at 05:47
  • I'm not parsing big files or anything like that, and as I understand, Parsec offers an easier *interface*. I use Parsec because of [this answer](http://stackoverflow.com/a/19213247/1276441) – chamini2 Sep 28 '15 at 05:58

3 Answers3

1

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

Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380
  • This would work for getting the input only on parses that succeed. I would like a way to get the input in either case. – chamini2 Sep 28 '15 at 05:59
1

Take a look at Megaparsec — a modern fork of Parsec, beginning from version 4.2.0 it allows to supply custom state at the beginning of parsing and extract parser state at the end (it doesn't matter if parser succeeds or fails). This allows to partially parse input, resume parsing, specify non-standard initial textual position, etc. See runParser' and runParserT'.


Disclosure: I'm one of the authors of Megaparsec.

Mark Karpov
  • 7,499
  • 2
  • 27
  • 62
0

I dug a bit into Parsec's internals and I wasn't able to find an easy way to recover the state information (which contains the stream).

An ad hoc solution would be to examine the error that you receive and then use its location information to deduce where the parsing had stopped. (Of course, this would only work if your stream supports seeking.)

Rufflewind
  • 8,545
  • 2
  • 35
  • 55