|
| Text.ParserCombinators.Parsec.IndentParser |
|
|
|
|
| Description |
A module to construct indentation aware parsers. Many programming
language have indentation based syntax rules e.g. python and Haskell.
This module exports combinators to create such parsers.
The input source can be thought of as a list of tokens. Abstractly
each token occurs at a line and a column and has a width. The column
number of a token measures is indentation. If t1 and t2 are two tokens
then we say that indentation of t1 is more than t2 if the column
number of occurrence of t1 is greater than that of t2.
Currently this module supports two kind of indentation based syntactic
structures which we now describe:
- Block
- A block of indentation c is a sequence of tokens with
indentation at least c. Examples for a block is a where clause of
Haskell with no explicit braces.
- Line fold
- A line fold starting at line l and indentation c is a
sequence of tokens that start at line l and possibly continue to
subsequent lines as long as the indentation is greater than c. Such
a sequence of lines need to be folded to a single line. An example
is MIME headers. Line folding based binding separation is used in
Haskell as well.
The module exports three combinators are indentParser, block
and lineFold. To construct parsers for indentation based grammars
one typically applies the indentParser. A block can then be
parsed using the combinator block and a line fold using
lineFold. Generating indentation aware tokenisers could be tricky.
Given a language description via the
Text.ParserCombinators.Parsec.Language.LanguageDef record use module
Text.ParserCombinators.Parsec.IndentParser.Token to generate its
tokeiser (this will apply indentParser on all tokenisers and then
the user can forget about indentParser combinator).
Warning:
Internally indentations are implemented using Parser states. If one
wants to use parser states as well then use the getState and
setState functions exported by this module instead of those exported
from the parsec library. Also use the parseTest and runParser
function exported from this module instead of the one exported from
Parsec.
|
|
| Synopsis |
|
|
|
|
| Parser type.
|
|
|
| An indentation aware parser.
|
|
|
|
|
| The mode of the indentation parser.
| | Constructors | | NoIndent | Ignore indentation
| | Block | In block mode
| | LineFold | In line fold mode
|
| Instances | |
|
|
|
| The combinator indentParser makes its input parser indentation
aware. Usually one would want to make all the tokenisers indentation
aware.
|
|
|
| The parser noIndent p runs p ignoring any indentation based
structure. This can be used to parse for example an explicitly braced
where clause in Haskell.
|
|
|
| The parser block p parses a block of p.
|
|
|
| The parser lineFold p parses a folded line of p.
|
|
|
The parser betweenOrBlock open close p parses p between open
and close. If open is matched p is parsed in NoIndent mode otherwise
a block p is parsed in Block mode. For eg. the parser for parsing
haskell where clause would look like
whereClause = do reserved where; betweenOrBlock bindings
|
|
|
| Similar to betweenOrBlock but uses lineFold instead of block.
|
|
| User state manipulation.
|
|
|
| Gets the current user state.
|
|
|
| Sets the user state.
|
|
| Running and testing.
|
|
|
|
|
|
| This is the function analogues to parseTest of the Parsec module.
Given an indent parser p :: IndentParser tok () a and a list of
tokens it runs the parser and prints the result.
|
|
| Produced by Haddock version 2.1.0 |