[Haskell-cafe] Re: Parsec is being weird at me

ChrisK haskell at list.mightyreason.com
Sat Aug 25 15:13:03 EDT 2007


Andrew Coppin wrote:
> Anybody want to explain to me why this doesn't work?
> 
>   ___         ___ _
>  / _ \ /\  /\/ __(_)
> / /_\// /_/ / /  | |      GHC Interactive, version 6.6.1, for Haskell 98.
> / /_\\/ __  / /___| |      http://www.haskell.org/ghc/
> \____/\/ /_/\____/|_|      Type :? for help.
> 
> Loading package base ... linking ... done.
> Prelude> :m Text.ParserCombinators.Parsec
> Prelude Text.ParserCombinators.Parsec> parseTest (endBy anyToken (char
> '#')) "abc#"
> Loading package parsec-2.0 ... linking ... done.
> parse error at (line 1, column 1):
> unexpected "b"
> expecting "#"

anyToken is singular: it accepts a single token, in this case 'a'.

Then endBy expects (char '#') to match and reads 'b' instead and gives the error
message.

So using (many anyToken) gets further:

> Prelude Text.ParserCombinators.Parsec> parseTest (endBy (many anyToken) (char '#')) "abc#"
> Loading package parsec-2.0 ... linking ... done.
> parse error at (line 1, column 1):
> unexpected end of input
> expecting "#"
> Prelude Text.ParserCombinators.Parsec> parseTe

Here (many anyToken) reads all of "abc#" and then endBy wants to read (char '#')
and get the end of input instead.

So the working version of endBy is thus:

> Prelude Text.ParserCombinators.Parsec> parseTest (endBy (many (noneOf "#")) (char '#')) "abc#"
> ["abc"]

Or you may need to not use endBy...



More information about the Haskell-Cafe mailing list