invertible-grammar-0.1.3: Invertible parsing combinators framework

Safe HaskellNone
LanguageHaskell2010

Data.InvertibleGrammar.TH

Synopsis

Documentation

grammarFor :: Name -> ExpQ Source #

Build a prism and the corresponding grammar that will match on the given constructor and convert it to reverse sequence of :- stacks.

E.g. consider a data type:

data FooBar a b c = Foo a b c | Bar

For constructor Foo

fooGrammar = $(grammarFor 'Foo)

will expand into

fooGrammar = PartialIso
  (\(c :- b :- a :- t) -> Foo a b c :- t)
  (\case { Foo a b c :- t -> Just $ c :- b :- a :- t; _ -> Nothing })

Note the order of elements on the stack:

ghci> :t fooGrammar
fooGrammar :: Grammar p (c :- (b :- (a :- t))) (FooBar a b c :- t)

match :: Name -> ExpQ Source #

Build prisms and corresponding grammars for all data constructors of given type. Expects grammars to zip built ones with.

$(match ''Maybe)

Will expand into a lambda:

(\nothingG justG -> ($(grammarFor 'Nothing) . nothingG) <>
                    ($(grammarFor 'Just)    . justG))