{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_HADDOCK not-home #-}
module Regex.Internal.Parser
( Parser(..)
, Node(..)
, compile
, compileBounded
, ParserState
, prepareParser
, stepParser
, finishParser
, Foldr
, parseFoldr
, parseNext
) where
import Control.Applicative
import Control.Monad.Trans.State.Strict
import Control.Monad.Fix
import Data.Maybe (isJust)
import Data.Primitive.SmallArray
import qualified Data.Foldable as F
import qualified GHC.Exts as X
import Regex.Internal.Regex (RE(..), Strictness(..), Greediness(..))
import Regex.Internal.Unique (Unique(..), UniqueSet)
import qualified Regex.Internal.Unique as U
data Parser c a where
PToken :: !(c -> Maybe a) -> Parser c a
PFmap :: !Strictness -> !(a1 -> a) -> !(Parser c a1) -> Parser c a
PFmap_ :: !(Node c a) -> Parser c a
PPure :: a -> Parser c a
PLiftA2 :: !Strictness -> !(a1 -> a2 -> a) -> !(Parser c a1) -> !(Parser c a2) -> Parser c a
PEmpty :: Parser c a
PAlt :: {-# UNPACK #-} !Unique -> !(Parser c a) -> !(Parser c a) -> {-# UNPACK #-} !(SmallArray (Parser c a)) -> Parser c a
PFoldGr :: {-# UNPACK #-} !Unique -> !Strictness -> !(a -> a1 -> a) -> a -> !(Parser c a1) -> Parser c a
PFoldMn :: {-# UNPACK #-} !Unique -> !Strictness -> !(a -> a1 -> a) -> a -> !(Parser c a1) -> Parser c a
PMany :: {-# UNPACK #-} !Unique -> !(a1 -> a) -> !(a2 -> a) -> !(a2 -> a1 -> a2) -> !a2 -> !(Parser c a1) -> Parser c a
data Node c a where
NAccept :: a -> Node c a
NGuard :: {-# UNPACK #-} !Unique -> Node c a -> Node c a
NToken :: !(c -> Maybe a1) -> !(Node c a) -> Node c a
NEmpty :: Node c a
NAlt :: !(Node c a) -> !(Node c a) -> {-# UNPACK #-} !(SmallArray (Node c a)) -> Node c a
compile :: RE c a -> Parser c a
compile :: forall c a. RE c a -> Parser c a
compile RE c a
re = State Unique (Parser c a) -> Unique -> Parser c a
forall s a. State s a -> s -> a
evalState (RE c a -> State Unique (Parser c a)
forall c a. RE c a -> State Unique (Parser c a)
compileToParser RE c a
re) (Int -> Unique
Unique Int
0)
nxtU :: State Unique Unique
nxtU :: State Unique Unique
nxtU = (Unique -> (Unique, Unique)) -> State Unique Unique
forall (m :: * -> *) s a. Monad m => (s -> (a, s)) -> StateT s m a
state ((Unique -> (Unique, Unique)) -> State Unique Unique)
-> (Unique -> (Unique, Unique)) -> State Unique Unique
forall a b. (a -> b) -> a -> b
$ \Unique
u -> let !u' :: Unique
u' = Int -> Unique
Unique (Unique -> Int
unUnique Unique
u Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) in (Unique
u, Unique
u')
compileToParser :: RE c a -> State Unique (Parser c a)
compileToParser :: forall c a. RE c a -> State Unique (Parser c a)
compileToParser RE c a
re = case RE c a
re of
RToken c -> Maybe a
t -> Parser c a -> State Unique (Parser c a)
forall a. a -> StateT Unique Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Parser c a -> State Unique (Parser c a))
-> Parser c a -> State Unique (Parser c a)
forall a b. (a -> b) -> a -> b
$ (c -> Maybe a) -> Parser c a
forall c a. (c -> Maybe a) -> Parser c a
PToken c -> Maybe a
t
RFmap Strictness
st a1 -> a
f RE c a1
re1 -> Strictness -> (a1 -> a) -> Parser c a1 -> Parser c a
forall a1 a c. Strictness -> (a1 -> a) -> Parser c a1 -> Parser c a
PFmap Strictness
st a1 -> a
f (Parser c a1 -> Parser c a)
-> StateT Unique Identity (Parser c a1)
-> State Unique (Parser c a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RE c a1 -> StateT Unique Identity (Parser c a1)
forall c a. RE c a -> State Unique (Parser c a)
compileToParser RE c a1
re1
RFmap_ a
a RE c a1
re1 -> Node c a -> Parser c a
forall c a. Node c a -> Parser c a
PFmap_ (Node c a -> Parser c a)
-> StateT Unique Identity (Node c a) -> State Unique (Parser c a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> RE c a1 -> StateT Unique Identity (Node c a)
forall c a a1. a -> RE c a1 -> State Unique (Node c a)
compileToNode a
a RE c a1
re1
RPure a
a -> Parser c a -> State Unique (Parser c a)
forall a. a -> StateT Unique Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Parser c a -> State Unique (Parser c a))
-> Parser c a -> State Unique (Parser c a)
forall a b. (a -> b) -> a -> b
$ a -> Parser c a
forall a c. a -> Parser c a
PPure a
a
RLiftA2 Strictness
st a1 -> a2 -> a
f RE c a1
re1 RE c a2
re2 ->
(Parser c a1 -> Parser c a2 -> Parser c a)
-> StateT Unique Identity (Parser c a1)
-> StateT Unique Identity (Parser c a2)
-> State Unique (Parser c a)
forall a b c.
(a -> b -> c)
-> StateT Unique Identity a
-> StateT Unique Identity b
-> StateT Unique Identity c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (Strictness
-> (a1 -> a2 -> a) -> Parser c a1 -> Parser c a2 -> Parser c a
forall a1 a3 a c.
Strictness
-> (a1 -> a3 -> a) -> Parser c a1 -> Parser c a3 -> Parser c a
PLiftA2 Strictness
st a1 -> a2 -> a
f) (RE c a1 -> StateT Unique Identity (Parser c a1)
forall c a. RE c a -> State Unique (Parser c a)
compileToParser RE c a1
re1) (RE c a2 -> StateT Unique Identity (Parser c a2)
forall c a. RE c a -> State Unique (Parser c a)
compileToParser RE c a2
re2)
RE c a
REmpty -> Parser c a -> State Unique (Parser c a)
forall a. a -> StateT Unique Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Parser c a
forall c a. Parser c a
PEmpty
RAlt RE c a
re01 RE c a
re02 -> do
u <- State Unique Unique
nxtU
let (re1,re2,res) = gatherAlts re01 re02
p1 <- compileToParser re1
p2 <- compileToParser re2
ps <- traverse compileToParser res
pure $ PAlt u p1 p2 (smallArrayFromList ps)
RFold Strictness
st Greediness
gr a -> a1 -> a
f a
z RE c a1
re1 -> do
u <- State Unique Unique
nxtU
_localU <- nxtU
case gr of
Greediness
Greedy -> Unique
-> Strictness -> (a -> a1 -> a) -> a -> Parser c a1 -> Parser c a
forall a a1 c.
Unique
-> Strictness -> (a -> a1 -> a) -> a -> Parser c a1 -> Parser c a
PFoldGr Unique
u Strictness
st a -> a1 -> a
f a
z (Parser c a1 -> Parser c a)
-> StateT Unique Identity (Parser c a1)
-> State Unique (Parser c a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RE c a1 -> StateT Unique Identity (Parser c a1)
forall c a. RE c a -> State Unique (Parser c a)
compileToParser RE c a1
re1
Greediness
Minimal -> Unique
-> Strictness -> (a -> a1 -> a) -> a -> Parser c a1 -> Parser c a
forall a a1 c.
Unique
-> Strictness -> (a -> a1 -> a) -> a -> Parser c a1 -> Parser c a
PFoldMn Unique
u Strictness
st a -> a1 -> a
f a
z (Parser c a1 -> Parser c a)
-> StateT Unique Identity (Parser c a1)
-> State Unique (Parser c a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RE c a1 -> StateT Unique Identity (Parser c a1)
forall c a. RE c a -> State Unique (Parser c a)
compileToParser RE c a1
re1
RMany a1 -> a
f1 a2 -> a
f2 a2 -> a1 -> a2
f a2
z RE c a1
re1 -> do
u <- State Unique Unique
nxtU
_localU <- nxtU
PMany u f1 f2 f z <$> compileToParser re1
compileToNode :: forall c a a1. a -> RE c a1 -> State Unique (Node c a)
compileToNode :: forall c a a1. a -> RE c a1 -> State Unique (Node c a)
compileToNode a
a RE c a1
re0 = RE c a1 -> Node c a -> State Unique (Node c a)
forall a2. RE c a2 -> Node c a -> State Unique (Node c a)
go RE c a1
re0 (a -> Node c a
forall a c. a -> Node c a
NAccept a
a)
where
go :: forall a2. RE c a2 -> Node c a -> State Unique (Node c a)
go :: forall a2. RE c a2 -> Node c a -> State Unique (Node c a)
go RE c a2
re Node c a
nxt = case RE c a2
re of
RToken c -> Maybe a2
t -> Node c a -> State Unique (Node c a)
forall a. a -> StateT Unique Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Node c a -> State Unique (Node c a))
-> Node c a -> State Unique (Node c a)
forall a b. (a -> b) -> a -> b
$ (c -> Maybe a2) -> Node c a -> Node c a
forall c a1 a. (c -> Maybe a1) -> Node c a -> Node c a
NToken c -> Maybe a2
t Node c a
nxt
RFmap Strictness
_ a1 -> a2
_ RE c a1
re1 -> RE c a1 -> Node c a -> State Unique (Node c a)
forall a2. RE c a2 -> Node c a -> State Unique (Node c a)
go RE c a1
re1 Node c a
nxt
RFmap_ a2
_ RE c a1
re1 -> RE c a1 -> Node c a -> State Unique (Node c a)
forall a2. RE c a2 -> Node c a -> State Unique (Node c a)
go RE c a1
re1 Node c a
nxt
RPure a2
_ -> Node c a -> State Unique (Node c a)
forall a. a -> StateT Unique Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Node c a
nxt
RLiftA2 Strictness
_ a1 -> a2 -> a2
_ RE c a1
re1 RE c a2
re2 -> RE c a2 -> Node c a -> State Unique (Node c a)
forall a2. RE c a2 -> Node c a -> State Unique (Node c a)
go RE c a2
re2 Node c a
nxt State Unique (Node c a)
-> (Node c a -> State Unique (Node c a)) -> State Unique (Node c a)
forall a b.
StateT Unique Identity a
-> (a -> StateT Unique Identity b) -> StateT Unique Identity b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= RE c a1 -> Node c a -> State Unique (Node c a)
forall a2. RE c a2 -> Node c a -> State Unique (Node c a)
go RE c a1
re1
RE c a2
REmpty -> Node c a -> State Unique (Node c a)
forall a. a -> StateT Unique Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Node c a
forall c a. Node c a
NEmpty
RAlt RE c a2
re01 RE c a2
re02 -> do
u <- State Unique Unique
nxtU
let nxt1 = Unique -> Node c a -> Node c a
forall c a. Unique -> Node c a -> Node c a
NGuard Unique
u Node c a
nxt
(re1,re2,res) = gatherAlts re01 re02
n1 <- go re1 nxt1
n2 <- go re2 nxt1
ns <- traverse (flip go nxt1) res
pure $ NAlt n1 n2 (smallArrayFromList ns)
RFold Strictness
_ Greediness
gr a2 -> a1 -> a2
_ a2
_ RE c a1
re1 -> Greediness -> RE c a1 -> Node c a -> State Unique (Node c a)
forall a2.
Greediness -> RE c a2 -> Node c a -> State Unique (Node c a)
goMany Greediness
gr RE c a1
re1 Node c a
nxt
RMany a1 -> a2
_ a2 -> a2
_ a2 -> a1 -> a2
_ a2
_ RE c a1
re1 -> Greediness -> RE c a1 -> Node c a -> State Unique (Node c a)
forall a2.
Greediness -> RE c a2 -> Node c a -> State Unique (Node c a)
goMany Greediness
Greedy RE c a1
re1 Node c a
nxt
goMany :: forall a2.
Greediness -> RE c a2 -> Node c a -> State Unique (Node c a)
goMany :: forall a2.
Greediness -> RE c a2 -> Node c a -> State Unique (Node c a)
goMany Greediness
gr RE c a2
re1 Node c a
nxt = do
u <- State Unique Unique
nxtU
mfix $ \Node c a
n -> do
ndown <- RE c a2 -> Node c a -> State Unique (Node c a)
forall a2. RE c a2 -> Node c a -> State Unique (Node c a)
go RE c a2
re1 Node c a
n
case gr of
Greediness
Greedy -> Node c a -> State Unique (Node c a)
forall a. a -> StateT Unique Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Node c a -> State Unique (Node c a))
-> Node c a -> State Unique (Node c a)
forall a b. (a -> b) -> a -> b
$ Unique -> Node c a -> Node c a
forall c a. Unique -> Node c a -> Node c a
NGuard Unique
u (Node c a -> Node c a -> SmallArray (Node c a) -> Node c a
forall c a.
Node c a -> Node c a -> SmallArray (Node c a) -> Node c a
NAlt Node c a
ndown Node c a
nxt SmallArray (Node c a)
forall a. SmallArray a
emptySmallArray)
Greediness
Minimal -> Node c a -> State Unique (Node c a)
forall a. a -> StateT Unique Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Node c a -> State Unique (Node c a))
-> Node c a -> State Unique (Node c a)
forall a b. (a -> b) -> a -> b
$ Unique -> Node c a -> Node c a
forall c a. Unique -> Node c a -> Node c a
NGuard Unique
u (Node c a -> Node c a -> SmallArray (Node c a) -> Node c a
forall c a.
Node c a -> Node c a -> SmallArray (Node c a) -> Node c a
NAlt Node c a
nxt Node c a
ndown SmallArray (Node c a)
forall a. SmallArray a
emptySmallArray)
gatherAlts :: RE c a -> RE c a -> (RE c a, RE c a, [RE c a])
gatherAlts :: forall c a. RE c a -> RE c a -> (RE c a, RE c a, [RE c a])
gatherAlts RE c a
re01 RE c a
re02 = case RE c a -> [RE c a] -> [RE c a]
forall {c} {a}. RE c a -> [RE c a] -> [RE c a]
go RE c a
re01 (RE c a -> [RE c a] -> [RE c a]
forall {c} {a}. RE c a -> [RE c a] -> [RE c a]
go RE c a
re02 []) of
RE c a
re11:RE c a
re12:[RE c a]
res -> (RE c a
re11, RE c a
re12, [RE c a]
res)
[RE c a]
_ -> [Char] -> (RE c a, RE c a, [RE c a])
forall a. [Char] -> a
errorWithoutStackTrace [Char]
"Regex.Internal.Parser.gatherAlts: impossible"
where
go :: RE c a -> [RE c a] -> [RE c a]
go (RAlt RE c a
re1 RE c a
re2) = RE c a -> [RE c a] -> [RE c a]
go RE c a
re1 ([RE c a] -> [RE c a])
-> ([RE c a] -> [RE c a]) -> [RE c a] -> [RE c a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RE c a -> [RE c a] -> [RE c a]
go RE c a
re2
go RE c a
re = (RE c a
reRE c a -> [RE c a] -> [RE c a]
forall a. a -> [a] -> [a]
:)
compileBounded :: Int -> RE c a -> Maybe (Parser c a)
compileBounded :: forall c a. Int -> RE c a -> Maybe (Parser c a)
compileBounded Int
lim RE c a
re =
if Int -> RE c a -> Bool
forall c a. Int -> RE c a -> Bool
checkSize Int
lim RE c a
re
then Parser c a -> Maybe (Parser c a)
forall a. a -> Maybe a
Just (Parser c a -> Maybe (Parser c a))
-> Parser c a -> Maybe (Parser c a)
forall a b. (a -> b) -> a -> b
$! RE c a -> Parser c a
forall c a. RE c a -> Parser c a
compile RE c a
re
else Maybe (Parser c a)
forall a. Maybe a
Nothing
checkSize :: Int -> RE c a -> Bool
checkSize :: forall c a. Int -> RE c a -> Bool
checkSize Int
lim RE c a
re0 = Maybe () -> Bool
forall a. Maybe a -> Bool
isJust (StateT Int Maybe () -> Int -> Maybe ()
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT (RE c a -> StateT Int Maybe ()
forall c a1. RE c a1 -> StateT Int Maybe ()
go RE c a
re0) Int
0)
where
go :: RE c a1 -> StateT Int Maybe ()
go :: forall c a1. RE c a1 -> StateT Int Maybe ()
go RE c a1
re = case RE c a1
re of
RToken c -> Maybe a1
_ -> StateT Int Maybe ()
inc
RFmap Strictness
_ a1 -> a1
_ RE c a1
re1 -> StateT Int Maybe ()
inc StateT Int Maybe () -> StateT Int Maybe () -> StateT Int Maybe ()
forall a b.
StateT Int Maybe a -> StateT Int Maybe b -> StateT Int Maybe b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> RE c a1 -> StateT Int Maybe ()
forall c a1. RE c a1 -> StateT Int Maybe ()
go RE c a1
re1
RFmap_ a1
_ RE c a1
re1 -> StateT Int Maybe ()
inc StateT Int Maybe () -> StateT Int Maybe () -> StateT Int Maybe ()
forall a b.
StateT Int Maybe a -> StateT Int Maybe b -> StateT Int Maybe b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> RE c a1 -> StateT Int Maybe ()
forall c a1. RE c a1 -> StateT Int Maybe ()
go RE c a1
re1
RPure a1
_ -> StateT Int Maybe ()
inc
RLiftA2 Strictness
_ a1 -> a2 -> a1
_ RE c a1
re1 RE c a2
re2 -> StateT Int Maybe ()
inc StateT Int Maybe () -> StateT Int Maybe () -> StateT Int Maybe ()
forall a b.
StateT Int Maybe a -> StateT Int Maybe b -> StateT Int Maybe b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> RE c a1 -> StateT Int Maybe ()
forall c a1. RE c a1 -> StateT Int Maybe ()
go RE c a1
re1 StateT Int Maybe () -> StateT Int Maybe () -> StateT Int Maybe ()
forall a b.
StateT Int Maybe a -> StateT Int Maybe b -> StateT Int Maybe b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> RE c a2 -> StateT Int Maybe ()
forall c a1. RE c a1 -> StateT Int Maybe ()
go RE c a2
re2
RE c a1
REmpty -> StateT Int Maybe ()
inc
RAlt RE c a1
re1 RE c a1
re2 -> StateT Int Maybe ()
inc StateT Int Maybe () -> StateT Int Maybe () -> StateT Int Maybe ()
forall a b.
StateT Int Maybe a -> StateT Int Maybe b -> StateT Int Maybe b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> RE c a1 -> StateT Int Maybe ()
forall c a1. RE c a1 -> StateT Int Maybe ()
go RE c a1
re1 StateT Int Maybe () -> StateT Int Maybe () -> StateT Int Maybe ()
forall a b.
StateT Int Maybe a -> StateT Int Maybe b -> StateT Int Maybe b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> RE c a1 -> StateT Int Maybe ()
forall c a1. RE c a1 -> StateT Int Maybe ()
go RE c a1
re2
RMany a1 -> a1
_ a2 -> a1
_ a2 -> a1 -> a2
_ a2
_ RE c a1
re1 -> StateT Int Maybe ()
inc StateT Int Maybe () -> StateT Int Maybe () -> StateT Int Maybe ()
forall a b.
StateT Int Maybe a -> StateT Int Maybe b -> StateT Int Maybe b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> RE c a1 -> StateT Int Maybe ()
forall c a1. RE c a1 -> StateT Int Maybe ()
go RE c a1
re1
RFold Strictness
_ Greediness
_ a1 -> a1 -> a1
_ a1
_ RE c a1
re1 -> StateT Int Maybe ()
inc StateT Int Maybe () -> StateT Int Maybe () -> StateT Int Maybe ()
forall a b.
StateT Int Maybe a -> StateT Int Maybe b -> StateT Int Maybe b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> RE c a1 -> StateT Int Maybe ()
forall c a1. RE c a1 -> StateT Int Maybe ()
go RE c a1
re1
inc :: StateT Int Maybe ()
inc = do
n <- StateT Int Maybe Int
forall (m :: * -> *) s. Monad m => StateT s m s
get
if n == lim
then empty
else put $! n+1
data Cont c b a where
CTop :: Cont c a a
CFmap :: !Strictness -> !(b -> a1) -> !(Cont c a1 a) -> Cont c b a
CFmap_ :: !(Node c a1) -> !(Cont c a1 a) -> Cont c b a
CLiftA2A :: !Strictness -> !(b -> a2 -> a3) -> !(Parser c a2) -> !(Cont c a3 a) -> Cont c b a
CLiftA2B :: !Strictness -> !(a1 -> b -> a3) -> a1 -> !(Cont c a3 a) -> Cont c b a
CAlt :: {-# UNPACK #-} !Unique -> !(Cont c b a) -> Cont c b a
CFoldGr :: {-# UNPACK #-} !Unique -> !Strictness -> !(Parser c b) -> !(a1 -> b -> a1) -> a1 -> !(Cont c a1 a) -> Cont c b a
CFoldMn :: {-# UNPACK #-} !Unique -> !Strictness -> !(Parser c b) -> !(a1 -> b -> a1) -> a1 -> !(Cont c a1 a) -> Cont c b a
CMany :: {-# UNPACK #-} !Unique -> !(Parser c b) -> !(b -> a2) -> !(a1 -> a2) -> !(a1 -> b -> a1) -> !a1 -> !(Cont c a2 a) -> Cont c b a
data NeedCList c a where
NeedCCons :: !(c -> Maybe b) -> !(Cont c b a) -> !(NeedCList c a) -> NeedCList c a
NeedCNil :: NeedCList c a
data StepState c a = StepState
{ forall c a. StepState c a -> UniqueSet
sSet :: {-# UNPACK #-} !UniqueSet
, forall c a. StepState c a -> NeedCList c a
sNeed :: !(NeedCList c a)
, forall c a. StepState c a -> Maybe a
sResult :: !(Maybe a)
}
stepStateZero :: StepState c a
stepStateZero :: forall c a. StepState c a
stepStateZero = UniqueSet -> NeedCList c a -> Maybe a -> StepState c a
forall c a. UniqueSet -> NeedCList c a -> Maybe a -> StepState c a
StepState UniqueSet
U.empty NeedCList c a
forall c a. NeedCList c a
NeedCNil Maybe a
forall a. Maybe a
Nothing
sMember :: Unique -> State (StepState c a) Bool
sMember :: forall c a. Unique -> State (StepState c a) Bool
sMember Unique
u = (StepState c a -> Bool) -> StateT (StepState c a) Identity Bool
forall (m :: * -> *) s a. Monad m => (s -> a) -> StateT s m a
gets ((StepState c a -> Bool) -> StateT (StepState c a) Identity Bool)
-> (StepState c a -> Bool) -> StateT (StepState c a) Identity Bool
forall a b. (a -> b) -> a -> b
$ \StepState c a
pt -> Unique -> UniqueSet -> Bool
U.member Unique
u (StepState c a -> UniqueSet
forall c a. StepState c a -> UniqueSet
sSet StepState c a
pt)
sInsert :: Unique -> State (StepState c a) ()
sInsert :: forall c a. Unique -> State (StepState c a) ()
sInsert Unique
u = (StepState c a -> StepState c a)
-> StateT (StepState c a) Identity ()
forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
modify' ((StepState c a -> StepState c a)
-> StateT (StepState c a) Identity ())
-> (StepState c a -> StepState c a)
-> StateT (StepState c a) Identity ()
forall a b. (a -> b) -> a -> b
$ \StepState c a
pt -> StepState c a
pt { sSet = U.insert u (sSet pt) }
down :: Parser c b -> Cont c b a -> StepState c a -> StepState c a
down :: forall c b a.
Parser c b -> Cont c b a -> StepState c a -> StepState c a
down Parser c b
p !Cont c b a
ct !StepState c a
pt = case Parser c b
p of
PToken c -> Maybe b
t -> StepState c a
pt { sNeed = NeedCCons t ct (sNeed pt) }
PFmap Strictness
st a1 -> b
f Parser c a1
p1 -> Parser c a1 -> Cont c a1 a -> StepState c a -> StepState c a
forall c b a.
Parser c b -> Cont c b a -> StepState c a -> StepState c a
down Parser c a1
p1 (Strictness -> (a1 -> b) -> Cont c b a -> Cont c a1 a
forall b a1 c a.
Strictness -> (b -> a1) -> Cont c a1 a -> Cont c b a
CFmap Strictness
st a1 -> b
f Cont c b a
ct) StepState c a
pt
PFmap_ Node c b
n -> Node c b -> Cont c b a -> StepState c a -> StepState c a
forall c b a.
Node c b -> Cont c b a -> StepState c a -> StepState c a
downNode Node c b
n Cont c b a
ct StepState c a
pt
PPure b
b -> b -> Cont c b a -> StepState c a -> StepState c a
forall b c a. b -> Cont c b a -> StepState c a -> StepState c a
up b
b Cont c b a
ct StepState c a
pt
PLiftA2 Strictness
st a1 -> a2 -> b
f Parser c a1
p1 Parser c a2
p2 -> Parser c a1 -> Cont c a1 a -> StepState c a -> StepState c a
forall c b a.
Parser c b -> Cont c b a -> StepState c a -> StepState c a
down Parser c a1
p1 (Strictness
-> (a1 -> a2 -> b) -> Parser c a2 -> Cont c b a -> Cont c a1 a
forall b a1 a3 c a.
Strictness
-> (b -> a1 -> a3) -> Parser c a1 -> Cont c a3 a -> Cont c b a
CLiftA2A Strictness
st a1 -> a2 -> b
f Parser c a2
p2 Cont c b a
ct) StepState c a
pt
Parser c b
PEmpty -> StepState c a
pt
PAlt Unique
u Parser c b
p1 Parser c b
p2 SmallArray (Parser c b)
ps ->
let ct1 :: Cont c b a
ct1 = Unique -> Cont c b a -> Cont c b a
forall c b a. Unique -> Cont c b a -> Cont c b a
CAlt Unique
u Cont c b a
ct
in (StepState c a -> Parser c b -> StepState c a)
-> StepState c a -> SmallArray (Parser c b) -> StepState c a
forall b a. (b -> a -> b) -> b -> SmallArray a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
F.foldl' (\StepState c a
pt' Parser c b
p' -> Parser c b -> Cont c b a -> StepState c a -> StepState c a
forall c b a.
Parser c b -> Cont c b a -> StepState c a -> StepState c a
down Parser c b
p' Cont c b a
ct1 StepState c a
pt') (Parser c b -> Cont c b a -> StepState c a -> StepState c a
forall c b a.
Parser c b -> Cont c b a -> StepState c a -> StepState c a
down Parser c b
p2 Cont c b a
ct1 (Parser c b -> Cont c b a -> StepState c a -> StepState c a
forall c b a.
Parser c b -> Cont c b a -> StepState c a -> StepState c a
down Parser c b
p1 Cont c b a
ct1 StepState c a
pt)) SmallArray (Parser c b)
ps
PFoldGr Unique
u Strictness
st b -> a1 -> b
f b
z Parser c a1
p1 -> (State (StepState c a) () -> StepState c a -> StepState c a)
-> StepState c a -> State (StepState c a) () -> StepState c a
forall a b c. (a -> b -> c) -> b -> a -> c
flip State (StepState c a) () -> StepState c a -> StepState c a
forall s a. State s a -> s -> s
execState StepState c a
pt (State (StepState c a) () -> StepState c a)
-> State (StepState c a) () -> StepState c a
forall a b. (a -> b) -> a -> b
$
StateT (StepState c a) Identity Bool
-> State (StepState c a) () -> State (StepState c a) ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
unlessM (Unique -> StateT (StepState c a) Identity Bool
forall c a. Unique -> State (StepState c a) Bool
sMember Unique
u) (State (StepState c a) () -> State (StepState c a) ())
-> State (StepState c a) () -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ do
Unique -> State (StepState c a) ()
forall c a. Unique -> State (StepState c a) ()
sInsert (Unique -> Unique
localU Unique
u)
(StepState c a -> StepState c a) -> State (StepState c a) ()
forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
modify' ((StepState c a -> StepState c a) -> State (StepState c a) ())
-> (StepState c a -> StepState c a) -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ Parser c a1 -> Cont c a1 a -> StepState c a -> StepState c a
forall c b a.
Parser c b -> Cont c b a -> StepState c a -> StepState c a
down Parser c a1
p1 (Unique
-> Strictness
-> Parser c a1
-> (b -> a1 -> b)
-> b
-> Cont c b a
-> Cont c a1 a
forall c b a1 a.
Unique
-> Strictness
-> Parser c b
-> (a1 -> b -> a1)
-> a1
-> Cont c a1 a
-> Cont c b a
CFoldGr Unique
u Strictness
st Parser c a1
p1 b -> a1 -> b
f b
z Cont c b a
ct)
StateT (StepState c a) Identity Bool
-> State (StepState c a) () -> State (StepState c a) ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
unlessM (Unique -> StateT (StepState c a) Identity Bool
forall c a. Unique -> State (StepState c a) Bool
sMember Unique
u) (State (StepState c a) () -> State (StepState c a) ())
-> State (StepState c a) () -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ do
Unique -> State (StepState c a) ()
forall c a. Unique -> State (StepState c a) ()
sInsert Unique
u
(StepState c a -> StepState c a) -> State (StepState c a) ()
forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
modify' ((StepState c a -> StepState c a) -> State (StepState c a) ())
-> (StepState c a -> StepState c a) -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ b -> Cont c b a -> StepState c a -> StepState c a
forall b c a. b -> Cont c b a -> StepState c a -> StepState c a
up b
z Cont c b a
ct
PFoldMn Unique
u Strictness
st b -> a1 -> b
f b
z Parser c a1
p1 -> (State (StepState c a) () -> StepState c a -> StepState c a)
-> StepState c a -> State (StepState c a) () -> StepState c a
forall a b c. (a -> b -> c) -> b -> a -> c
flip State (StepState c a) () -> StepState c a -> StepState c a
forall s a. State s a -> s -> s
execState StepState c a
pt (State (StepState c a) () -> StepState c a)
-> State (StepState c a) () -> StepState c a
forall a b. (a -> b) -> a -> b
$
StateT (StepState c a) Identity Bool
-> State (StepState c a) () -> State (StepState c a) ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
unlessM (Unique -> StateT (StepState c a) Identity Bool
forall c a. Unique -> State (StepState c a) Bool
sMember Unique
u) (State (StepState c a) () -> State (StepState c a) ())
-> State (StepState c a) () -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ do
StateT (StepState c a) Identity Bool
-> State (StepState c a) () -> State (StepState c a) ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
unlessM (Unique -> StateT (StepState c a) Identity Bool
forall c a. Unique -> State (StepState c a) Bool
sMember (Unique -> Unique
localU Unique
u)) (State (StepState c a) () -> State (StepState c a) ())
-> State (StepState c a) () -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ do
(StepState c a -> StepState c a) -> State (StepState c a) ()
forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
modify' ((StepState c a -> StepState c a) -> State (StepState c a) ())
-> (StepState c a -> StepState c a) -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ b -> Cont c b a -> StepState c a -> StepState c a
forall b c a. b -> Cont c b a -> StepState c a -> StepState c a
up b
z Cont c b a
ct
Unique -> State (StepState c a) ()
forall c a. Unique -> State (StepState c a) ()
sInsert Unique
u
(StepState c a -> StepState c a) -> State (StepState c a) ()
forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
modify' ((StepState c a -> StepState c a) -> State (StepState c a) ())
-> (StepState c a -> StepState c a) -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ Parser c a1 -> Cont c a1 a -> StepState c a -> StepState c a
forall c b a.
Parser c b -> Cont c b a -> StepState c a -> StepState c a
down Parser c a1
p1 (Unique
-> Strictness
-> Parser c a1
-> (b -> a1 -> b)
-> b
-> Cont c b a
-> Cont c a1 a
forall c b a1 a.
Unique
-> Strictness
-> Parser c b
-> (a1 -> b -> a1)
-> a1
-> Cont c a1 a
-> Cont c b a
CFoldMn Unique
u Strictness
st Parser c a1
p1 b -> a1 -> b
f b
z Cont c b a
ct)
PMany Unique
u a1 -> b
f1 a2 -> b
f2 a2 -> a1 -> a2
f a2
z Parser c a1
p1 -> (State (StepState c a) () -> StepState c a -> StepState c a)
-> StepState c a -> State (StepState c a) () -> StepState c a
forall a b c. (a -> b -> c) -> b -> a -> c
flip State (StepState c a) () -> StepState c a -> StepState c a
forall s a. State s a -> s -> s
execState StepState c a
pt (State (StepState c a) () -> StepState c a)
-> State (StepState c a) () -> StepState c a
forall a b. (a -> b) -> a -> b
$
StateT (StepState c a) Identity Bool
-> State (StepState c a) () -> State (StepState c a) ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
unlessM (Unique -> StateT (StepState c a) Identity Bool
forall c a. Unique -> State (StepState c a) Bool
sMember Unique
u) (State (StepState c a) () -> State (StepState c a) ())
-> State (StepState c a) () -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ do
Unique -> State (StepState c a) ()
forall c a. Unique -> State (StepState c a) ()
sInsert (Unique -> Unique
localU Unique
u)
(StepState c a -> StepState c a) -> State (StepState c a) ()
forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
modify' ((StepState c a -> StepState c a) -> State (StepState c a) ())
-> (StepState c a -> StepState c a) -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ Parser c a1 -> Cont c a1 a -> StepState c a -> StepState c a
forall c b a.
Parser c b -> Cont c b a -> StepState c a -> StepState c a
down Parser c a1
p1 (Unique
-> Parser c a1
-> (a1 -> b)
-> (a2 -> b)
-> (a2 -> a1 -> a2)
-> a2
-> Cont c b a
-> Cont c a1 a
forall c b a1 a3 a.
Unique
-> Parser c b
-> (b -> a1)
-> (a3 -> a1)
-> (a3 -> b -> a3)
-> a3
-> Cont c a1 a
-> Cont c b a
CMany Unique
u Parser c a1
p1 a1 -> b
f1 a2 -> b
f2 a2 -> a1 -> a2
f a2
z Cont c b a
ct)
StateT (StepState c a) Identity Bool
-> State (StepState c a) () -> State (StepState c a) ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
unlessM (Unique -> StateT (StepState c a) Identity Bool
forall c a. Unique -> State (StepState c a) Bool
sMember Unique
u) (State (StepState c a) () -> State (StepState c a) ())
-> State (StepState c a) () -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ do
Unique -> State (StepState c a) ()
forall c a. Unique -> State (StepState c a) ()
sInsert Unique
u
let !x :: b
x = a2 -> b
f2 a2
z
(StepState c a -> StepState c a) -> State (StepState c a) ()
forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
modify' ((StepState c a -> StepState c a) -> State (StepState c a) ())
-> (StepState c a -> StepState c a) -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ b -> Cont c b a -> StepState c a -> StepState c a
forall b c a. b -> Cont c b a -> StepState c a -> StepState c a
up b
x Cont c b a
ct
downNode :: Node c b -> Cont c b a -> StepState c a -> StepState c a
downNode :: forall c b a.
Node c b -> Cont c b a -> StepState c a -> StepState c a
downNode Node c b
n0 !Cont c b a
ct = Node c b -> StepState c a -> StepState c a
go Node c b
n0
where
go :: Node c b -> StepState c a -> StepState c a
go Node c b
n !StepState c a
pt = case Node c b
n of
NAccept b
b -> b -> Cont c b a -> StepState c a -> StepState c a
forall b c a. b -> Cont c b a -> StepState c a -> StepState c a
up b
b Cont c b a
ct StepState c a
pt
NGuard Unique
u Node c b
n1
| Unique -> UniqueSet -> Bool
U.member Unique
u (StepState c a -> UniqueSet
forall c a. StepState c a -> UniqueSet
sSet StepState c a
pt) -> StepState c a
pt
| Bool
otherwise -> Node c b -> StepState c a -> StepState c a
go Node c b
n1 (StepState c a
pt { sSet = U.insert u (sSet pt) })
NToken c -> Maybe a1
t Node c b
nxt ->
StepState c a
pt { sNeed = NeedCCons t (CFmap_ nxt ct) (sNeed pt) }
Node c b
NEmpty -> StepState c a
pt
NAlt Node c b
n1 Node c b
n2 SmallArray (Node c b)
ns -> (StepState c a -> Node c b -> StepState c a)
-> StepState c a -> SmallArray (Node c b) -> StepState c a
forall b a. (b -> a -> b) -> b -> SmallArray a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
F.foldl' ((Node c b -> StepState c a -> StepState c a)
-> StepState c a -> Node c b -> StepState c a
forall a b c. (a -> b -> c) -> b -> a -> c
flip Node c b -> StepState c a -> StepState c a
go) (Node c b -> StepState c a -> StepState c a
go Node c b
n2 (Node c b -> StepState c a -> StepState c a
go Node c b
n1 StepState c a
pt)) SmallArray (Node c b)
ns
up :: b -> Cont c b a -> StepState c a -> StepState c a
up :: forall b c a. b -> Cont c b a -> StepState c a -> StepState c a
up b
b Cont c b a
ct !StepState c a
pt = case Cont c b a
ct of
Cont c b a
CTop -> StepState c a
pt { sResult = sResult pt <|> Just b }
CFmap Strictness
st b -> a1
f Cont c a1 a
ct1 -> case Strictness
st of
Strictness
Strict -> let !x :: a1
x = b -> a1
f b
b in a1 -> Cont c a1 a -> StepState c a -> StepState c a
forall b c a. b -> Cont c b a -> StepState c a -> StepState c a
up a1
x Cont c a1 a
ct1 StepState c a
pt
Strictness
NonStrict -> a1 -> Cont c a1 a -> StepState c a -> StepState c a
forall b c a. b -> Cont c b a -> StepState c a -> StepState c a
up (b -> a1
f b
b) Cont c a1 a
ct1 StepState c a
pt
CFmap_ Node c a1
n Cont c a1 a
ct1 -> Node c a1 -> Cont c a1 a -> StepState c a -> StepState c a
forall c b a.
Node c b -> Cont c b a -> StepState c a -> StepState c a
downNode Node c a1
n Cont c a1 a
ct1 StepState c a
pt
CLiftA2A Strictness
st b -> a2 -> a3
f Parser c a2
p1 Cont c a3 a
ct1 -> Parser c a2 -> Cont c a2 a -> StepState c a -> StepState c a
forall c b a.
Parser c b -> Cont c b a -> StepState c a -> StepState c a
down Parser c a2
p1 (Strictness -> (b -> a2 -> a3) -> b -> Cont c a3 a -> Cont c a2 a
forall a1 b a3 c a.
Strictness -> (a1 -> b -> a3) -> a1 -> Cont c a3 a -> Cont c b a
CLiftA2B Strictness
st b -> a2 -> a3
f b
b Cont c a3 a
ct1) StepState c a
pt
CLiftA2B Strictness
st a1 -> b -> a3
f a1
a Cont c a3 a
ct1 -> case Strictness
st of
Strictness
Strict -> let !x :: a3
x = a1 -> b -> a3
f a1
a b
b in a3 -> Cont c a3 a -> StepState c a -> StepState c a
forall b c a. b -> Cont c b a -> StepState c a -> StepState c a
up a3
x Cont c a3 a
ct1 StepState c a
pt
Strictness
NonStrict -> a3 -> Cont c a3 a -> StepState c a -> StepState c a
forall b c a. b -> Cont c b a -> StepState c a -> StepState c a
up (a1 -> b -> a3
f a1
a b
b) Cont c a3 a
ct1 StepState c a
pt
CAlt Unique
u Cont c b a
ct1 -> (State (StepState c a) () -> StepState c a -> StepState c a)
-> StepState c a -> State (StepState c a) () -> StepState c a
forall a b c. (a -> b -> c) -> b -> a -> c
flip State (StepState c a) () -> StepState c a -> StepState c a
forall s a. State s a -> s -> s
execState StepState c a
pt (State (StepState c a) () -> StepState c a)
-> State (StepState c a) () -> StepState c a
forall a b. (a -> b) -> a -> b
$
StateT (StepState c a) Identity Bool
-> State (StepState c a) () -> State (StepState c a) ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
unlessM (Unique -> StateT (StepState c a) Identity Bool
forall c a. Unique -> State (StepState c a) Bool
sMember Unique
u) (State (StepState c a) () -> State (StepState c a) ())
-> State (StepState c a) () -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ do
Unique -> State (StepState c a) ()
forall c a. Unique -> State (StepState c a) ()
sInsert Unique
u
(StepState c a -> StepState c a) -> State (StepState c a) ()
forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
modify' ((StepState c a -> StepState c a) -> State (StepState c a) ())
-> (StepState c a -> StepState c a) -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ b -> Cont c b a -> StepState c a -> StepState c a
forall b c a. b -> Cont c b a -> StepState c a -> StepState c a
up b
b Cont c b a
ct1
CFoldGr Unique
u Strictness
st Parser c b
p1 a1 -> b -> a1
f a1
z Cont c a1 a
ct1 -> (State (StepState c a) () -> StepState c a -> StepState c a)
-> StepState c a -> State (StepState c a) () -> StepState c a
forall a b c. (a -> b -> c) -> b -> a -> c
flip State (StepState c a) () -> StepState c a -> StepState c a
forall s a. State s a -> s -> s
execState StepState c a
pt (State (StepState c a) () -> StepState c a)
-> State (StepState c a) () -> StepState c a
forall a b. (a -> b) -> a -> b
$
StateT (StepState c a) Identity Bool
-> State (StepState c a) () -> State (StepState c a) ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
unlessM (Unique -> StateT (StepState c a) Identity Bool
forall c a. Unique -> State (StepState c a) Bool
sMember Unique
u) (State (StepState c a) () -> State (StepState c a) ())
-> State (StepState c a) () -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ do
lc <- Unique -> StateT (StepState c a) Identity Bool
forall c a. Unique -> State (StepState c a) Bool
sMember (Unique -> Unique
localU Unique
u)
if lc then do
sInsert u
modify' $ up z ct1
else do
let go a1
z1 = do
(StepState c a -> StepState c a) -> State (StepState c a) ()
forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
modify' ((StepState c a -> StepState c a) -> State (StepState c a) ())
-> (StepState c a -> StepState c a) -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ Parser c b -> Cont c b a -> StepState c a -> StepState c a
forall c b a.
Parser c b -> Cont c b a -> StepState c a -> StepState c a
down Parser c b
p1 (Unique
-> Strictness
-> Parser c b
-> (a1 -> b -> a1)
-> a1
-> Cont c a1 a
-> Cont c b a
forall c b a1 a.
Unique
-> Strictness
-> Parser c b
-> (a1 -> b -> a1)
-> a1
-> Cont c a1 a
-> Cont c b a
CFoldGr Unique
u Strictness
st Parser c b
p1 a1 -> b -> a1
f a1
z1 Cont c a1 a
ct1)
Unique -> State (StepState c a) ()
forall c a. Unique -> State (StepState c a) ()
sInsert Unique
u
(StepState c a -> StepState c a) -> State (StepState c a) ()
forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
modify' ((StepState c a -> StepState c a) -> State (StepState c a) ())
-> (StepState c a -> StepState c a) -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ a1 -> Cont c a1 a -> StepState c a -> StepState c a
forall b c a. b -> Cont c b a -> StepState c a -> StepState c a
up a1
z1 Cont c a1 a
ct1
{-# INLINE go #-}
case st of
Strictness
Strict -> let !z1 :: a1
z1 = a1 -> b -> a1
f a1
z b
b in a1 -> State (StepState c a) ()
go a1
z1
Strictness
NonStrict -> a1 -> State (StepState c a) ()
go (a1 -> b -> a1
f a1
z b
b)
CFoldMn Unique
u Strictness
st Parser c b
p1 a1 -> b -> a1
f a1
z Cont c a1 a
ct1 -> (State (StepState c a) () -> StepState c a -> StepState c a)
-> StepState c a -> State (StepState c a) () -> StepState c a
forall a b c. (a -> b -> c) -> b -> a -> c
flip State (StepState c a) () -> StepState c a -> StepState c a
forall s a. State s a -> s -> s
execState StepState c a
pt (State (StepState c a) () -> StepState c a)
-> State (StepState c a) () -> StepState c a
forall a b. (a -> b) -> a -> b
$
StateT (StepState c a) Identity Bool
-> State (StepState c a) () -> State (StepState c a) ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
unlessM (Unique -> StateT (StepState c a) Identity Bool
forall c a. Unique -> State (StepState c a) Bool
sMember Unique
u) (State (StepState c a) () -> State (StepState c a) ())
-> State (StepState c a) () -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ do
let go :: a1 -> State (StepState c a) ()
go a1
z1 = do
Unique -> State (StepState c a) ()
forall c a. Unique -> State (StepState c a) ()
sInsert (Unique -> Unique
localU Unique
u)
(StepState c a -> StepState c a) -> State (StepState c a) ()
forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
modify' ((StepState c a -> StepState c a) -> State (StepState c a) ())
-> (StepState c a -> StepState c a) -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ a1 -> Cont c a1 a -> StepState c a -> StepState c a
forall b c a. b -> Cont c b a -> StepState c a -> StepState c a
up a1
z1 Cont c a1 a
ct1
StateT (StepState c a) Identity Bool
-> State (StepState c a) () -> State (StepState c a) ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
unlessM (Unique -> StateT (StepState c a) Identity Bool
forall c a. Unique -> State (StepState c a) Bool
sMember Unique
u) (State (StepState c a) () -> State (StepState c a) ())
-> State (StepState c a) () -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ do
Unique -> State (StepState c a) ()
forall c a. Unique -> State (StepState c a) ()
sInsert Unique
u
(StepState c a -> StepState c a) -> State (StepState c a) ()
forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
modify' ((StepState c a -> StepState c a) -> State (StepState c a) ())
-> (StepState c a -> StepState c a) -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ Parser c b -> Cont c b a -> StepState c a -> StepState c a
forall c b a.
Parser c b -> Cont c b a -> StepState c a -> StepState c a
down Parser c b
p1 (Unique
-> Strictness
-> Parser c b
-> (a1 -> b -> a1)
-> a1
-> Cont c a1 a
-> Cont c b a
forall c b a1 a.
Unique
-> Strictness
-> Parser c b
-> (a1 -> b -> a1)
-> a1
-> Cont c a1 a
-> Cont c b a
CFoldMn Unique
u Strictness
st Parser c b
p1 a1 -> b -> a1
f a1
z1 Cont c a1 a
ct1)
{-# INLINE go #-}
case Strictness
st of
Strictness
Strict -> let !z1 :: a1
z1 = a1 -> b -> a1
f a1
z b
b in a1 -> State (StepState c a) ()
go a1
z1
Strictness
NonStrict -> a1 -> State (StepState c a) ()
go (a1 -> b -> a1
f a1
z b
b)
CMany Unique
u Parser c b
p1 b -> a2
f1 a1 -> a2
f2 a1 -> b -> a1
f a1
z Cont c a2 a
ct1 -> (State (StepState c a) () -> StepState c a -> StepState c a)
-> StepState c a -> State (StepState c a) () -> StepState c a
forall a b c. (a -> b -> c) -> b -> a -> c
flip State (StepState c a) () -> StepState c a -> StepState c a
forall s a. State s a -> s -> s
execState StepState c a
pt (State (StepState c a) () -> StepState c a)
-> State (StepState c a) () -> StepState c a
forall a b. (a -> b) -> a -> b
$
StateT (StepState c a) Identity Bool
-> State (StepState c a) () -> State (StepState c a) ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
unlessM (Unique -> StateT (StepState c a) Identity Bool
forall c a. Unique -> State (StepState c a) Bool
sMember Unique
u) (State (StepState c a) () -> State (StepState c a) ())
-> State (StepState c a) () -> State (StepState c a) ()
forall a b. (a -> b) -> a -> b
$ do
lc <- Unique -> StateT (StepState c a) Identity Bool
forall c a. Unique -> State (StepState c a) Bool
sMember (Unique -> Unique
localU Unique
u)
if lc then do
sInsert u
let !x = b -> a2
f1 b
b
modify' $ up x ct1
else do
let !z1 = a1 -> b -> a1
f a1
z b
b
modify' $ down p1 (CMany u p1 f1 f2 f z1 ct1)
sInsert u
let !x = a1 -> a2
f2 a1
z1
modify' $ up x ct1
localU :: Unique -> Unique
localU :: Unique -> Unique
localU = Int -> Unique
Unique (Int -> Unique) -> (Unique -> Int) -> Unique -> Unique
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (Int -> Int) -> (Unique -> Int) -> Unique -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unique -> Int
unUnique
data ParserState c a = ParserState
{ forall c a. ParserState c a -> NeedCList c a
psNeed :: !(NeedCList c a)
, forall c a. ParserState c a -> Maybe a
psResult :: !(Maybe a)
}
prepareParser :: Parser c a -> Maybe (ParserState c a)
prepareParser :: forall c a. Parser c a -> Maybe (ParserState c a)
prepareParser Parser c a
p = StepState c a -> Maybe (ParserState c a)
forall c a. StepState c a -> Maybe (ParserState c a)
toParserState (Parser c a -> Cont c a a -> StepState c a -> StepState c a
forall c b a.
Parser c b -> Cont c b a -> StepState c a -> StepState c a
down Parser c a
p Cont c a a
forall c a. Cont c a a
CTop StepState c a
forall c a. StepState c a
stepStateZero)
stepParser :: ParserState c a -> c -> Maybe (ParserState c a)
stepParser :: forall c a. ParserState c a -> c -> Maybe (ParserState c a)
stepParser ParserState c a
ps c
c = case ParserState c a -> NeedCList c a
forall c a. ParserState c a -> NeedCList c a
psNeed ParserState c a
ps of
NeedCList c a
NeedCNil -> Maybe (ParserState c a)
forall a. Maybe a
Nothing
NeedCList c a
needs -> StepState c a -> Maybe (ParserState c a)
forall c a. StepState c a -> Maybe (ParserState c a)
toParserState (NeedCList c a -> StepState c a
go NeedCList c a
needs)
where
go :: NeedCList c a -> StepState c a
go (NeedCCons c -> Maybe b
t Cont c b a
ct NeedCList c a
rest) =
let !pt :: StepState c a
pt = NeedCList c a -> StepState c a
go NeedCList c a
rest
in StepState c a -> (b -> StepState c a) -> Maybe b -> StepState c a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe StepState c a
pt (\b
b -> b -> Cont c b a -> StepState c a -> StepState c a
forall b c a. b -> Cont c b a -> StepState c a -> StepState c a
up b
b Cont c b a
ct StepState c a
pt) (c -> Maybe b
t c
c)
go NeedCList c a
NeedCNil = StepState c a
forall c a. StepState c a
stepStateZero
{-# INLINE stepParser #-}
finishParser :: ParserState c a -> Maybe a
finishParser :: forall c a. ParserState c a -> Maybe a
finishParser = ParserState c a -> Maybe a
forall c a. ParserState c a -> Maybe a
psResult
toParserState :: StepState c a -> Maybe (ParserState c a)
toParserState :: forall c a. StepState c a -> Maybe (ParserState c a)
toParserState StepState c a
ss = case (StepState c a -> NeedCList c a
forall c a. StepState c a -> NeedCList c a
sNeed StepState c a
ss, StepState c a -> Maybe a
forall c a. StepState c a -> Maybe a
sResult StepState c a
ss) of
(NeedCList c a
NeedCNil, Maybe a
Nothing) -> Maybe (ParserState c a)
forall a. Maybe a
Nothing
(NeedCList c a
need, Maybe a
result) -> ParserState c a -> Maybe (ParserState c a)
forall a. a -> Maybe a
Just (ParserState c a -> Maybe (ParserState c a))
-> ParserState c a -> Maybe (ParserState c a)
forall a b. (a -> b) -> a -> b
$! ParserState { psNeed :: NeedCList c a
psNeed = NeedCList c a
need, psResult :: Maybe a
psResult = Maybe a
result }
type Foldr f a = forall b. (a -> b -> b) -> b -> f -> b
parseFoldr :: Foldr f c -> Parser c a -> f -> Maybe a
parseFoldr :: forall f c a. Foldr f c -> Parser c a -> f -> Maybe a
parseFoldr Foldr f c
fr = \Parser c a
p f
xs -> Parser c a -> Maybe (ParserState c a)
forall c a. Parser c a -> Maybe (ParserState c a)
prepareParser Parser c a
p Maybe (ParserState c a) -> (ParserState c a -> Maybe a) -> Maybe a
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (c -> (ParserState c a -> Maybe a) -> ParserState c a -> Maybe a)
-> (ParserState c a -> Maybe a) -> f -> ParserState c a -> Maybe a
Foldr f c
fr c -> (ParserState c a -> Maybe a) -> ParserState c a -> Maybe a
forall {c} {a} {b}.
c -> (ParserState c a -> Maybe b) -> ParserState c a -> Maybe b
f ParserState c a -> Maybe a
forall c a. ParserState c a -> Maybe a
finishParser f
xs
where
f :: c -> (ParserState c a -> Maybe b) -> ParserState c a -> Maybe b
f c
c ParserState c a -> Maybe b
k = (ParserState c a -> Maybe b) -> ParserState c a -> Maybe b
forall a b. (a -> b) -> a -> b
X.oneShot (\ParserState c a
ps -> ParserState c a -> c -> Maybe (ParserState c a)
forall c a. ParserState c a -> c -> Maybe (ParserState c a)
stepParser ParserState c a
ps c
c Maybe (ParserState c a) -> (ParserState c a -> Maybe b) -> Maybe b
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ParserState c a -> Maybe b
k)
{-# INLINE parseFoldr #-}
parseNext :: Monad m => Parser c a -> m (Maybe c) -> m (Maybe a)
parseNext :: forall (m :: * -> *) c a.
Monad m =>
Parser c a -> m (Maybe c) -> m (Maybe a)
parseNext Parser c a
p m (Maybe c)
next = case Parser c a -> Maybe (ParserState c a)
forall c a. Parser c a -> Maybe (ParserState c a)
prepareParser Parser c a
p of
Maybe (ParserState c a)
Nothing -> Maybe a -> m (Maybe a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing
Just ParserState c a
ps -> ParserState c a -> m (Maybe a)
loop ParserState c a
ps
where
loop :: ParserState c a -> m (Maybe a)
loop ParserState c a
ps = m (Maybe c)
next m (Maybe c) -> (Maybe c -> m (Maybe a)) -> m (Maybe a)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Maybe c
m -> case Maybe c
m of
Maybe c
Nothing -> Maybe a -> m (Maybe a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ParserState c a -> Maybe a
forall c a. ParserState c a -> Maybe a
finishParser ParserState c a
ps)
Just c
c -> case ParserState c a -> c -> Maybe (ParserState c a)
forall c a. ParserState c a -> c -> Maybe (ParserState c a)
stepParser ParserState c a
ps c
c of
Maybe (ParserState c a)
Nothing -> Maybe a -> m (Maybe a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing
Just ParserState c a
ps' -> ParserState c a -> m (Maybe a)
loop ParserState c a
ps'
{-# INLINE parseNext #-}
unlessM :: Monad m => m Bool -> m () -> m ()
unlessM :: forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
unlessM m Bool
mb m ()
mx = do
b <- m Bool
mb
if b then pure () else mx