{-# LANGUAGE CPP #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Streamly.Internal.Data.Parser
(
Parser (..)
, fromFold
, any
, all
, yield
, yieldM
, die
, dieM
, peek
, eof
, satisfy
, take
, takeEQ
, takeGE
, lookAhead
, takeWhile
, takeWhile1
, sliceSepBy
, sliceSepByMax
, sliceEndWith
, sliceBeginWith
, wordBy
, groupBy
, eqBy
, splitWith
, teeWith
, teeWithFst
, teeWithMin
, deintercalate
, shortest
, longest
, sequence
, count
, countBetween
, many
, some
, manyTill
, choice
)
where
import Control.Exception (assert)
import Control.Monad.Catch (MonadCatch, MonadThrow(..))
import Prelude
hiding (any, all, take, takeWhile, sequence)
import Streamly.Internal.Data.Fold.Types (Fold(..))
import Streamly.Internal.Data.Parser.Tee
import Streamly.Internal.Data.Parser.Types
import Streamly.Internal.Data.Strict
{-# INLINE fromFold #-}
fromFold :: Monad m => Fold m a b -> Parser m a b
fromFold :: forall (m :: * -> *) a b. Monad m => Fold m a b -> Parser m a b
fromFold (Fold s -> a -> m s
fstep m s
finitial s -> m b
fextract) = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser forall {b}. s -> a -> m (Step s b)
step m s
finitial s -> m b
fextract
where
step :: s -> a -> m (Step s b)
step s
s a
a = forall s b. Int -> s -> Step s b
Yield Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> a -> m s
fstep s
s a
a
{-# INLINABLE any #-}
any :: Monad m => (a -> Bool) -> Parser m a Bool
any :: forall (m :: * -> *) a. Monad m => (a -> Bool) -> Parser m a Bool
any a -> Bool
predicate = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser forall {m :: * -> *}. Monad m => Bool -> a -> m (Step Bool Bool)
step m Bool
initial forall (m :: * -> *) a. Monad m => a -> m a
return
where
initial :: m Bool
initial = forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
step :: Bool -> a -> m (Step Bool Bool)
step Bool
s a
a = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
if Bool
s
then forall s b. Int -> b -> Step s b
Stop Int
0 Bool
True
else
if a -> Bool
predicate a
a
then forall s b. Int -> b -> Step s b
Stop Int
0 Bool
True
else forall s b. Int -> s -> Step s b
Yield Int
0 Bool
False
{-# INLINABLE all #-}
all :: Monad m => (a -> Bool) -> Parser m a Bool
all :: forall (m :: * -> *) a. Monad m => (a -> Bool) -> Parser m a Bool
all a -> Bool
predicate = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser forall {m :: * -> *}. Monad m => Bool -> a -> m (Step Bool Bool)
step m Bool
initial forall (m :: * -> *) a. Monad m => a -> m a
return
where
initial :: m Bool
initial = forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
step :: Bool -> a -> m (Step Bool Bool)
step Bool
s a
a = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
if Bool
s
then
if a -> Bool
predicate a
a
then forall s b. Int -> s -> Step s b
Yield Int
0 Bool
True
else forall s b. Int -> b -> Step s b
Stop Int
0 Bool
False
else forall s b. Int -> b -> Step s b
Stop Int
0 Bool
False
{-# INLINABLE peek #-}
peek :: MonadThrow m => Parser m a a
peek :: forall (m :: * -> *) a. MonadThrow m => Parser m a a
peek = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser forall {m :: * -> *} {b} {s}. Monad m => () -> b -> m (Step s b)
step m ()
initial forall {m :: * -> *} {a}. MonadThrow m => () -> m a
extract
where
initial :: m ()
initial = forall (m :: * -> *) a. Monad m => a -> m a
return ()
step :: () -> b -> m (Step s b)
step () b
a = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> b -> Step s b
Stop Int
1 b
a
extract :: () -> m a
extract () = forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
"peek: end of input"
{-# INLINABLE eof #-}
eof :: Monad m => Parser m a ()
eof :: forall (m :: * -> *) a. Monad m => Parser m a ()
eof = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser forall {m :: * -> *} {p} {s} {b}.
Monad m =>
() -> p -> m (Step s b)
step m ()
initial forall (m :: * -> *) a. Monad m => a -> m a
return
where
initial :: m ()
initial = forall (m :: * -> *) a. Monad m => a -> m a
return ()
step :: () -> p -> m (Step s b)
step () p
_ = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. String -> Step s b
Error String
"eof: not at end of input"
{-# INLINE satisfy #-}
satisfy :: MonadThrow m => (a -> Bool) -> Parser m a a
satisfy :: forall (m :: * -> *) a. MonadThrow m => (a -> Bool) -> Parser m a a
satisfy a -> Bool
predicate = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser forall {m :: * -> *} {s}. Monad m => () -> a -> m (Step s a)
step m ()
initial forall {m :: * -> *} {p} {a}. MonadThrow m => p -> m a
extract
where
initial :: m ()
initial = forall (m :: * -> *) a. Monad m => a -> m a
return ()
step :: () -> a -> m (Step s a)
step () a
a = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
if a -> Bool
predicate a
a
then forall s b. Int -> b -> Step s b
Stop Int
0 a
a
else forall s b. String -> Step s b
Error String
"satisfy: predicate failed"
extract :: p -> m a
extract p
_ = forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
"satisfy: end of input"
{-# INLINE take #-}
take :: Monad m => Int -> Fold m a b -> Parser m a b
take :: forall (m :: * -> *) a b.
Monad m =>
Int -> Fold m a b -> Parser m a b
take Int
n (Fold s -> a -> m s
fstep m s
finitial s -> m b
fextract) = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step m (Tuple' Int s)
initial forall {a}. Tuple' a s -> m b
extract
where
initial :: m (Tuple' Int s)
initial = forall a b. a -> b -> Tuple' a b
Tuple' Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
finitial
step :: Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step (Tuple' Int
i s
r) a
a = do
s
res <- s -> a -> m s
fstep s
r a
a
let i1 :: Int
i1 = Int
i forall a. Num a => a -> a -> a
+ Int
1
s1 :: Tuple' Int s
s1 = forall a b. a -> b -> Tuple' a b
Tuple' Int
i1 s
res
if Int
i1 forall a. Ord a => a -> a -> Bool
< Int
n
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Yield Int
0 Tuple' Int s
s1
else forall s b. Int -> b -> Step s b
Stop Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
fextract s
res
extract :: Tuple' a s -> m b
extract (Tuple' a
_ s
r) = s -> m b
fextract s
r
{-# INLINE takeEQ #-}
takeEQ :: MonadThrow m => Int -> Fold m a b -> Parser m a b
takeEQ :: forall (m :: * -> *) a b.
MonadThrow m =>
Int -> Fold m a b -> Parser m a b
takeEQ Int
n (Fold s -> a -> m s
fstep m s
finitial s -> m b
fextract) = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step m (Tuple' Int s)
initial Tuple' Int s -> m b
extract
where
initial :: m (Tuple' Int s)
initial = forall a b. a -> b -> Tuple' a b
Tuple' Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
finitial
step :: Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step (Tuple' Int
i s
r) a
a = do
s
res <- s -> a -> m s
fstep s
r a
a
let i1 :: Int
i1 = Int
i forall a. Num a => a -> a -> a
+ Int
1
s1 :: Tuple' Int s
s1 = forall a b. a -> b -> Tuple' a b
Tuple' Int
i1 s
res
if Int
i1 forall a. Ord a => a -> a -> Bool
< Int
n then forall (m :: * -> *) a. Monad m => a -> m a
return (forall s b. Int -> s -> Step s b
Skip Int
0 Tuple' Int s
s1) else forall s b. Int -> b -> Step s b
Stop Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
fextract s
res
extract :: Tuple' Int s -> m b
extract (Tuple' Int
i s
r) =
if Int
n forall a. Eq a => a -> a -> Bool
== Int
i
then s -> m b
fextract s
r
else forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
err
where
err :: String
err =
String
"takeEQ: Expecting exactly " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n
forall a. [a] -> [a] -> [a]
++ String
" elements, got " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
i
{-# INLINE takeGE #-}
takeGE :: MonadThrow m => Int -> Fold m a b -> Parser m a b
takeGE :: forall (m :: * -> *) a b.
MonadThrow m =>
Int -> Fold m a b -> Parser m a b
takeGE Int
n (Fold s -> a -> m s
fstep m s
finitial s -> m b
fextract) = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser forall {b}. Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step m (Tuple' Int s)
initial Tuple' Int s -> m b
extract
where
initial :: m (Tuple' Int s)
initial = forall a b. a -> b -> Tuple' a b
Tuple' Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
finitial
step :: Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step (Tuple' Int
i s
r) a
a = do
s
res <- s -> a -> m s
fstep s
r a
a
let i1 :: Int
i1 = Int
i forall a. Num a => a -> a -> a
+ Int
1
s1 :: Tuple' Int s
s1 = forall a b. a -> b -> Tuple' a b
Tuple' Int
i1 s
res
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
if Int
i1 forall a. Ord a => a -> a -> Bool
< Int
n
then forall s b. Int -> s -> Step s b
Skip Int
0 Tuple' Int s
s1
else forall s b. Int -> s -> Step s b
Yield Int
0 Tuple' Int s
s1
extract :: Tuple' Int s -> m b
extract (Tuple' Int
i s
r) = s -> m b
fextract s
r forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall {m :: * -> *} {a}. MonadThrow m => a -> m a
f
where
err :: String
err =
String
"takeGE: Expecting at least " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n
forall a. [a] -> [a] -> [a]
++ String
" elements, got only " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
i
f :: a -> m a
f a
x =
if Int
i forall a. Ord a => a -> a -> Bool
>= Int
n
then forall (m :: * -> *) a. Monad m => a -> m a
return a
x
else forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
err
{-# INLINE takeWhile #-}
takeWhile :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
takeWhile :: forall (m :: * -> *) a b.
Monad m =>
(a -> Bool) -> Fold m a b -> Parser m a b
takeWhile a -> Bool
predicate (Fold s -> a -> m s
fstep m s
finitial s -> m b
fextract) =
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 s -> m b
fextract
where
initial :: m s
initial = m s
finitial
step :: s -> a -> m (Step s b)
step s
s a
a =
if a -> Bool
predicate a
a
then forall s b. Int -> s -> Step s b
Yield Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> a -> m s
fstep s
s a
a
else forall s b. Int -> b -> Step s b
Stop Int
1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
fextract s
s
{-# INLINE takeWhile1 #-}
takeWhile1 :: MonadThrow m => (a -> Bool) -> Fold m a b -> Parser m a b
takeWhile1 :: forall (m :: * -> *) a b.
MonadThrow m =>
(a -> Bool) -> Fold m a b -> Parser m a b
takeWhile1 a -> Bool
predicate (Fold s -> a -> m s
fstep m s
finitial s -> m b
fextract) =
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser Maybe s -> a -> m (Step (Maybe s) b)
step forall {a}. m (Maybe a)
initial Maybe s -> m b
extract
where
initial :: m (Maybe a)
initial = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
step :: Maybe s -> a -> m (Step (Maybe s) b)
step Maybe s
Nothing a
a =
if a -> Bool
predicate a
a
then do
s
s <- m s
finitial
s
r <- s -> a -> m s
fstep s
s a
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Yield Int
0 (forall a. a -> Maybe a
Just s
r)
else forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. String -> Step s b
Error String
"takeWhile1: empty"
step (Just s
s) a
a =
if a -> Bool
predicate a
a
then do
s
r <- s -> a -> m s
fstep s
s a
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Yield Int
0 (forall a. a -> Maybe a
Just s
r)
else do
b
b <- s -> m b
fextract s
s
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> b -> Step s b
Stop Int
1 b
b
extract :: Maybe s -> m b
extract Maybe s
Nothing = forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
"takeWhile1: end of input"
extract (Just s
s) = s -> m b
fextract s
s
{-# INLINABLE sliceSepBy #-}
sliceSepBy :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
sliceSepBy :: forall (m :: * -> *) a b.
Monad m =>
(a -> Bool) -> Fold m a b -> Parser m a b
sliceSepBy a -> Bool
predicate (Fold s -> a -> m s
fstep m s
finitial s -> m b
fextract) =
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 s -> m b
fextract
where
initial :: m s
initial = m s
finitial
step :: s -> a -> m (Step s b)
step s
s a
a =
if Bool -> Bool
not (a -> Bool
predicate a
a)
then forall s b. Int -> s -> Step s b
Yield Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> a -> m s
fstep s
s a
a
else forall s b. Int -> b -> Step s b
Stop Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
fextract s
s
{-# INLINABLE sliceEndWith #-}
sliceEndWith ::
(a -> Bool) -> Fold m a b -> Parser m a b
sliceEndWith :: forall a (m :: * -> *) b. (a -> Bool) -> Fold m a b -> Parser m a b
sliceEndWith = forall a. HasCallStack => a
undefined
{-# INLINABLE sliceBeginWith #-}
sliceBeginWith ::
(a -> Bool) -> Fold m a b -> Parser m a b
sliceBeginWith :: forall a (m :: * -> *) b. (a -> Bool) -> Fold m a b -> Parser m a b
sliceBeginWith = forall a. HasCallStack => a
undefined
{-# INLINABLE sliceSepByMax #-}
sliceSepByMax :: Monad m
=> (a -> Bool) -> Int -> Fold m a b -> Parser m a b
sliceSepByMax :: forall (m :: * -> *) a b.
Monad m =>
(a -> Bool) -> Int -> Fold m a b -> Parser m a b
sliceSepByMax a -> Bool
predicate Int
cnt (Fold s -> a -> m s
fstep m s
finitial s -> m b
fextract) =
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step m (Tuple' Int s)
initial forall {a}. Tuple' a s -> m b
extract
where
initial :: m (Tuple' Int s)
initial = forall a b. a -> b -> Tuple' a b
Tuple' Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
finitial
step :: Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step (Tuple' Int
i s
r) a
a = do
s
res <- s -> a -> m s
fstep s
r a
a
let i1 :: Int
i1 = Int
i forall a. Num a => a -> a -> a
+ Int
1
s1 :: Tuple' Int s
s1 = forall a b. a -> b -> Tuple' a b
Tuple' Int
i1 s
res
if Bool -> Bool
not (a -> Bool
predicate a
a) Bool -> Bool -> Bool
&& Int
i1 forall a. Ord a => a -> a -> Bool
< Int
cnt
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Yield Int
0 Tuple' Int s
s1
else do
b
b <- s -> m b
fextract s
res
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> b -> Step s b
Stop Int
0 b
b
extract :: Tuple' a s -> m b
extract (Tuple' a
_ s
r) = s -> m b
fextract s
r
{-# INLINABLE wordBy #-}
wordBy ::
(a -> Bool) -> Fold m a b -> Parser m a b
wordBy :: forall a (m :: * -> *) b. (a -> Bool) -> Fold m a b -> Parser m a b
wordBy = forall a. HasCallStack => a
undefined
{-# INLINABLE groupBy #-}
groupBy ::
(a -> a -> Bool) -> Fold m a b -> Parser m a b
groupBy :: forall a (m :: * -> *) b.
(a -> a -> Bool) -> Fold m a b -> Parser m a b
groupBy = forall a. HasCallStack => a
undefined
{-# INLINE eqBy #-}
eqBy :: MonadThrow m => (a -> a -> Bool) -> [a] -> Parser m a ()
eqBy :: forall (m :: * -> *) a.
MonadThrow m =>
(a -> a -> Bool) -> [a] -> Parser m a ()
eqBy a -> a -> Bool
cmp [a]
str = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser forall {m :: * -> *}. Monad m => [a] -> a -> m (Step [a] ())
step m [a]
initial forall {m :: * -> *} {t :: * -> *} {a} {a}.
(MonadThrow m, Foldable t) =>
t a -> m a
extract
where
initial :: m [a]
initial = forall (m :: * -> *) a. Monad m => a -> m a
return [a]
str
step :: [a] -> a -> m (Step [a] ())
step [] a
_ = forall a. HasCallStack => String -> a
error String
"Bug: unreachable"
step [a
x] a
a = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
if a
x a -> a -> Bool
`cmp` a
a
then forall s b. Int -> b -> Step s b
Stop Int
0 ()
else forall s b. String -> Step s b
Error forall a b. (a -> b) -> a -> b
$
String
"eqBy: failed, at the last element"
step (a
x:[a]
xs) a
a = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
if a
x a -> a -> Bool
`cmp` a
a
then forall s b. Int -> s -> Step s b
Skip Int
0 [a]
xs
else forall s b. String -> Step s b
Error forall a b. (a -> b) -> a -> b
$
String
"eqBy: failed, yet to match " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs) forall a. [a] -> [a] -> [a]
++ String
" elements"
extract :: t a -> m a
extract t a
xs = forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError forall a b. (a -> b) -> a -> b
$
String
"eqBy: end of input, yet to match " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall (t :: * -> *) a. Foldable t => t a -> Int
length t a
xs) forall a. [a] -> [a] -> [a]
++ String
" elements"
{-# INLINE lookAhead #-}
lookAhead :: MonadThrow m => Parser m a b -> Parser m a b
lookAhead :: forall (m :: * -> *) a b.
MonadThrow m =>
Parser m a b -> Parser m a b
lookAhead (Parser s -> a -> m (Step s b)
step1 m s
initial1 s -> m b
_) =
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step m (Tuple' Int s)
initial forall {m :: * -> *} {a} {b} {a}.
(MonadThrow m, Show a) =>
Tuple' a b -> m a
extract
where
initial :: m (Tuple' Int s)
initial = forall a b. a -> b -> Tuple' a b
Tuple' Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
initial1
step :: Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step (Tuple' Int
cnt s
st) a
a = do
Step s b
r <- s -> a -> m (Step s b)
step1 s
st a
a
let cnt1 :: Int
cnt1 = Int
cnt forall a. Num a => a -> a -> a
+ Int
1
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s b
r of
Yield Int
_ s
s -> forall s b. Int -> s -> Step s b
Skip Int
0 (forall a b. a -> b -> Tuple' a b
Tuple' Int
cnt1 s
s)
Skip Int
n s
s -> forall s b. Int -> s -> Step s b
Skip Int
n (forall a b. a -> b -> Tuple' a b
Tuple' (Int
cnt1 forall a. Num a => a -> a -> a
- Int
n) s
s)
Stop Int
_ b
b -> forall s b. Int -> b -> Step s b
Stop Int
cnt1 b
b
Error String
err -> forall s b. String -> Step s b
Error String
err
extract :: Tuple' a b -> m a
extract (Tuple' a
n b
_) = forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError forall a b. (a -> b) -> a -> b
$
String
"lookAhead: end of input after consuming " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show a
n forall a. [a] -> [a] -> [a]
++ String
" elements"
{-# INLINE deintercalate #-}
deintercalate ::
Fold m a y -> Parser m x a
-> Fold m b z -> Parser m x b
-> Parser m x (y, z)
deintercalate :: forall (m :: * -> *) a y x b z.
Fold m a y
-> Parser m x a -> Fold m b z -> Parser m x b -> Parser m x (y, z)
deintercalate = forall a. HasCallStack => a
undefined
{-# INLINE sequence #-}
sequence ::
Fold m b c -> t (Parser m a b) -> Parser m a c
sequence :: forall (m :: * -> *) b c (t :: * -> *) a.
Fold m b c -> t (Parser m a b) -> Parser m a c
sequence Fold m b c
_f t (Parser m a b)
_p = forall a. HasCallStack => a
undefined
{-# INLINE choice #-}
choice ::
t (Parser m a b) -> Parser m a b
choice :: forall (t :: * -> *) (m :: * -> *) a b.
t (Parser m a b) -> Parser m a b
choice t (Parser m a b)
_ps = forall a. HasCallStack => a
undefined
{-# INLINE many #-}
many :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
many :: forall (m :: * -> *) b c a.
MonadCatch m =>
Fold m b c -> Parser m a b -> Parser m a c
many = forall (m :: * -> *) b c a.
MonadCatch m =>
Fold m b c -> Parser m a b -> Parser m a c
splitMany
{-# INLINE some #-}
some :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
some :: forall (m :: * -> *) b c a.
MonadCatch m =>
Fold m b c -> Parser m a b -> Parser m a c
some = forall (m :: * -> *) b c a.
MonadCatch m =>
Fold m b c -> Parser m a b -> Parser m a c
splitSome
{-# INLINE countBetween #-}
countBetween ::
Int -> Int -> Fold m b c -> Parser m a b -> Parser m a c
countBetween :: forall (m :: * -> *) b c a.
Int -> Int -> Fold m b c -> Parser m a b -> Parser m a c
countBetween Int
_m Int
_n Fold m b c
_f = forall a. HasCallStack => a
undefined
{-# INLINE count #-}
count ::
Int -> Fold m b c -> Parser m a b -> Parser m a c
count :: forall (m :: * -> *) b c a.
Int -> Fold m b c -> Parser m a b -> Parser m a c
count Int
n = forall (m :: * -> *) b c a.
Int -> Int -> Fold m b c -> Parser m a b -> Parser m a c
countBetween Int
n Int
n
data ManyTillState fs sr sl = ManyTillR Int fs sr | ManyTillL fs sl
{-# INLINE manyTill #-}
manyTill :: MonadCatch m
=> Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c
manyTill :: forall (m :: * -> *) b c a x.
MonadCatch m =>
Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c
manyTill (Fold s -> b -> m s
fstep m s
finitial s -> m c
fextract)
(Parser s -> a -> m (Step s b)
stepL m s
initialL s -> m b
extractL)
(Parser s -> a -> m (Step s x)
stepR m s
initialR s -> m x
_) =
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser ManyTillState s s s -> a -> m (Step (ManyTillState s s s) c)
step forall {sl}. m (ManyTillState s s sl)
initial forall {sr}. ManyTillState s sr s -> m c
extract
where
initial :: m (ManyTillState s s sl)
initial = do
s
fs <- m s
finitial
forall fs sr sl. Int -> fs -> sr -> ManyTillState fs sr sl
ManyTillR Int
0 s
fs forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
initialR
step :: ManyTillState s s s -> a -> m (Step (ManyTillState s s s) c)
step (ManyTillR Int
cnt s
fs s
st) a
a = do
Step s x
r <- s -> a -> m (Step s x)
stepR s
st a
a
case Step s x
r of
Yield Int
n s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Yield Int
n (forall fs sr sl. Int -> fs -> sr -> ManyTillState fs sr sl
ManyTillR Int
0 s
fs s
s)
Skip Int
n s
s -> do
forall a. HasCallStack => Bool -> a -> a
assert (Int
cnt forall a. Num a => a -> a -> a
+ Int
1 forall a. Num a => a -> a -> a
- Int
n forall a. Ord a => a -> a -> Bool
>= Int
0) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Skip Int
n (forall fs sr sl. Int -> fs -> sr -> ManyTillState fs sr sl
ManyTillR (Int
cnt forall a. Num a => a -> a -> a
+ Int
1 forall a. Num a => a -> a -> a
- Int
n) s
fs s
s)
Stop Int
n x
_ -> do
c
b <- s -> m c
fextract s
fs
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> b -> Step s b
Stop Int
n c
b
Error String
_ -> do
s
rR <- m s
initialL
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Skip (Int
cnt forall a. Num a => a -> a -> a
+ Int
1) (forall fs sr sl. fs -> sl -> ManyTillState fs sr sl
ManyTillL s
fs s
rR)
step (ManyTillL s
fs s
st) a
a = do
Step s b
r <- s -> a -> m (Step s b)
stepL s
st a
a
case Step s b
r of
Yield Int
n s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Yield Int
n (forall fs sr sl. fs -> sl -> ManyTillState fs sr sl
ManyTillL s
fs s
s)
Skip Int
n s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Skip Int
n (forall fs sr sl. fs -> sl -> ManyTillState fs sr sl
ManyTillL s
fs s
s)
Stop Int
n b
b -> do
s
fs1 <- s -> b -> m s
fstep s
fs b
b
s
l <- m s
initialR
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Skip Int
n (forall fs sr sl. Int -> fs -> sr -> ManyTillState fs sr sl
ManyTillR Int
0 s
fs1 s
l)
Error String
err -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. String -> Step s b
Error String
err
extract :: ManyTillState s sr s -> m c
extract (ManyTillL s
fs s
sR) = s -> m b
extractL s
sR forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> b -> m s
fstep s
fs forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> m c
fextract
extract (ManyTillR Int
_ s
fs sr
_) = s -> m c
fextract s
fs