[Haskell-cafe] parsec: problem combining lookAhead with many1 (bug?)

silly8888 silly8888 at gmail.com
Thu Aug 7 14:25:23 UTC 2014


Suppose that we have the following parser:

  p = lookAhead (char 'a') >> char 'b'

If we use it like so

  parse p "" "a"

we get the following error:

Left (line 1, column 1):
unexpected "a"
expecting "b"

What happened is that char 'a' succeeded by consuming the 'a' from the
input and then lookAhead rewinded the input stream (as it does on
success). Then, char 'b' tries to parse (again) the first character of
the input and fails. Everything works as expected.

Now let's slightly modify our parser:

  p' = lookAhead (many1 $ char 'a') >> char 'b'

I've only added a many1. I was expecting this parser to give the same
error as the previous one: many1 $ char 'a' will succeed consuming one
'a' and then lookAhead will rewind the input (as it does on success).
Thus when we call char 'b' we are going to be in the beginning of the
input again. Well, that doesn't happen:

Left (line 1, column 2):
unexpected end of input
expecting "b"

As you can see, lookAhead did not rewind the input as it was supposed to.


More information about the Haskell-Cafe mailing list