[Haskell-beginners] Parsec problem

Kees Bleijenberg k.bleijenberg at lijbrandt.nl
Sun Feb 3 10:28:57 CET 2013



> 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.

Thanks. This solves it.  spaces does more then just reading spaces.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130203/042675ca/attachment-0001.htm>

More information about the Beginners mailing list