[Haskell-cafe] Optional EOF in Parsec.
Antoine Latter
aslatter at gmail.com
Sat Apr 4 00:13:03 EDT 2009
On Fri, Apr 3, 2009 at 8:17 PM, Kannan Goundan <kannan at cakoose.com> wrote:
>
> I'm writing a parser with Parsec. In the input language, elements of a sequence
> are separated by commas:
>
> [1, 2, 3]
>
> However, instead of a comma, you can also use an EOL:
>
> [1, 2
> 3]
>
> Anywhere else, EOL is considered ignorable whitespace. So it's not as simple as
> just making EOL a token and looking for (comma | eol).
>
Untested, but hopefully enough so you get an idea of where to start:
> -- End of line parser. Consumes the carriage return, if present.
> eol :: Parser ()
> eol = eof <|> char '\n'
> -- list-element separator.
> listSep :: Parser ()
> listSep = eol <|> (char ',' >> spaces)
> -- list parser. The list may be empty - denoted by "[]"
> myListOf :: Parser a -> Parser [a]
> myListOf p =
> char '[' >>
> sepBy p listSep >>= \vals ->
> char ']' >>
> return vals
This would probably be better off with a custom version of the
'spaces' parser that didn't parse newlines.
Antoine
More information about the Haskell-Cafe
mailing list