[Haskell-cafe] Re: Parsec is being weird at me
Andrew Coppin
andrewcoppin at btinternet.com
Sat Aug 25 15:18:29 EDT 2007
ChrisK wrote:
> 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...
>
But hang on a minute...
"many" parses 0 or more occurrances of an item.
"sepBy" parses 0 or more occurrances of an item, seperated by another item.
"endBy" parses 0 or more occurrances of an item, terminated by another item.
"sepEndBy" parses 0 or more occurrances of an item, seperated *and*
terminated by another item.
...except that "endBy" doesn't seem to be working right. :-S
More information about the Haskell-Cafe
mailing list