Safe Haskell | Safe |
---|---|
Language | Haskell98 |
Synopsis
- type IndentT m = ReaderT Indentation m
- type IndentParserT s u m a = ParsecT s u (IndentT m) a
- type IndentParser s u a = IndentParserT s u Identity a
- runIndent :: IndentT Identity a -> a
- runIndentParserT :: (Monad m, Stream s (IndentT m) t) => IndentParserT s u m a -> u -> SourceName -> s -> m (Either ParseError a)
- runIndentParser :: Stream s (IndentT Identity) t => IndentParser s u a -> u -> SourceName -> s -> Either ParseError a
- withBlock :: (Monad m, Stream s (IndentT m) z) => (a -> [b] -> c) -> IndentParserT s u m a -> IndentParserT s u m b -> IndentParserT s u m c
- withBlock' :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m a -> IndentParserT s u m b -> IndentParserT s u m [b]
- block :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m a -> IndentParserT s u m [a]
- indented :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m ()
- same :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m ()
- sameOrIndented :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m ()
- checkIndent :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m ()
- topLevel :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m ()
- notTopLevel :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m ()
- withPos :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m a -> IndentParserT s u m a
- indentBrackets :: (Monad m, Stream s (IndentT m) z) => GenTokenParser s u (IndentT m) -> IndentParserT s u m a -> IndentParserT s u m a
- indentAngles :: (Monad m, Stream s (IndentT m) z) => GenTokenParser s u (IndentT m) -> IndentParserT s u m a -> IndentParserT s u m a
- indentBraces :: (Monad m, Stream s (IndentT m) z) => GenTokenParser s u (IndentT m) -> IndentParserT s u m a -> IndentParserT s u m a
- indentParens :: (Monad m, Stream s (IndentT m) z) => GenTokenParser s u (IndentT m) -> IndentParserT s u m a -> IndentParserT s u m a
- (<+/>) :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m (a -> b) -> IndentParserT s u m a -> IndentParserT s u m b
- (<-/>) :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m a -> IndentParserT s u m b -> IndentParserT s u m a
- (<*/>) :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m ([a] -> b) -> IndentParserT s u m a -> IndentParserT s u m b
- (<?/>) :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m (a -> b) -> Optional s u m a -> IndentParserT s u m b
- data Optional s u m a = Opt a (IndentParserT s u m 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 IndentT m = ReaderT Indentation m Source #
Indentation transformer.
type IndentParserT s u m a = ParsecT s u (IndentT m) a Source #
Indentation sensitive parser type. Usually m
will be Identity
as with
any ParsecT
. In that case you can use the simpler IndentParser
type.
type IndentParser s u a = IndentParserT s u Identity a Source #
A simplified IndentParserT
.
:: (Monad m, Stream s (IndentT m) t) | |
=> IndentParserT s u m a | Parser to run |
-> u | User state |
-> SourceName | Source name |
-> s | Input for the parser |
-> m (Either ParseError a) | Result |
This is a convenience function which wraps runIndentT
and runParserT
.
:: Stream s (IndentT Identity) t | |
=> IndentParser s u a | Parser to run |
-> u | User state |
-> SourceName | Source name |
-> s | Input for the parser |
-> Either ParseError a | Result |
This is another convenience function. Use this instead of
runIndentParserT
if m
is Identity
.
Blocks
withBlock :: (Monad m, Stream s (IndentT m) z) => (a -> [b] -> c) -> IndentParserT s u m a -> IndentParserT s u m b -> IndentParserT s u m c Source #
parses withBlock
f a p a
followed by an indented block of p
combining them with f
withBlock' :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m a -> IndentParserT s u m b -> IndentParserT s u m [b] Source #
Like withBlock
, but throws away initial parse result
block :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m a -> IndentParserT s u m [a] Source #
Parses a block of lines at the same indentation level
Indentation Checking
indented :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m () Source #
Parses only when indented past the level of the reference
same :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m () Source #
Parses only on the same line as the reference
sameOrIndented :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m () Source #
Parses only when indented past the level of the reference or on the same line
checkIndent :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m () Source #
Ensures the current indentation level matches that of the reference
topLevel :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m () Source #
Ensures that there is no indentation.
notTopLevel :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m () Source #
Ensures that there is at least some indentation.
withPos :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m a -> IndentParserT s u m a Source #
Parses using the current location for indentation reference
Paired characters
indentBrackets :: (Monad m, Stream s (IndentT m) z) => GenTokenParser s u (IndentT m) -> IndentParserT s u m a -> IndentParserT s u m a Source #
parses with surrounding brackets
indentAngles :: (Monad m, Stream s (IndentT m) z) => GenTokenParser s u (IndentT m) -> IndentParserT s u m a -> IndentParserT s u m a Source #
parses with surrounding angle brackets
indentBraces :: (Monad m, Stream s (IndentT m) z) => GenTokenParser s u (IndentT m) -> IndentParserT s u m a -> IndentParserT s u m a Source #
parses with surrounding braces
indentParens :: (Monad m, Stream s (IndentT m) z) => GenTokenParser s u (IndentT m) -> IndentParserT s u m a -> IndentParserT s u m a Source #
parses with surrounding parentheses
Line Fold Chaining
Any chain using these combinators must used with withPos
(<+/>) :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m (a -> b) -> IndentParserT s u m a -> IndentParserT s u m b Source #
(<-/>) :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m a -> IndentParserT s u m b -> IndentParserT s u m a Source #
(<*/>) :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m ([a] -> b) -> IndentParserT s u m a -> IndentParserT s u m b Source #
Like <+/>
but applies the second parser many times
(<?/>) :: (Monad m, Stream s (IndentT m) z) => IndentParserT s u m (a -> b) -> Optional s u m a -> IndentParserT s u m b Source #