| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
Data.InvertibleGrammar
Synopsis
- 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.
Instances
| Bitraversable (:-) Source # | |
Defined in Data.InvertibleGrammar.Base Methods bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> (a :- b) -> f (c :- d) # | |
| Bifoldable (:-) Source # | |
| Bifunctor (:-) Source # | |
| Functor ((:-) h) Source # | |
| Foldable ((:-) h) Source # | |
Defined in Data.InvertibleGrammar.Base Methods fold :: Monoid m => (h :- m) -> m # foldMap :: Monoid m => (a -> m) -> (h :- a) -> m # foldr :: (a -> b -> b) -> b -> (h :- a) -> b # foldr' :: (a -> b -> b) -> b -> (h :- a) -> b # foldl :: (b -> a -> b) -> b -> (h :- a) -> b # foldl' :: (b -> a -> b) -> b -> (h :- a) -> b # foldr1 :: (a -> a -> a) -> (h :- a) -> a # foldl1 :: (a -> a -> a) -> (h :- a) -> a # elem :: Eq a => a -> (h :- a) -> Bool # maximum :: Ord a => (h :- a) -> a # minimum :: Ord a => (h :- a) -> a # | |
| Traversable ((:-) h) Source # | |
| (Eq h, Eq t) => Eq (h :- t) Source # | |
| (Show h, Show t) => Show (h :- t) Source # | |
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.
Constructors
| ErrorMessage | |
Fields
| |
Instances
type ContextError c e a = ContextErrorT c e Identity a Source #
data Propagation p Source #
Instances
| Eq (Propagation p) Source # | |
Defined in Data.InvertibleGrammar.Monad Methods (==) :: Propagation p -> Propagation p -> Bool # (/=) :: Propagation p -> Propagation p -> Bool # | |
| Ord (Propagation p) Source # | |
Defined in Data.InvertibleGrammar.Monad Methods compare :: Propagation p -> Propagation p -> Ordering # (<) :: Propagation p -> Propagation p -> Bool # (<=) :: Propagation p -> Propagation p -> Bool # (>) :: Propagation p -> Propagation p -> Bool # (>=) :: Propagation p -> Propagation p -> Bool # max :: Propagation p -> Propagation p -> Propagation p # min :: Propagation p -> Propagation p -> Propagation p # | |
| Show p => Show (Propagation p) Source # | |
Defined in Data.InvertibleGrammar.Monad Methods showsPrec :: Int -> Propagation p -> ShowS # show :: Propagation p -> String # showList :: [Propagation p] -> ShowS # | |
data GrammarError p Source #
Instances
| Show p => Show (GrammarError p) Source # | |
Defined in Data.InvertibleGrammar.Monad Methods showsPrec :: Int -> GrammarError p -> ShowS # show :: GrammarError p -> String # showList :: [GrammarError p] -> ShowS # | |
| Semigroup (GrammarError p) Source # | |
Defined in Data.InvertibleGrammar.Monad Methods (<>) :: GrammarError p -> GrammarError p -> GrammarError p # sconcat :: NonEmpty (GrammarError p) -> GrammarError p # stimes :: Integral b => b -> GrammarError p -> GrammarError p # | |
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.