[Haskell-cafe] Re: Parsec question

Christian Maeder Christian.Maeder at dfki.de
Fri Apr 17 12:43:10 EDT 2009


Michael Mossey wrote:
> Here's what I have so far. It works, but it's a bit weird to consume the
> // as part of the text rather than the keyword. That happens because the
> try( string "//" ), which is part of the end arg to manyTill, consumes
> the // when it succeeds. But maybe it is the most natural way to express
> the problem.

use lookAhead!

> parseKeyword :: Parser String
> parseKeyword = many1 (alphaNum <|> char '_')

  parseKeyword = string "//" >> many1 (alphaNum <|> char '_')

> parseText :: Parser String
> parseText = manyTill anyChar ((try (string "//") >> return ())
>                               <|> eof)

  parseText = manyTill anyChar
    $ (lookAhead (try $ string "//") >> return ())
      <|> eof

(untested)
C.


More information about the Haskell-Cafe mailing list