[Haskell-cafe] parsec and multiple simultaneous token types

Stephen Tetley stephen.tetley at gmail.com
Mon Mar 29 06:07:12 EDT 2010

Hi Jeremy

Have you considered rolling your own parser?

If you don't want say the expression parser or the language defs,
quite a bit of Parsec's machinery is now standard-ish. For instance,
most of the combinators in Text.ParserCombinators.Parsec.Combinator
are general control operators and can be made on top of an applicative
functor with Alternative. I posted a set of them to haskell-cafe a few
months ago, though later I found a bug in one of them. Ross Paterson
has put versions of the permutation combinators that need only
Applicative / Alternative on Hackage:


If you don't want to go so far, you might still want to make the
nextSegment parser higher order - at the moment it looks like you are
calling nextSegment to pull a lexeme into some context and using the
sequencing of the do-notation to run the next parser on the context.
It might be preferable if nextSegment operated more like this:

h_then_o <- nextSegment (char 'h' >> char 'o')

I'm guessing an implementation something like

nextSegment :: GenParser String a -> GenParser String a
nextSegment p = do
  xs <- token
  let ans = runParserSomehow p xs
  return ans

Best wishes


More information about the Haskell-Cafe mailing list