Safe Haskell | Safe-Infered |
---|
- type IndentParser s u a = ParsecT s u (State SourcePos) a
- runIndent :: SourceName -> State SourcePos a -> a
- withBlock :: Stream s (State SourcePos) z => (a -> [b] -> c) -> IndentParser s u a -> IndentParser s u b -> IndentParser s u c
- withBlock' :: Stream s (State SourcePos) z => IndentParser s u a -> IndentParser s u b -> IndentParser s u [b]
- block :: Stream s (State SourcePos) z => IndentParser s u a -> IndentParser s u [a]
- indented :: Stream s (State SourcePos) z => IndentParser s u ()
- same :: Stream s (State SourcePos) z => IndentParser s u ()
- sameOrIndented :: Stream s (State SourcePos) z => IndentParser s u ()
- checkIndent :: Stream s (State SourcePos) z => IndentParser s u ()
- withPos :: Stream s (State SourcePos) z => IndentParser s u a -> IndentParser s u a
- indentBrackets :: Stream s (State SourcePos) z => GenTokenParser s u (State SourcePos) -> IndentParser s u a -> IndentParser s u a
- indentAngles :: Stream s (State SourcePos) z => GenTokenParser s u (State SourcePos) -> IndentParser s u a -> IndentParser s u a
- indentBraces :: Stream s (State SourcePos) z => GenTokenParser s u (State SourcePos) -> IndentParser s u a -> IndentParser s u a
- indentParens :: Stream s (State SourcePos) z => GenTokenParser s u (State SourcePos) -> IndentParser s u a -> IndentParser s u a
- (<+/>) :: Stream s (State SourcePos) z => IndentParser s u (a -> b) -> IndentParser s u a -> IndentParser s u b
- (<-/>) :: Stream s (State SourcePos) z => IndentParser s u a -> IndentParser s u b -> IndentParser s u a
- (<*/>) :: Stream s (State SourcePos) z => IndentParser s u ([a] -> b) -> IndentParser s u a -> IndentParser s u b
- (<?/>) :: Stream s (State SourcePos) z => IndentParser s u (a -> b) -> Optional s u a -> IndentParser s u b
- data Optional s u a = Opt a (IndentParser s u a)
Documentation
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.
Types
type IndentParser s u a = ParsecT s u (State SourcePos) aSource
Indentation sensitive parser type. Usually m
will
be Identity
as with any ParsecT
runIndent :: SourceName -> State SourcePos a -> aSource
Run the result of an indentation sensitive parse
Blocks
withBlock :: Stream s (State SourcePos) z => (a -> [b] -> c) -> IndentParser s u a -> IndentParser s u b -> IndentParser s u cSource
parses withBlock
f a p a
followed by an indented block of p
combining them with f
withBlock' :: Stream s (State SourcePos) z => IndentParser s u a -> IndentParser s u b -> IndentParser s u [b]Source
Like withBlock
, but throws away initial parse result
block :: Stream s (State SourcePos) z => IndentParser s u a -> IndentParser s u [a]Source
Parses a block of lines at the same indentation level
Indentation Checking
indented :: Stream s (State SourcePos) z => IndentParser s u ()Source
Parses only when indented past the level of the reference
same :: Stream s (State SourcePos) z => IndentParser s u ()Source
Parses only on the same line as the reference
sameOrIndented :: Stream s (State SourcePos) z => IndentParser s u ()Source
Parses only when indented past the level of the reference or on the same line
checkIndent :: Stream s (State SourcePos) z => IndentParser s u ()Source
Ensures the current indentation level matches that of the reference
withPos :: Stream s (State SourcePos) z => IndentParser s u a -> IndentParser s u aSource
Parses using the current location for indentation reference
Paired characters
indentBrackets :: Stream s (State SourcePos) z => GenTokenParser s u (State SourcePos) -> IndentParser s u a -> IndentParser s u aSource
parses with surrounding brackets
indentAngles :: Stream s (State SourcePos) z => GenTokenParser s u (State SourcePos) -> IndentParser s u a -> IndentParser s u aSource
parses with surrounding angle brackets
indentBraces :: Stream s (State SourcePos) z => GenTokenParser s u (State SourcePos) -> IndentParser s u a -> IndentParser s u aSource
parses with surrounding braces
indentParens :: Stream s (State SourcePos) z => GenTokenParser s u (State SourcePos) -> IndentParser s u a -> IndentParser s u aSource
parses with surrounding parentheses
Line Fold Chaining
Any chain using these combinators must used with withPos
(<+/>) :: Stream s (State SourcePos) z => IndentParser s u (a -> b) -> IndentParser s u a -> IndentParser s u bSource
(<-/>) :: Stream s (State SourcePos) z => IndentParser s u a -> IndentParser s u b -> IndentParser s u aSource
(<*/>) :: Stream s (State SourcePos) z => IndentParser s u ([a] -> b) -> IndentParser s u a -> IndentParser s u bSource
Like <+/>
but applies the second parser many times
(<?/>) :: Stream s (State SourcePos) z => IndentParser s u (a -> b) -> Optional s u a -> IndentParser s u bSource