Safe Haskell | None |
---|
Create parsers from grammars dynamically (at runtime).
- mkDynamicParser :: (Token t, Token t', Typeable a) => (t -> t', t' -> t) -> Grammar t a -> Parser t a
- constrWrapper :: (t -> CTok t, CTok t -> t)
- idWrapper :: (t -> t, t -> t)
- 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
:: (Token t, Token t', Typeable a) | |
=> (t -> t', t' -> t) | Token wrapper and unwrapper |
-> Grammar t a | Language grammar |
-> Parser t a |
Make a parser at runtime given a grammar
constrWrapper :: (t -> CTok t, CTok t -> t)Source
Wrap the input tokens in the CTok
datatype, which has Eq
and Ord
instances which only look at the constructors of the input values.
This is for use as an argument to mkDynamicParser
.
Example, which will evaluate to True
:
CTok (Just 1) == CTok (Just 2)
This is useful when using a lexer that may give back a list of something like:
data Token = Ident String | Number Integer | LParen | RParen | Plus | ...
If you want to specify a grammar that accepts any Ident
and any Number
and not just specific ones, use constrWrapper
.
idWrapper :: (t -> t, t -> t)Source
Don't wrap the input tokens.
This is for use as an argument to mkDynamicParser
.
An example usage of idWrapper
is if the parser operates directly on
String
.
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.