Attoparsec parsing fails but should not have a proper return

I am using Attoparsec, which is the reverse by default. However, the following line:

parseOnly  (string "foo" *> many1 anyChar <* string "bar") "fooxxxbar"

      

crash:

Left "not enough input"

      

Why is this so? If it many1 anyChar

decides to parse only three characters ( xxx

), it should succeed. And he should consider doing it at some point due to backtracking, shouldn't he?

What is the correct way to do the regex equivalent /foo(.*)bar/

using Attoparsec?

+3


source to share


1 answer


I am using Attoparsec, which is the reverse by default.

Not really. Attoparsec supports backtracking, but only in some explicit situations (where the documentation says it does). Its goal is high performance parsing and, unsurprisingly, does not perform well with rollback.



Are you looking for manyTill

or manyTill'

. Note that the backtracking behavior is mentioned in the documentation.

ghci> manyTill1 p e = (:) <$> p <*> manyTill p e 
ghci> parseOnly (string "foo" *> manyTill1 anyChar (string "bar")) "fooxxxbar"
Right "xxx"

      

+2


source







All Articles