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.Class

Contents

Description

The classes of types' grammars, over their tokens, regular, context-free & context-sensitive, & numerals, by their digits. Haskell types' grammars, equivalent to their Read & Show instances, are in Control.Lens.Grammar.Prec.

Synopsis

Documentation

class Grammar token a => RegGrammar token a where Source #

A type's regular grammar, over its tokens, which is also its context-free & context-sensitive grammar.

Methods

regGrammar :: RegGrammatical token p => p a a Source #

class CtxGrammar token a => Grammar token a where Source #

A type's context-free grammar, over its tokens, which is also its context-sensitive grammar; its regular grammar, by default.

Minimal complete definition

Nothing

Methods

grammar :: Grammatical token p => p a a Source #

default grammar :: (RegGrammar token a, Grammatical token p) => p a a Source #

Instances

Instances details
Grammar Char (Bnf (RegEx Char)) Source #

The context-free Grammar of Bnfs of RegExpressions of characters, by which they're IsString & IsList. That means that it can generate a self-hosted definition.

Bnfs of RegExpressions of characters are an embedded domain specific language of Backus-Naur forms extended by regular expression strings.

Such a Bnf consists of a distinguished "start" RegEx, and a set of named RegEx rules.

>>> putStringLn (rule "baz" (terminal "foo" >|< terminal "bar") :: Bnf (RegEx Char))
{start} = \q{baz}
{baz} = foo|bar

Like RegExpressions, they have a string-like interface.

>>> let bnf = fromString "{start} = foo|bar" :: Bnf (RegEx Char)
>>> putStringLn bnf
{start} = foo|bar
>>> toList bnf
"{start} = foo|bar"
>>> :type toList bnf
toList bnf :: [Char]

They can be generated from context-free Control.Lens.Grammar.Grammars with Control.Lens.Grammar.regbnfG.

Like RegExpressions, they can be constructed using terminal, Tokenized, Monoid and KleeneStarAlgebra combinators. But they also support BackusNaurForm rules and ruleRecs.

>>> putStringLn (rule "baz" (bnf >|< terminal "baz"))
{start} = \q{baz}
{baz} = foo|bar|baz
>>> putStringLn (ruleRec "∞-loop" (\x -> x) :: Bnf (RegEx Char))
{start} = \q{∞-loop}
{∞-loop} = \q{∞-loop}
>>> putStringLn (regbnfG (type (Bnf (RegEx Char))))
{start} = \q{regbnf}
{alternate} = \q{sequence}(\|\q{sequence})*
{atom} = \\q\q{nonterminal}|\\t\{\q{regex}\}|\q{class}|\(\q{regex}\)
{bounds} = \q{count}(,\q{count}?)?
{category} = Ll|Lu|Lt|Lm|Lo|Mn|Mc|Me|Nd|Nl|No|Pc|Pd|Ps|Pe|Pi|Pf|Po|Sm|Sc|Sk|So|Zs|Zl|Zp|Cc|Cf|Cs|Co|Cn
{char} = [^\(\)\*\+\?\[\\\]\^\{\|\}\P{Cc}]|\\\q{char-escaped}
{char-control} = x\{[0-9A-Fa-f]+\}
{char-escaped} = [\(\)\*\+\?\[\\\]\^\{\|\}]|\q{char-control}
{class} = \q{class-one-of}|\q{class-not-one-of}
{class-category} = \\p\{\q{category}\}|\\P\{\q{category}(\|\q{category})*\}
{class-char} = [^\(\)\*\+\-\?\[\\\]\^\{\|\}\P{Cc}]|\\\q{class-char-escaped}
{class-char-escaped} = [\(\)\*\+\-\?\[\\\]\^\{\|\}]|\q{char-control}
{class-not-one-of} = \q{class-category}|\[\^\q{class-range}*\q{class-category}?\]
{class-one-of} = \q{char}|\[\q{class-range}*\]
{class-range} = \q{class-char}(-\q{class-char})?
{count} = [0-9]+
{expression} = \q{atom}\?|\q{atom}\*|\q{atom}\+|\q{atom}\{\q{bounds}\}|\q{atom}
{nonterminal} = \{\q{char}*\}
{regbnf} = \{start\} = \q{regex}(\x{a}\q{nonterminal} = \q{regex})*
{regex} = \q{alternate}
{sequence} = \q{expression}*
Instance details

Defined in Control.Lens.Grammar.RegEx

Grammar Char (RegEx Char) Source #

The context-free Grammar of RegExpressions of characters, in the syntax of regular expression strings, by which they're IsString & IsList. It can't be a Control.Lens.Grammar.RegGrammar, since regular expression strings include parenthesization. But balanceBy parentheses are a context-free language.

RegExpressions of characters are an embedded domain specific language of regular expression strings.

Since they are strings, they have a string-like interface.

>>> let rex = fromString "ab|c" :: RegEx Char
>>> putStringLn rex
ab|c
>>> toList rex
"ab|c"

They can be generated from Control.Lens.Grammar.RegGrammars with Control.Lens.Grammar.regexG.

>>> toList (runGrammor (terminal "a" >* terminal "b" >|< terminal "c") :: RegEx Char)
"ab|c"

A regular expression string is stored as an algebraic datatype, a RegEx.

>>> rex
RegExam (Alternate (Sequence (RegExam (OneOf (fromList "a"))) (RegExam (OneOf (fromList "b"))) []) (RegExam (OneOf (fromList "c"))) [])

They're similar to regular expression strings in many other programming languages. We can use them to see if a word and pattern are Matching.

>>> "ab" =~ rex
True
>>> "c" =~ rex
True
>>> "xyz" =~ rex
False

Like Control.Lens.Grammar.RegGrammars, they can use all the terminal & Tokenized combinators. Unlike Control.Lens.Grammar.RegGrammars, instead of using Monoidal and Alternator combinators, they use Monoid and KleeneStarAlgebra combinators.

>>> toList (terminal "a" <> terminal "b" >|< terminal "c" :: RegEx Char)
"ab|c"
>>> toList (mempty :: RegEx Char)
""

Since they're a KleeneStarAlgebra, they support Kleene quantifiers.

>>> toList (starK rex)
"(ab|c)*"
>>> toList (plusK rex)
"(ab|c)+"
>>> toList (optK rex)
"(ab|c)?"

Like other regular expression languages, they support character classes.

>>> toList (oneOf "abc" :: RegEx Char)
"[a-c]"
>>> toList (notOneOf "abc" :: RegEx Char)
"[^a-c]"

The character classes are used for failure, matching no character or string, as well as the wildcard, matching any single character.

>>> toList (falseP :: RegEx Char)
"[]"
>>> toList (anyToken :: RegEx Char)
"[^]"

Additional forms of character classes test for a character's GeneralCategory.

>>> toList (asIn LowercaseLetter :: RegEx Char)
"\\p{Ll}"
>>> toList (notAsIn Control :: RegEx Char)
"\\P{Cc}"

KleeneStarAlgebras support alternation >|<, and the Tokenized combinators are all negatable. However, we'd like to be able to take the intersection of character classes as well. they can combine characters' tokenClasses using BooleanAlgebra combinators.

>>> toList (tokenClass (notOneOf "abc" >&< notOneOf "xyz") :: RegEx Char)
"[^a-cx-z]"
>>> toList (tokenClass (oneOf "abcxyz" >&< notOneOf "xyz") :: RegEx Char)
"[a-c]"
>>> toList (tokenClass (notOneOf "#$%" >&< notAsIn Control) :: RegEx Char)
"[^#-%\\P{Cc}]"
>>> toList (tokenClass (allP notAsIn [MathSymbol, Control]) :: RegEx Char)
"\\P{Sm|Cc}"
>>> toList (tokenClass (notP (oneOf "xyz")) :: RegEx Char)
"[^x-z]"

Ill-formed regular expression strings normalize to failure.

>>> toList (fromString ")(" :: RegEx Char)
"[]"
>>> putStringLn (regbnfG (RegEx Char))
{start} = \q{regex}
{alternate} = \q{sequence}(\|\q{sequence})*
{atom} = \\q\q{nonterminal}|\\t\{\q{regex}\}|\q{class}|\(\q{regex}\)
{bounds} = \q{count}(,\q{count}?)?
{category} = Ll|Lu|Lt|Lm|Lo|Mn|Mc|Me|Nd|Nl|No|Pc|Pd|Ps|Pe|Pi|Pf|Po|Sm|Sc|Sk|So|Zs|Zl|Zp|Cc|Cf|Cs|Co|Cn
{char} = [^\(\)\*\+\?\[\\\]\^\{\|\}\P{Cc}]|\\\q{char-escaped}
{char-control} = x\{[0-9A-Fa-f]+\}
{char-escaped} = [\(\)\*\+\?\[\\\]\^\{\|\}]|\q{char-control}
{class} = \q{class-one-of}|\q{class-not-one-of}
{class-category} = \\p\{\q{category}\}|\\P\{\q{category}(\|\q{category})*\}
{class-char} = [^\(\)\*\+\-\?\[\\\]\^\{\|\}\P{Cc}]|\\\q{class-char-escaped}
{class-char-escaped} = [\(\)\*\+\-\?\[\\\]\^\{\|\}]|\q{char-control}
{class-not-one-of} = \q{class-category}|\[\^\q{class-range}*\q{class-category}?\]
{class-one-of} = \q{char}|\[\q{class-range}*\]
{class-range} = \q{class-char}(-\q{class-char})?
{count} = [0-9]+
{expression} = \q{atom}\?|\q{atom}\*|\q{atom}\+|\q{atom}\{\q{bounds}\}|\q{atom}
{nonterminal} = \{\q{char}*\}
{regex} = \q{alternate}
{sequence} = \q{expression}*
Instance details

Defined in Control.Lens.Grammar.RegEx

class CtxGrammar token a where Source #

A type's context-sensitive grammar, over its tokens; its context-free grammar, by default.

Minimal complete definition

Nothing

Methods

ctxGrammar :: CtxGrammatical token p => p a a Source #

default ctxGrammar :: (Grammar token a, CtxGrammatical token p) => p a a Source #

Instances

Instances details
CtxGrammar Char (Bnf (RegEx Char)) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

CtxGrammar Char (RegEx Char) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Numerals

digit :: (Integral n, Choice p, Tokenized Char (p Char Char)) => n -> p n n Source #

A digit in a base, from 2 to 36, by its value: 0-9, then letters, printing in lowercase, & parsing either case.

>>> [d | (d, "") <- parseP (digit (16 :: Int) :: Parsor String [] Int Int) "F"]
[15]

numeral :: (Integral n, Alternator p, Tokenized Char (p Char Char)) => n -> p n n Source #

A natural number in a base, from 2 to 36, by its digits, most significant first, printing without leading zeros, & parsing them too, an improper iso.

>>> ($ "") <$> printP (numeral (16 :: Integer) :: Printor String Maybe Integer Integer) 255
Just "ff"