1

Say I have an attoparsec parser, x.

I am looking to create a function f :: Int -> Parser a -> Parser a, such that if y = f n x, then:

  • y fails if x fails
  • y fails if x succeeds and x does not consume n bytes
  • y succeeds otherwise

How would I go about doing this?

Cameron Martin
  • 5,952
  • 2
  • 40
  • 53

1 Answers1

5

You can use match to implement it:

f n x = do
    (bs, res) <- match x
    guard (BS.length bs >= n)
    return res

You should check that this interacts with (<|>) in an acceptable way before putting it to heavy use.

Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380
  • In `binary` this function is packaged as `isolate`. Perhaps it is more sensible there since `binary` tracks the number of bytes consumed, but it strikes me as a good addition to the attoparsec API (in terms of tokens, not bytes, so Text would not be counting bytes for example). – Thomas M. DuBuisson Aug 04 '16 at 17:01