| 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.RegEx
Description
Regular expressions form a Kleene star algebra. See Kleene, Representation of Events in Nerve Nets and Finite Automata
Synopsis
- data RegEx token
- = SeqEmpty
- | Sequence (RegEx token) (RegEx token) [RegEx token]
- | NonTerminal String
- | KleeneStar (RegEx token)
- | KleeneOpt (RegEx token)
- | KleenePlus (RegEx token)
- | KleeneTimes Int (Maybe Int) (RegEx token)
- | Backtracking (RegEx token)
- | RegExam (TokenExam token (RegEx token))
- 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)
RegEx
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, |
| Backtracking (RegEx token) | A pattern to backtrack from, by |
| RegExam (TokenExam token (RegEx token)) |
Instances
| CtxGrammar Char (Bnf (RegEx Char)) Source # | |||||
Defined in Control.Lens.Grammar.RegEx | |||||
| CtxGrammar Char (RegEx Char) Source # | |||||
Defined in Control.Lens.Grammar.RegEx Methods ctxGrammar :: CtxGrammatical Char p => p (RegEx Char) (RegEx Char) Source # | |||||
| Grammar Char (Bnf (RegEx Char)) Source # | The context-free
Such a
Like
They can be generated from context-free Like
| ||||
| Grammar Char (RegEx Char) Source # | The context-free
Since they are strings, they have a string-like interface.
They can be generated from
A regular expression string is stored as an algebraic datatype, a
They're similar to regular expression strings in many other
programming languages. We can use them to see if a word and pattern
are
Like
Since they're a
Like other regular expression languages, they support character classes.
The character classes are used for failure, matching no character or string, as well as the wildcard, matching any single character.
Additional forms of character classes test for a character's
Ill-formed regular expression strings normalize to failure.
| ||||
Defined in Control.Lens.Grammar.RegEx | |||||
| Categorized token => TerminalSymbol token (RegEx token) Source # | |||||
Defined in Control.Lens.Grammar.RegEx | |||||
| Categorized token => TokenAlgebra token (RegEx token) Source # | |||||
Defined in Control.Lens.Grammar.RegEx Methods tokenClass :: TokenClass token -> RegEx token Source # | |||||
| Categorized token => Tokenized token (RegEx token) Source # | |||||
Defined in Control.Lens.Grammar.RegEx | |||||
| (Categorized token, HasTrie token) => HasTrie (RegEx token) Source # | |||||
Defined in Control.Lens.Grammar.RegEx Associated Types
| |||||
| Categorized token => DisjunctiveMonoid (RegEx token) Source # | Alternatives are associative, as normalized by | ||||
| Categorized token => KleeneStarAlgebra (RegEx token) Source # | |||||
| Tried (RegEx token) Source # | A | ||||
| Categorized token => LaidOut (RegEx token) Source # | Either layout, any indentation. | ||||
| Lined (RegEx Char) Source # | A newline, then any indentation. | ||||
| NonTerminalSymbol (RegEx token) Source # | |||||
Defined in Control.Lens.Grammar.RegEx Methods nonTerminal :: String -> RegEx token Source # | |||||
| Categorized token => Monoid (RegEx token) Source # | |||||
| Categorized token => Semigroup (RegEx token) Source # | Sequences are associative, as items normalized by | ||||
| token ~ Char => IsString (RegEx token) Source # | |||||
Defined in Control.Lens.Grammar.RegEx Methods fromString :: String -> RegEx token # | |||||
| Generic (RegEx token) Source # | |||||
Defined in Control.Lens.Grammar.RegEx Associated Types | |||||
| token ~ Char => IsList (RegEx token) Source # | A regular expression string, by its | ||||
Defined in Control.Lens.Grammar.RegEx Associated Types
| |||||
| (Categorized token, Read token, Read (Categorize token)) => Read (RegEx token) Source # | |||||
| (Categorized token, Show token, Show (Categorize token)) => Show (RegEx token) Source # | |||||
| Categorized token => Eq (RegEx token) Source # | |||||
| Categorized token => Ord (RegEx token) Source # | |||||
Defined in Control.Lens.Grammar.RegEx | |||||
| Categorized token => Matching [token] (Bnf (RegEx token)) Source # | |||||
| Categorized token => Matching [token] (RegEx token) Source # | |||||
| data (RegEx token) :->: b Source # | |||||
Defined in Control.Lens.Grammar.RegEx data (RegEx token) :->: b = RegExTrie {
| |||||
| type Rep (RegEx token) Source # | |||||
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 # | |||||
Defined in Control.Lens.Grammar.RegEx | |||||
Bnf
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))
Instances
| CtxGrammar Char (Bnf (RegEx Char)) Source # | |
Defined in Control.Lens.Grammar.RegEx | |
| Grammar Char (Bnf (RegEx Char)) Source # | The context-free
Such a
Like
They can be generated from context-free Like
|
| (Ord rule, TerminalSymbol token rule) => TerminalSymbol token (Bnf rule) Source # | |
Defined in Control.Lens.Grammar.RegEx | |
| (Ord rule, TokenAlgebra token rule) => TokenAlgebra token (Bnf rule) Source # | |
Defined in Control.Lens.Grammar.RegEx Methods tokenClass :: TokenClass token -> Bnf rule Source # | |
| (Ord rule, Tokenized token rule) => Tokenized token (Bnf rule) Source # | |
Defined in Control.Lens.Grammar.RegEx | |
| (Ord rule, DisjunctiveMonoid rule) => DisjunctiveMonoid (Bnf rule) Source # | |
| (Ord rule, KleeneStarAlgebra rule) => KleeneStarAlgebra (Bnf rule) Source # | |
| (Ord rule, NonTerminalSymbol rule) => BackusNaurForm (Bnf rule) Source # | |
| (Ord rule, NonTerminalSymbol rule) => Ruled (Bnf rule) Source # | |
| Tried rule => Tried (Bnf rule) Source # | |
| (Ord rule, LaidOut rule) => LaidOut (Bnf rule) Source # | |
| (Ord rule, Lined rule) => Lined (Bnf rule) Source # | |
Defined in Control.Lens.Grammar.Pretty | |
| (Ord rule, NonTerminalSymbol rule) => NonTerminalSymbol (Bnf rule) Source # | |
Defined in Control.Lens.Grammar.RegEx Methods nonTerminal :: String -> Bnf rule Source # | |
| (Ord rule, Monoid rule) => Monoid (Bnf rule) Source # | |
| (Ord rule, Semigroup rule) => Semigroup (Bnf rule) Source # | |
| rule ~ RegEx Char => IsString (Bnf rule) Source # | |
Defined in Control.Lens.Grammar.RegEx Methods fromString :: String -> Bnf rule # | |
| rule ~ RegEx Char => IsList (Bnf rule) Source # | |
| (Read rule, Ord rule) => Read (Bnf rule) Source # | |
| Show rule => Show (Bnf rule) Source # | |
| Eq rule => Eq (Bnf rule) Source # | |
| Ord rule => Ord (Bnf rule) Source # | |
Defined in Control.Lens.Grammar.RegEx | |
| Categorized token => Matching [token] (Bnf (RegEx token)) Source # | |
| type Item (Bnf rule) Source # | |
Defined in Control.Lens.Grammar.RegEx | |
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.