I'm having trouble working out how to use any of the functions in the Text.Parsec.Indent
module provided by the indents
package for Haskell, which is a sort of add-on for Parsec.
What do all these functions do? How are they to be used?
I can understand the brief Haddock description of withBlock
, and I've found examples of how to use withBlock
, runIndent
and the IndentParser
type here, here and here. I can also understand the documentation for the four parsers indentBrackets
and friends. But many things are still confusing me.
In particular:
What is the difference between
withBlock f a p
anddo aa <- a pp <- block p return f aa pp
Likewise, what's the difference between
withBlock' a p
anddo {a; block p}
In the family of functions
indented
and friends, what is ‘the level of the reference’? That is, what is ‘the reference’?Again, with the functions
indented
and friends, how are they to be used? With the exception ofwithPos
, it looks like they take no arguments and are all of typeIParser ()
(IParser defined like this or this) so I'm guessing that all they can do is to produce an error or not and that they should appear in ado
block, but I can't figure out the details.I did at least find some examples on the usage of
withPos
in the source code, so I can probably figure that out if I stare at it for long enough.<+/>
comes with the helpful description “<+/>
is to indentation sensitive parsers whatap
is to monads” which is great if you want to spend several sessions trying to wrap your head aroundap
and then work out how that's analogous to a parser. The other three combinators are then defined with reference to<+/>
, making the whole group unapproachable to a newcomer.Do I need to use these? Can I just ignore them and use
do
instead?The ordinary
lexeme
combinator andwhiteSpace
parser from Parsec will happily consume newlines in the middle of a multi-token construct without complaining. But in an indentation-style language, sometimes you want to stop parsing a lexical construct or throw an error if a line is broken and the next line is indented less than it should be. How do I go about doing this in Parsec?In the language I am trying to parse, ideally the rules for when a lexical structure is allowed to continue on to the next line should depend on what tokens appear at the end of the first line or the beginning of the subsequent line. Is there an easy way to achieve this in Parsec? (If it is difficult then it is not something which I need to concern myself with at this time.)