[Haskell-cafe] Parsec Expected Type

Tillmann Rendel rendel at rbg.informatik.tu-darmstadt.de
Sun Mar 30 07:29:35 EDT 2008


Paul Keir wrote: 
> What I'd like is to parse either the string "parameter", or the 
> string ":". I'm using 'reserved' and 'symbol' because they seem to 
> correspond well to the concepts in the language I'm parsing. 

You may consider using reservedOp for ":", depending on how ":+" should be parsed:

  for ":+" use reservedOp
  for ":" "+" use symbol

If you use reserved, then ":name" will be parsed as ":name" not ":" "name" as you probably expect. generally, reserved is for identifier-like keywords, and reservedOp for operator-like keywords.

> Perhaps I'd express my confusion better if I ask: Why are 'reserved'
> and 'symbol' different types?

I have no idea. They aren't in the Parsec manual on Daans site:

  http://legacy.cs.uu.nl/daan/download/parsec/parsec.html

You can fix this by defining

  reserved name = ParsecToken.reserved tokenParser name >> return name

instead of

  reserved = ParsecToken.reserved tokenParser

to "import" the reserved component from the tokenParser to the toplevel.
Now,

  reserved :: String -> CharParser st String

Another option is to fix it the other way, by defining

  symbol name = ParsecToken.symbol tokenParser name >> return ()

or to fix it in a ad-hoc manner, by defining

  ignored = (>> return ())

and using it in the approbiate places, like

  parameterOrColon = reserved "parameter" <|> ignored (symbol ":")

Tillmann


More information about the Haskell-Cafe mailing list