[Haskell-cafe] Question about implementing an off-side rule in
Parsec 2
Jason Dagit
dagit at codersbase.com
Mon Apr 27 17:58:41 EDT 2009
On Mon, Apr 27, 2009 at 2:41 PM, Bas van Gijzel <nenekotan at gmail.com> wrote:
> Hello everyone,
>
> 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.
I've never tried to implement a whitespace sensitive parser like you'd
need for Haskell or Python, but my thought was that maybe you use a
stack and you push/pop from it when the indentation level changes.
That way, I think you could isolate the part of the parser that
handles changes in indentation from the rest. Maybe I haven't thought
about this enough yet because I'm not sure what I would store on the
stack. In some sense, you want the indentation level change to
determine which production(s) of the grammar you are looking for. You
might also use it to track scope so that you know the scope of the
names in the program source.
> 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.
I'd start by playing with some toy examples in the State monad, then
try implementing a State monad. Only look at the real implementation
when you get stuck. Once you get State, then go back to Parsec which
is more complex. This way you'll be in over your head less.
Good luck,
Jason
More information about the Haskell-Cafe
mailing list