treating spaces with Parsec

Pixel pixel at mandriva.com
Mon Dec 8 06:55:37 EST 2008


"ben.a" <benedikt.ahrens at web.de> writes:

[...]

> "LC.app : 0 , 2 22. abs : 1 ."

[...]

> line = do
>         spaces
>         constru <- word
>         spaces
>         (char ':')
>         spaces
>         ar <- sepBy number separator
>         return (constru, ar)

[...]

> --------   a separator between two integers is a space or a comma
> separator :: Parser ()
> separator = skipMany1 (space <|> char ',')

the culprit is "sepBy number separator": for things like "1 2 " it
will read a separator, and so will wait for the next number, and won't
expect eol.

replace "sepBy number separator" with "endBy number separator" and see
it will work.

this is quite ugly though since it allows "xxx : 1, ."


a better solution could be to use:

separated_numbers1 :: Parser [Integer]
separated_numbers1 =
    do x <- number
       spaces
       xs <- (char ',' >> spaces >> separated_numbers1) <|> option [] separated_numbers1
       return (x : xs)


More information about the Libraries mailing list