Safe Haskell | None |
---|---|
Language | Haskell2010 |
Monadic combinators for Earley Parsing
- data Gram a
- alts :: [Gram a] -> Gram a
- fail :: Gram a
- many :: Gram a -> Gram [a]
- skipWhile :: Gram () -> Gram ()
- data Lang t a
- getToken :: Lang t (Gram t)
- data NT a
- createNamedNT :: Show a => String -> Lang t (NT a)
- referenceNT :: NT a -> Gram a
- declare :: Show a => String -> Lang t (NT a, Gram a)
- produce :: NT a -> Gram a -> Lang t ()
- fix :: Show a => String -> (Gram a -> Lang t (Gram a)) -> Lang t (Gram a)
- data Parsing a = Parsing {}
- data SyntaxError
- parseAmb :: (Show a, Show t) => Lang t (Gram a) -> [t] -> Parsing (Either SyntaxError [a])
- data Ambiguity = Ambiguity String Pos Pos
- data ParseError
- parse :: (Show a, Show t) => Lang t (Gram a) -> [t] -> Parsing (Either ParseError a)
- newtype Eff = Eff Int
- type Pos = Int
Grammars, Non-terminals, Productions
Type of grammars. RHS of a production rules. Synthesizing values of type a
. Monadic construction allows context-sensitive grammars.
alts :: [Gram a] -> Gram a Source
Grammar constructed from a list of alteratives. Alternations may be nested; we are not restricted to just having alternate productions for a given non-terminal.
many :: Gram a -> Gram [a] Source
Grammar for kleene *
. Constructs an infinite RHS. Use in preference to right-recursion via a non-terminal, which has quadratic inefficiency.
skipWhile :: Gram () -> Gram () Source
Kleene *
which ignores the synthesized value.
skipWhile p == do _ <- many p; return ()
Type for language definition over terminals (tokens) of type t
. A collection of production rules together with an entry point. Constructed monadically.
Type of non-terminals. LHS of a production rules. Carrying values of type a
.
createNamedNT :: Show a => String -> Lang t (NT a) Source
Create a fresh non-terminal within a language definition. The name is only used for debugging and reporting ambiguity. This is a low level primitive. Simpler to use declare
.
referenceNT :: NT a -> Gram a Source
Reference a non-terminal on the RHS of a production. This is a low level primitive. Simpler to use declare
.
declare :: Show a => String -> Lang t (NT a, Gram a) Source
Convenience combination of createNamedNT
and referenceNT
, returning a pair of values for a fresh non-terminal, for use on the LHS/RHS.
produce :: NT a -> Gram a -> Lang t () Source
Define a language production, linking the LHS and RHS of the rule.
fix :: Show a => String -> (Gram a -> Lang t (Gram a)) -> Lang t (Gram a) Source
Combination of declare/produce to allow reference to a grammar within its own defintion. Use this for language with left-recursion.
Parsing
data SyntaxError Source
Type describing a syntax-error encountered during parsing. In all cases the final position reached before the error is reported. This position is automatically determined by the Early parsing algorithm.
Eq SyntaxError Source | |
Show SyntaxError Source |
parseAmb :: (Show a, Show t) => Lang t (Gram a) -> [t] -> Parsing (Either SyntaxError [a]) Source
Entry-point to run a parse. Allows ambiguity. Parses a list of tokens using a Lang/Gram definition. Returns all parses or a syntax-error.
Type describing a parse ambiguity for a specific non-terminal (name), across a position range. This may be reported as an error by the parse
entry point.
data ParseError Source
Union of SyntaxError
and Ambiguity
, for reporting errors from parse
.
Eq ParseError Source | |
Show ParseError Source |
parse :: (Show a, Show t) => Lang t (Gram a) -> [t] -> Parsing (Either ParseError a) Source
Entry-point to run a parse. Rejects ambiguity. Parse a list of tokens using a Lang/Gram definition. Returns the single parse or a parse-error.
Type to represent the effort taken during parsing. Used by some unit-tests.
Eff Int |