haskell / attoparsec

A fast Haskell library for parsing ByteStrings
http://hackage.haskell.org/package/attoparsec
Other
514 stars 93 forks source link

Delimited parsers #129

Open Lysxia opened 7 years ago

Lysxia commented 7 years ago

A common situation is to parse an encoding prefixed by its length. So you first parse the length as an integer n, and then you would like to run a (sub)parser p :: Parser a only on the next n bytes. I could think of two solutions for users today:

It would be nice for attoparsec to have combinators to delimit the input that a subparser gets to see, like span and splitAt in pipes-parse.

What do you think of such an addition? Is there a better solution?

Lysxia commented 7 years ago

48 and #95 were in this situation before, with solutions that correspond to the first item above.

joeyh commented 5 years ago

I keep needing to do this kind of thing in my attoparsec parsers, and only on discovering this bug am I shaking the feeling that I'm somehow using attoparsec wrong to need to use parseOnly within a parser so frequently.

With me, it often comes up while writing something like Parser a -> Parser b, which needs to pick out the delimited data and run the sub-parser over it.

Checking endOfInput seems like one thing that can easily be gotten wrong when doing this. The example in #95 perhaps forgot to do that. I wonder if a combinator for this should require the sub-parser to consume all the input?