{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Streamly.Internal.Data.Parser.Types
(
Step (..)
, Parser (..)
, ParseError (..)
, yield
, yieldM
, splitWith
, die
, dieM
, splitSome
, splitMany
, alt
)
where
import Control.Applicative (Alternative(..))
import Control.Exception (assert, Exception(..))
import Control.Monad (MonadPlus(..))
import Control.Monad.Catch (MonadCatch, try, throwM, MonadThrow)
import Fusion.Plugin.Types (Fuse(..))
import Streamly.Internal.Data.Fold (Fold(..), toList)
import Streamly.Internal.Data.Strict (Tuple3'(..))
{-# ANN type Step Fuse #-}
data Step s b =
Yield Int s
| Skip Int s
| Stop Int b
| Error String
instance Functor (Step s) where
{-# INLINE fmap #-}
fmap :: (a -> b) -> Step s a -> Step s b
fmap a -> b
_ (Yield Int
n s
s) = Int -> s -> Step s b
forall s b. Int -> s -> Step s b
Yield Int
n s
s
fmap a -> b
_ (Skip Int
n s
s) = Int -> s -> Step s b
forall s b. Int -> s -> Step s b
Skip Int
n s
s
fmap a -> b
f (Stop Int
n a
b) = Int -> b -> Step s b
forall s b. Int -> b -> Step s b
Stop Int
n (a -> b
f a
b)
fmap a -> b
_ (Error String
err) = String -> Step s b
forall s b. String -> Step s b
Error String
err
data Parser m a b =
forall s. Parser (s -> a -> m (Step s b)) (m s) (s -> m b)
newtype ParseError = ParseError String
deriving Int -> ParseError -> ShowS
[ParseError] -> ShowS
ParseError -> String
(Int -> ParseError -> ShowS)
-> (ParseError -> String)
-> ([ParseError] -> ShowS)
-> Show ParseError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ParseError] -> ShowS
$cshowList :: [ParseError] -> ShowS
show :: ParseError -> String
$cshow :: ParseError -> String
showsPrec :: Int -> ParseError -> ShowS
$cshowsPrec :: Int -> ParseError -> ShowS
Show
instance Exception ParseError where
displayException :: ParseError -> String
displayException (ParseError String
err) = String
err
instance Functor m => Functor (Parser m a) where
{-# INLINE fmap #-}
fmap :: (a -> b) -> Parser m a a -> Parser m a b
fmap a -> b
f (Parser s -> a -> m (Step s a)
step1 m s
initial s -> m a
extract) =
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser s -> a -> m (Step s b)
step m s
initial ((a -> b) -> (s -> m a) -> s -> m b
forall (f :: * -> *) (f :: * -> *) a b.
(Functor f, Functor f) =>
(a -> b) -> f (f a) -> f (f b)
fmap2 a -> b
f s -> m a
extract)
where
step :: s -> a -> m (Step s b)
step s
s a
b = (a -> b) -> m (Step s a) -> m (Step s b)
forall (f :: * -> *) (f :: * -> *) a b.
(Functor f, Functor f) =>
(a -> b) -> f (f a) -> f (f b)
fmap2 a -> b
f (s -> a -> m (Step s a)
step1 s
s a
b)
fmap2 :: (a -> b) -> f (f a) -> f (f b)
fmap2 a -> b
g = (f a -> f b) -> f (f a) -> f (f b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
g)
{-# INLINE yield #-}
yield :: Monad m => b -> Parser m a b
yield :: b -> Parser m a b
yield b
b = (() -> a -> m (Step () b)) -> m () -> (() -> m b) -> Parser m a b
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser (\()
_ a
_ -> Step () b -> m (Step () b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step () b -> m (Step () b)) -> Step () b -> m (Step () b)
forall a b. (a -> b) -> a -> b
$ Int -> b -> Step () b
forall s b. Int -> b -> Step s b
Stop Int
1 b
b)
(() -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
(\()
_ -> b -> m b
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
b)
{-# INLINE yieldM #-}
yieldM :: Monad m => m b -> Parser m a b
yieldM :: m b -> Parser m a b
yieldM m b
b = (() -> a -> m (Step () b)) -> m () -> (() -> m b) -> Parser m a b
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser (\()
_ a
_ -> Int -> b -> Step () b
forall s b. Int -> b -> Step s b
Stop Int
1 (b -> Step () b) -> m b -> m (Step () b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m b
b)
(() -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
(\()
_ -> m b
b)
{-# ANN type SeqParseState Fuse #-}
data SeqParseState sl f sr = SeqParseL sl | SeqParseR f sr
{-# INLINE splitWith #-}
splitWith :: Monad m
=> (a -> b -> c) -> Parser m x a -> Parser m x b -> Parser m x c
splitWith :: (a -> b -> c) -> Parser m x a -> Parser m x b -> Parser m x c
splitWith a -> b -> c
func (Parser s -> x -> m (Step s a)
stepL m s
initialL s -> m a
extractL)
(Parser s -> x -> m (Step s b)
stepR m s
initialR s -> m b
extractR) =
(SeqParseState s (b -> c) s
-> x -> m (Step (SeqParseState s (b -> c) s) c))
-> m (SeqParseState s (b -> c) s)
-> (SeqParseState s (b -> c) s -> m c)
-> Parser m x c
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser SeqParseState s (b -> c) s
-> x -> m (Step (SeqParseState s (b -> c) s) c)
step m (SeqParseState s (b -> c) s)
forall f sr. m (SeqParseState s f sr)
initial SeqParseState s (b -> c) s -> m c
extract
where
initial :: m (SeqParseState s f sr)
initial = s -> SeqParseState s f sr
forall sl f sr. sl -> SeqParseState sl f sr
SeqParseL (s -> SeqParseState s f sr) -> m s -> m (SeqParseState s f sr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
initialL
step :: SeqParseState s (b -> c) s
-> x -> m (Step (SeqParseState s (b -> c) s) c)
step (SeqParseL s
st) x
a = do
Step s a
r <- s -> x -> m (Step s a)
stepL s
st x
a
case Step s a
r of
Yield Int
_ s
s -> Step (SeqParseState s (b -> c) s) c
-> m (Step (SeqParseState s (b -> c) s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SeqParseState s (b -> c) s) c
-> m (Step (SeqParseState s (b -> c) s) c))
-> Step (SeqParseState s (b -> c) s) c
-> m (Step (SeqParseState s (b -> c) s) c)
forall a b. (a -> b) -> a -> b
$ Int
-> SeqParseState s (b -> c) s
-> Step (SeqParseState s (b -> c) s) c
forall s b. Int -> s -> Step s b
Skip Int
0 (s -> SeqParseState s (b -> c) s
forall sl f sr. sl -> SeqParseState sl f sr
SeqParseL s
s)
Skip Int
n s
s -> Step (SeqParseState s (b -> c) s) c
-> m (Step (SeqParseState s (b -> c) s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SeqParseState s (b -> c) s) c
-> m (Step (SeqParseState s (b -> c) s) c))
-> Step (SeqParseState s (b -> c) s) c
-> m (Step (SeqParseState s (b -> c) s) c)
forall a b. (a -> b) -> a -> b
$ Int
-> SeqParseState s (b -> c) s
-> Step (SeqParseState s (b -> c) s) c
forall s b. Int -> s -> Step s b
Skip Int
n (s -> SeqParseState s (b -> c) s
forall sl f sr. sl -> SeqParseState sl f sr
SeqParseL s
s)
Stop Int
n a
b -> Int
-> SeqParseState s (b -> c) s
-> Step (SeqParseState s (b -> c) s) c
forall s b. Int -> s -> Step s b
Skip Int
n (SeqParseState s (b -> c) s -> Step (SeqParseState s (b -> c) s) c)
-> m (SeqParseState s (b -> c) s)
-> m (Step (SeqParseState s (b -> c) s) c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((b -> c) -> s -> SeqParseState s (b -> c) s
forall sl f sr. f -> sr -> SeqParseState sl f sr
SeqParseR (a -> b -> c
func a
b) (s -> SeqParseState s (b -> c) s)
-> m s -> m (SeqParseState s (b -> c) s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
initialR)
Error String
err -> Step (SeqParseState s (b -> c) s) c
-> m (Step (SeqParseState s (b -> c) s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SeqParseState s (b -> c) s) c
-> m (Step (SeqParseState s (b -> c) s) c))
-> Step (SeqParseState s (b -> c) s) c
-> m (Step (SeqParseState s (b -> c) s) c)
forall a b. (a -> b) -> a -> b
$ String -> Step (SeqParseState s (b -> c) s) c
forall s b. String -> Step s b
Error String
err
step (SeqParseR b -> c
f s
st) x
a = do
Step s b
r <- s -> x -> m (Step s b)
stepR s
st x
a
Step (SeqParseState s (b -> c) s) c
-> m (Step (SeqParseState s (b -> c) s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SeqParseState s (b -> c) s) c
-> m (Step (SeqParseState s (b -> c) s) c))
-> Step (SeqParseState s (b -> c) s) c
-> m (Step (SeqParseState s (b -> c) s) c)
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
Yield Int
n s
s -> Int
-> SeqParseState s (b -> c) s
-> Step (SeqParseState s (b -> c) s) c
forall s b. Int -> s -> Step s b
Yield Int
n ((b -> c) -> s -> SeqParseState s (b -> c) s
forall sl f sr. f -> sr -> SeqParseState sl f sr
SeqParseR b -> c
f s
s)
Skip Int
n s
s -> Int
-> SeqParseState s (b -> c) s
-> Step (SeqParseState s (b -> c) s) c
forall s b. Int -> s -> Step s b
Skip Int
n ((b -> c) -> s -> SeqParseState s (b -> c) s
forall sl f sr. f -> sr -> SeqParseState sl f sr
SeqParseR b -> c
f s
s)
Stop Int
n b
b -> Int -> c -> Step (SeqParseState s (b -> c) s) c
forall s b. Int -> b -> Step s b
Stop Int
n (b -> c
f b
b)
Error String
err -> String -> Step (SeqParseState s (b -> c) s) c
forall s b. String -> Step s b
Error String
err
extract :: SeqParseState s (b -> c) s -> m c
extract (SeqParseR b -> c
f s
sR) = (b -> c) -> m b -> m c
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> c
f (s -> m b
extractR s
sR)
extract (SeqParseL s
sL) = do
a
rL <- s -> m a
extractL s
sL
s
sR <- m s
initialR
b
rR <- s -> m b
extractR s
sR
c -> m c
forall (m :: * -> *) a. Monad m => a -> m a
return (c -> m c) -> c -> m c
forall a b. (a -> b) -> a -> b
$ a -> b -> c
func a
rL b
rR
instance Monad m => Applicative (Parser m a) where
{-# INLINE pure #-}
pure :: a -> Parser m a a
pure = a -> Parser m a a
forall (m :: * -> *) b a. Monad m => b -> Parser m a b
yield
{-# INLINE (<*>) #-}
<*> :: Parser m a (a -> b) -> Parser m a a -> Parser m a b
(<*>) = ((a -> b) -> a -> b)
-> Parser m a (a -> b) -> Parser m a a -> Parser m a b
forall (m :: * -> *) a b c x.
Monad m =>
(a -> b -> c) -> Parser m x a -> Parser m x b -> Parser m x c
splitWith (a -> b) -> a -> b
forall a. a -> a
id
{-# ANN type AltParseState Fuse #-}
data AltParseState sl sr = AltParseL Int sl | AltParseR sr
{-# INLINE alt #-}
alt :: Monad m => Parser m x a -> Parser m x a -> Parser m x a
alt :: Parser m x a -> Parser m x a -> Parser m x a
alt (Parser s -> x -> m (Step s a)
stepL m s
initialL s -> m a
extractL) (Parser s -> x -> m (Step s a)
stepR m s
initialR s -> m a
extractR) =
(AltParseState s s -> x -> m (Step (AltParseState s s) a))
-> m (AltParseState s s)
-> (AltParseState s s -> m a)
-> Parser m x a
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser AltParseState s s -> x -> m (Step (AltParseState s s) a)
step m (AltParseState s s)
forall sr. m (AltParseState s sr)
initial AltParseState s s -> m a
extract
where
initial :: m (AltParseState s sr)
initial = Int -> s -> AltParseState s sr
forall sl sr. Int -> sl -> AltParseState sl sr
AltParseL Int
0 (s -> AltParseState s sr) -> m s -> m (AltParseState s sr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
initialL
step :: AltParseState s s -> x -> m (Step (AltParseState s s) a)
step (AltParseL Int
cnt s
st) x
a = do
Step s a
r <- s -> x -> m (Step s a)
stepL s
st x
a
case Step s a
r of
Yield Int
n s
s -> Step (AltParseState s s) a -> m (Step (AltParseState s s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (AltParseState s s) a -> m (Step (AltParseState s s) a))
-> Step (AltParseState s s) a -> m (Step (AltParseState s s) a)
forall a b. (a -> b) -> a -> b
$ Int -> AltParseState s s -> Step (AltParseState s s) a
forall s b. Int -> s -> Step s b
Yield Int
n (Int -> s -> AltParseState s s
forall sl sr. Int -> sl -> AltParseState sl sr
AltParseL Int
0 s
s)
Skip Int
n s
s -> do
Bool -> m () -> m ()
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
cnt Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0) (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
Step (AltParseState s s) a -> m (Step (AltParseState s s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (AltParseState s s) a -> m (Step (AltParseState s s) a))
-> Step (AltParseState s s) a -> m (Step (AltParseState s s) a)
forall a b. (a -> b) -> a -> b
$ Int -> AltParseState s s -> Step (AltParseState s s) a
forall s b. Int -> s -> Step s b
Skip Int
n (Int -> s -> AltParseState s s
forall sl sr. Int -> sl -> AltParseState sl sr
AltParseL (Int
cnt Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) s
s)
Stop Int
n a
b -> Step (AltParseState s s) a -> m (Step (AltParseState s s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (AltParseState s s) a -> m (Step (AltParseState s s) a))
-> Step (AltParseState s s) a -> m (Step (AltParseState s s) a)
forall a b. (a -> b) -> a -> b
$ Int -> a -> Step (AltParseState s s) a
forall s b. Int -> b -> Step s b
Stop Int
n a
b
Error String
_ -> do
s
rR <- m s
initialR
Step (AltParseState s s) a -> m (Step (AltParseState s s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (AltParseState s s) a -> m (Step (AltParseState s s) a))
-> Step (AltParseState s s) a -> m (Step (AltParseState s s) a)
forall a b. (a -> b) -> a -> b
$ Int -> AltParseState s s -> Step (AltParseState s s) a
forall s b. Int -> s -> Step s b
Skip (Int
cnt Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (s -> AltParseState s s
forall sl sr. sr -> AltParseState sl sr
AltParseR s
rR)
step (AltParseR s
st) x
a = do
Step s a
r <- s -> x -> m (Step s a)
stepR s
st x
a
Step (AltParseState s s) a -> m (Step (AltParseState s s) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (AltParseState s s) a -> m (Step (AltParseState s s) a))
-> Step (AltParseState s s) a -> m (Step (AltParseState s s) a)
forall a b. (a -> b) -> a -> b
$ case Step s a
r of
Yield Int
n s
s -> Int -> AltParseState s s -> Step (AltParseState s s) a
forall s b. Int -> s -> Step s b
Yield Int
n (s -> AltParseState s s
forall sl sr. sr -> AltParseState sl sr
AltParseR s
s)
Skip Int
n s
s -> Int -> AltParseState s s -> Step (AltParseState s s) a
forall s b. Int -> s -> Step s b
Skip Int
n (s -> AltParseState s s
forall sl sr. sr -> AltParseState sl sr
AltParseR s
s)
Stop Int
n a
b -> Int -> a -> Step (AltParseState s s) a
forall s b. Int -> b -> Step s b
Stop Int
n a
b
Error String
err -> String -> Step (AltParseState s s) a
forall s b. String -> Step s b
Error String
err
extract :: AltParseState s s -> m a
extract (AltParseR s
sR) = s -> m a
extractR s
sR
extract (AltParseL Int
_ s
sL) = s -> m a
extractL s
sL
{-# INLINE splitMany #-}
splitMany :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
splitMany :: Fold m b c -> Parser m a b -> Parser m a c
splitMany (Fold s -> b -> m s
fstep m s
finitial s -> m c
fextract) (Parser s -> a -> m (Step s b)
step1 m s
initial1 s -> m b
extract1) =
(Tuple3' s Int s -> a -> m (Step (Tuple3' s Int s) c))
-> m (Tuple3' s Int s) -> (Tuple3' s Int s -> m c) -> Parser m a c
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser Tuple3' s Int s -> a -> m (Step (Tuple3' s Int s) c)
step m (Tuple3' s Int s)
initial Tuple3' s Int s -> m c
forall b. Tuple3' s b s -> m c
extract
where
initial :: m (Tuple3' s Int s)
initial = do
s
ps <- m s
initial1
s
fs <- m s
finitial
Tuple3' s Int s -> m (Tuple3' s Int s)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (s -> Int -> s -> Tuple3' s Int s
forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' s
ps Int
0 s
fs)
{-# INLINE step #-}
step :: Tuple3' s Int s -> a -> m (Step (Tuple3' s Int s) c)
step (Tuple3' s
st Int
cnt s
fs) a
a = do
Step s b
r <- s -> a -> m (Step s b)
step1 s
st a
a
let cnt1 :: Int
cnt1 = Int
cnt Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
case Step s b
r of
Yield Int
_ s
s -> Step (Tuple3' s Int s) c -> m (Step (Tuple3' s Int s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple3' s Int s) c -> m (Step (Tuple3' s Int s) c))
-> Step (Tuple3' s Int s) c -> m (Step (Tuple3' s Int s) c)
forall a b. (a -> b) -> a -> b
$ Int -> Tuple3' s Int s -> Step (Tuple3' s Int s) c
forall s b. Int -> s -> Step s b
Skip Int
0 (s -> Int -> s -> Tuple3' s Int s
forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' s
s Int
cnt1 s
fs)
Skip Int
n s
s -> do
Bool -> m () -> m ()
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
cnt1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0) (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
Step (Tuple3' s Int s) c -> m (Step (Tuple3' s Int s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple3' s Int s) c -> m (Step (Tuple3' s Int s) c))
-> Step (Tuple3' s Int s) c -> m (Step (Tuple3' s Int s) c)
forall a b. (a -> b) -> a -> b
$ Int -> Tuple3' s Int s -> Step (Tuple3' s Int s) c
forall s b. Int -> s -> Step s b
Skip Int
n (s -> Int -> s -> Tuple3' s Int s
forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' s
s (Int
cnt1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) s
fs)
Stop Int
n b
b -> do
s
s <- m s
initial1
s
fs1 <- s -> b -> m s
fstep s
fs b
b
Step (Tuple3' s Int s) c -> m (Step (Tuple3' s Int s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple3' s Int s) c -> m (Step (Tuple3' s Int s) c))
-> Step (Tuple3' s Int s) c -> m (Step (Tuple3' s Int s) c)
forall a b. (a -> b) -> a -> b
$ Int -> Tuple3' s Int s -> Step (Tuple3' s Int s) c
forall s b. Int -> s -> Step s b
Skip Int
n (s -> Int -> s -> Tuple3' s Int s
forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' s
s Int
0 s
fs1)
Error String
_ -> do
c
xs <- s -> m c
fextract s
fs
Step (Tuple3' s Int s) c -> m (Step (Tuple3' s Int s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple3' s Int s) c -> m (Step (Tuple3' s Int s) c))
-> Step (Tuple3' s Int s) c -> m (Step (Tuple3' s Int s) c)
forall a b. (a -> b) -> a -> b
$ Int -> c -> Step (Tuple3' s Int s) c
forall s b. Int -> b -> Step s b
Stop Int
cnt1 c
xs
extract :: Tuple3' s b s -> m c
extract (Tuple3' s
s b
_ s
fs) = do
Either ParseError b
r <- m b -> m (Either ParseError b)
forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> m (Either e a)
try (m b -> m (Either ParseError b)) -> m b -> m (Either ParseError b)
forall a b. (a -> b) -> a -> b
$ s -> m b
extract1 s
s
case Either ParseError b
r of
Left (ParseError
_ :: ParseError) -> s -> m c
fextract s
fs
Right b
b -> s -> b -> m s
fstep s
fs b
b m s -> (s -> m c) -> m c
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> m c
fextract
{-# INLINE splitSome #-}
splitSome :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
splitSome :: Fold m b c -> Parser m a b -> Parser m a c
splitSome (Fold s -> b -> m s
fstep m s
finitial s -> m c
fextract) (Parser s -> a -> m (Step s b)
step1 m s
initial1 s -> m b
extract1) =
(Tuple3' s Int (Either s s)
-> a -> m (Step (Tuple3' s Int (Either s s)) c))
-> m (Tuple3' s Int (Either s s))
-> (Tuple3' s Int (Either s s) -> m c)
-> Parser m a c
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser Tuple3' s Int (Either s s)
-> a -> m (Step (Tuple3' s Int (Either s s)) c)
step m (Tuple3' s Int (Either s s))
forall b. m (Tuple3' s Int (Either s b))
initial Tuple3' s Int (Either s s) -> m c
forall b. Tuple3' s b (Either s s) -> m c
extract
where
initial :: m (Tuple3' s Int (Either s b))
initial = do
s
ps <- m s
initial1
s
fs <- m s
finitial
Tuple3' s Int (Either s b) -> m (Tuple3' s Int (Either s b))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (s -> Int -> Either s b -> Tuple3' s Int (Either s b)
forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' s
ps Int
0 (s -> Either s b
forall a b. a -> Either a b
Left s
fs))
{-# INLINE step #-}
step :: Tuple3' s Int (Either s s)
-> a -> m (Step (Tuple3' s Int (Either s s)) c)
step (Tuple3' s
st Int
_ (Left s
fs)) a
a = do
Step s b
r <- s -> a -> m (Step s b)
step1 s
st a
a
case Step s b
r of
Yield Int
_ s
s -> Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c))
-> Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c)
forall a b. (a -> b) -> a -> b
$ Int
-> Tuple3' s Int (Either s s)
-> Step (Tuple3' s Int (Either s s)) c
forall s b. Int -> s -> Step s b
Skip Int
0 (s -> Int -> Either s s -> Tuple3' s Int (Either s s)
forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' s
s Int
forall a. (?callStack::CallStack) => a
undefined (s -> Either s s
forall a b. a -> Either a b
Left s
fs))
Skip Int
n s
s -> Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c))
-> Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c)
forall a b. (a -> b) -> a -> b
$ Int
-> Tuple3' s Int (Either s s)
-> Step (Tuple3' s Int (Either s s)) c
forall s b. Int -> s -> Step s b
Skip Int
n (s -> Int -> Either s s -> Tuple3' s Int (Either s s)
forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' s
s Int
forall a. (?callStack::CallStack) => a
undefined (s -> Either s s
forall a b. a -> Either a b
Left s
fs))
Stop Int
n b
b -> do
s
s <- m s
initial1
s
fs1 <- s -> b -> m s
fstep s
fs b
b
Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c))
-> Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c)
forall a b. (a -> b) -> a -> b
$ Int
-> Tuple3' s Int (Either s s)
-> Step (Tuple3' s Int (Either s s)) c
forall s b. Int -> s -> Step s b
Skip Int
n (s -> Int -> Either s s -> Tuple3' s Int (Either s s)
forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' s
s Int
0 (s -> Either s s
forall a b. b -> Either a b
Right s
fs1))
Error String
err -> Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c))
-> Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c)
forall a b. (a -> b) -> a -> b
$ String -> Step (Tuple3' s Int (Either s s)) c
forall s b. String -> Step s b
Error String
err
step (Tuple3' s
st Int
cnt (Right s
fs)) a
a = do
Step s b
r <- s -> a -> m (Step s b)
step1 s
st a
a
let cnt1 :: Int
cnt1 = Int
cnt Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
case Step s b
r of
Yield Int
_ s
s -> Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c))
-> Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c)
forall a b. (a -> b) -> a -> b
$ Int
-> Tuple3' s Int (Either s s)
-> Step (Tuple3' s Int (Either s s)) c
forall s b. Int -> s -> Step s b
Yield Int
0 (s -> Int -> Either s s -> Tuple3' s Int (Either s s)
forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' s
s Int
cnt1 (s -> Either s s
forall a b. b -> Either a b
Right s
fs))
Skip Int
n s
s -> do
Bool -> m () -> m ()
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
cnt1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0) (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c))
-> Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c)
forall a b. (a -> b) -> a -> b
$ Int
-> Tuple3' s Int (Either s s)
-> Step (Tuple3' s Int (Either s s)) c
forall s b. Int -> s -> Step s b
Skip Int
n (s -> Int -> Either s s -> Tuple3' s Int (Either s s)
forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' s
s (Int
cnt1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) (s -> Either s s
forall a b. b -> Either a b
Right s
fs))
Stop Int
n b
b -> do
s
s <- m s
initial1
s
fs1 <- s -> b -> m s
fstep s
fs b
b
Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c))
-> Step (Tuple3' s Int (Either s s)) c
-> m (Step (Tuple3' s Int (Either s s)) c)
forall a b. (a -> b) -> a -> b
$ Int
-> Tuple3' s Int (Either s s)
-> Step (Tuple3' s Int (Either s s)) c
forall s b. Int -> s -> Step s b
Skip Int
n (s -> Int -> Either s s -> Tuple3' s Int (Either s s)
forall a b c. a -> b -> c -> Tuple3' a b c
Tuple3' s
s Int
0 (s -> Either s s
forall a b. b -> Either a b
Right s
fs1))
Error String
_ -> Int -> c -> Step (Tuple3' s Int (Either s s)) c
forall s b. Int -> b -> Step s b
Stop Int
cnt1 (c -> Step (Tuple3' s Int (Either s s)) c)
-> m c -> m (Step (Tuple3' s Int (Either s s)) c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m c
fextract s
fs
extract :: Tuple3' s b (Either s s) -> m c
extract (Tuple3' s
s b
_ (Left s
fs)) = s -> m b
extract1 s
s m b -> (b -> m s) -> m s
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> b -> m s
fstep s
fs m s -> (s -> m c) -> m c
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> m c
fextract
extract (Tuple3' s
s b
_ (Right s
fs)) = do
Either ParseError b
r <- m b -> m (Either ParseError b)
forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> m (Either e a)
try (m b -> m (Either ParseError b)) -> m b -> m (Either ParseError b)
forall a b. (a -> b) -> a -> b
$ s -> m b
extract1 s
s
case Either ParseError b
r of
Left (ParseError
_ :: ParseError) -> s -> m c
fextract s
fs
Right b
b -> s -> b -> m s
fstep s
fs b
b m s -> (s -> m c) -> m c
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> m c
fextract
{-# INLINE die #-}
die :: MonadThrow m => String -> Parser m a b
die :: String -> Parser m a b
die String
err =
(() -> a -> m (Step () b)) -> m () -> (() -> m b) -> Parser m a b
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser (\()
_ a
_ -> Step () b -> m (Step () b)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step () b -> m (Step () b)) -> Step () b -> m (Step () b)
forall a b. (a -> b) -> a -> b
$ String -> Step () b
forall s b. String -> Step s b
Error String
err)
(() -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
(\()
_ -> ParseError -> m b
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (ParseError -> m b) -> ParseError -> m b
forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
err)
{-# INLINE dieM #-}
dieM :: MonadThrow m => m String -> Parser m a b
dieM :: m String -> Parser m a b
dieM m String
err =
(() -> a -> m (Step () b)) -> m () -> (() -> m b) -> Parser m a b
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser (\()
_ a
_ -> String -> Step () b
forall s b. String -> Step s b
Error (String -> Step () b) -> m String -> m (Step () b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m String
err)
(() -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
(\()
_ -> m String
err m String -> (String -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ParseError -> m b
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (ParseError -> m b) -> (String -> ParseError) -> String -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ParseError
ParseError)
instance MonadCatch m => Alternative (Parser m a) where
{-# INLINE empty #-}
empty :: Parser m a a
empty = String -> Parser m a a
forall (m :: * -> *) a b. MonadThrow m => String -> Parser m a b
die String
"empty"
{-# INLINE (<|>) #-}
<|> :: Parser m a a -> Parser m a a -> Parser m a a
(<|>) = Parser m a a -> Parser m a a -> Parser m a a
forall (m :: * -> *) x a.
Monad m =>
Parser m x a -> Parser m x a -> Parser m x a
alt
{-# INLINE many #-}
many :: Parser m a a -> Parser m a [a]
many = Fold m a [a] -> Parser m a a -> Parser m a [a]
forall (m :: * -> *) b c a.
MonadCatch m =>
Fold m b c -> Parser m a b -> Parser m a c
splitMany Fold m a [a]
forall (m :: * -> *) a. Monad m => Fold m a [a]
toList
{-# INLINE some #-}
some :: Parser m a a -> Parser m a [a]
some = Fold m a [a] -> Parser m a a -> Parser m a [a]
forall (m :: * -> *) b c a.
MonadCatch m =>
Fold m b c -> Parser m a b -> Parser m a c
splitSome Fold m a [a]
forall (m :: * -> *) a. Monad m => Fold m a [a]
toList
{-# ANN type ConcatParseState Fuse #-}
data ConcatParseState sl p = ConcatParseL sl | ConcatParseR p
instance Monad m => Monad (Parser m a) where
{-# INLINE return #-}
return :: a -> Parser m a a
return = a -> Parser m a a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
{-# INLINE (>>=) #-}
(Parser s -> a -> m (Step s a)
stepL m s
initialL s -> m a
extractL) >>= :: Parser m a a -> (a -> Parser m a b) -> Parser m a b
>>= a -> Parser m a b
func = (ConcatParseState s (Parser m a b)
-> a -> m (Step (ConcatParseState s (Parser m a b)) b))
-> m (ConcatParseState s (Parser m a b))
-> (ConcatParseState s (Parser m a b) -> m b)
-> Parser m a b
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser ConcatParseState s (Parser m a b)
-> a -> m (Step (ConcatParseState s (Parser m a b)) b)
step m (ConcatParseState s (Parser m a b))
forall p. m (ConcatParseState s p)
initial ConcatParseState s (Parser m a b) -> m b
forall a. ConcatParseState s (Parser m a b) -> m b
extract
where
initial :: m (ConcatParseState s p)
initial = s -> ConcatParseState s p
forall sl p. sl -> ConcatParseState sl p
ConcatParseL (s -> ConcatParseState s p) -> m s -> m (ConcatParseState s p)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
initialL
step :: ConcatParseState s (Parser m a b)
-> a -> m (Step (ConcatParseState s (Parser m a b)) b)
step (ConcatParseL s
st) a
a = do
Step s a
r <- s -> a -> m (Step s a)
stepL s
st a
a
Step (ConcatParseState s (Parser m a b)) b
-> m (Step (ConcatParseState s (Parser m a b)) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ConcatParseState s (Parser m a b)) b
-> m (Step (ConcatParseState s (Parser m a b)) b))
-> Step (ConcatParseState s (Parser m a b)) b
-> m (Step (ConcatParseState s (Parser m a b)) b)
forall a b. (a -> b) -> a -> b
$ case Step s a
r of
Yield Int
_ s
s -> Int
-> ConcatParseState s (Parser m a b)
-> Step (ConcatParseState s (Parser m a b)) b
forall s b. Int -> s -> Step s b
Skip Int
0 (s -> ConcatParseState s (Parser m a b)
forall sl p. sl -> ConcatParseState sl p
ConcatParseL s
s)
Skip Int
n s
s -> Int
-> ConcatParseState s (Parser m a b)
-> Step (ConcatParseState s (Parser m a b)) b
forall s b. Int -> s -> Step s b
Skip Int
n (s -> ConcatParseState s (Parser m a b)
forall sl p. sl -> ConcatParseState sl p
ConcatParseL s
s)
Stop Int
n a
b -> Int
-> ConcatParseState s (Parser m a b)
-> Step (ConcatParseState s (Parser m a b)) b
forall s b. Int -> s -> Step s b
Skip Int
n (Parser m a b -> ConcatParseState s (Parser m a b)
forall sl p. p -> ConcatParseState sl p
ConcatParseR (a -> Parser m a b
func a
b))
Error String
err -> String -> Step (ConcatParseState s (Parser m a b)) b
forall s b. String -> Step s b
Error String
err
step (ConcatParseR (Parser s -> a -> m (Step s b)
stepR m s
initialR s -> m b
extractR)) a
a = do
s
st <- m s
initialR
Step s b
r <- s -> a -> m (Step s b)
stepR s
st a
a
Step (ConcatParseState s (Parser m a b)) b
-> m (Step (ConcatParseState s (Parser m a b)) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ConcatParseState s (Parser m a b)) b
-> m (Step (ConcatParseState s (Parser m a b)) b))
-> Step (ConcatParseState s (Parser m a b)) b
-> m (Step (ConcatParseState s (Parser m a b)) b)
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
Yield Int
n s
s ->
Int
-> ConcatParseState s (Parser m a b)
-> Step (ConcatParseState s (Parser m a b)) b
forall s b. Int -> s -> Step s b
Yield Int
n (Parser m a b -> ConcatParseState s (Parser m a b)
forall sl p. p -> ConcatParseState sl p
ConcatParseR ((s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser s -> a -> m (Step s b)
stepR (s -> m s
forall (m :: * -> *) a. Monad m => a -> m a
return s
s) s -> m b
extractR))
Skip Int
n s
s ->
Int
-> ConcatParseState s (Parser m a b)
-> Step (ConcatParseState s (Parser m a b)) b
forall s b. Int -> s -> Step s b
Skip Int
n (Parser m a b -> ConcatParseState s (Parser m a b)
forall sl p. p -> ConcatParseState sl p
ConcatParseR ((s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser s -> a -> m (Step s b)
stepR (s -> m s
forall (m :: * -> *) a. Monad m => a -> m a
return s
s) s -> m b
extractR))
Stop Int
n b
b -> Int -> b -> Step (ConcatParseState s (Parser m a b)) b
forall s b. Int -> b -> Step s b
Stop Int
n b
b
Error String
err -> String -> Step (ConcatParseState s (Parser m a b)) b
forall s b. String -> Step s b
Error String
err
extract :: ConcatParseState s (Parser m a b) -> m b
extract (ConcatParseR (Parser s -> a -> m (Step s b)
_ m s
initialR s -> m b
extractR)) =
m s
initialR m s -> (s -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> m b
extractR
extract (ConcatParseL s
sL) = s -> m a
extractL s
sL m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Parser m a b -> m b
forall (m :: * -> *) a b. Monad m => Parser m a b -> m b
f (Parser m a b -> m b) -> (a -> Parser m a b) -> a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Parser m a b
func
where
f :: Parser m a b -> m b
f (Parser s -> a -> m (Step s b)
_ m s
initialR s -> m b
extractR) = m s
initialR m s -> (s -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> m b
extractR
instance MonadCatch m => MonadPlus (Parser m a) where
{-# INLINE mzero #-}
mzero :: Parser m a a
mzero = String -> Parser m a a
forall (m :: * -> *) a b. MonadThrow m => String -> Parser m a b
die String
"mzero"
{-# INLINE mplus #-}
mplus :: Parser m a a -> Parser m a a -> Parser m a a
mplus = Parser m a a -> Parser m a a -> Parser m a a
forall (m :: * -> *) x a.
Monad m =>
Parser m x a -> Parser m x a -> Parser m x a
alt