Safe Haskell | None |
---|---|
Language | Haskell2010 |
- data Grammar p a b
- data h :- t
- forward :: Grammar p a b -> a -> ContextError (Propagation p) (GrammarError p) b
- backward :: Grammar p a b -> b -> ContextError (Propagation p) (GrammarError p) a
- module Data.InvertibleGrammar.Combinators
- runGrammar :: p -> ContextError (Propagation p) (GrammarError p) a -> Either (ErrorMessage p) a
- runGrammarDoc :: Pretty p => p -> ContextError (Propagation p) (GrammarError p) a -> Either (Doc ann) a
- runGrammarString :: Show p => p -> ContextError (Propagation p) (GrammarError p) a -> Either String a
- data ErrorMessage p = ErrorMessage {
- emPosition :: p
- emAnnotations :: [Text]
- emExpected :: Set Text
- emGot :: Maybe Text
- type ContextError c e a = ContextErrorT c e Identity a
- data Propagation p
- data GrammarError p
- data Mismatch
- expected :: Text -> Mismatch
- unexpected :: Text -> Mismatch
Base
Representation of an invertible grammar -- a grammar that can be run either "forwards" and "backwards".
For a grammar Grammar p a b
, running it forwards will take a
value of type a
and possibly produce a value of type b
. Running
it backwards will take a value of type b
and possibly produce an
a
. If a value cannot be produced, an error message is generated.
As a common example, running a Grammar
forwards corresponds to
parsing and running backwards corresponds to prettyprinting.
That is, the grammar defines a partial isomorphism between two values.
"Cons" pair of a heterogenous list or a stack with potentially
polymophic tail. E.g. "first" :- 2 :- (3,4) :- t
Isomorphic to a tuple with two elments, but is much more convenient for nested pairs.
forward :: Grammar p a b -> a -> ContextError (Propagation p) (GrammarError p) b Source #
Run Grammar
forwards.
For Grammar p a b
, given a value of type a
tries to produce a
value of type b
, otherwise reports an error with position of type
p
.
backward :: Grammar p a b -> b -> ContextError (Propagation p) (GrammarError p) a Source #
Run Grammar
backwards.
For Grammar p a b
, given a value of type b
tries to produce a
value of type a
, otherwise reports an error with position of type
p
.
Combinators
Running grammars
runGrammar :: p -> ContextError (Propagation p) (GrammarError p) a -> Either (ErrorMessage p) a Source #
Run a forward
or backward
pass of a Grammar
.
runGrammarDoc :: Pretty p => p -> ContextError (Propagation p) (GrammarError p) a -> Either (Doc ann) a Source #
Run a forward
or backward
pass of a Grammar
, report errors
as pretty printed Doc
message.
runGrammarString :: Show p => p -> ContextError (Propagation p) (GrammarError p) a -> Either String a Source #
Run a forward
or backward
pass of a Grammar
, report errors
as String
message.
Error messages
data ErrorMessage p Source #
Grammar
run error messages type.
ErrorMessage | |
|
Eq p => Eq (ErrorMessage p) Source # | |
Ord p => Ord (ErrorMessage p) Source # | |
Generic (ErrorMessage p) Source # | |
Pretty p => Pretty (ErrorMessage p) Source # | |
type Rep (ErrorMessage p) Source # | |
type ContextError c e a = ContextErrorT c e Identity a Source #
data Propagation p Source #
Eq (Propagation p) Source # | |
Ord (Propagation p) Source # | |
Show p => Show (Propagation p) Source # | |
data GrammarError p Source #
Show p => Show (GrammarError p) Source # | |
Semigroup (GrammarError p) Source # | |
Data type to encode mismatches during parsing or generation, kept
abstract. Use expected
and unexpected
constructors to build a
mismatch report.
expected :: Text -> Mismatch Source #
Construct a mismatch report with specified expectation. Can be
appended to other expectations and unexpected
reports to clarify
a mismatch.
unexpected :: Text -> Mismatch Source #
Construct a mismatch report with information what occurred during the processing but was not expected.