| 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 BackusNaurForm bnf where
- data Bnf rule = Bnf {}
- liftBnf0 :: Ord a => a -> Bnf a
- liftBnf1 :: (Coercible a b, Ord b) => (a -> b) -> Bnf a -> Bnf b
- liftBnf2 :: (Coercible a c, Coercible b c, Ord c) => (a -> b -> c) -> Bnf a -> Bnf b -> Bnf c
- diffB :: (Categorized token, HasTrie token) => [token] -> Bnf (RegEx token) -> Bnf (RegEx token)
BackusNaurForm
class BackusNaurForm bnf where Source #
BackusNaurForm grammar combinators formalize traced
rule abstraction and general recursion with ruleRec,
related by this invariant.
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 default methods for a BackusNaurForm.
rule _ = id
ruleRec _ = 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
rule :: String -> bnf -> bnf Source #
Rule abstraction.
ruleRec :: String -> (bnf -> bnf) -> bnf Source #
General recursion.
Instances
| BackusNaurForm RegBnf Source # | |
| BackusNaurForm (ReadP a) Source # | |
| (Ord rule, NonTerminalSymbol rule) => BackusNaurForm (Bnf rule) Source # | |
| BackusNaurForm k => BackusNaurForm (Grammor k a b) Source # | |
| BackusNaurForm (Parsector s 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 # | |
A Bnf consists of a distinguished starting rule
and a set of named rules. When a Bnf supports NonTerminalSymbols,
then it supports the BackusNaurForm interface
by replacing recursive calls with nonTerminals.
ruleRec label f = rule label (f (nonTerminal label))
Instances
liftBnf1 :: (Coercible a b, Ord b) => (a -> b) -> Bnf a -> Bnf b Source #
Lift a function of rules to Bnfs.
liftBnf2 :: (Coercible a c, Coercible b c, Ord c) => (a -> b -> c) -> Bnf a -> Bnf b -> Bnf c Source #
Lift a binary function of rules to Bnfs.
diffB :: (Categorized token, HasTrie token) => [token] -> Bnf (RegEx token) -> Bnf (RegEx token) Source #
The Brzozowski derivative of a
RegExtended Bnf, with memoization.
word =~ diffB prefix pattern = prefix <> word =~ pattern
Unfortunately, despite elegance & optimization, Brzozowski's pattern matching algorithm is worst case exponential in grammar size. See Might, Darais & Spiewak, Parsing With Derivatives.