Parsec problem

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Parsec problem

Daniel Fischer
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>

Reply | Threaded
Open this post in threaded view
|

Parsec problem

Kees Bleijenberg
Hi,

 

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

Kees

 

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