[Haskell-cafe] Re: [Parsec] No identEnd in ParsecToken?
On Tue, Sep 05, 2006 at 03:46:16PM +0100, Chris Kuklewicz [EMAIL PROTECTED] wrote a message of 69 lines which said: Fixing this may be as simple as identifier = try $ do start - letter rest - many (alphaNum | char '-') end - letter return ([start] ++ rest ++ [end]) ? characters authorized for identifiers It does not work for me (and neither does the second). The try argument always fails, probably because the term many on the rest line is greedy and swallows the ending letter. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: [Parsec] No identEnd in ParsecToken?
On Tue, Sep 05, 2006 at 04:17:41PM +0200, Stephane Bortzmeyer [EMAIL PROTECTED] wrote a message of 25 lines which said: I'm trying to use Parsec for a language which have identifiers where the '-' character is allowed only inside identifiers, not at the start or the end. [My grammar was underspecified, I also want to disallow two consecutive dashes.] Many thanks to Malcolm Wallace, Chris Kuklewicz and Udo Stenzel for their help and ideas. It seems there is no solution for ParsecToken (so I have to drop it). Among the two solutions which work for me (Malcolm Wallace's and Udo Stenzel's), I choosed the one by Udo because it is the one I understand the best. Here is my final version (rewritten in my style, errors are mine and not Udo's), thanks again: import Text.ParserCombinators.Parsec hiding (spaces) spaces = many1 (char ' ') inner_minus = do char '-' lookAhead alphaNum return '-' identifier = do start - letter rest - many (alphaNum | try inner_minus) return (start:rest) ? identifier identifiers = do result - identifier `sepBy` spaces eof return result main = do -- Legal parseTest identifiers foo bar parseTest identifiers foo-bar baz go-to parseTest identifiers a b3 c56 e56-y7 gag-3456 -- Illegal parseTest identifiers 1llegal parseTest identifiers illegal- more parseTest identifiers ill--egal more parseTest identifiers illegal -more ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: [Parsec] No identEnd in ParsecToken?
Stephane Bortzmeyer [EMAIL PROTECTED] writes: On Tue, Sep 05, 2006 at 04:17:41PM +0200, Stephane Bortzmeyer [EMAIL PROTECTED] wrote a message of 25 lines which said: I'm trying to use Parsec for a language which have identifiers where the '-' character is allowed only inside identifiers, not at the start or the end. I'm not really familiar with Parsec (I wrote my own limited backtrack parser years ago, and haven't quite got round to updating my brain), and while (judging by threads like this one) it seems to be harder to use than one would hope, this particular problem doesn't look as hard to me as all that. [My grammar was underspecified, I also want to disallow two consecutive dashes.] [...] Here is my final version (rewritten in my style, errors are mine and not Udo's), thanks again: inner_minus = do char '-' lookAhead alphaNum return '-' identifier = do start - letter rest - many (alphaNum | try inner_minus) return (start:rest) ? identifier I'd have thought something like the following was the 'obvious' way of doing it: chThen c r = do a - c; as - r; return (a:as) identifier = do start - letter `chThen` many alphaNum; rest - many (char '-' `chThen` many1 alphaNum) return (start++concat rest) ? identifier ie, your identifiers are an initial sequence of non-minuses beginning with a letter, and then an optional sequence of non-minuses preceded by a minus. Or have I lost the plot somewhere? Aside: Is there already name for `chThen`? ie (liftM2 (:)); I had a feeling we were avoiding liftM friends for some reason. -- Jón Fairbairn [EMAIL PROTECTED] http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html (updated 2006-07-14) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe