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

Malcolm Wallace Malcolm.Wallace at cs.york.ac.uk
Tue Sep 5 10:31:06 EDT 2006


Stephane Bortzmeyer <bortzmeyer at nic.fr> wrote:

> identifier = do
>     start <- letter
>     rest <- many (alphaNum <|> char '-') 
>     end <- letter       
>     return ([start] ++ rest ++ [end])
>   <?> "characters authorized for identifiers"
> 
> because the parser created by "many" is greedy: it consumes
> everything, including the final letter.

How about eating chunks of alphaNum, then chunks of '-', in alternation.
You just need to flatten the returned list of words to a single word.

    identifier = do
      init <- many alphaNum
      rest <- many ( do dash <- many1 (char '-')
                        alfa <- many1 alphaNum
                        return (dash++alfa) )
      return (concat (init:rest))

Regards,
    Malcolm


More information about the Haskell-Cafe mailing list