#include "inline.hs"
module Streamly.Internal.Data.Parser.ParserK.Type
(
Parser (..)
, fromPure
, fromEffect
, die
, toParserK
, fromParserK
)
where
import Control.Applicative (Alternative(..), liftA2)
import Control.Exception (assert, Exception(..))
import Control.Monad (MonadPlus(..), ap)
import Control.Monad.Catch (MonadCatch, MonadThrow(..), try)
#if MIN_VERSION_base(4,9,0)
import qualified Control.Monad.Fail as Fail
#endif
#if !(MIN_VERSION_base(4,10,0))
import Data.Semigroup ((<>))
#endif
import Streamly.Internal.Control.Exception
import qualified Streamly.Internal.Data.Parser.ParserD.Type as D
data Driver m a r =
Stop !Int r
| Partial !Int (Maybe a -> m (Driver m a r))
| Continue !Int (Maybe a -> m (Driver m a r))
| Failed String
instance Functor m => Functor (Driver m a) where
fmap :: (a -> b) -> Driver m a a -> Driver m a b
fmap a -> b
f (Stop Int
n a
r) = Int -> b -> Driver m a b
forall (m :: * -> *) a r. Int -> r -> Driver m a r
Stop Int
n (a -> b
f a
r)
fmap a -> b
f (Partial Int
n Maybe a -> m (Driver m a a)
yld) = Int -> (Maybe a -> m (Driver m a b)) -> Driver m a b
forall (m :: * -> *) a r.
Int -> (Maybe a -> m (Driver m a r)) -> Driver m a r
Partial Int
n ((Driver m a a -> Driver m a b)
-> m (Driver m a a) -> m (Driver m a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> Driver m a a -> Driver m a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) (m (Driver m a a) -> m (Driver m a b))
-> (Maybe a -> m (Driver m a a)) -> Maybe a -> m (Driver m a b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> m (Driver m a a)
yld)
fmap a -> b
f (Continue Int
n Maybe a -> m (Driver m a a)
yld) = Int -> (Maybe a -> m (Driver m a b)) -> Driver m a b
forall (m :: * -> *) a r.
Int -> (Maybe a -> m (Driver m a r)) -> Driver m a r
Continue Int
n ((Driver m a a -> Driver m a b)
-> m (Driver m a a) -> m (Driver m a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> Driver m a a -> Driver m a b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) (m (Driver m a a) -> m (Driver m a b))
-> (Maybe a -> m (Driver m a a)) -> Maybe a -> m (Driver m a b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> m (Driver m a a)
yld)
fmap a -> b
_ (Failed String
e) = String -> Driver m a b
forall (m :: * -> *) a r. String -> Driver m a r
Failed String
e
data Parse b =
Done !Int !b
| Error !String
instance Functor Parse where
fmap :: (a -> b) -> Parse a -> Parse b
fmap a -> b
f (Done Int
n a
b) = Int -> b -> Parse b
forall b. Int -> b -> Parse b
Done Int
n (a -> b
f a
b)
fmap a -> b
_ (Error String
e) = String -> Parse b
forall b. String -> Parse b
Error String
e
newtype Parser m a b = MkParser
{ Parser m a b
-> forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
runParser :: forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
}
{-# INLINE_NORMAL parseDToK #-}
parseDToK
:: MonadCatch m
=> (s -> a -> m (D.Step s b))
-> m (D.Initial s b)
-> (s -> m b)
-> Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
parseDToK :: (s -> a -> m (Step s b))
-> m (Initial s b)
-> (s -> m b)
-> Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
parseDToK s -> a -> m (Step s b)
pstep m (Initial s b)
initial s -> m b
extract Int
leftover (Int
0, Int
_) (Int, Int) -> Parse b -> m (Driver m a r)
cont = do
Initial s b
res <- m (Initial s b)
initial
case Initial s b
res of
D.IPartial s
r -> Driver m a r -> m (Driver m a r)
forall (m :: * -> *) a. Monad m => a -> m a
return (Driver m a r -> m (Driver m a r))
-> Driver m a r -> m (Driver m a r)
forall a b. (a -> b) -> a -> b
$ Int -> (Maybe a -> m (Driver m a r)) -> Driver m a r
forall (m :: * -> *) a r.
Int -> (Maybe a -> m (Driver m a r)) -> Driver m a r
Continue Int
leftover (m s -> Maybe a -> m (Driver m a r)
parseCont (s -> m s
forall (m :: * -> *) a. Monad m => a -> m a
return s
r))
D.IDone b
b -> (Int, Int) -> Parse b -> m (Driver m a r)
cont (Int
0,Int
0) (Int -> b -> Parse b
forall b. Int -> b -> Parse b
Done Int
0 b
b)
D.IError String
err -> (Int, Int) -> Parse b -> m (Driver m a r)
cont (Int
0,Int
0) (String -> Parse b
forall b. String -> Parse b
Error String
err)
where
parseCont :: m s -> Maybe a -> m (Driver m a r)
parseCont m s
pst (Just a
x) = do
s
r <- m s
pst
Step s b
pRes <- s -> a -> m (Step s b)
pstep s
r a
x
case Step s b
pRes of
D.Done Int
n b
b -> (Int, Int) -> Parse b -> m (Driver m a r)
cont (Int
0,Int
0) (Int -> b -> Parse b
forall b. Int -> b -> Parse b
Done Int
n b
b)
D.Error String
err -> (Int, Int) -> Parse b -> m (Driver m a r)
cont (Int
0,Int
0) (String -> Parse b
forall b. String -> Parse b
Error String
err)
D.Partial Int
n s
pst1 -> Driver m a r -> m (Driver m a r)
forall (m :: * -> *) a. Monad m => a -> m a
return (Driver m a r -> m (Driver m a r))
-> Driver m a r -> m (Driver m a r)
forall a b. (a -> b) -> a -> b
$ Int -> (Maybe a -> m (Driver m a r)) -> Driver m a r
forall (m :: * -> *) a r.
Int -> (Maybe a -> m (Driver m a r)) -> Driver m a r
Partial Int
n (m s -> Maybe a -> m (Driver m a r)
parseCont (s -> m s
forall (m :: * -> *) a. Monad m => a -> m a
return s
pst1))
D.Continue Int
n s
pst1 -> Driver m a r -> m (Driver m a r)
forall (m :: * -> *) a. Monad m => a -> m a
return (Driver m a r -> m (Driver m a r))
-> Driver m a r -> m (Driver m a r)
forall a b. (a -> b) -> a -> b
$ Int -> (Maybe a -> m (Driver m a r)) -> Driver m a r
forall (m :: * -> *) a r.
Int -> (Maybe a -> m (Driver m a r)) -> Driver m a r
Continue Int
n (m s -> Maybe a -> m (Driver m a r)
parseCont (s -> m s
forall (m :: * -> *) a. Monad m => a -> m a
return s
pst1))
parseCont m s
acc Maybe a
Nothing = do
s
pst <- m s
acc
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
extract s
pst
case Either ParseError b
r of
Left (ParseError
e :: D.ParseError) -> (Int, Int) -> Parse b -> m (Driver m a r)
cont (Int
0,Int
0) (String -> Parse b
forall b. String -> Parse b
Error (ParseError -> String
forall e. Exception e => e -> String
displayException ParseError
e))
Right b
b -> (Int, Int) -> Parse b -> m (Driver m a r)
cont (Int
0,Int
0) (Int -> b -> Parse b
forall b. Int -> b -> Parse b
Done Int
0 b
b)
parseDToK s -> a -> m (Step s b)
pstep m (Initial s b)
initial s -> m b
extract Int
leftover (Int
level, Int
count) (Int, Int) -> Parse b -> m (Driver m a r)
cont = do
Initial s b
res <- m (Initial s b)
initial
case Initial s b
res of
D.IPartial s
r -> Driver m a r -> m (Driver m a r)
forall (m :: * -> *) a. Monad m => a -> m a
return (Driver m a r -> m (Driver m a r))
-> Driver m a r -> m (Driver m a r)
forall a b. (a -> b) -> a -> b
$ Int -> (Maybe a -> m (Driver m a r)) -> Driver m a r
forall (m :: * -> *) a r.
Int -> (Maybe a -> m (Driver m a r)) -> Driver m a r
Continue Int
leftover (Int -> m s -> Maybe a -> m (Driver m a r)
parseCont Int
count (s -> m s
forall (m :: * -> *) a. Monad m => a -> m a
return s
r))
D.IDone b
b -> (Int, Int) -> Parse b -> m (Driver m a r)
cont (Int
level,Int
count) (Int -> b -> Parse b
forall b. Int -> b -> Parse b
Done Int
0 b
b)
D.IError String
err -> (Int, Int) -> Parse b -> m (Driver m a r)
cont (Int
level,Int
count) (String -> Parse b
forall b. String -> Parse b
Error String
err)
where
parseCont :: Int -> m s -> Maybe a -> m (Driver m a r)
parseCont !Int
cnt m s
pst (Just a
x) = do
let !cnt1 :: Int
cnt1 = Int
cnt Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
s
r <- m s
pst
Step s b
pRes <- s -> a -> m (Step s b)
pstep s
r a
x
case Step s b
pRes of
D.Done Int
n b
b -> do
Bool -> m () -> m ()
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
cnt1) (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
(Int, Int) -> Parse b -> m (Driver m a r)
cont (Int
level, Int
cnt1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) (Int -> b -> Parse b
forall b. Int -> b -> Parse b
Done Int
n b
b)
D.Error String
err ->
(Int, Int) -> Parse b -> m (Driver m a r)
cont (Int
level, Int
cnt1) (String -> Parse b
forall b. String -> Parse b
Error String
err)
D.Partial Int
n s
pst1 -> do
Bool -> m () -> m ()
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
cnt1) (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
Driver m a r -> m (Driver m a r)
forall (m :: * -> *) a. Monad m => a -> m a
return (Driver m a r -> m (Driver m a r))
-> Driver m a r -> m (Driver m a r)
forall a b. (a -> b) -> a -> b
$ Int -> (Maybe a -> m (Driver m a r)) -> Driver m a r
forall (m :: * -> *) a r.
Int -> (Maybe a -> m (Driver m a r)) -> Driver m a r
Partial Int
n (Int -> m s -> Maybe a -> m (Driver m a r)
parseCont (Int
cnt1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) (s -> m s
forall (m :: * -> *) a. Monad m => a -> m a
return s
pst1))
D.Continue Int
n s
pst1 -> do
Bool -> m () -> m ()
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
cnt1) (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
Driver m a r -> m (Driver m a r)
forall (m :: * -> *) a. Monad m => a -> m a
return (Driver m a r -> m (Driver m a r))
-> Driver m a r -> m (Driver m a r)
forall a b. (a -> b) -> a -> b
$ Int -> (Maybe a -> m (Driver m a r)) -> Driver m a r
forall (m :: * -> *) a r.
Int -> (Maybe a -> m (Driver m a r)) -> Driver m a r
Continue Int
n (Int -> m s -> Maybe a -> m (Driver m a r)
parseCont (Int
cnt1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) (s -> m s
forall (m :: * -> *) a. Monad m => a -> m a
return s
pst1))
parseCont Int
cnt m s
acc Maybe a
Nothing = do
s
pst <- m s
acc
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
extract s
pst
let s :: (Int, Int)
s = (Int
level, Int
cnt)
case Either ParseError b
r of
Left (ParseError
e :: D.ParseError) -> (Int, Int) -> Parse b -> m (Driver m a r)
cont (Int, Int)
s (String -> Parse b
forall b. String -> Parse b
Error (ParseError -> String
forall e. Exception e => e -> String
displayException ParseError
e))
Right b
b -> (Int, Int) -> Parse b -> m (Driver m a r)
cont (Int, Int)
s (Int -> b -> Parse b
forall b. Int -> b -> Parse b
Done Int
0 b
b)
{-# INLINE_LATE toParserK #-}
toParserK :: MonadCatch m => D.Parser m a b -> Parser m a b
toParserK :: Parser m a b -> Parser m a b
toParserK (D.Parser s -> a -> m (Step s b)
step m (Initial s b)
initial s -> m b
extract) =
(forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
forall (m :: * -> *) a b.
(forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
MkParser ((forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b)
-> (forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
forall a b. (a -> b) -> a -> b
$ (s -> a -> m (Step s b))
-> m (Initial s b)
-> (s -> m b)
-> Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
forall (m :: * -> *) s a b r.
MonadCatch m =>
(s -> a -> m (Step s b))
-> m (Initial s b)
-> (s -> m b)
-> Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
parseDToK s -> a -> m (Step s b)
step m (Initial s b)
initial s -> m b
extract
{-# INLINE parserDone #-}
parserDone :: Monad m => (Int, Int) -> Parse b -> m (Driver m a b)
parserDone :: (Int, Int) -> Parse b -> m (Driver m a b)
parserDone (Int
0,Int
_) (Done Int
n b
b) = Driver m a b -> m (Driver m a b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Driver m a b -> m (Driver m a b))
-> Driver m a b -> m (Driver m a b)
forall a b. (a -> b) -> a -> b
$ Int -> b -> Driver m a b
forall (m :: * -> *) a r. Int -> r -> Driver m a r
Stop Int
n b
b
parserDone (Int, Int)
st (Done Int
_ b
_) =
String -> m (Driver m a b)
forall a. (?callStack::CallStack) => String -> a
error (String -> m (Driver m a b)) -> String -> m (Driver m a b)
forall a b. (a -> b) -> a -> b
$ String
"Bug: fromParserK: inside alternative: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ (Int, Int) -> String
forall a. Show a => a -> String
show (Int, Int)
st
parserDone (Int, Int)
_ (Error String
e) = Driver m a b -> m (Driver m a b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Driver m a b -> m (Driver m a b))
-> Driver m a b -> m (Driver m a b)
forall a b. (a -> b) -> a -> b
$ String -> Driver m a b
forall (m :: * -> *) a r. String -> Driver m a r
Failed String
e
extractParse :: MonadThrow m => (Maybe a -> m (Driver m a b)) -> m b
Maybe a -> m (Driver m a b)
cont = do
Driver m a b
r <- Maybe a -> m (Driver m a b)
cont Maybe a
forall a. Maybe a
Nothing
case Driver m a b
r of
Stop Int
_ b
b -> b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b
Partial Int
_ Maybe a -> m (Driver m a b)
_ -> String -> m b
forall a. (?callStack::CallStack) => String -> a
error String
"Bug: extractParse got Partial"
Continue Int
_ Maybe a -> m (Driver m a b)
cont1 -> (Maybe a -> m (Driver m a b)) -> m b
forall (m :: * -> *) a b.
MonadThrow m =>
(Maybe a -> m (Driver m a b)) -> m b
extractParse Maybe a -> m (Driver m a b)
cont1
Failed String
e -> 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
D.ParseError String
e
data FromParserK b c = FPKDone !Int !b | FPKCont c
{-# INLINE_LATE fromParserK #-}
fromParserK :: MonadThrow m => Parser m a b -> D.Parser m a b
fromParserK :: Parser m a b -> Parser m a b
fromParserK Parser m a b
parser = (FromParserK b (Maybe a -> m (Driver m a b))
-> a -> m (Step (FromParserK b (Maybe a -> m (Driver m a b))) b))
-> m (Initial (FromParserK b (Maybe a -> m (Driver m a b))) b)
-> (FromParserK b (Maybe a -> m (Driver m a b)) -> m b)
-> Parser m a b
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Initial s b) -> (s -> m b) -> Parser m a b
D.Parser FromParserK b (Maybe a -> m (Driver m a b))
-> a -> m (Step (FromParserK b (Maybe a -> m (Driver m a b))) b)
forall (m :: * -> *) b a (m :: * -> *) a b.
Monad m =>
FromParserK b (Maybe a -> m (Driver m a b))
-> a -> m (Step (FromParserK b (Maybe a -> m (Driver m a b))) b)
step m (Initial (FromParserK b (Maybe a -> m (Driver m a b))) b)
forall b.
m (Initial (FromParserK b (Maybe a -> m (Driver m a b))) b)
initial FromParserK b (Maybe a -> m (Driver m a b)) -> m b
forall (m :: * -> *) a a.
MonadThrow m =>
FromParserK a (Maybe a -> m (Driver m a a)) -> m a
extract
where
initial :: m (Initial (FromParserK b (Maybe a -> m (Driver m a b))) b)
initial = do
Driver m a b
r <- Parser m a b
-> Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a b))
-> m (Driver m a b)
forall (m :: * -> *) a b.
Parser m a b
-> forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
runParser Parser m a b
parser Int
0 (Int
0,Int
0) (Int, Int) -> Parse b -> m (Driver m a b)
forall (m :: * -> *) b a.
Monad m =>
(Int, Int) -> Parse b -> m (Driver m a b)
parserDone
Initial (FromParserK b (Maybe a -> m (Driver m a b))) b
-> m (Initial (FromParserK b (Maybe a -> m (Driver m a b))) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Initial (FromParserK b (Maybe a -> m (Driver m a b))) b
-> m (Initial (FromParserK b (Maybe a -> m (Driver m a b))) b))
-> Initial (FromParserK b (Maybe a -> m (Driver m a b))) b
-> m (Initial (FromParserK b (Maybe a -> m (Driver m a b))) b)
forall a b. (a -> b) -> a -> b
$ case Driver m a b
r of
Stop Int
n b
b -> FromParserK b (Maybe a -> m (Driver m a b))
-> Initial (FromParserK b (Maybe a -> m (Driver m a b))) b
forall s b. s -> Initial s b
D.IPartial (FromParserK b (Maybe a -> m (Driver m a b))
-> Initial (FromParserK b (Maybe a -> m (Driver m a b))) b)
-> FromParserK b (Maybe a -> m (Driver m a b))
-> Initial (FromParserK b (Maybe a -> m (Driver m a b))) b
forall a b. (a -> b) -> a -> b
$ Int -> b -> FromParserK b (Maybe a -> m (Driver m a b))
forall b c. Int -> b -> FromParserK b c
FPKDone Int
n b
b
Failed String
e -> String -> Initial (FromParserK b (Maybe a -> m (Driver m a b))) b
forall s b. String -> Initial s b
D.IError String
e
Partial Int
_ Maybe a -> m (Driver m a b)
cont -> FromParserK b (Maybe a -> m (Driver m a b))
-> Initial (FromParserK b (Maybe a -> m (Driver m a b))) b
forall s b. s -> Initial s b
D.IPartial (FromParserK b (Maybe a -> m (Driver m a b))
-> Initial (FromParserK b (Maybe a -> m (Driver m a b))) b)
-> FromParserK b (Maybe a -> m (Driver m a b))
-> Initial (FromParserK b (Maybe a -> m (Driver m a b))) b
forall a b. (a -> b) -> a -> b
$ (Maybe a -> m (Driver m a b))
-> FromParserK b (Maybe a -> m (Driver m a b))
forall b c. c -> FromParserK b c
FPKCont Maybe a -> m (Driver m a b)
cont
Continue Int
_ Maybe a -> m (Driver m a b)
cont -> FromParserK b (Maybe a -> m (Driver m a b))
-> Initial (FromParserK b (Maybe a -> m (Driver m a b))) b
forall s b. s -> Initial s b
D.IPartial (FromParserK b (Maybe a -> m (Driver m a b))
-> Initial (FromParserK b (Maybe a -> m (Driver m a b))) b)
-> FromParserK b (Maybe a -> m (Driver m a b))
-> Initial (FromParserK b (Maybe a -> m (Driver m a b))) b
forall a b. (a -> b) -> a -> b
$ (Maybe a -> m (Driver m a b))
-> FromParserK b (Maybe a -> m (Driver m a b))
forall b c. c -> FromParserK b c
FPKCont Maybe a -> m (Driver m a b)
cont
step :: FromParserK b (Maybe a -> m (Driver m a b))
-> a -> m (Step (FromParserK b (Maybe a -> m (Driver m a b))) b)
step (FPKDone Int
n b
b) a
_ = do
Bool -> m ()
forall (f :: * -> *). Applicative f => Bool -> f ()
assertM (Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0)
Step (FromParserK b (Maybe a -> m (Driver m a b))) b
-> m (Step (FromParserK b (Maybe a -> m (Driver m a b))) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (FromParserK b (Maybe a -> m (Driver m a b))) b
-> m (Step (FromParserK b (Maybe a -> m (Driver m a b))) b))
-> Step (FromParserK b (Maybe a -> m (Driver m a b))) b
-> m (Step (FromParserK b (Maybe a -> m (Driver m a b))) b)
forall a b. (a -> b) -> a -> b
$ Int -> b -> Step (FromParserK b (Maybe a -> m (Driver m a b))) b
forall s b. Int -> b -> Step s b
D.Done (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) b
b
step (FPKCont Maybe a -> m (Driver m a b)
cont) a
a = do
Driver m a b
r <- Maybe a -> m (Driver m a b)
cont (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
Step (FromParserK b (Maybe a -> m (Driver m a b))) b
-> m (Step (FromParserK b (Maybe a -> m (Driver m a b))) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (FromParserK b (Maybe a -> m (Driver m a b))) b
-> m (Step (FromParserK b (Maybe a -> m (Driver m a b))) b))
-> Step (FromParserK b (Maybe a -> m (Driver m a b))) b
-> m (Step (FromParserK b (Maybe a -> m (Driver m a b))) b)
forall a b. (a -> b) -> a -> b
$ case Driver m a b
r of
Stop Int
n b
b -> Int -> b -> Step (FromParserK b (Maybe a -> m (Driver m a b))) b
forall s b. Int -> b -> Step s b
D.Done Int
n b
b
Failed String
e -> String -> Step (FromParserK b (Maybe a -> m (Driver m a b))) b
forall s b. String -> Step s b
D.Error String
e
Partial Int
n Maybe a -> m (Driver m a b)
cont1 -> Int
-> FromParserK b (Maybe a -> m (Driver m a b))
-> Step (FromParserK b (Maybe a -> m (Driver m a b))) b
forall s b. Int -> s -> Step s b
D.Partial Int
n ((Maybe a -> m (Driver m a b))
-> FromParserK b (Maybe a -> m (Driver m a b))
forall b c. c -> FromParserK b c
FPKCont Maybe a -> m (Driver m a b)
cont1)
Continue Int
n Maybe a -> m (Driver m a b)
cont1 -> Int
-> FromParserK b (Maybe a -> m (Driver m a b))
-> Step (FromParserK b (Maybe a -> m (Driver m a b))) b
forall s b. Int -> s -> Step s b
D.Continue Int
n ((Maybe a -> m (Driver m a b))
-> FromParserK b (Maybe a -> m (Driver m a b))
forall b c. c -> FromParserK b c
FPKCont Maybe a -> m (Driver m a b)
cont1)
extract :: FromParserK a (Maybe a -> m (Driver m a a)) -> m a
extract (FPKDone Int
_ a
b) = a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
b
extract (FPKCont Maybe a -> m (Driver m a a)
cont) = (Maybe a -> m (Driver m a a)) -> m a
forall (m :: * -> *) a b.
MonadThrow m =>
(Maybe a -> m (Driver m a b)) -> m b
extractParse Maybe a -> m (Driver m a a)
cont
#ifndef DISABLE_FUSION
{-# RULES "fromParserK/toParserK fusion" [2]
forall s. toParserK (fromParserK s) = s #-}
{-# RULES "toParserK/fromParserK fusion" [2]
forall s. fromParserK (toParserK s) = s #-}
#endif
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 m a a
parser = (forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
forall (m :: * -> *) a b.
(forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
MkParser ((forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b)
-> (forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
forall a b. (a -> b) -> a -> b
$ \Int
lo (Int, Int)
st (Int, Int) -> Parse b -> m (Driver m a r)
yieldk ->
let yld :: (Int, Int) -> Parse a -> m (Driver m a r)
yld (Int, Int)
s Parse a
res = (Int, Int) -> Parse b -> m (Driver m a r)
yieldk (Int, Int)
s ((a -> b) -> Parse a -> Parse b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Parse a
res)
in Parser m a a
-> Int
-> (Int, Int)
-> ((Int, Int) -> Parse a -> m (Driver m a r))
-> m (Driver m a r)
forall (m :: * -> *) a b.
Parser m a b
-> forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
runParser Parser m a a
parser Int
lo (Int, Int)
st (Int, Int) -> Parse a -> m (Driver m a r)
yld
{-# INLINE fromPure #-}
fromPure :: b -> Parser m a b
fromPure :: b -> Parser m a b
fromPure b
b = (forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
forall (m :: * -> *) a b.
(forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
MkParser ((forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b)
-> (forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
forall a b. (a -> b) -> a -> b
$ \Int
lo (Int, Int)
st (Int, Int) -> Parse b -> m (Driver m a r)
yieldk -> (Int, Int) -> Parse b -> m (Driver m a r)
yieldk (Int, Int)
st (Int -> b -> Parse b
forall b. Int -> b -> Parse b
Done Int
lo b
b)
{-# INLINE fromEffect #-}
fromEffect :: Monad m => m b -> Parser m a b
fromEffect :: m b -> Parser m a b
fromEffect m b
eff = (forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
forall (m :: * -> *) a b.
(forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
MkParser ((forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b)
-> (forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
forall a b. (a -> b) -> a -> b
$ \Int
lo (Int, Int)
st (Int, Int) -> Parse b -> m (Driver m a r)
yieldk -> m b
eff m b -> (b -> m (Driver m a r)) -> m (Driver m a r)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \b
b -> (Int, Int) -> Parse b -> m (Driver m a r)
yieldk (Int, Int)
st (Int -> b -> Parse b
forall b. Int -> b -> Parse b
Done Int
lo b
b)
instance Monad m => Applicative (Parser m a) where
{-# INLINE pure #-}
pure :: a -> Parser m a a
pure = a -> Parser m a a
forall b (m :: * -> *) a. b -> Parser m a b
fromPure
{-# INLINE (<*>) #-}
<*> :: Parser m a (a -> b) -> Parser m a a -> Parser m a b
(<*>) = Parser m a (a -> b) -> Parser m a a -> Parser m a b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
{-# INLINE (*>) #-}
Parser m a a
m1 *> :: Parser m a a -> Parser m a b -> Parser m a b
*> Parser m a b
m2 = (forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
forall (m :: * -> *) a b.
(forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
MkParser ((forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b)
-> (forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
forall a b. (a -> b) -> a -> b
$ \Int
lo (Int, Int)
st (Int, Int) -> Parse b -> m (Driver m a r)
yieldk ->
let yield1 :: (Int, Int) -> Parse b -> m (Driver m a r)
yield1 (Int, Int)
s (Done Int
n b
_) = Parser m a b
-> Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
forall (m :: * -> *) a b.
Parser m a b
-> forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
runParser Parser m a b
m2 Int
n (Int, Int)
s (Int, Int) -> Parse b -> m (Driver m a r)
yieldk
yield1 (Int, Int)
s (Error String
e) = (Int, Int) -> Parse b -> m (Driver m a r)
yieldk (Int, Int)
s (String -> Parse b
forall b. String -> Parse b
Error String
e)
in Parser m a a
-> Int
-> (Int, Int)
-> ((Int, Int) -> Parse a -> m (Driver m a r))
-> m (Driver m a r)
forall (m :: * -> *) a b.
Parser m a b
-> forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
runParser Parser m a a
m1 Int
lo (Int, Int)
st (Int, Int) -> Parse a -> m (Driver m a r)
forall b. (Int, Int) -> Parse b -> m (Driver m a r)
yield1
{-# INLINE (<*) #-}
Parser m a a
m1 <* :: Parser m a a -> Parser m a b -> Parser m a a
<* Parser m a b
m2 = (forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse a -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a a
forall (m :: * -> *) a b.
(forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
MkParser ((forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse a -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a a)
-> (forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse a -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a a
forall a b. (a -> b) -> a -> b
$ \Int
lo (Int, Int)
st (Int, Int) -> Parse a -> m (Driver m a r)
yieldk ->
let yield1 :: (Int, Int) -> Parse a -> m (Driver m a r)
yield1 (Int, Int)
s (Done Int
n a
b) =
let yield2 :: (Int, Int) -> Parse b -> m (Driver m a r)
yield2 (Int, Int)
s1 (Done Int
n1 b
_) = (Int, Int) -> Parse a -> m (Driver m a r)
yieldk (Int, Int)
s1 (Int -> a -> Parse a
forall b. Int -> b -> Parse b
Done Int
n1 a
b)
yield2 (Int, Int)
s1 (Error String
e) = (Int, Int) -> Parse a -> m (Driver m a r)
yieldk (Int, Int)
s1 (String -> Parse a
forall b. String -> Parse b
Error String
e)
in Parser m a b
-> Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
forall (m :: * -> *) a b.
Parser m a b
-> forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
runParser Parser m a b
m2 Int
n (Int, Int)
s (Int, Int) -> Parse b -> m (Driver m a r)
forall b. (Int, Int) -> Parse b -> m (Driver m a r)
yield2
yield1 (Int, Int)
s (Error String
e) = (Int, Int) -> Parse a -> m (Driver m a r)
yieldk (Int, Int)
s (String -> Parse a
forall b. String -> Parse b
Error String
e)
in Parser m a a
-> Int
-> (Int, Int)
-> ((Int, Int) -> Parse a -> m (Driver m a r))
-> m (Driver m a r)
forall (m :: * -> *) a b.
Parser m a b
-> forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
runParser Parser m a a
m1 Int
lo (Int, Int)
st (Int, Int) -> Parse a -> m (Driver m a r)
yield1
#if MIN_VERSION_base(4,10,0)
{-# INLINE liftA2 #-}
liftA2 :: (a -> b -> c) -> Parser m a a -> Parser m a b -> Parser m a c
liftA2 a -> b -> c
f Parser m a a
x = Parser m a (b -> c) -> Parser m a b -> Parser m a c
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>) ((a -> b -> c) -> Parser m a a -> Parser m a (b -> c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b -> c
f Parser m a a
x)
#endif
{-# INLINE die #-}
die :: String -> Parser m a b
die :: String -> Parser m a b
die String
err = (forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
forall (m :: * -> *) a b.
(forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
MkParser (\Int
_ (Int, Int)
st (Int, Int) -> Parse b -> m (Driver m a r)
yieldk -> (Int, Int) -> Parse b -> m (Driver m a r)
yieldk (Int, Int)
st (String -> Parse b
forall b. String -> Parse b
Error String
err))
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 m a a
m >>= :: Parser m a a -> (a -> Parser m a b) -> Parser m a b
>>= a -> Parser m a b
k = (forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
forall (m :: * -> *) a b.
(forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
MkParser ((forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b)
-> (forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
forall a b. (a -> b) -> a -> b
$ \Int
lo (Int, Int)
st (Int, Int) -> Parse b -> m (Driver m a r)
yieldk ->
let yield1 :: (Int, Int) -> Parse a -> m (Driver m a r)
yield1 (Int, Int)
s (Done Int
n a
b) = Parser m a b
-> Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
forall (m :: * -> *) a b.
Parser m a b
-> forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
runParser (a -> Parser m a b
k a
b) Int
n (Int, Int)
s (Int, Int) -> Parse b -> m (Driver m a r)
yieldk
yield1 (Int, Int)
s (Error String
e) = (Int, Int) -> Parse b -> m (Driver m a r)
yieldk (Int, Int)
s (String -> Parse b
forall b. String -> Parse b
Error String
e)
in Parser m a a
-> Int
-> (Int, Int)
-> ((Int, Int) -> Parse a -> m (Driver m a r))
-> m (Driver m a r)
forall (m :: * -> *) a b.
Parser m a b
-> forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
runParser Parser m a a
m Int
lo (Int, Int)
st (Int, Int) -> Parse a -> m (Driver m a r)
yield1
{-# INLINE (>>) #-}
>> :: Parser m a a -> Parser m a b -> Parser m a b
(>>) = Parser m a a -> Parser m a b -> Parser m a b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)
#if !(MIN_VERSION_base(4,13,0))
{-# INLINE fail #-}
fail = die
#endif
#if MIN_VERSION_base(4,9,0)
instance Monad m => Fail.MonadFail (Parser m a) where
{-# INLINE fail #-}
fail :: String -> Parser m a a
fail = String -> Parser m a a
forall (m :: * -> *) a b. String -> Parser m a b
die
#endif
instance Monad m => Alternative (Parser m a) where
{-# INLINE empty #-}
empty :: Parser m a a
empty = String -> Parser m a a
forall (m :: * -> *) a b. String -> Parser m a b
die String
"empty"
{-# INLINE (<|>) #-}
Parser m a a
m1 <|> :: Parser m a a -> Parser m a a -> Parser m a a
<|> Parser m a a
m2 = (forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse a -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a a
forall (m :: * -> *) a b.
(forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a b
MkParser ((forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse a -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a a)
-> (forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse a -> m (Driver m a r))
-> m (Driver m a r))
-> Parser m a a
forall a b. (a -> b) -> a -> b
$ \Int
lo (Int
level, Int
_) (Int, Int) -> Parse a -> m (Driver m a r)
yieldk ->
let yield1 :: (Int, Int) -> Parse a -> m (Driver m a r)
yield1 (Int
0, Int
_) Parse a
_ = String -> m (Driver m a r)
forall a. (?callStack::CallStack) => String -> a
error String
"0 nest level in Alternative"
yield1 (Int
lvl, Int
_) (Done Int
n a
b) = (Int, Int) -> Parse a -> m (Driver m a r)
yieldk (Int
lvl Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1, Int
0) (Int -> a -> Parse a
forall b. Int -> b -> Parse b
Done Int
n a
b)
yield1 (Int
lvl, Int
cnt) (Error String
_) = Parser m a a
-> Int
-> (Int, Int)
-> ((Int, Int) -> Parse a -> m (Driver m a r))
-> m (Driver m a r)
forall (m :: * -> *) a b.
Parser m a b
-> forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
runParser Parser m a a
m2 Int
cnt (Int
lvl Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1, Int
0) (Int, Int) -> Parse a -> m (Driver m a r)
yieldk
in Parser m a a
-> Int
-> (Int, Int)
-> ((Int, Int) -> Parse a -> m (Driver m a r))
-> m (Driver m a r)
forall (m :: * -> *) a b.
Parser m a b
-> forall r.
Int
-> (Int, Int)
-> ((Int, Int) -> Parse b -> m (Driver m a r))
-> m (Driver m a r)
runParser Parser m a a
m1 Int
lo (Int
level Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1, Int
0) (Int, Int) -> Parse a -> m (Driver m a r)
yield1
{-# INLINE many #-}
many :: Parser m a a -> Parser m a [a]
many Parser m a a
v = Parser m a [a]
many_v
where
many_v :: Parser m a [a]
many_v = Parser m a [a]
some_v Parser m a [a] -> Parser m a [a] -> Parser m a [a]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [a] -> Parser m a [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
some_v :: Parser m a [a]
some_v = (:) (a -> [a] -> [a]) -> Parser m a a -> Parser m a ([a] -> [a])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser m a a
v Parser m a ([a] -> [a]) -> Parser m a [a] -> Parser m a [a]
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser m a [a]
many_v
{-# INLINE some #-}
some :: Parser m a a -> Parser m a [a]
some Parser m a a
v = Parser m a [a]
some_v
where
many_v :: Parser m a [a]
many_v = Parser m a [a]
some_v Parser m a [a] -> Parser m a [a] -> Parser m a [a]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [a] -> Parser m a [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
some_v :: Parser m a [a]
some_v = (:) (a -> [a] -> [a]) -> Parser m a a -> Parser m a ([a] -> [a])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser m a a
v Parser m a ([a] -> [a]) -> Parser m a [a] -> Parser m a [a]
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser m a [a]
many_v
instance Monad m => MonadPlus (Parser m a) where
{-# INLINE mzero #-}
mzero :: Parser m a a
mzero = String -> Parser m a a
forall (m :: * -> *) a b. 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 (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)