invertible-grammar-0.1.2: Invertible parsing combinators framework

Safe HaskellNone
LanguageHaskell2010

Data.InvertibleGrammar

Contents

Synopsis

Base

data Grammar p a b Source #

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.

Instances
Category (Grammar p :: Type -> Type -> Type) Source # 
Instance details

Defined in Data.InvertibleGrammar.Base

Methods

id :: Grammar p a a #

(.) :: Grammar p b c -> Grammar p a b -> Grammar p a c #

Semigroup (Grammar p a b) Source # 
Instance details

Defined in Data.InvertibleGrammar.Base

Methods

(<>) :: Grammar p a b -> Grammar p a b -> Grammar p a b #

sconcat :: NonEmpty (Grammar p a b) -> Grammar p a b #

stimes :: Integral b0 => b0 -> Grammar p a b -> Grammar p a b #

data h :- t infixr 5 Source #

"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 # 
Instance details

Defined in Data.InvertibleGrammar.Base

Methods

bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> (a :- b) -> f (c :- d) #

Bifoldable (:-) Source # 
Instance details

Defined in Data.InvertibleGrammar.Base

Methods

bifold :: Monoid m => (m :- m) -> m #

bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> (a :- b) -> m #

bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> (a :- b) -> c #

bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> (a :- b) -> c #

Bifunctor (:-) Source # 
Instance details

Defined in Data.InvertibleGrammar.Base

Methods

bimap :: (a -> b) -> (c -> d) -> (a :- c) -> b :- d #

first :: (a -> b) -> (a :- c) -> b :- c #

second :: (b -> c) -> (a :- b) -> a :- c #

Functor ((:-) h) Source # 
Instance details

Defined in Data.InvertibleGrammar.Base

Methods

fmap :: (a -> b) -> (h :- a) -> h :- b #

(<$) :: a -> (h :- b) -> h :- a #

Foldable ((:-) h) Source # 
Instance details

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 #

toList :: (h :- a) -> [a] #

null :: (h :- a) -> Bool #

length :: (h :- a) -> Int #

elem :: Eq a => a -> (h :- a) -> Bool #

maximum :: Ord a => (h :- a) -> a #

minimum :: Ord a => (h :- a) -> a #

sum :: Num a => (h :- a) -> a #

product :: Num a => (h :- a) -> a #

Traversable ((:-) h) Source # 
Instance details

Defined in Data.InvertibleGrammar.Base

Methods

traverse :: Applicative f => (a -> f b) -> (h :- a) -> f (h :- b) #

sequenceA :: Applicative f => (h :- f a) -> f (h :- a) #

mapM :: Monad m => (a -> m b) -> (h :- a) -> m (h :- b) #

sequence :: Monad m => (h :- m a) -> m (h :- a) #

(Eq h, Eq t) => Eq (h :- t) Source # 
Instance details

Defined in Data.InvertibleGrammar.Base

Methods

(==) :: (h :- t) -> (h :- t) -> Bool #

(/=) :: (h :- t) -> (h :- t) -> Bool #

(Show h, Show t) => Show (h :- t) Source # 
Instance details

Defined in Data.InvertibleGrammar.Base

Methods

showsPrec :: Int -> (h :- t) -> ShowS #

show :: (h :- t) -> String #

showList :: [h :- t] -> ShowS #

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 
Instances
Eq p => Eq (ErrorMessage p) Source # 
Instance details

Defined in Data.InvertibleGrammar.Monad

Ord p => Ord (ErrorMessage p) Source # 
Instance details

Defined in Data.InvertibleGrammar.Monad

Generic (ErrorMessage p) Source # 
Instance details

Defined in Data.InvertibleGrammar.Monad

Associated Types

type Rep (ErrorMessage p) :: Type -> Type #

Methods

from :: ErrorMessage p -> Rep (ErrorMessage p) x #

to :: Rep (ErrorMessage p) x -> ErrorMessage p #

Pretty p => Pretty (ErrorMessage p) Source # 
Instance details

Defined in Data.InvertibleGrammar.Monad

Methods

pretty :: ErrorMessage p -> Doc ann #

prettyList :: [ErrorMessage p] -> Doc ann #

type Rep (ErrorMessage p) Source # 
Instance details

Defined in Data.InvertibleGrammar.Monad

type Rep (ErrorMessage p) = D1 (MetaData "ErrorMessage" "Data.InvertibleGrammar.Monad" "invertible-grammar-0.1.2-Ew0PKDGpjqxJibdFZhsN2Z" False) (C1 (MetaCons "ErrorMessage" PrefixI True) ((S1 (MetaSel (Just "emPosition") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 p) :*: S1 (MetaSel (Just "emAnnotations") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [Text])) :*: (S1 (MetaSel (Just "emExpected") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (Set Text)) :*: S1 (MetaSel (Just "emGot") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (Maybe Text)))))

type ContextError c e a = ContextErrorT c e Identity a Source #

data Mismatch 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.