{- |
Module      : Control.Lens.Grammar.Analysis
Description : static analyses of grammars
Copyright   : (C) 2026 - Eitan Chatav
License     : BSD-style (see the file LICENSE)
Maintainer  : Eitan Chatav <eitan.chatav@gmail.com>
Stability   : provisional
Portability : non-portable

Static analyses of `RegEx`tended t'Bnf's, 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.
-}

module Control.Lens.Grammar.Analysis
  ( -- * Folding
    foldRegEx
  , bnfRules
  , fixRules
    -- * Leading atoms
  , Leading (..)
  , firsts
  , firstOf
  , leftRecursive
    -- * Guards
  , Guard (..)
  , follows
    -- * LL(1)
  , ll1Conflicts
  , LL1Conflict (..)
  , LL1ConflictSort (..)
  ) where

import Control.Lens.Grammar.RegEx
import Control.Lens.Grammar.Token
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Monoid.Disjunctive
import Data.Set (Set)
import qualified Data.Set as Set

-- $setup
-- >>> :set -XOverloadedStrings
-- >>> import Control.Lens.Grammar

{- | 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
-}
foldRegEx
  :: KleeneStarAlgebra k
  => (TokenClass token -> k) -- ^ token classes
  -> (String -> k) -- ^ nonterminals
  -> RegEx token -> k
foldRegEx :: forall k token.
KleeneStarAlgebra k =>
(TokenClass token -> k) -> (String -> k) -> RegEx token -> k
foldRegEx TokenClass token -> k
tok String -> k
nonterm = RegEx token -> k
go
  where
    go :: RegEx token -> k
go = \case
      RegEx token
SeqEmpty -> k
forall a. Monoid a => a
mempty
      Sequence RegEx token
x RegEx token
y [RegEx token]
zs -> [k] -> k
forall a. Monoid a => [a] -> a
mconcat ((RegEx token -> k) -> [RegEx token] -> [k]
forall a b. (a -> b) -> [a] -> [b]
map RegEx token -> k
go (RegEx token
x RegEx token -> [RegEx token] -> [RegEx token]
forall a. a -> [a] -> [a]
: RegEx token
y RegEx token -> [RegEx token] -> [RegEx token]
forall a. a -> [a] -> [a]
: [RegEx token]
zs))
      NonTerminal String
name -> String -> k
nonterm String
name
      KleeneStar RegEx token
x -> k -> k
forall k. KleeneStarAlgebra k => k -> k
starK (RegEx token -> k
go RegEx token
x)
      KleeneOpt RegEx token
x -> k -> k
forall k. KleeneStarAlgebra k => k -> k
optK (RegEx token -> k
go RegEx token
x)
      KleenePlus RegEx token
x -> k -> k
forall k. KleeneStarAlgebra k => k -> k
plusK (RegEx token -> k
go RegEx token
x)
      KleeneTimes Int
lo Maybe Int
hi RegEx token
x -> Int -> Maybe Int -> k -> k
forall k. KleeneStarAlgebra k => Int -> Maybe Int -> k -> k
quantK Int
lo Maybe Int
hi (RegEx token -> k
go RegEx token
x)
      Backtracking RegEx token
x -> RegEx token -> k
go RegEx token
x
      RegExam (OneOf Set token
toks) -> TokenClass token -> k
tok (TokenExam token (TokenClass token) -> TokenClass token
forall token.
TokenExam token (TokenClass token) -> TokenClass token
TokenClass (Set token -> TokenExam token (TokenClass token)
forall token alg. Set token -> TokenExam token alg
OneOf Set token
toks))
      RegExam (NotOneOf Set token
toks CategoryTest token
test) -> TokenClass token -> k
tok (TokenExam token (TokenClass token) -> TokenClass token
forall token.
TokenExam token (TokenClass token) -> TokenClass token
TokenClass (Set token
-> CategoryTest token -> TokenExam token (TokenClass token)
forall token alg.
Set token -> CategoryTest token -> TokenExam token alg
NotOneOf Set token
toks CategoryTest token
test))
      RegExam (Alternate RegEx token
x RegEx token
y [RegEx token]
zs) -> (k -> k -> k) -> [k] -> k
forall a. (a -> a -> a) -> [a] -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 k -> k -> k
forall a. DisjunctiveMonoid a => a -> a -> a
(>|<) ((RegEx token -> k) -> [RegEx token] -> [k]
forall a b. (a -> b) -> [a] -> [b]
map RegEx token -> k
go (RegEx token
x RegEx token -> [RegEx token] -> [RegEx token]
forall a. a -> [a] -> [a]
: RegEx token
y RegEx token -> [RegEx token] -> [RegEx token]
forall a. a -> [a] -> [a]
: [RegEx token]
zs))

{- | The rules of a t'Bnf', by name,
a name's rules joined as alternatives.
-}
bnfRules :: Categorized token => Bnf (RegEx token) -> Map String (RegEx token)
bnfRules :: forall token.
Categorized token =>
Bnf (RegEx token) -> Map String (RegEx token)
bnfRules = (RegEx token -> RegEx token -> RegEx token)
-> [(String, RegEx token)] -> Map String (RegEx token)
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
Map.fromListWith ((RegEx token -> RegEx token -> RegEx token)
-> RegEx token -> RegEx token -> RegEx token
forall a b c. (a -> b -> c) -> b -> a -> c
flip RegEx token -> RegEx token -> RegEx token
forall a. DisjunctiveMonoid a => a -> a -> a
(>|<)) ([(String, RegEx token)] -> Map String (RegEx token))
-> (Bnf (RegEx token) -> [(String, RegEx token)])
-> Bnf (RegEx token)
-> Map String (RegEx token)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set (String, RegEx token) -> [(String, RegEx token)]
forall a. Set a -> [a]
Set.toList (Set (String, RegEx token) -> [(String, RegEx token)])
-> (Bnf (RegEx token) -> Set (String, RegEx token))
-> Bnf (RegEx token)
-> [(String, RegEx token)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bnf (RegEx token) -> Set (String, RegEx token)
forall rule. Bnf rule -> Set (String, rule)
rulesBnf

{- | The least fixpoint of an analysis of the rules of a t'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.
-}
fixRules
  :: (Categorized token, Eq k)
  => k -- ^ bottom
  -> (Map String k -> RegEx token -> k) -- ^ a body, given the rules
  -> Bnf (RegEx token) -> Map String k
fixRules :: forall token k.
(Categorized token, Eq k) =>
k
-> (Map String k -> RegEx token -> k)
-> Bnf (RegEx token)
-> Map String k
fixRules k
bottom Map String k -> RegEx token -> k
analyze Bnf (RegEx token)
bnf = Map String k -> Map String k
go (k
bottom k -> Map String (RegEx token) -> Map String k
forall a b. a -> Map String b -> Map String a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Map String (RegEx token)
rules)
  where
    rules :: Map String (RegEx token)
rules = Bnf (RegEx token) -> Map String (RegEx token)
forall token.
Categorized token =>
Bnf (RegEx token) -> Map String (RegEx token)
bnfRules Bnf (RegEx token)
bnf
    go :: Map String k -> Map String k
go Map String k
env = let env' :: Map String k
env' = Map String k -> RegEx token -> k
analyze Map String k
env (RegEx token -> k) -> Map String (RegEx token) -> Map String k
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map String (RegEx token)
rules in
      if Map String k
env' Map String k -> Map String k -> Bool
forall a. Eq a => a -> a -> Bool
== Map String k
env then Map String k
env else Map String k -> Map String k
go Map String k
env'

{- | 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"))}
-}
data Leading a = Leading
  { forall a. Leading a -> Bool
leadingNullable :: Bool -- ^ whether a word can be empty
  , forall a. Leading a -> a
leadingAtoms :: a -- ^ the atoms a word can lead with
  } deriving stock (Leading a -> Leading a -> Bool
(Leading a -> Leading a -> Bool)
-> (Leading a -> Leading a -> Bool) -> Eq (Leading a)
forall a. Eq a => Leading a -> Leading a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Leading a -> Leading a -> Bool
== :: Leading a -> Leading a -> Bool
$c/= :: forall a. Eq a => Leading a -> Leading a -> Bool
/= :: Leading a -> Leading a -> Bool
Eq, Eq (Leading a)
Eq (Leading a) =>
(Leading a -> Leading a -> Ordering)
-> (Leading a -> Leading a -> Bool)
-> (Leading a -> Leading a -> Bool)
-> (Leading a -> Leading a -> Bool)
-> (Leading a -> Leading a -> Bool)
-> (Leading a -> Leading a -> Leading a)
-> (Leading a -> Leading a -> Leading a)
-> Ord (Leading a)
Leading a -> Leading a -> Bool
Leading a -> Leading a -> Ordering
Leading a -> Leading a -> Leading a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (Leading a)
forall a. Ord a => Leading a -> Leading a -> Bool
forall a. Ord a => Leading a -> Leading a -> Ordering
forall a. Ord a => Leading a -> Leading a -> Leading a
$ccompare :: forall a. Ord a => Leading a -> Leading a -> Ordering
compare :: Leading a -> Leading a -> Ordering
$c< :: forall a. Ord a => Leading a -> Leading a -> Bool
< :: Leading a -> Leading a -> Bool
$c<= :: forall a. Ord a => Leading a -> Leading a -> Bool
<= :: Leading a -> Leading a -> Bool
$c> :: forall a. Ord a => Leading a -> Leading a -> Bool
> :: Leading a -> Leading a -> Bool
$c>= :: forall a. Ord a => Leading a -> Leading a -> Bool
>= :: Leading a -> Leading a -> Bool
$cmax :: forall a. Ord a => Leading a -> Leading a -> Leading a
max :: Leading a -> Leading a -> Leading a
$cmin :: forall a. Ord a => Leading a -> Leading a -> Leading a
min :: Leading a -> Leading a -> Leading a
Ord, Int -> Leading a -> ShowS
[Leading a] -> ShowS
Leading a -> String
(Int -> Leading a -> ShowS)
-> (Leading a -> String)
-> ([Leading a] -> ShowS)
-> Show (Leading a)
forall a. Show a => Int -> Leading a -> ShowS
forall a. Show a => [Leading a] -> ShowS
forall a. Show a => Leading a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Leading a -> ShowS
showsPrec :: Int -> Leading a -> ShowS
$cshow :: forall a. Show a => Leading a -> String
show :: Leading a -> String
$cshowList :: forall a. Show a => [Leading a] -> ShowS
showList :: [Leading a] -> ShowS
Show, ReadPrec [Leading a]
ReadPrec (Leading a)
Int -> ReadS (Leading a)
ReadS [Leading a]
(Int -> ReadS (Leading a))
-> ReadS [Leading a]
-> ReadPrec (Leading a)
-> ReadPrec [Leading a]
-> Read (Leading a)
forall a. Read a => ReadPrec [Leading a]
forall a. Read a => ReadPrec (Leading a)
forall a. Read a => Int -> ReadS (Leading a)
forall a. Read a => ReadS [Leading a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Read a => Int -> ReadS (Leading a)
readsPrec :: Int -> ReadS (Leading a)
$creadList :: forall a. Read a => ReadS [Leading a]
readList :: ReadS [Leading a]
$creadPrec :: forall a. Read a => ReadPrec (Leading a)
readPrec :: ReadPrec (Leading a)
$creadListPrec :: forall a. Read a => ReadPrec [Leading a]
readListPrec :: ReadPrec [Leading a]
Read)
instance DisjunctiveMonoid a => Semigroup (Leading a) where
  Leading Bool
null0 a
atoms0 <> :: Leading a -> Leading a -> Leading a
<> Leading Bool
null1 a
atoms1 =
    Bool -> a -> Leading a
forall a. Bool -> a -> Leading a
Leading (Bool
null0 Bool -> Bool -> Bool
&& Bool
null1) (if Bool
null0 then a
atoms0 a -> a -> a
forall a. DisjunctiveMonoid a => a -> a -> a
>|< a
atoms1 else a
atoms0)
instance DisjunctiveMonoid a => Monoid (Leading a) where
  mempty :: Leading a
mempty = Bool -> a -> Leading a
forall a. Bool -> a -> Leading a
Leading Bool
True a
forall a. DisjunctiveMonoid a => a
falseP
instance DisjunctiveMonoid a => DisjunctiveMonoid (Leading a) where
  falseP :: Leading a
falseP = Bool -> a -> Leading a
forall a. Bool -> a -> Leading a
Leading Bool
False a
forall a. DisjunctiveMonoid a => a
falseP
  Leading Bool
null0 a
atoms0 >|< :: Leading a -> Leading a -> Leading a
>|< Leading Bool
null1 a
atoms1 =
    Bool -> a -> Leading a
forall a. Bool -> a -> Leading a
Leading (Bool
null0 Bool -> Bool -> Bool
|| Bool
null1) (a
atoms0 a -> a -> a
forall a. DisjunctiveMonoid a => a -> a -> a
>|< a
atoms1)
instance DisjunctiveMonoid a => KleeneStarAlgebra (Leading a) where
  starK :: Leading a -> Leading a
starK (Leading Bool
_ a
atoms) = Bool -> a -> Leading a
forall a. Bool -> a -> Leading a
Leading Bool
True a
atoms
  plusK :: Leading a -> Leading a
plusK = Leading a -> Leading a
forall a. a -> a
id
  optK :: Leading a -> Leading a
optK (Leading Bool
_ a
atoms) = Bool -> a -> Leading a
forall a. Bool -> a -> Leading a
Leading Bool
True a
atoms

{- | The nullability & first tokens of each rule of a t'Bnf'. -}
firsts
  :: Categorized token
  => Bnf (RegEx token) -> Map String (Leading (TokenClass token))
firsts :: forall token.
Categorized token =>
Bnf (RegEx token) -> Map String (Leading (TokenClass token))
firsts = Leading (TokenClass token)
-> (Map String (Leading (TokenClass token))
    -> RegEx token -> Leading (TokenClass token))
-> Bnf (RegEx token)
-> Map String (Leading (TokenClass token))
forall token k.
(Categorized token, Eq k) =>
k
-> (Map String k -> RegEx token -> k)
-> Bnf (RegEx token)
-> Map String k
fixRules Leading (TokenClass token)
forall a. DisjunctiveMonoid a => a
falseP Map String (Leading (TokenClass token))
-> RegEx token -> Leading (TokenClass token)
forall token.
Categorized token =>
Map String (Leading (TokenClass token))
-> RegEx token -> Leading (TokenClass token)
firstOf

{- | The nullability & first tokens of a `RegEx`,
given those of the rules it names.
-}
firstOf
  :: Categorized token
  => Map String (Leading (TokenClass token))
  -> RegEx token -> Leading (TokenClass token)
firstOf :: forall token.
Categorized token =>
Map String (Leading (TokenClass token))
-> RegEx token -> Leading (TokenClass token)
firstOf Map String (Leading (TokenClass token))
rules =
  (TokenClass token -> Leading (TokenClass token))
-> (String -> Leading (TokenClass token))
-> RegEx token
-> Leading (TokenClass token)
forall k token.
KleeneStarAlgebra k =>
(TokenClass token -> k) -> (String -> k) -> RegEx token -> k
foldRegEx (Bool -> TokenClass token -> Leading (TokenClass token)
forall a. Bool -> a -> Leading a
Leading Bool
False) (\String
name -> Leading (TokenClass token)
-> String
-> Map String (Leading (TokenClass token))
-> Leading (TokenClass token)
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault Leading (TokenClass token)
forall a. DisjunctiveMonoid a => a
falseP String
name Map String (Leading (TokenClass token))
rules)

{- | The rules of a t'Bnf' which can name themselves
before reading a token, on which a predictive parser diverges.
-}
leftRecursive :: Categorized token => Bnf (RegEx token) -> Set String
leftRecursive :: forall token. Categorized token => Bnf (RegEx token) -> Set String
leftRecursive Bnf (RegEx token)
bnf = (String -> Bool) -> Set String -> Set String
forall a. (a -> Bool) -> Set a -> Set a
Set.filter (\String
name -> String
name String -> Set String -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` String -> Set String
reach String
name) Set String
names
  where
    rules :: Map String (RegEx token)
rules = Bnf (RegEx token) -> Map String (RegEx token)
forall token.
Categorized token =>
Bnf (RegEx token) -> Map String (RegEx token)
bnfRules Bnf (RegEx token)
bnf
    names :: Set String
names = Map String (RegEx token) -> Set String
forall k a. Map k a -> Set k
Map.keysSet Map String (RegEx token)
rules
    nullables :: Map String Bool
nullables = Leading (TokenClass token) -> Bool
forall a. Leading a -> Bool
leadingNullable (Leading (TokenClass token) -> Bool)
-> Map String (Leading (TokenClass token)) -> Map String Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bnf (RegEx token) -> Map String (Leading (TokenClass token))
forall token.
Categorized token =>
Bnf (RegEx token) -> Map String (Leading (TokenClass token))
firsts Bnf (RegEx token)
bnf
    -- the rules a body can name before reading a token
    corners :: RegEx token -> Leading (Set String)
corners = (TokenClass token -> Leading (Set String))
-> (String -> Leading (Set String))
-> RegEx token
-> Leading (Set String)
forall k token.
KleeneStarAlgebra k =>
(TokenClass token -> k) -> (String -> k) -> RegEx token -> k
foldRegEx (Leading (Set String) -> TokenClass token -> Leading (Set String)
forall a b. a -> b -> a
const Leading (Set String)
forall a. DisjunctiveMonoid a => a
falseP) ((String -> Leading (Set String))
 -> RegEx token -> Leading (Set String))
-> (String -> Leading (Set String))
-> RegEx token
-> Leading (Set String)
forall a b. (a -> b) -> a -> b
$ \String
name ->
      Bool -> Set String -> Leading (Set String)
forall a. Bool -> a -> Leading a
Leading (Bool -> String -> Map String Bool -> Bool
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault Bool
False String
name Map String Bool
nullables) (String -> Set String
forall a. a -> Set a
Set.singleton String
name)
    leftCorners :: Map String (Set String)
leftCorners = Leading (Set String) -> Set String
forall a. Leading a -> a
leadingAtoms (Leading (Set String) -> Set String)
-> (RegEx token -> Leading (Set String))
-> RegEx token
-> Set String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RegEx token -> Leading (Set String)
corners (RegEx token -> Set String)
-> Map String (RegEx token) -> Map String (Set String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map String (RegEx token)
rules
    reach :: String -> Set String
reach String
name = Set String -> [String] -> Set String
search Set String
forall a. Set a
Set.empty (String -> [String]
corners' String
name)
    corners' :: String -> [String]
corners' String
name = Set String -> [String]
forall a. Set a -> [a]
Set.toList (Set String -> String -> Map String (Set String) -> Set String
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault Set String
forall a. Set a
Set.empty String
name Map String (Set String)
leftCorners)
    search :: Set String -> [String] -> Set String
search Set String
seen = \case
      [] -> Set String
seen
      String
next : [String]
rest
        | String
next String -> Set String -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set String
seen -> Set String -> [String] -> Set String
search Set String
seen [String]
rest
        | Bool
otherwise -> Set String -> [String] -> Set String
search (String -> Set String -> Set String
forall a. Ord a => a -> Set a -> Set a
Set.insert String
next Set String
seen) (String -> [String]
corners' String
next [String] -> [String] -> [String]
forall a. Semigroup a => a -> a -> a
<> [String]
rest)

{- | 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 t'Guard'.
-}
data Guard token = Guard
  { forall token. Guard token -> Bool
guardEnd :: Bool -- ^ whether the input may end
  , forall token. Guard token -> TokenClass token
guardNext :: TokenClass token -- ^ which tokens may be next
  }
deriving stock instance Categorized token => Eq (Guard token)
deriving stock instance Categorized token => Ord (Guard token)
deriving stock instance
  (Categorized token, Show token, Show (Categorize token))
    => Show (Guard token)
instance Categorized token => DisjunctiveMonoid (Guard token) where
  falseP :: Guard token
falseP = Bool -> TokenClass token -> Guard token
forall token. Bool -> TokenClass token -> Guard token
Guard Bool
False TokenClass token
forall a. DisjunctiveMonoid a => a
falseP
  Guard Bool
end0 TokenClass token
next0 >|< :: Guard token -> Guard token -> Guard token
>|< Guard Bool
end1 TokenClass token
next1 = Bool -> TokenClass token -> Guard token
forall token. Bool -> TokenClass token -> Guard token
Guard (Bool
end0 Bool -> Bool -> Bool
|| Bool
end1) (TokenClass token
next0 TokenClass token -> TokenClass token -> TokenClass token
forall a. DisjunctiveMonoid a => a -> a -> a
>|< TokenClass token
next1)
instance Categorized token => BooleanAlgebra (Guard token) where
  trueP :: Guard token
trueP = Bool -> TokenClass token -> Guard token
forall token. Bool -> TokenClass token -> Guard token
Guard Bool
True TokenClass token
forall b. BooleanAlgebra b => b
trueP
  notP :: Guard token -> Guard token
notP (Guard Bool
end TokenClass token
next) = Bool -> TokenClass token -> Guard token
forall token. Bool -> TokenClass token -> Guard token
Guard (Bool -> Bool
not Bool
end) (TokenClass token -> TokenClass token
forall b. BooleanAlgebra b => b -> b
notP TokenClass token
next)
  Guard Bool
end0 TokenClass token
next0 >&< :: Guard token -> Guard token -> Guard token
>&< Guard Bool
end1 TokenClass token
next1 = Bool -> TokenClass token -> Guard token
forall token. Bool -> TokenClass token -> Guard token
Guard (Bool
end0 Bool -> Bool -> Bool
&& Bool
end1) (TokenClass token
next0 TokenClass token -> TokenClass token -> TokenClass token
forall b. BooleanAlgebra b => b -> b -> b
>&< TokenClass token
next1)

-- the guard of a word, then of what follows it
guardOf
  :: Categorized token
  => Leading (TokenClass token) -> Guard token -> Guard token
guardOf :: forall token.
Categorized token =>
Leading (TokenClass token) -> Guard token -> Guard token
guardOf (Leading Bool
nullable TokenClass token
atoms) Guard token
after
  | Bool
nullable = Bool -> TokenClass token -> Guard token
forall token. Bool -> TokenClass token -> Guard token
Guard Bool
False TokenClass token
atoms Guard token -> Guard token -> Guard token
forall a. DisjunctiveMonoid a => a -> a -> a
>|< Guard token
after
  | Bool
otherwise = Bool -> TokenClass token -> Guard token
forall token. Bool -> TokenClass token -> Guard token
Guard Bool
False TokenClass token
atoms

-- each of a sequence's items, with the guard of what follows it
sequenced
  :: Categorized token
  => (RegEx token -> Leading (TokenClass token))
  -> Guard token -> [RegEx token] -> [(RegEx token, Guard token)]
sequenced :: forall token.
Categorized token =>
(RegEx token -> Leading (TokenClass token))
-> Guard token -> [RegEx token] -> [(RegEx token, Guard token)]
sequenced RegEx token -> Leading (TokenClass token)
first Guard token
after = (Guard token, [(RegEx token, Guard token)])
-> [(RegEx token, Guard token)]
forall a b. (a, b) -> b
snd ((Guard token, [(RegEx token, Guard token)])
 -> [(RegEx token, Guard token)])
-> ([RegEx token] -> (Guard token, [(RegEx token, Guard token)]))
-> [RegEx token]
-> [(RegEx token, Guard token)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RegEx token
 -> (Guard token, [(RegEx token, Guard token)])
 -> (Guard token, [(RegEx token, Guard token)]))
-> (Guard token, [(RegEx token, Guard token)])
-> [RegEx token]
-> (Guard token, [(RegEx token, Guard token)])
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr RegEx token
-> (Guard token, [(RegEx token, Guard token)])
-> (Guard token, [(RegEx token, Guard token)])
step (Guard token
after, [])
  where
    step :: RegEx token
-> (Guard token, [(RegEx token, Guard token)])
-> (Guard token, [(RegEx token, Guard token)])
step RegEx token
item (Guard token
guard, [(RegEx token, Guard token)]
acc) = (Leading (TokenClass token) -> Guard token -> Guard token
forall token.
Categorized token =>
Leading (TokenClass token) -> Guard token -> Guard token
guardOf (RegEx token -> Leading (TokenClass token)
first RegEx token
item) Guard token
guard, (RegEx token
item, Guard token
guard) (RegEx token, Guard token)
-> [(RegEx token, Guard token)] -> [(RegEx token, Guard token)]
forall a. a -> [a] -> [a]
: [(RegEx token, Guard token)]
acc)

{- | The tokens which may follow each rule of a t'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"))})]
-}
follows :: Categorized token => Bnf (RegEx token) -> Map String (Guard token)
follows :: forall token.
Categorized token =>
Bnf (RegEx token) -> Map String (Guard token)
follows Bnf (RegEx token)
bnf = Map String (Guard token) -> Map String (Guard token)
go (Guard token
forall a. DisjunctiveMonoid a => a
falseP Guard token -> Map String (RegEx token) -> Map String (Guard token)
forall a b. a -> Map String b -> Map String a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Map String (RegEx token)
rules)
  where
    rules :: Map String (RegEx token)
rules = Bnf (RegEx token) -> Map String (RegEx token)
forall token.
Categorized token =>
Bnf (RegEx token) -> Map String (RegEx token)
bnfRules Bnf (RegEx token)
bnf
    first :: RegEx token -> Leading (TokenClass token)
first = Map String (Leading (TokenClass token))
-> RegEx token -> Leading (TokenClass token)
forall token.
Categorized token =>
Map String (Leading (TokenClass token))
-> RegEx token -> Leading (TokenClass token)
firstOf (Bnf (RegEx token) -> Map String (Leading (TokenClass token))
forall token.
Categorized token =>
Bnf (RegEx token) -> Map String (Leading (TokenClass token))
firsts Bnf (RegEx token)
bnf)
    go :: Map String (Guard token) -> Map String (Guard token)
go Map String (Guard token)
env = let env' :: Map String (Guard token)
env' = (Guard token -> Guard token -> Guard token)
-> Map String (Guard token)
-> Map String (Guard token)
-> Map String (Guard token)
forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith Guard token -> Guard token -> Guard token
forall a. DisjunctiveMonoid a => a -> a -> a
(>|<) (Guard token
forall a. DisjunctiveMonoid a => a
falseP Guard token -> Map String (RegEx token) -> Map String (Guard token)
forall a b. a -> Map String b -> Map String a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Map String (RegEx token)
rules) (Map String (Guard token) -> Map String (Guard token)
callers Map String (Guard token)
env) in
      if Map String (Guard token)
env' Map String (Guard token) -> Map String (Guard token) -> Bool
forall a. Eq a => a -> a -> Bool
== Map String (Guard token)
env then Map String (Guard token)
env else Map String (Guard token) -> Map String (Guard token)
go Map String (Guard token)
env'
    callers :: Map String (Guard token) -> Map String (Guard token)
callers Map String (Guard token)
env = (Guard token -> Guard token -> Guard token)
-> [Map String (Guard token)] -> Map String (Guard token)
forall (f :: * -> *) k a.
(Foldable f, Ord k) =>
(a -> a -> a) -> f (Map k a) -> Map k a
Map.unionsWith Guard token -> Guard token -> Guard token
forall a. DisjunctiveMonoid a => a -> a -> a
(>|<) ([Map String (Guard token)] -> Map String (Guard token))
-> [Map String (Guard token)] -> Map String (Guard token)
forall a b. (a -> b) -> a -> b
$
      Guard token -> RegEx token -> Map String (Guard token)
followed (Bool -> TokenClass token -> Guard token
forall token. Bool -> TokenClass token -> Guard token
Guard Bool
True TokenClass token
forall a. DisjunctiveMonoid a => a
falseP) (Bnf (RegEx token) -> RegEx token
forall rule. Bnf rule -> rule
startBnf Bnf (RegEx token)
bnf)
      Map String (Guard token)
-> [Map String (Guard token)] -> [Map String (Guard token)]
forall a. a -> [a] -> [a]
: [ Guard token -> RegEx token -> Map String (Guard token)
followed (Guard token -> String -> Map String (Guard token) -> Guard token
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault Guard token
forall a. DisjunctiveMonoid a => a
falseP String
name Map String (Guard token)
env) RegEx token
body
        | (String
name, RegEx token
body) <- Map String (RegEx token) -> [(String, RegEx token)]
forall k a. Map k a -> [(k, a)]
Map.toList Map String (RegEx token)
rules ]
    followed :: Guard token -> RegEx token -> Map String (Guard token)
followed Guard token
after = \case
      Sequence RegEx token
x RegEx token
y [RegEx token]
zs -> (Guard token -> Guard token -> Guard token)
-> [Map String (Guard token)] -> Map String (Guard token)
forall (f :: * -> *) k a.
(Foldable f, Ord k) =>
(a -> a -> a) -> f (Map k a) -> Map k a
Map.unionsWith Guard token -> Guard token -> Guard token
forall a. DisjunctiveMonoid a => a -> a -> a
(>|<)
        [Guard token -> RegEx token -> Map String (Guard token)
followed Guard token
guard RegEx token
item | (RegEx token
item, Guard token
guard) <- (RegEx token -> Leading (TokenClass token))
-> Guard token -> [RegEx token] -> [(RegEx token, Guard token)]
forall token.
Categorized token =>
(RegEx token -> Leading (TokenClass token))
-> Guard token -> [RegEx token] -> [(RegEx token, Guard token)]
sequenced RegEx token -> Leading (TokenClass token)
first Guard token
after (RegEx token
x RegEx token -> [RegEx token] -> [RegEx token]
forall a. a -> [a] -> [a]
: RegEx token
y RegEx token -> [RegEx token] -> [RegEx token]
forall a. a -> [a] -> [a]
: [RegEx token]
zs)]
      NonTerminal String
name -> String -> Guard token -> Map String (Guard token)
forall k a. k -> a -> Map k a
Map.singleton String
name Guard token
after
      KleeneStar RegEx token
x -> Guard token -> RegEx token -> Map String (Guard token)
followed (RegEx token -> Guard token
again RegEx token
x) RegEx token
x
      KleenePlus RegEx token
x -> Guard token -> RegEx token -> Map String (Guard token)
followed (RegEx token -> Guard token
again RegEx token
x) RegEx token
x
      Backtracking RegEx token
x -> Guard token -> RegEx token -> Map String (Guard token)
followed Guard token
after RegEx token
x
      KleeneTimes Int
lo Maybe Int
hi RegEx token
x -> Guard token -> RegEx token -> Map String (Guard token)
followed Guard token
after (Int -> Maybe Int -> RegEx token -> RegEx token
forall token.
Categorized token =>
Int -> Maybe Int -> RegEx token -> RegEx token
unrolled Int
lo Maybe Int
hi RegEx token
x)
      KleeneOpt RegEx token
x -> Guard token -> RegEx token -> Map String (Guard token)
followed Guard token
after RegEx token
x
      RegExam (Alternate RegEx token
x RegEx token
y [RegEx token]
zs) -> (Guard token -> Guard token -> Guard token)
-> [Map String (Guard token)] -> Map String (Guard token)
forall (f :: * -> *) k a.
(Foldable f, Ord k) =>
(a -> a -> a) -> f (Map k a) -> Map k a
Map.unionsWith Guard token -> Guard token -> Guard token
forall a. DisjunctiveMonoid a => a -> a -> a
(>|<) ((RegEx token -> Map String (Guard token))
-> [RegEx token] -> [Map String (Guard token)]
forall a b. (a -> b) -> [a] -> [b]
map (Guard token -> RegEx token -> Map String (Guard token)
followed Guard token
after) (RegEx token
x RegEx token -> [RegEx token] -> [RegEx token]
forall a. a -> [a] -> [a]
: RegEx token
y RegEx token -> [RegEx token] -> [RegEx token]
forall a. a -> [a] -> [a]
: [RegEx token]
zs))
      RegEx token
_ -> Map String (Guard token)
forall k a. Map k a
Map.empty
      where
        again :: RegEx token -> Guard token
again RegEx token
x = Bool -> TokenClass token -> Guard token
forall token. Bool -> TokenClass token -> Guard token
Guard Bool
False (Leading (TokenClass token) -> TokenClass token
forall a. Leading a -> a
leadingAtoms (RegEx token -> Leading (TokenClass token)
first RegEx token
x)) Guard token -> Guard token -> Guard token
forall a. DisjunctiveMonoid a => a -> a -> a
>|< Guard token
after

{- | The conflicts which keep a `RegEx`tended t'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
t'Data.Profunctor.Grammar.Parsector.Parsector' 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))
[]
-}
ll1Conflicts
  :: Categorized token => Bnf (RegEx token) -> [LL1Conflict token]
ll1Conflicts :: forall token.
Categorized token =>
Bnf (RegEx token) -> [LL1Conflict token]
ll1Conflicts Bnf (RegEx token)
bnf =
  [ Maybe String
-> LL1ConflictSort
-> TokenClass token
-> RegEx token
-> LL1Conflict token
forall token.
Maybe String
-> LL1ConflictSort
-> TokenClass token
-> RegEx token
-> LL1Conflict token
LL1Conflict (String -> Maybe String
forall a. a -> Maybe a
Just String
name) LL1ConflictSort
LL1LeftRecursion TokenClass token
forall a. DisjunctiveMonoid a => a
falseP (String -> RegEx token
forall token. String -> RegEx token
NonTerminal String
name)
  | String
name <- Set String -> [String]
forall a. Set a -> [a]
Set.toList (Bnf (RegEx token) -> Set String
forall token. Categorized token => Bnf (RegEx token) -> Set String
leftRecursive Bnf (RegEx token)
bnf)
  ]
  [LL1Conflict token] -> [LL1Conflict token] -> [LL1Conflict token]
forall a. Semigroup a => a -> a -> a
<> Maybe String -> RegEx token -> Guard token -> [LL1Conflict token]
check Maybe String
forall a. Maybe a
Nothing (Bnf (RegEx token) -> RegEx token
forall rule. Bnf rule -> rule
startBnf Bnf (RegEx token)
bnf) (Bool -> TokenClass token -> Guard token
forall token. Bool -> TokenClass token -> Guard token
Guard Bool
True TokenClass token
forall a. DisjunctiveMonoid a => a
falseP)
  [LL1Conflict token] -> [LL1Conflict token] -> [LL1Conflict token]
forall a. Semigroup a => a -> a -> a
<> [[LL1Conflict token]] -> [LL1Conflict token]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
    [ Maybe String -> RegEx token -> Guard token -> [LL1Conflict token]
check (String -> Maybe String
forall a. a -> Maybe a
Just String
name) RegEx token
body (Guard token -> String -> Map String (Guard token) -> Guard token
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault Guard token
forall a. DisjunctiveMonoid a => a
falseP String
name Map String (Guard token)
followers)
    | (String
name, RegEx token
body) <- Map String (RegEx token) -> [(String, RegEx token)]
forall k a. Map k a -> [(k, a)]
Map.toList (Bnf (RegEx token) -> Map String (RegEx token)
forall token.
Categorized token =>
Bnf (RegEx token) -> Map String (RegEx token)
bnfRules Bnf (RegEx token)
bnf)
    ]
  where
    first :: RegEx token -> Leading (TokenClass token)
first = Map String (Leading (TokenClass token))
-> RegEx token -> Leading (TokenClass token)
forall token.
Categorized token =>
Map String (Leading (TokenClass token))
-> RegEx token -> Leading (TokenClass token)
firstOf (Bnf (RegEx token) -> Map String (Leading (TokenClass token))
forall token.
Categorized token =>
Bnf (RegEx token) -> Map String (Leading (TokenClass token))
firsts Bnf (RegEx token)
bnf)
    followers :: Map String (Guard token)
followers = Bnf (RegEx token) -> Map String (Guard token)
forall token.
Categorized token =>
Bnf (RegEx token) -> Map String (Guard token)
follows Bnf (RegEx token)
bnf
    nullable :: RegEx token -> Bool
nullable = Leading (TokenClass token) -> Bool
forall a. Leading a -> Bool
leadingNullable (Leading (TokenClass token) -> Bool)
-> (RegEx token -> Leading (TokenClass token))
-> RegEx token
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RegEx token -> Leading (TokenClass token)
first
    atoms :: RegEx token -> TokenClass token
atoms = Leading (TokenClass token) -> TokenClass token
forall a. Leading a -> a
leadingAtoms (Leading (TokenClass token) -> TokenClass token)
-> (RegEx token -> Leading (TokenClass token))
-> RegEx token
-> TokenClass token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RegEx token -> Leading (TokenClass token)
first
    check :: Maybe String -> RegEx token -> Guard token -> [LL1Conflict token]
check Maybe String
name RegEx token
rex Guard token
after = case RegEx token
rex of
      Sequence RegEx token
x RegEx token
y [RegEx token]
zs -> [[LL1Conflict token]] -> [LL1Conflict token]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
        [Maybe String -> RegEx token -> Guard token -> [LL1Conflict token]
check Maybe String
name RegEx token
item Guard token
guard | (RegEx token
item, Guard token
guard) <- (RegEx token -> Leading (TokenClass token))
-> Guard token -> [RegEx token] -> [(RegEx token, Guard token)]
forall token.
Categorized token =>
(RegEx token -> Leading (TokenClass token))
-> Guard token -> [RegEx token] -> [(RegEx token, Guard token)]
sequenced RegEx token -> Leading (TokenClass token)
first Guard token
after (RegEx token
x RegEx token -> [RegEx token] -> [RegEx token]
forall a. a -> [a] -> [a]
: RegEx token
y RegEx token -> [RegEx token] -> [RegEx token]
forall a. a -> [a] -> [a]
: [RegEx token]
zs)]
      KleeneStar RegEx token
x -> RegEx token -> [LL1Conflict token]
repetition RegEx token
x [LL1Conflict token] -> [LL1Conflict token] -> [LL1Conflict token]
forall a. Semigroup a => a -> a -> a
<> Maybe String -> RegEx token -> Guard token -> [LL1Conflict token]
check Maybe String
name RegEx token
x (RegEx token -> Guard token
again RegEx token
x)
      KleenePlus RegEx token
x -> RegEx token -> [LL1Conflict token]
repetition RegEx token
x [LL1Conflict token] -> [LL1Conflict token] -> [LL1Conflict token]
forall a. Semigroup a => a -> a -> a
<> Maybe String -> RegEx token -> Guard token -> [LL1Conflict token]
check Maybe String
name RegEx token
x (RegEx token -> Guard token
again RegEx token
x)
      KleeneOpt RegEx token
x -> RegEx token -> [LL1Conflict token]
repetition RegEx token
x [LL1Conflict token] -> [LL1Conflict token] -> [LL1Conflict token]
forall a. Semigroup a => a -> a -> a
<> Maybe String -> RegEx token -> Guard token -> [LL1Conflict token]
check Maybe String
name RegEx token
x Guard token
after
      Backtracking RegEx token
x -> Maybe String -> RegEx token -> Guard token -> [LL1Conflict token]
check Maybe String
name RegEx token
x Guard token
after
      KleeneTimes Int
lo Maybe Int
hi RegEx token
x -> Maybe String -> RegEx token -> Guard token -> [LL1Conflict token]
check Maybe String
name (Int -> Maybe Int -> RegEx token -> RegEx token
forall token.
Categorized token =>
Int -> Maybe Int -> RegEx token -> RegEx token
unrolled Int
lo Maybe Int
hi RegEx token
x) Guard token
after
      RegExam (Alternate RegEx token
x RegEx token
y [RegEx token]
zs) ->
        [[LL1Conflict token]] -> [LL1Conflict token]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [RegEx token -> RegEx token -> [LL1Conflict token]
alternation RegEx token
a RegEx token
b | (RegEx token
a, [RegEx token]
bs) <- [RegEx token] -> [(RegEx token, [RegEx token])]
forall {a}. [a] -> [(a, [a])]
pairs (RegEx token
x RegEx token -> [RegEx token] -> [RegEx token]
forall a. a -> [a] -> [a]
: RegEx token
y RegEx token -> [RegEx token] -> [RegEx token]
forall a. a -> [a] -> [a]
: [RegEx token]
zs), RegEx token
b <- [RegEx token]
bs]
        [LL1Conflict token] -> [LL1Conflict token] -> [LL1Conflict token]
forall a. Semigroup a => a -> a -> a
<> (RegEx token -> [LL1Conflict token])
-> [RegEx token] -> [LL1Conflict token]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\RegEx token
a -> Maybe String -> RegEx token -> Guard token -> [LL1Conflict token]
check Maybe String
name RegEx token
a Guard token
after) (RegEx token
x RegEx token -> [RegEx token] -> [RegEx token]
forall a. a -> [a] -> [a]
: RegEx token
y RegEx token -> [RegEx token] -> [RegEx token]
forall a. a -> [a] -> [a]
: [RegEx token]
zs)
      RegEx token
_ -> []
      where
        conflict :: LL1ConflictSort -> TokenClass token -> LL1Conflict token
conflict LL1ConflictSort
sort TokenClass token
toks = Maybe String
-> LL1ConflictSort
-> TokenClass token
-> RegEx token
-> LL1Conflict token
forall token.
Maybe String
-> LL1ConflictSort
-> TokenClass token
-> RegEx token
-> LL1Conflict token
LL1Conflict Maybe String
name LL1ConflictSort
sort TokenClass token
toks RegEx token
rex
        again :: RegEx token -> Guard token
again RegEx token
x = Bool -> TokenClass token -> Guard token
forall token. Bool -> TokenClass token -> Guard token
Guard Bool
False (RegEx token -> TokenClass token
atoms RegEx token
x) Guard token -> Guard token -> Guard token
forall a. DisjunctiveMonoid a => a -> a -> a
>|< Guard token
after
        overlapping :: a -> [a]
overlapping a
toks
          | a
toks a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. DisjunctiveMonoid a => a
falseP = []
          | Bool
otherwise = [a
toks]
        repetition :: RegEx token -> [LL1Conflict token]
repetition RegEx token
x =
          [ LL1ConflictSort -> TokenClass token -> LL1Conflict token
conflict LL1ConflictSort
LL1NullableRepetition TokenClass token
forall a. DisjunctiveMonoid a => a
falseP | RegEx token -> Bool
nullable RegEx token
x ]
          [LL1Conflict token] -> [LL1Conflict token] -> [LL1Conflict token]
forall a. Semigroup a => a -> a -> a
<> [ LL1ConflictSort -> TokenClass token -> LL1Conflict token
conflict LL1ConflictSort
LL1FirstFollow TokenClass token
toks
             | Bool -> Bool
not (RegEx token -> Bool
forall {token}. RegEx token -> Bool
tried RegEx token
x)
             , TokenClass token
toks <- TokenClass token -> [TokenClass token]
forall {a}. (Eq a, DisjunctiveMonoid a) => a -> [a]
overlapping (RegEx token -> TokenClass token
atoms RegEx token
x TokenClass token -> TokenClass token -> TokenClass token
forall b. BooleanAlgebra b => b -> b -> b
>&< Guard token -> TokenClass token
forall token. Guard token -> TokenClass token
guardNext Guard token
after) ]
        -- an alternative, & one after it
        alternation :: RegEx token -> RegEx token -> [LL1Conflict token]
alternation RegEx token
a RegEx token
b =
          [ LL1ConflictSort -> TokenClass token -> LL1Conflict token
conflict LL1ConflictSort
LL1FirstFirst TokenClass token
toks
          | Bool -> Bool
not (RegEx token -> Bool
forall {token}. RegEx token -> Bool
tried RegEx token
a)
          , TokenClass token
toks <- TokenClass token -> [TokenClass token]
forall {a}. (Eq a, DisjunctiveMonoid a) => a -> [a]
overlapping (RegEx token -> TokenClass token
atoms RegEx token
a TokenClass token -> TokenClass token -> TokenClass token
forall b. BooleanAlgebra b => b -> b -> b
>&< RegEx token -> TokenClass token
atoms RegEx token
b) ]
          [LL1Conflict token] -> [LL1Conflict token] -> [LL1Conflict token]
forall a. Semigroup a => a -> a -> a
<> [ LL1ConflictSort -> TokenClass token -> LL1Conflict token
conflict LL1ConflictSort
LL1NullableAlternatives TokenClass token
forall a. DisjunctiveMonoid a => a
falseP
             | RegEx token -> Bool
nullable RegEx token
a, RegEx token -> Bool
nullable RegEx token
b ]
          [LL1Conflict token] -> [LL1Conflict token] -> [LL1Conflict token]
forall a. Semigroup a => a -> a -> a
<> [ LL1ConflictSort -> TokenClass token -> LL1Conflict token
conflict LL1ConflictSort
LL1NullableFirst (RegEx token -> TokenClass token
atoms RegEx token
b)
             | RegEx token -> Bool
nullable RegEx token
a, Bool -> Bool
not (RegEx token -> Bool
nullable RegEx token
b), RegEx token -> TokenClass token
atoms RegEx token
b TokenClass token -> TokenClass token -> Bool
forall a. Eq a => a -> a -> Bool
/= TokenClass token
forall a. DisjunctiveMonoid a => a
falseP ]
          [LL1Conflict token] -> [LL1Conflict token] -> [LL1Conflict token]
forall a. Semigroup a => a -> a -> a
<> [ LL1ConflictSort -> TokenClass token -> LL1Conflict token
conflict LL1ConflictSort
LL1FirstFollow TokenClass token
toks
             | RegEx token -> Bool
nullable RegEx token
b, Bool -> Bool
not (RegEx token -> Bool
nullable RegEx token
a), Bool -> Bool
not (RegEx token -> Bool
forall {token}. RegEx token -> Bool
tried RegEx token
a)
             , TokenClass token
toks <- TokenClass token -> [TokenClass token]
forall {a}. (Eq a, DisjunctiveMonoid a) => a -> [a]
overlapping (RegEx token -> TokenClass token
atoms RegEx token
a TokenClass token -> TokenClass token -> TokenClass token
forall b. BooleanAlgebra b => b -> b -> b
>&< Guard token -> TokenClass token
forall token. Guard token -> TokenClass token
guardNext Guard token
after) ]
    pairs :: [a] -> [(a, [a])]
pairs = \case
      [] -> []
      a
a : [a]
bs -> (a
a, [a]
bs) (a, [a]) -> [(a, [a])] -> [(a, [a])]
forall a. a -> [a] -> [a]
: [a] -> [(a, [a])]
pairs [a]
bs
    -- a pattern which backtracks, after reading tokens, by `try`
    tried :: RegEx token -> Bool
tried = \case
      Backtracking RegEx token
_ -> Bool
True
      RegEx token
_ -> Bool
False

{- | 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.
-}
data LL1Conflict token = LL1Conflict
  { forall token. LL1Conflict token -> Maybe String
conflictRule :: Maybe String
  , forall token. LL1Conflict token -> LL1ConflictSort
conflictSort :: LL1ConflictSort
  , forall token. LL1Conflict token -> TokenClass token
conflictTokens :: TokenClass token
  , forall token. LL1Conflict token -> RegEx token
conflictAt :: RegEx token
  }
deriving stock instance Categorized token => Eq (LL1Conflict token)
deriving stock instance
  (Categorized token, Show token, Show (Categorize token))
    => Show (LL1Conflict token)

-- | Sorts of `LL1Conflict`s.
data LL1ConflictSort
  = 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
  deriving stock (LL1ConflictSort -> LL1ConflictSort -> Bool
(LL1ConflictSort -> LL1ConflictSort -> Bool)
-> (LL1ConflictSort -> LL1ConflictSort -> Bool)
-> Eq LL1ConflictSort
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LL1ConflictSort -> LL1ConflictSort -> Bool
== :: LL1ConflictSort -> LL1ConflictSort -> Bool
$c/= :: LL1ConflictSort -> LL1ConflictSort -> Bool
/= :: LL1ConflictSort -> LL1ConflictSort -> Bool
Eq, Eq LL1ConflictSort
Eq LL1ConflictSort =>
(LL1ConflictSort -> LL1ConflictSort -> Ordering)
-> (LL1ConflictSort -> LL1ConflictSort -> Bool)
-> (LL1ConflictSort -> LL1ConflictSort -> Bool)
-> (LL1ConflictSort -> LL1ConflictSort -> Bool)
-> (LL1ConflictSort -> LL1ConflictSort -> Bool)
-> (LL1ConflictSort -> LL1ConflictSort -> LL1ConflictSort)
-> (LL1ConflictSort -> LL1ConflictSort -> LL1ConflictSort)
-> Ord LL1ConflictSort
LL1ConflictSort -> LL1ConflictSort -> Bool
LL1ConflictSort -> LL1ConflictSort -> Ordering
LL1ConflictSort -> LL1ConflictSort -> LL1ConflictSort
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: LL1ConflictSort -> LL1ConflictSort -> Ordering
compare :: LL1ConflictSort -> LL1ConflictSort -> Ordering
$c< :: LL1ConflictSort -> LL1ConflictSort -> Bool
< :: LL1ConflictSort -> LL1ConflictSort -> Bool
$c<= :: LL1ConflictSort -> LL1ConflictSort -> Bool
<= :: LL1ConflictSort -> LL1ConflictSort -> Bool
$c> :: LL1ConflictSort -> LL1ConflictSort -> Bool
> :: LL1ConflictSort -> LL1ConflictSort -> Bool
$c>= :: LL1ConflictSort -> LL1ConflictSort -> Bool
>= :: LL1ConflictSort -> LL1ConflictSort -> Bool
$cmax :: LL1ConflictSort -> LL1ConflictSort -> LL1ConflictSort
max :: LL1ConflictSort -> LL1ConflictSort -> LL1ConflictSort
$cmin :: LL1ConflictSort -> LL1ConflictSort -> LL1ConflictSort
min :: LL1ConflictSort -> LL1ConflictSort -> LL1ConflictSort
Ord, Int -> LL1ConflictSort -> ShowS
[LL1ConflictSort] -> ShowS
LL1ConflictSort -> String
(Int -> LL1ConflictSort -> ShowS)
-> (LL1ConflictSort -> String)
-> ([LL1ConflictSort] -> ShowS)
-> Show LL1ConflictSort
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LL1ConflictSort -> ShowS
showsPrec :: Int -> LL1ConflictSort -> ShowS
$cshow :: LL1ConflictSort -> String
show :: LL1ConflictSort -> String
$cshowList :: [LL1ConflictSort] -> ShowS
showList :: [LL1ConflictSort] -> ShowS
Show, ReadPrec [LL1ConflictSort]
ReadPrec LL1ConflictSort
Int -> ReadS LL1ConflictSort
ReadS [LL1ConflictSort]
(Int -> ReadS LL1ConflictSort)
-> ReadS [LL1ConflictSort]
-> ReadPrec LL1ConflictSort
-> ReadPrec [LL1ConflictSort]
-> Read LL1ConflictSort
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS LL1ConflictSort
readsPrec :: Int -> ReadS LL1ConflictSort
$creadList :: ReadS [LL1ConflictSort]
readList :: ReadS [LL1ConflictSort]
$creadPrec :: ReadPrec LL1ConflictSort
readPrec :: ReadPrec LL1ConflictSort
$creadListPrec :: ReadPrec [LL1ConflictSort]
readListPrec :: ReadPrec [LL1ConflictSort]
Read, Int -> LL1ConflictSort
LL1ConflictSort -> Int
LL1ConflictSort -> [LL1ConflictSort]
LL1ConflictSort -> LL1ConflictSort
LL1ConflictSort -> LL1ConflictSort -> [LL1ConflictSort]
LL1ConflictSort
-> LL1ConflictSort -> LL1ConflictSort -> [LL1ConflictSort]
(LL1ConflictSort -> LL1ConflictSort)
-> (LL1ConflictSort -> LL1ConflictSort)
-> (Int -> LL1ConflictSort)
-> (LL1ConflictSort -> Int)
-> (LL1ConflictSort -> [LL1ConflictSort])
-> (LL1ConflictSort -> LL1ConflictSort -> [LL1ConflictSort])
-> (LL1ConflictSort -> LL1ConflictSort -> [LL1ConflictSort])
-> (LL1ConflictSort
    -> LL1ConflictSort -> LL1ConflictSort -> [LL1ConflictSort])
-> Enum LL1ConflictSort
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: LL1ConflictSort -> LL1ConflictSort
succ :: LL1ConflictSort -> LL1ConflictSort
$cpred :: LL1ConflictSort -> LL1ConflictSort
pred :: LL1ConflictSort -> LL1ConflictSort
$ctoEnum :: Int -> LL1ConflictSort
toEnum :: Int -> LL1ConflictSort
$cfromEnum :: LL1ConflictSort -> Int
fromEnum :: LL1ConflictSort -> Int
$cenumFrom :: LL1ConflictSort -> [LL1ConflictSort]
enumFrom :: LL1ConflictSort -> [LL1ConflictSort]
$cenumFromThen :: LL1ConflictSort -> LL1ConflictSort -> [LL1ConflictSort]
enumFromThen :: LL1ConflictSort -> LL1ConflictSort -> [LL1ConflictSort]
$cenumFromTo :: LL1ConflictSort -> LL1ConflictSort -> [LL1ConflictSort]
enumFromTo :: LL1ConflictSort -> LL1ConflictSort -> [LL1ConflictSort]
$cenumFromThenTo :: LL1ConflictSort
-> LL1ConflictSort -> LL1ConflictSort -> [LL1ConflictSort]
enumFromThenTo :: LL1ConflictSort
-> LL1ConflictSort -> LL1ConflictSort -> [LL1ConflictSort]
Enum, LL1ConflictSort
LL1ConflictSort -> LL1ConflictSort -> Bounded LL1ConflictSort
forall a. a -> a -> Bounded a
$cminBound :: LL1ConflictSort
minBound :: LL1ConflictSort
$cmaxBound :: LL1ConflictSort
maxBound :: LL1ConflictSort
Bounded)

-- | Counts, unrolled, @x{lo,hi}@ as @lo@ times @x@ & then
-- @x*@, or @hi - lo@ nested options.
unrolled :: Categorized token => Int -> Maybe Int -> RegEx token -> RegEx token
unrolled :: forall token.
Categorized token =>
Int -> Maybe Int -> RegEx token -> RegEx token
unrolled Int
lo Maybe Int
hi RegEx token
x = [RegEx token] -> RegEx token
forall a. Monoid a => [a] -> a
mconcat (Int -> RegEx token -> [RegEx token]
forall a. Int -> a -> [a]
replicate Int
lo RegEx token
x) RegEx token -> RegEx token -> RegEx token
forall a. Semigroup a => a -> a -> a
<> case Maybe Int
hi of
  Maybe Int
Nothing -> RegEx token -> RegEx token
forall k. KleeneStarAlgebra k => k -> k
starK RegEx token
x
  Just Int
m -> (Int -> RegEx token -> RegEx token)
-> RegEx token -> [Int] -> RegEx token
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\Int
_ RegEx token
rest -> RegEx token -> RegEx token
forall k. KleeneStarAlgebra k => k -> k
optK (RegEx token
x RegEx token -> RegEx token -> RegEx token
forall a. Semigroup a => a -> a -> a
<> RegEx token
rest)) RegEx token
forall a. Monoid a => a
mempty [Int
lo Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 .. Int
m]