distributors-grammar
Copyright(C) 2026 - Eitan Chatav
LicenseBSD-style (see the file LICENSE)
MaintainerEitan Chatav <eitan.chatav@gmail.com>
Stabilityprovisional
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Control.Lens.Grammar.BackusNaur

Description

Synopsis

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

Methods

ruleRec :: String -> (bnf -> bnf) -> bnf Source #

General recursion.

Instances

Instances details
(Ord rule, NonTerminalSymbol rule) => BackusNaurForm (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

ruleRec :: String -> (Bnf rule -> Bnf rule) -> Bnf rule Source #

BackusNaurForm (ReadP a) Source # 
Instance details

Defined in Control.Lens.Grammar.BackusNaur

Methods

ruleRec :: String -> (ReadP a -> ReadP a) -> ReadP a Source #

BackusNaurForm (ReadPrec a) Source # 
Instance details

Defined in Control.Lens.Grammar.BackusNaur

Methods

ruleRec :: String -> (ReadPrec a -> ReadPrec a) -> ReadPrec a Source #

BackusNaurForm (p a b) => BackusNaurForm (Precedence p a b) Source # 
Instance details

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

Defined in Data.Profunctor.Grammar

Methods

ruleRec :: String -> (Grammor k a b -> Grammor k a b) -> Grammor k a b Source #

BackusNaurForm (Parsector s a b) Source # 
Instance details

Defined in Data.Profunctor.Grammar.Parsector

Methods

ruleRec :: String -> (Parsector s a b -> Parsector s a b) -> Parsector s a b Source #

BackusNaurForm (Prettor w f a b) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

ruleRec :: String -> (Prettor w f a b -> Prettor w f a b) -> Prettor w f a b Source #

BackusNaurForm (Parsor s m a b) Source # 
Instance details

Defined in Data.Profunctor.Grammar

Methods

ruleRec :: String -> (Parsor s m a b -> Parsor s m a b) -> Parsor s m a b Source #

BackusNaurForm (Printor s m a b) Source # 
Instance details

Defined in Data.Profunctor.Grammar.Printor

Methods

ruleRec :: String -> (Printor s m a b -> Printor s m a b) -> Printor s m a b Source #

(forall x. BackusNaurForm (f x)) => BackusNaurForm (Joker f a b) Source # 
Instance details

Defined in Control.Lens.Grammar.BackusNaur

Methods

ruleRec :: String -> (Joker f a b -> Joker f a b) -> Joker f a b Source #

class Ruled p where 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

Methods

rule :: String -> p -> p Source #

Subgrammar abstraction.

Instances

Instances details
(Ord rule, NonTerminalSymbol rule) => Ruled (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

rule :: String -> Bnf rule -> Bnf rule Source #

Ruled (ReadP a) Source # 
Instance details

Defined in Control.Lens.Grammar.BackusNaur

Methods

rule :: String -> ReadP a -> ReadP a Source #

Ruled (ReadPrec a) Source # 
Instance details

Defined in Control.Lens.Grammar.BackusNaur

Methods

rule :: String -> ReadPrec a -> ReadPrec a Source #

Ruled (p a b) => Ruled (Precedence p a b) Source # 
Instance details

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

Defined in Data.Profunctor.Grammar

Methods

rule :: String -> Grammor k a b -> Grammor k a b Source #

Ruled (Parsector s a b) Source # 
Instance details

Defined in Data.Profunctor.Grammar.Parsector

Methods

rule :: String -> Parsector s a b -> Parsector s a b Source #

Ruled (Prettor w f a b) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

rule :: String -> Prettor w f a b -> Prettor w f a b Source #

Ruled (Parsor s m a b) Source # 
Instance details

Defined in Data.Profunctor.Grammar

Methods

rule :: String -> Parsor s m a b -> Parsor s m a b Source #

Ruled (Printor s m a b) Source # 
Instance details

Defined in Data.Profunctor.Grammar.Printor

Methods

rule :: String -> Printor s m a b -> Printor s m a b Source #

(forall x. Ruled (f x)) => Ruled (Joker f a b) Source # 
Instance details

Defined in Control.Lens.Grammar.BackusNaur

Methods

rule :: String -> Joker f a b -> 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.

class Tried p where Source #

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

Methods

try :: p -> p Source #

Backtracking upon failure.

Instances

Instances details
Tried rule => Tried (Bnf rule) Source #

try the start of a Bnf, recording it as its rules do.

Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

try :: Bnf rule -> Bnf rule Source #

Tried (RegEx token) Source #

A RegEx records try, but for patterns of the empty word or none, which can't fail after reading a token.

Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

try :: RegEx token -> RegEx token Source #

Tried (ReadP a) Source # 
Instance details

Defined in Control.Lens.Grammar.BackusNaur

Methods

try :: ReadP a -> ReadP a Source #

Tried (ReadPrec a) Source # 
Instance details

Defined in Control.Lens.Grammar.BackusNaur

Methods

try :: ReadPrec a -> ReadPrec a Source #

Tried (p a b) => Tried (Precedence p a b) Source # 
Instance details

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

Defined in Data.Profunctor.Grammar

Methods

try :: Grammor k a b -> Grammor k a b Source #

Tried (Parsector s a b) Source # 
Instance details

Defined in Data.Profunctor.Grammar.Parsector

Methods

try :: Parsector s a b -> Parsector s a b Source #

Tried (Prettor w f a b) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

try :: Prettor w f a b -> Prettor w f a b Source #

Tried (Parsor s m a b) Source # 
Instance details

Defined in Data.Profunctor.Grammar

Methods

try :: Parsor s m a b -> Parsor s m a b Source #

Tried (Printor s m a b) Source # 
Instance details

Defined in Data.Profunctor.Grammar.Printor

Methods

try :: Printor s m a b -> Printor s m a b Source #

(forall x. Tried (f x)) => Tried (Joker f a b) Source # 
Instance details

Defined in Control.Lens.Grammar.BackusNaur

Methods

try :: Joker f a b -> 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.