Safe Haskell | None |
---|
Create parsers from grammars statically (at compile time).
- mkStaticParser :: (Typeable a, ToPat t, Token t, Lift t) => Grammar t a -> ExpQ -> ExpQ
- class ToPat a where
- toConstrPat :: (Token t, Lift t) => t -> PatQ
- type ParseResult t a = Either (ParseError t) a
- type Parser t a = [t] -> ParseResult t a
- data ParseError t
- = ParseError {
- expectedTokens :: [Tok t]
- position :: Integer
- | InternalParserError { }
- = ParseError {
- showError :: Show t => ParseError t -> String
- parse :: Show t => Parser t a -> [t] -> a
Documentation
:: (Typeable a, ToPat t, Token t, Lift t) | |
=> Grammar t a | The grammar |
-> ExpQ | The Template Haskell representation of the grammar |
-> ExpQ | The representation of a parser of type
|
Make a static parser from a grammar.
Example usage:
g :: Grammar s a gparser = $(mkStaticParser g [|g|])
Note that gparser
must be in a different module than g
, due to
Template Haskell restrictions.
The token type of the grammar must also be an instance of ToPat
, and the
result type an instance of Typeable
(the GHC extension
DeriveDataTypeable may be useful for this).
If there are conflicts in the parsing tables, they will be displayed as warnings when compiling the parser.
Make a Template Haskell pattern from a value.
This is used to create a case expression from a parsing table when using
mkStaticParser
, and it is thus required that the token type that the
parser is to operate on is an instance of this class.
The parser will behave differently depending on how its ToPat
instance
works. If only comparing constructors (toConstrPat
), it will regard
Just 1
as the same compared to Just 2
.
toConstrPat
and Language.Haskell.TH can help in creating an instance.
toConstrPat :: (Token t, Lift t) => t -> PatQSource
Automatically create a ToPat
instance which only compares the constructor
of the token type. For example, the pattern yielded from using this on the
value Just 3
is the pattern Just _
.
Example usage:
instance ToPat TokenType where toPat = toConstrPat
type ParseResult t a = Either (ParseError t) aSource
The result of running a parser
type Parser t a = [t] -> ParseResult t aSource
The type of a parser generated by Grempa
data ParseError t Source
The different kinds of errors that can occur
ParseError | The parser did not get an accepted string of tokens. |
| |
InternalParserError | This should not happen. Please file a bug report if it does. |
Functor ParseError | |
Show t => Show (ParseError t) |
showError :: Show t => ParseError t -> StringSource
Make a prettier error string from a ParseError
.
This shows the position as an index into the input string of tokens, which
may not always be preferable, as that position may differ to the position
in the input if it is first processed by a lexer.
It also shows the expected tokens.