[Haskell-cafe] Question about implementing an off-side rule in Parsec 2

Bernie Pope florbitous at gmail.com
Wed Apr 29 00:04:22 EDT 2009

2009/4/28 Bas van Gijzel <nenekotan at gmail.com>
> I'm doing a bachelor project focused on comparing parsers. One of the
> parser libraries I'm using is Parsec (2) and I'm going to implement a very
> small subset of haskell with it, with as most important feature the off-side
> rule (indentation based parsing) used in function definitions and possibly
> in where clauses.
> But I'm still a bit stuck on how to implement this cleanly. I tried to
> search for some examples on blogs but I haven't found anything yet. As far
> as I can see the way to go would be using getState and updateState methods
> defined in Parsec.Prim and to use the methods in Parsec.Pos to compare the
> difference in indendation for tokens.
> But I haven't completely wrapped my head around any state monad yet and I
> don't understand Parsec enough yet to see how to use the methods Parsec.Pos
> and state easily. Some examples or pointers to something to read would
> really be helpful.

Parsing a simple form of the offside rule is discussed in the paper:

    Monadic Parser Combinators, Hutton and Meijer, 1996

    see section 8, page 30.

Their parsers are similar in style to Parsec, but you may need to do some

I haven't thought about it hard, but I suspect their approach is not
efficient for deeply nested examples, due to repeated processing of the
token stream (but I could be wrong, and maybe it doesn't matter for what you
are trying to do).

I followed their approach in a toy language once, and the result was very
pleasing to read in code.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090429/e567b0e2/attachment.htm

More information about the Haskell-Cafe mailing list