[Haskell-cafe] Re: [Parsec] No identEnd in ParsecToken?

Stephane Bortzmeyer bortzmeyer at nic.fr
Tue Sep 5 16:41:13 EDT 2006

On Tue, Sep 05, 2006 at 04:17:41PM +0200,
 Stephane Bortzmeyer <bortzmeyer at nic.fr> 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
   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"

More information about the Haskell-Cafe mailing list