[Haskell-cafe] Parsec expressions with alphaNum operators

Chris Casinghino chris.casinghino at gmail.com
Mon Apr 7 12:33:37 EDT 2008


2008/4/7 Paul Keir <pkeir at dcs.gla.ac.uk>:
> I'm using buildExpressionParser, and I'd like to use alphanumeric operator
> characters. I get an (unexpected "a") error though. With a test string like
> "-a" if "a" is used in any of the "reservedOpNames". I'm aiming for the
> Fortran operators like ".and.".
> ...

reservedOp checks to make sure that whatever it parses isn't
immediately followed by another valid operator character (the idea
being, I think, that then whoever wrote the string being parsed
probably meant for that other longer operator to be one token).  This
causes your error, since it can't parse the reservedOp "~", and "~a"
itself is not anything.  You'll notice that, for example, "~ a" parses
just fine.

I don't seen any perfect solution.  If you want to use the token and
expression modules in this way, you'll have to redefine your operators
so as not to intersect with your valid identifiers.  Alternatively,
you can roll your own token parsers (though parsec does what it does
for good reasons, so you might see some unexpected behavior).  Or you
could just mandate that operators must be followed by a space.

Good luck!

--Chris Casinghino

More information about the Haskell-Cafe mailing list