
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.