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

Description

Regular expressions form a Kleene star algebra. See Kleene, Representation of Events in Nerve Nets and Finite Automata

Synopsis

RegEx

data RegEx token Source #

The RegExpression type forms the prototypical KleeneStarAlgebra. It is also a TokenAlgebra and tokenClass acts homomorphically on disjunction. RegExpressions may be understood as normalizations of KleeneStarAlgebra expressions generated by lexical token expressions.

Constructors

SeqEmpty 
Sequence (RegEx token) (RegEx token) [RegEx token]

Two or more items, none of them sequences or empty.

NonTerminal String 
KleeneStar (RegEx token) 
KleeneOpt (RegEx token) 
KleenePlus (RegEx token) 
KleeneTimes Int (Maybe Int) (RegEx token)

Between some numbers of times, inclusive, x{lo,hi} or x{lo,}, normalized by quantK: either unbounded, at least twice, or bounded, at most twice or more, never below its lower bound, at least none; other counts being KleeneStar, KleenePlus, KleeneOpt, the item itself, or empty; of an item neither empty nor failing.

Backtracking (RegEx token)

A pattern to backtrack from, by try, whose language is its own.

RegExam (TokenExam token (RegEx token)) 

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

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

Categorized token => TerminalSymbol token (RegEx token) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

terminal :: [token] -> RegEx token Source #

Categorized token => TokenAlgebra token (RegEx token) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

tokenClass :: TokenClass token -> RegEx token Source #

Categorized token => Tokenized token (RegEx token) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

anyToken :: RegEx token Source #

token :: token -> RegEx token Source #

oneOf :: Foldable f => f token -> RegEx token Source #

notOneOf :: Foldable f => f token -> RegEx token Source #

asIn :: Categorize token -> RegEx token Source #

notAsIn :: Categorize token -> RegEx token Source #

(Categorized token, HasTrie token) => HasTrie (RegEx token) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Associated Types

data (RegEx token) :->: b 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

trie :: (RegEx token -> b) -> RegEx token :->: b #

untrie :: (RegEx token :->: b) -> RegEx token -> b #

enumerate :: (RegEx token :->: b) -> [(RegEx token, b)] #

Categorized token => DisjunctiveMonoid (RegEx token) Source #

Alternatives are associative, as normalized by alternated.

Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

falseP :: RegEx token #

(>|<) :: RegEx token -> RegEx token -> RegEx token #

Categorized token => KleeneStarAlgebra (RegEx token) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

starK :: RegEx token -> RegEx token #

plusK :: RegEx token -> RegEx token #

optK :: RegEx token -> RegEx token #

quantK :: Int -> Maybe Int -> RegEx token -> RegEx token #

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 #

Categorized token => LaidOut (RegEx token) Source #

Either layout, any indentation.

Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

flatAltP :: RegEx token -> RegEx token -> RegEx token Source #

nestP :: Int -> RegEx token -> RegEx token Source #

groupP :: RegEx token -> RegEx token Source #

alignP :: RegEx token -> RegEx token Source #

Lined (RegEx Char) Source #

A newline, then any indentation.

Instance details

Defined in Control.Lens.Grammar.Pretty

NonTerminalSymbol (RegEx token) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

nonTerminal :: String -> RegEx token Source #

Categorized token => Monoid (RegEx token) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

mempty :: RegEx token #

mappend :: RegEx token -> RegEx token -> RegEx token #

mconcat :: [RegEx token] -> RegEx token #

Categorized token => Semigroup (RegEx token) Source #

Sequences are associative, as items normalized by sequenced.

Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

(<>) :: RegEx token -> RegEx token -> RegEx token #

sconcat :: NonEmpty (RegEx token) -> RegEx token #

stimes :: Integral b => b -> RegEx token -> RegEx token #

token ~ Char => IsString (RegEx token) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

fromString :: String -> RegEx token #

Generic (RegEx token) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Associated Types

type Rep (RegEx token) 
Instance details

Defined in Control.Lens.Grammar.RegEx

type Rep (RegEx token) = D1 ('MetaData "RegEx" "Control.Lens.Grammar.RegEx" "distributors-grammar-0.1.0.0-inplace" 'False) (((C1 ('MetaCons "SeqEmpty" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Sequence" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (RegEx token)) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (RegEx token)) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [RegEx token])))) :+: (C1 ('MetaCons "NonTerminal" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 String)) :+: C1 ('MetaCons "KleeneStar" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (RegEx token))))) :+: ((C1 ('MetaCons "KleeneOpt" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (RegEx token))) :+: C1 ('MetaCons "KleenePlus" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (RegEx token)))) :+: (C1 ('MetaCons "KleeneTimes" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe Int)) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (RegEx token)))) :+: (C1 ('MetaCons "Backtracking" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (RegEx token))) :+: C1 ('MetaCons "RegExam" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (TokenExam token (RegEx token))))))))

Methods

from :: RegEx token -> Rep (RegEx token) x #

to :: Rep (RegEx token) x -> RegEx token #

token ~ Char => IsList (RegEx token) Source #

A regular expression string, by its Grammar.

Instance details

Defined in Control.Lens.Grammar.RegEx

Associated Types

type Item (RegEx token) 
Instance details

Defined in Control.Lens.Grammar.RegEx

type Item (RegEx token) = token

Methods

fromList :: [Item (RegEx token)] -> RegEx token #

fromListN :: Int -> [Item (RegEx token)] -> RegEx token #

toList :: RegEx token -> [Item (RegEx token)] #

(Categorized token, Read token, Read (Categorize token)) => Read (RegEx token) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

readsPrec :: Int -> ReadS (RegEx token) #

readList :: ReadS [RegEx token] #

readPrec :: ReadPrec (RegEx token) #

readListPrec :: ReadPrec [RegEx token] #

(Categorized token, Show token, Show (Categorize token)) => Show (RegEx token) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

showsPrec :: Int -> RegEx token -> ShowS #

show :: RegEx token -> String #

showList :: [RegEx token] -> ShowS #

Categorized token => Eq (RegEx token) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

(==) :: RegEx token -> RegEx token -> Bool #

(/=) :: RegEx token -> RegEx token -> Bool #

Categorized token => Ord (RegEx token) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

compare :: RegEx token -> RegEx token -> Ordering #

(<) :: RegEx token -> RegEx token -> Bool #

(<=) :: RegEx token -> RegEx token -> Bool #

(>) :: RegEx token -> RegEx token -> Bool #

(>=) :: RegEx token -> RegEx token -> Bool #

max :: RegEx token -> RegEx token -> RegEx token #

min :: RegEx token -> RegEx token -> RegEx token #

Categorized token => Matching [token] (Bnf (RegEx token)) Source # 
Instance details

Defined in Control.Lens.Grammar.Machine

Methods

(=~) :: [token] -> Bnf (RegEx token) -> Bool Source #

Categorized token => Matching [token] (RegEx token) Source # 
Instance details

Defined in Control.Lens.Grammar.Machine

Methods

(=~) :: [token] -> RegEx token -> Bool Source #

data (RegEx token) :->: b Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

type Rep (RegEx token) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

type Rep (RegEx token) = D1 ('MetaData "RegEx" "Control.Lens.Grammar.RegEx" "distributors-grammar-0.1.0.0-inplace" 'False) (((C1 ('MetaCons "SeqEmpty" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Sequence" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (RegEx token)) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (RegEx token)) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [RegEx token])))) :+: (C1 ('MetaCons "NonTerminal" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 String)) :+: C1 ('MetaCons "KleeneStar" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (RegEx token))))) :+: ((C1 ('MetaCons "KleeneOpt" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (RegEx token))) :+: C1 ('MetaCons "KleenePlus" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (RegEx token)))) :+: (C1 ('MetaCons "KleeneTimes" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Maybe Int)) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (RegEx token)))) :+: (C1 ('MetaCons "Backtracking" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (RegEx token))) :+: C1 ('MetaCons "RegExam" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (TokenExam token (RegEx token))))))))
type Item (RegEx token) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

type Item (RegEx token) = token

Bnf

data Bnf rule 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.

prop> ruleRec label f = rule label (f (nonTerminal label))

Constructors

Bnf 

Fields

Instances

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

Defined in Control.Lens.Grammar.RegEx

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

(Ord rule, TerminalSymbol token rule) => TerminalSymbol token (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

terminal :: [token] -> Bnf rule Source #

(Ord rule, TokenAlgebra token rule) => TokenAlgebra token (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

tokenClass :: TokenClass token -> Bnf rule Source #

(Ord rule, Tokenized token rule) => Tokenized token (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

anyToken :: Bnf rule Source #

token :: token -> Bnf rule Source #

oneOf :: Foldable f => f token -> Bnf rule Source #

notOneOf :: Foldable f => f token -> Bnf rule Source #

asIn :: Categorize token -> Bnf rule Source #

notAsIn :: Categorize token -> Bnf rule Source #

(Ord rule, DisjunctiveMonoid rule) => DisjunctiveMonoid (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

falseP :: Bnf rule #

(>|<) :: Bnf rule -> Bnf rule -> Bnf rule #

(Ord rule, KleeneStarAlgebra rule) => KleeneStarAlgebra (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

starK :: Bnf rule -> Bnf rule #

plusK :: Bnf rule -> Bnf rule #

optK :: Bnf rule -> Bnf rule #

quantK :: Int -> Maybe Int -> Bnf rule -> Bnf rule #

(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 #

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

Defined in Control.Lens.Grammar.RegEx

Methods

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

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 #

(Ord rule, LaidOut rule) => LaidOut (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

flatAltP :: Bnf rule -> Bnf rule -> Bnf rule Source #

nestP :: Int -> Bnf rule -> Bnf rule Source #

groupP :: Bnf rule -> Bnf rule Source #

alignP :: Bnf rule -> Bnf rule Source #

(Ord rule, Lined rule) => Lined (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.Pretty

Methods

hardlineP :: Bnf rule Source #

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

Defined in Control.Lens.Grammar.RegEx

Methods

nonTerminal :: String -> Bnf rule Source #

(Ord rule, Monoid rule) => Monoid (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

mempty :: Bnf rule #

mappend :: Bnf rule -> Bnf rule -> Bnf rule #

mconcat :: [Bnf rule] -> Bnf rule #

(Ord rule, Semigroup rule) => Semigroup (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

(<>) :: Bnf rule -> Bnf rule -> Bnf rule #

sconcat :: NonEmpty (Bnf rule) -> Bnf rule #

stimes :: Integral b => b -> Bnf rule -> Bnf rule #

rule ~ RegEx Char => IsString (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

fromString :: String -> Bnf rule #

rule ~ RegEx Char => IsList (Bnf rule) Source #

A Bnf of regular expression strings, by its Grammar.

Instance details

Defined in Control.Lens.Grammar.RegEx

Associated Types

type Item (Bnf rule) 
Instance details

Defined in Control.Lens.Grammar.RegEx

type Item (Bnf rule) = Char

Methods

fromList :: [Item (Bnf rule)] -> Bnf rule #

fromListN :: Int -> [Item (Bnf rule)] -> Bnf rule #

toList :: Bnf rule -> [Item (Bnf rule)] #

(Read rule, Ord rule) => Read (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

readsPrec :: Int -> ReadS (Bnf rule) #

readList :: ReadS [Bnf rule] #

readPrec :: ReadPrec (Bnf rule) #

readListPrec :: ReadPrec [Bnf rule] #

Show rule => Show (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

showsPrec :: Int -> Bnf rule -> ShowS #

show :: Bnf rule -> String #

showList :: [Bnf rule] -> ShowS #

Eq rule => Eq (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

(==) :: Bnf rule -> Bnf rule -> Bool #

(/=) :: Bnf rule -> Bnf rule -> Bool #

Ord rule => Ord (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

Methods

compare :: Bnf rule -> Bnf rule -> Ordering #

(<) :: Bnf rule -> Bnf rule -> Bool #

(<=) :: Bnf rule -> Bnf rule -> Bool #

(>) :: Bnf rule -> Bnf rule -> Bool #

(>=) :: Bnf rule -> Bnf rule -> Bool #

max :: Bnf rule -> Bnf rule -> Bnf rule #

min :: Bnf rule -> Bnf rule -> Bnf rule #

Categorized token => Matching [token] (Bnf (RegEx token)) Source # 
Instance details

Defined in Control.Lens.Grammar.Machine

Methods

(=~) :: [token] -> Bnf (RegEx token) -> Bool Source #

type Item (Bnf rule) Source # 
Instance details

Defined in Control.Lens.Grammar.RegEx

type Item (Bnf rule) = Char

liftBnf0 :: Ord a => a -> Bnf a Source #

Lift a rule to a Bnf.

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.

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

Regular expression strings