1

I'm writing a parser for a language in Haskell with Alex + Happy.

What I want to do is: in Alex, skip whitespaces and newlines, but keep them as state, and then emit tokens which contain the newlines and the indent before the token.

I guess I could emit extra tokens for indent and newlines, and then collapse them later, but I'd prefer a cleaner approach.

Is there any way to wrap the token processing in alex in a monad that carries the indent/newline information and is accesible inside the actions that emit tokens?

theduke
  • 3,027
  • 4
  • 29
  • 28
  • Possibly useful: http://stackoverflow.com/questions/20315739/how-to-use-an-alex-monadic-lexer-with-happy I had troubles too understanding how to use monadic lexer and I found the documentation is really unhelpful. – Bakuriu Jul 01 '16 at 11:14
  • [Related question for the equivalent Parsec problem, that I asked recently](http://stackoverflow.com/questions/37529673/is-there-an-established-way-to-write-parsers-that-can-reconstruct-their-exact-in). (I haven't found a really satisfying solution yet; in the project that prompted be to this question I chose, for once, not to try and be clever – I just wrote all the handling for whitespace-preservation by hand. That turned out to work pretty well.) – leftaroundabout Jul 01 '16 at 12:25

0 Answers0