| Copyright | (C) 2026 - Eitan Chatav |
|---|---|
| License | BSD-style (see the file LICENSE) |
| Maintainer | Eitan Chatav <eitan.chatav@gmail.com> |
| Stability | provisional |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Control.Lens.Grammar.BackusNaur
Contents
Description
See Naur & Backus, et al. Report on the Algorithmic Language ALGOL 60.
Synopsis
- class Ruled bnf => BackusNaurForm bnf where
- class Ruled p where
- failP :: (Ruled p, DisjunctiveMonoid p) => String -> p
- ruleTyped :: (Typeable a, Ruled (p a a)) => p a a -> p a a
- class Tried p where
- try :: p -> p
- type RegGrammatical token (p :: Type -> Type -> Type) = (forall x y. (x ~ (), y ~ ()) => TerminalSymbol token (p x y), forall x y. (x ~ token, y ~ token) => TokenAlgebra token (p x y), Alternator p, forall x y. Tried (p x y))
- type Grammatical token (p :: Type -> Type -> Type) = (RegGrammatical token p, forall x y. BackusNaurForm (p x y))
- type CtxGrammatical token (p :: Type -> Type -> Type) = (Grammatical token p, Filtrator p, forall x. MonadPlus (p x), forall x. MonadFail (p x))
BackusNaurForm
class Ruled bnf => BackusNaurForm bnf where Source #
BackusNaurForm grammar combinators formalize traced
rule abstraction and general recursion with ruleRec,
related by this invariant.
prop> rule label bnf = ruleRec label (\_ -> bnf)
The BackusNaurForm interface is reminiscent of
two distinct notions of "trace".
First as a traced Cartesian monoidal category
which models general recursion abstractly,
and second as a trace-like label for rule abstraction.
The category (->) already has a traced (,)-monoidal structure
in the form of unfirst = loop
or equivalently the fixpoint function fix,
determining the default method for a BackusNaurForm,
by which the invariant holds for any Ruled rule.
prop> ruleRec label = rule label . fix
The BackusNaurForm interface permits overloading these methods,
and tracing them with a label.
Both context-free Grammars
& CtxGrammars
support the BackusNaurForm interface.
See Breitner, Showcasing Applicative,
for the original interface.
Minimal complete definition
Nothing
Instances
| (Ord rule, NonTerminalSymbol rule) => BackusNaurForm (Bnf rule) Source # | |
| BackusNaurForm (ReadP a) Source # | |
| BackusNaurForm (ReadPrec a) Source # | |
| BackusNaurForm (p a b) => BackusNaurForm (Precedence p a b) Source # | |
Defined in Control.Lens.Grammar.Prec Methods ruleRec :: String -> (Precedence p a b -> Precedence p a b) -> Precedence p a b Source # | |
| BackusNaurForm k => BackusNaurForm (Grammor k a b) Source # | |
| BackusNaurForm (Parsector s a b) Source # | |
| BackusNaurForm (Prettor w f a b) Source # | |
| BackusNaurForm (Parsor s m a b) Source # | |
| BackusNaurForm (Printor s m a b) Source # | |
| (forall x. BackusNaurForm (f x)) => BackusNaurForm (Joker f a b) Source # | |
Ruled grammar combinators formalize
rule abstraction, labeling a subgrammar,
which a BackusNaurForm names as a nonterminal,
& a parser may report on failure.
prop> rule _ = id
Minimal complete definition
Nothing
Instances
| (Ord rule, NonTerminalSymbol rule) => Ruled (Bnf rule) Source # | |
| Ruled (ReadP a) Source # | |
| Ruled (ReadPrec a) Source # | |
| Ruled (p a b) => Ruled (Precedence p a b) Source # | |
Defined in Control.Lens.Grammar.Prec Methods rule :: String -> Precedence p a b -> Precedence p a b Source # | |
| Ruled k => Ruled (Grammor k a b) Source # | |
| Ruled (Parsector s a b) Source # | |
| Ruled (Prettor w f a b) Source # | |
| Ruled (Parsor s m a b) Source # | |
| Ruled (Printor s m a b) Source # | |
| (forall x. Ruled (f x)) => Ruled (Joker f a b) Source # | |
failP :: (Ruled p, DisjunctiveMonoid p) => String -> p Source #
A nonterminal symbol to indicate a failure.
ruleTyped :: (Typeable a, Ruled (p a a)) => p a a -> p a a Source #
A rule for a Haskell type, like a type definition,
named by its Typeable type, so that each type's rule is its own.
Rule names follow a convention: a module-qualified word, like M.N.x,
names a definition by its qualified name, & may be displayed unqualified,
as x, whenever that's unambiguous. So a type's rule is named by its
fully qualified type, such as
Data.Map.Internal.Map Data.Text.Internal.Text GHC.Types.Int,
displayed as Map Text Int.
Tried grammar combinators mark subgrammars to backtrack
upon failure, by try, in a predictive parser,
like Parsector.
Use try on a subgrammar which isn't LL(1) but still is LL(∞).
try doesn't change the language of a grammar,
so the default, for grammars which can't record it, is
prop> try = id
Minimal complete definition
Nothing
Instances
| Tried rule => Tried (Bnf rule) Source # | |
| Tried (RegEx token) Source # | A |
| Tried (ReadP a) Source # | |
| Tried (ReadPrec a) Source # | |
| Tried (p a b) => Tried (Precedence p a b) Source # | |
Defined in Control.Lens.Grammar.Prec Methods try :: Precedence p a b -> Precedence p a b Source # | |
| Tried k => Tried (Grammor k a b) Source # | |
| Tried (Parsector s a b) Source # | |
| Tried (Prettor w f a b) Source # | |
| Tried (Parsor s m a b) Source # | |
| Tried (Printor s m a b) Source # | |
| (forall x. Tried (f x)) => Tried (Joker f a b) Source # | |
Grammatical
type RegGrammatical token (p :: Type -> Type -> Type) = (forall x y. (x ~ (), y ~ ()) => TerminalSymbol token (p x y), forall x y. (x ~ token, y ~ token) => TokenAlgebra token (p x y), Alternator p, forall x y. Tried (p x y)) Source #
The Alternators of terminal symbols, Tokenized combinators
& tokenClasses, which are Tried, & run
Control.Lens.Grammar.RegGrammars.
type Grammatical token (p :: Type -> Type -> Type) = (RegGrammatical token p, forall x y. BackusNaurForm (p x y)) Source #
The RegGrammatical BackusNaurForms, which run Grammars.
type CtxGrammatical token (p :: Type -> Type -> Type) = (Grammatical token p, Filtrator p, forall x. MonadPlus (p x), forall x. MonadFail (p x)) Source #
The Grammatical Filtrators, MonadPluses & MonadFails,
which run CtxGrammars.