[Haskell-beginners] Parsec problem

Daniel Fischer daniel.is.fischer at googlemail.com
Sat Feb 2 15:24:11 CET 2013


On Saturday 02 February 2013, 15:00:52, Kees Bleijenberg wrote:

> module Main(main) where 
> 
> import Text.ParserCombinators.Parsec
>
> parseToNewLine = do
>                     line <- manyTill anyChar newline
>                     return line
>
> keyValue = do
>                     fieldName <- many letter 
>                     spaces
>                     char '='
>                     spaces
>                     fieldValue <- parseToNewLine 
>                     return (fieldName,fieldValue)
>
> main = parseTest keyValue "key=\n"
>
> I don’t understand why te code above doesn’t parse to (“key”,””)

Because the newline is already consumed by the `spaces`. So parseToNewLine 
gets an empty string as input, and fails on that.

> parseToNewLine “\n” parses to “"
>
> parseTest keyValue “a=b\n” works fine and parses to (“a”,”b”) 

The input of parseToNewLine must contain a newline, or it fails. In the last 
example, the `spaces` after `char '='` stops at reaching the 'b', so the 
newline remains. In the first (problematic) example, all the remaining input 
after the '=' consists of whitespace.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130202/1583c98b/attachment.htm>


More information about the Beginners mailing list