| 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.Analysis
Contents
Description
Static analyses of RegExtended Bnfs, as generated from context-free
grammars by Control.Lens.Grammar.regbnfG, over any token.
A RegEx is the free KleeneStarAlgebra of token classes &
nonterminals, so an analysis which composes, like nullability or the
first tokens of a word, is the unique homomorphism out of it, foldRegEx,
into another KleeneStarAlgebra, with a least fixpoint, fixRules,
for the rules naming each other. An analysis which depends on context,
like the tokens which may follow a rule, flows down from its callers,
in follows. The LL(1) conflicts, ll1Conflicts, are built of them.
Synopsis
- foldRegEx :: KleeneStarAlgebra k => (TokenClass token -> k) -> (String -> k) -> RegEx token -> k
- bnfRules :: Categorized token => Bnf (RegEx token) -> Map String (RegEx token)
- fixRules :: (Categorized token, Eq k) => k -> (Map String k -> RegEx token -> k) -> Bnf (RegEx token) -> Map String k
- data Leading a = Leading {
- leadingNullable :: Bool
- leadingAtoms :: a
- firsts :: Categorized token => Bnf (RegEx token) -> Map String (Leading (TokenClass token))
- firstOf :: Categorized token => Map String (Leading (TokenClass token)) -> RegEx token -> Leading (TokenClass token)
- leftRecursive :: Categorized token => Bnf (RegEx token) -> Set String
- data Guard token = Guard {
- guardEnd :: Bool
- guardNext :: TokenClass token
- follows :: Categorized token => Bnf (RegEx token) -> Map String (Guard token)
- ll1Conflicts :: Categorized token => Bnf (RegEx token) -> [LL1Conflict token]
- data LL1Conflict token = LL1Conflict {
- conflictRule :: Maybe String
- conflictSort :: LL1ConflictSort
- conflictTokens :: TokenClass token
- conflictAt :: RegEx token
- data LL1ConflictSort
Folding
Arguments
| :: KleeneStarAlgebra k | |
| => (TokenClass token -> k) | token classes |
| -> (String -> k) | nonterminals |
| -> RegEx token | |
| -> k |
The homomorphism out of the free KleeneStarAlgebra of token
classes & nonterminals, a RegEx, into any other, given the
interpretations of its token classes & its nonterminals.
prop> foldRegEx tokenClass nonTerminal = id
bnfRules :: Categorized token => Bnf (RegEx token) -> Map String (RegEx token) Source #
The rules of a Bnf, by name,
a name's rules joined as alternatives.
Arguments
| :: (Categorized token, Eq k) | |
| => k | bottom |
| -> (Map String k -> RegEx token -> k) | a body, given the rules |
| -> Bnf (RegEx token) | |
| -> Map String k |
The least fixpoint of an analysis of the rules of a Bnf,
from bottom, interpreting each rule's body given the analyses of the
rules it names, until they stabilize, as they do for a monotone analysis
into a lattice of finite height.
Leading atoms
Whether a word can be empty, & the atoms it can lead with,
a KleeneStarAlgebra which, at token classes, is the first tokens of
words, & at sets of rule names, their left corners.
>>>foldRegEx (Leading False) (const falseP) ("a*b|c?" :: RegEx Char)Leading {leadingNullable = True, leadingAtoms = TokenClass (OneOf (fromList "abc"))}
Constructors
| Leading | |
Fields
| |
Instances
| DisjunctiveMonoid a => DisjunctiveMonoid (Leading a) Source # | |
| DisjunctiveMonoid a => KleeneStarAlgebra (Leading a) Source # | |
| DisjunctiveMonoid a => Monoid (Leading a) Source # | |
| DisjunctiveMonoid a => Semigroup (Leading a) Source # | |
| Read a => Read (Leading a) Source # | |
| Show a => Show (Leading a) Source # | |
| Eq a => Eq (Leading a) Source # | |
| Ord a => Ord (Leading a) Source # | |
firsts :: Categorized token => Bnf (RegEx token) -> Map String (Leading (TokenClass token)) Source #
The nullability & first tokens of each rule of a Bnf.
firstOf :: Categorized token => Map String (Leading (TokenClass token)) -> RegEx token -> Leading (TokenClass token) Source #
The nullability & first tokens of a RegEx,
given those of the rules it names.
leftRecursive :: Categorized token => Bnf (RegEx token) -> Set String Source #
The rules of a Bnf which can name themselves
before reading a token, on which a predictive parser diverges.
Guards
A guard on the next token, or the end of the input,
a BooleanAlgebra, by which a predictive parser decides.
The tokens which may follow a rule are a Guard.
Constructors
| Guard | |
Fields
| |
Instances
| Categorized token => BooleanAlgebra (Guard token) Source # | |
| Categorized token => DisjunctiveMonoid (Guard token) Source # | |
| (Categorized token, Show token, Show (Categorize token)) => Show (Guard token) Source # | |
| Categorized token => Eq (Guard token) Source # | |
| Categorized token => Ord (Guard token) Source # | |
Defined in Control.Lens.Grammar.Analysis | |
follows :: Categorized token => Bnf (RegEx token) -> Map String (Guard token) Source #
The tokens which may follow each rule of a Bnf,
& whether its input may end, the least fixpoint of their callers'.
>>>follows ("{start} = \\q{a}b|\\q{a}\n{a} = c" :: Bnf (RegEx Char))fromList [("a",Guard {guardEnd = True, guardNext = TokenClass (OneOf (fromList "b"))})]
LL(1)
ll1Conflicts :: Categorized token => Bnf (RegEx token) -> [LL1Conflict token] Source #
The conflicts which keep a RegExtended Bnf from being LL(1),
that is, from being parsed predictively by one token of lookahead,
committing to an alternative by its first token, as
tParsector does.
An empty list means the grammar is LL(1).
Alternatives are tried in order, so besides the usual conditions,
that alternatives start with distinct tokens & that repetitions
& options start with tokens which can't follow them,
a nullable alternative mustn't precede one which can consume tokens,
since it would succeed first, without consuming them.
Left recursion is reported too, since a predictive parser diverges on it.
A conflict which a try resolves isn't reported: of alternatives,
the first of which backtracks, or of a repetition which backtracks,
since a predictive parser then tries what follows anew.
>>>map conflictSort (ll1Conflicts ("{start} = ab|ac" :: Bnf (RegEx Char)))[LL1FirstFirst]>>>ll1Conflicts ("{start} = ab|c" :: Bnf (RegEx Char))[]
data LL1Conflict token Source #
A conflict keeping a grammar from being LL(1),
in a rule, or Nothing for the start expression,
of a sort, with the tokens in conflict, at a subexpression.
Constructors
| LL1Conflict | |
Fields
| |
Instances
| (Categorized token, Show token, Show (Categorize token)) => Show (LL1Conflict token) Source # | |
Defined in Control.Lens.Grammar.Analysis Methods showsPrec :: Int -> LL1Conflict token -> ShowS # show :: LL1Conflict token -> String # showList :: [LL1Conflict token] -> ShowS # | |
| Categorized token => Eq (LL1Conflict token) Source # | |
Defined in Control.Lens.Grammar.Analysis Methods (==) :: LL1Conflict token -> LL1Conflict token -> Bool # (/=) :: LL1Conflict token -> LL1Conflict token -> Bool # | |
data LL1ConflictSort Source #
Sorts of LL1Conflicts.
Constructors
| LL1FirstFirst | alternatives which start with the same tokens |
| LL1FirstFollow | an option, repetition or nullable alternative which starts with tokens which may follow it |
| LL1NullableAlternatives | alternatives which both match the empty word |
| LL1NullableFirst | a nullable alternative before one which consumes tokens |
| LL1NullableRepetition | an option or repetition of a pattern which matches the empty word |
| LL1LeftRecursion | a rule which names itself before reading a token |