{-# OPTIONS_GHC -Wno-deprecations #-}
module Streamly.Internal.Data.Array.Stream
(
Array.chunksOf
, Array.pinnedChunksOf
, Array.bufferChunks
, concat
, Array.flattenArrays
, concatRev
, Array.flattenArraysRev
, interpose
, interposeSuffix
, intercalateSuffix
, unlines
, foldBreak
, foldBreakD
, parseBreak
, K.parseBreakChunks
, K.parseChunks
, runArrayFold
, runArrayFoldBreak
, runArrayParserDBreak
, runArrayFoldMany
, toArray
, lpackArraysChunksOf
, compact
, splitOn
, splitOnSuffix
)
where
#include "ArrayMacros.h"
#include "inline.hs"
import Data.Bifunctor (second)
import Control.Exception (assert)
import Control.Monad.IO.Class (MonadIO(..))
import Data.Proxy (Proxy(..))
import Data.Word (Word8)
import Streamly.Internal.Data.Unbox (Unbox(..))
import Fusion.Plugin.Types (Fuse(..))
import GHC.Exts (SpecConstrAnnotation(..))
import GHC.Types (SPEC(..))
import Prelude hiding (null, last, (!!), read, concat, unlines)
import Streamly.Data.Fold (Fold)
import Streamly.Internal.Data.Array.Type (Array(..))
import Streamly.Internal.Data.Fold.Chunked (ChunkFold(..))
import Streamly.Internal.Data.Parser (ParseError(..))
import Streamly.Internal.Data.Stream (Stream)
import Streamly.Internal.Data.StreamK (StreamK, fromStream, toStream)
import Streamly.Internal.Data.SVar.Type (adaptState, defState)
import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
import qualified Streamly.Data.Fold as FL
import qualified Streamly.Internal.Data.Array as A
import qualified Streamly.Internal.Data.Array as Array
import qualified Streamly.Internal.Data.MutArray.Type as MA
import qualified Streamly.Internal.Data.MutArray.Stream as AS
import qualified Streamly.Internal.Data.Fold.Type as FL (Fold(..), Step(..))
import qualified Streamly.Internal.Data.Parser as PR
import qualified Streamly.Internal.Data.Parser as PRD
(Parser(..), Initial(..))
import qualified Streamly.Internal.Data.Stream as D
import qualified Streamly.Internal.Data.StreamK as K
{-# INLINE concat #-}
concat :: (Monad m, Unbox a) => Stream m (Array a) -> Stream m a
concat :: forall (m :: * -> *) a.
(Monad m, Unbox a) =>
Stream m (Array a) -> Stream m a
concat = forall (m :: * -> *) a b.
Monad m =>
Unfold m a b -> Stream m a -> Stream m b
D.unfoldMany forall (m :: * -> *) a. (Monad m, Unbox a) => Unfold m (Array a) a
A.reader
{-# INLINE concatRev #-}
concatRev :: (Monad m, Unbox a) => Stream m (Array a) -> Stream m a
concatRev :: forall (m :: * -> *) a.
(Monad m, Unbox a) =>
Stream m (Array a) -> Stream m a
concatRev = forall (m :: * -> *) a b.
Monad m =>
Unfold m a b -> Stream m a -> Stream m b
D.unfoldMany forall (m :: * -> *) a. (Monad m, Unbox a) => Unfold m (Array a) a
A.readerRev
{-# INLINE interpose #-}
interpose :: (Monad m, Unbox a) => a -> Stream m (Array a) -> Stream m a
interpose :: forall (m :: * -> *) a.
(Monad m, Unbox a) =>
a -> Stream m (Array a) -> Stream m a
interpose a
x = forall (m :: * -> *) c b.
Monad m =>
c -> Unfold m b c -> Stream m b -> Stream m c
D.interpose a
x forall (m :: * -> *) a. (Monad m, Unbox a) => Unfold m (Array a) a
A.reader
{-# INLINE intercalateSuffix #-}
intercalateSuffix :: (Monad m, Unbox a)
=> Array a -> Stream m (Array a) -> Stream m a
intercalateSuffix :: forall (m :: * -> *) a.
(Monad m, Unbox a) =>
Array a -> Stream m (Array a) -> Stream m a
intercalateSuffix = forall (m :: * -> *) b c.
Monad m =>
Unfold m b c -> b -> Stream m b -> Stream m c
D.intercalateSuffix forall (m :: * -> *) a. (Monad m, Unbox a) => Unfold m (Array a) a
A.reader
{-# INLINE interposeSuffix #-}
interposeSuffix :: (Monad m, Unbox a)
=> a -> Stream m (Array a) -> Stream m a
interposeSuffix :: forall (m :: * -> *) a.
(Monad m, Unbox a) =>
a -> Stream m (Array a) -> Stream m a
interposeSuffix a
x = forall (m :: * -> *) c b.
Monad m =>
c -> Unfold m b c -> Stream m b -> Stream m c
D.interposeSuffix a
x forall (m :: * -> *) a. (Monad m, Unbox a) => Unfold m (Array a) a
A.reader
data FlattenState s =
OuterLoop s
| InnerLoop s !MA.MutableByteArray !Int !Int
{-# INLINE_NORMAL unlines #-}
unlines :: forall m a. (MonadIO m, Unbox a)
=> a -> D.Stream m (Array a) -> D.Stream m a
unlines :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
a -> Stream m (Array a) -> Stream m a
unlines a
sep (D.Stream State StreamK m (Array a) -> s -> m (Step s (Array a))
step s
state) = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {m :: * -> *} {a}.
State StreamK m a -> FlattenState s -> m (Step (FlattenState s) a)
step' (forall s. s -> FlattenState s
OuterLoop s
state)
where
{-# INLINE_LATE step' #-}
step' :: State StreamK m a -> FlattenState s -> m (Step (FlattenState s) a)
step' State StreamK m a
gst (OuterLoop s
st) = do
Step s (Array a)
r <- State StreamK m (Array a) -> s -> m (Step s (Array a))
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s (Array a)
r of
D.Yield Array{Int
MutByteArray
arrEnd :: forall a. Array a -> Int
arrStart :: forall a. Array a -> Int
arrContents :: forall a. Array a -> MutByteArray
arrEnd :: Int
arrStart :: Int
arrContents :: MutByteArray
..} s
s ->
forall s a. s -> Step s a
D.Skip (forall s. s -> MutByteArray -> Int -> Int -> FlattenState s
InnerLoop s
s MutByteArray
arrContents Int
arrStart Int
arrEnd)
D.Skip s
s -> forall s a. s -> Step s a
D.Skip (forall s. s -> FlattenState s
OuterLoop s
s)
Step s (Array a)
D.Stop -> forall s a. Step s a
D.Stop
step' State StreamK m a
_ (InnerLoop s
st MutByteArray
_ Int
p Int
end) | Int
p forall a. Eq a => a -> a -> Bool
== Int
end =
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield a
sep forall a b. (a -> b) -> a -> b
$ forall s. s -> FlattenState s
OuterLoop s
st
step' State StreamK m a
_ (InnerLoop s
st MutByteArray
contents Int
p Int
end) = do
a
x <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => Int -> MutByteArray -> IO a
peekByteIndex Int
p MutByteArray
contents
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield a
x (forall s. s -> MutByteArray -> Int -> Int -> FlattenState s
InnerLoop s
st MutByteArray
contents (INDEX_NEXT(p,a)) end)
{-# INLINE_NORMAL packArraysChunksOf #-}
packArraysChunksOf :: (MonadIO m, Unbox a)
=> Int -> D.Stream m (Array a) -> D.Stream m (Array a)
packArraysChunksOf :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m (Array a) -> Stream m (Array a)
packArraysChunksOf Int
n Stream m (Array a)
str =
forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
D.map forall a. MutArray a -> Array a
A.unsafeFreeze forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m (MutArray a) -> Stream m (MutArray a)
AS.packArraysChunksOf Int
n forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
D.map forall a. Array a -> MutArray a
A.unsafeThaw Stream m (Array a)
str
{-# INLINE_NORMAL lpackArraysChunksOf #-}
lpackArraysChunksOf :: (MonadIO m, Unbox a)
=> Int -> Fold m (Array a) () -> Fold m (Array a) ()
lpackArraysChunksOf :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m (Array a) () -> Fold m (Array a) ()
lpackArraysChunksOf Int
n Fold m (Array a) ()
fld =
forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
FL.lmap forall a. Array a -> MutArray a
A.unsafeThaw forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m (MutArray a) () -> Fold m (MutArray a) ()
AS.lpackArraysChunksOf Int
n (forall a b (m :: * -> *) r. (a -> b) -> Fold m b r -> Fold m a r
FL.lmap forall a. MutArray a -> Array a
A.unsafeFreeze Fold m (Array a) ()
fld)
{-# INLINE compact #-}
compact :: (MonadIO m, Unbox a)
=> Int -> Stream m (Array a) -> Stream m (Array a)
compact :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m (Array a) -> Stream m (Array a)
compact = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m (Array a) -> Stream m (Array a)
packArraysChunksOf
data SplitState s arr
= Initial s
| Buffering s arr
| Splitting s arr
| Yielding arr (SplitState s arr)
| Finishing
{-# INLINE_NORMAL _splitOn #-}
_splitOn
:: MonadIO m
=> Word8
-> D.Stream m (Array Word8)
-> D.Stream m (Array Word8)
_splitOn :: forall (m :: * -> *).
MonadIO m =>
Word8 -> Stream m (Array Word8) -> Stream m (Array Word8)
_splitOn Word8
byte (D.Stream State StreamK m (Array Word8) -> s -> m (Step s (Array Word8))
step s
state) = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State StreamK m (Array Word8)
-> SplitState s (Array Word8)
-> m (Step (SplitState s (Array Word8)) (Array Word8))
step' (forall s arr. s -> SplitState s arr
Initial s
state)
where
{-# INLINE_LATE step' #-}
step' :: State StreamK m (Array Word8)
-> SplitState s (Array Word8)
-> m (Step (SplitState s (Array Word8)) (Array Word8))
step' State StreamK m (Array Word8)
gst (Initial s
st) = do
Step s (Array Word8)
r <- State StreamK m (Array Word8) -> s -> m (Step s (Array Word8))
step State StreamK m (Array Word8)
gst s
st
case Step s (Array Word8)
r of
D.Yield Array Word8
arr s
s -> do
(Array Word8
arr1, Maybe (Array Word8)
marr2) <- forall (m :: * -> *).
MonadIO m =>
Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
A.breakOn Word8
byte Array Word8
arr
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Maybe (Array Word8)
marr2 of
Maybe (Array Word8)
Nothing -> forall s a. s -> Step s a
D.Skip (forall s arr. s -> arr -> SplitState s arr
Buffering s
s Array Word8
arr1)
Just Array Word8
arr2 -> forall s a. s -> Step s a
D.Skip (forall s arr. arr -> SplitState s arr -> SplitState s arr
Yielding Array Word8
arr1 (forall s arr. s -> arr -> SplitState s arr
Splitting s
s Array Word8
arr2))
D.Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip (forall s arr. s -> SplitState s arr
Initial s
s)
Step s (Array Word8)
D.Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
D.Stop
step' State StreamK m (Array Word8)
gst (Buffering s
st Array Word8
buf) = do
Step s (Array Word8)
r <- State StreamK m (Array Word8) -> s -> m (Step s (Array Word8))
step State StreamK m (Array Word8)
gst s
st
case Step s (Array Word8)
r of
D.Yield Array Word8
arr s
s -> do
(Array Word8
arr1, Maybe (Array Word8)
marr2) <- forall (m :: * -> *).
MonadIO m =>
Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
A.breakOn Word8
byte Array Word8
arr
Array Word8
buf' <- forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Array a -> Array a -> m (Array a)
A.splice Array Word8
buf Array Word8
arr1
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Maybe (Array Word8)
marr2 of
Maybe (Array Word8)
Nothing -> forall s a. s -> Step s a
D.Skip (forall s arr. s -> arr -> SplitState s arr
Buffering s
s Array Word8
buf')
Just Array Word8
x -> forall s a. s -> Step s a
D.Skip (forall s arr. arr -> SplitState s arr -> SplitState s arr
Yielding Array Word8
buf' (forall s arr. s -> arr -> SplitState s arr
Splitting s
s Array Word8
x))
D.Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip (forall s arr. s -> arr -> SplitState s arr
Buffering s
s Array Word8
buf)
Step s (Array Word8)
D.Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
if forall a. Array a -> Int
A.byteLength Array Word8
buf forall a. Eq a => a -> a -> Bool
== Int
0
then forall s a. Step s a
D.Stop
else forall s a. s -> Step s a
D.Skip (forall s arr. arr -> SplitState s arr -> SplitState s arr
Yielding Array Word8
buf forall s arr. SplitState s arr
Finishing)
step' State StreamK m (Array Word8)
_ (Splitting s
st Array Word8
buf) = do
(Array Word8
arr1, Maybe (Array Word8)
marr2) <- forall (m :: * -> *).
MonadIO m =>
Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
A.breakOn Word8
byte Array Word8
buf
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Maybe (Array Word8)
marr2 of
Maybe (Array Word8)
Nothing -> forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall s arr. s -> arr -> SplitState s arr
Buffering s
st Array Word8
arr1
Just Array Word8
arr2 -> forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall s arr. arr -> SplitState s arr -> SplitState s arr
Yielding Array Word8
arr1 (forall s arr. s -> arr -> SplitState s arr
Splitting s
st Array Word8
arr2)
step' State StreamK m (Array Word8)
_ (Yielding Array Word8
arr SplitState s (Array Word8)
next) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield Array Word8
arr SplitState s (Array Word8)
next
step' State StreamK m (Array Word8)
_ SplitState s (Array Word8)
Finishing = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
D.Stop
{-# INLINE splitOn #-}
splitOn
:: (MonadIO m)
=> Word8
-> Stream m (Array Word8)
-> Stream m (Array Word8)
splitOn :: forall (m :: * -> *).
MonadIO m =>
Word8 -> Stream m (Array Word8) -> Stream m (Array Word8)
splitOn Word8
byte = forall (m :: * -> *) (f :: * -> *) a.
Monad m =>
(f a -> m (f a, Maybe (f a)))
-> (f a -> f a -> m (f a)) -> Stream m (f a) -> Stream m (f a)
D.splitInnerBy (forall (m :: * -> *).
MonadIO m =>
Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
A.breakOn Word8
byte) forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Array a -> Array a -> m (Array a)
A.splice
{-# INLINE splitOnSuffix #-}
splitOnSuffix
:: (MonadIO m)
=> Word8
-> Stream m (Array Word8)
-> Stream m (Array Word8)
splitOnSuffix :: forall (m :: * -> *).
MonadIO m =>
Word8 -> Stream m (Array Word8) -> Stream m (Array Word8)
splitOnSuffix Word8
byte = forall (m :: * -> *) (f :: * -> *) a.
(Monad m, Eq (f a), Monoid (f a)) =>
(f a -> m (f a, Maybe (f a)))
-> (f a -> f a -> m (f a)) -> Stream m (f a) -> Stream m (f a)
D.splitInnerBySuffix (forall (m :: * -> *).
MonadIO m =>
Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
A.breakOn Word8
byte) forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Array a -> Array a -> m (Array a)
A.splice
{-# INLINE_NORMAL foldBreakD #-}
foldBreakD :: forall m a b. (MonadIO m, Unbox a) =>
Fold m a b -> D.Stream m (Array a) -> m (b, D.Stream m (Array a))
foldBreakD :: forall (m :: * -> *) a b.
(MonadIO m, Unbox a) =>
Fold m a b -> Stream m (Array a) -> m (b, Stream m (Array a))
foldBreakD (FL.Fold s -> a -> m (Step s b)
fstep m (Step s b)
initial s -> m b
_ s -> m b
final) stream :: Stream m (Array a)
stream@(D.Stream State StreamK m (Array a) -> s -> m (Step s (Array a))
step s
state) = do
Step s b
res <- m (Step s b)
initial
case Step s b
res of
FL.Partial s
fs -> SPEC -> s -> s -> m (b, Stream m (Array a))
go SPEC
SPEC s
state s
fs
FL.Done b
fb -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (b
fb, Stream m (Array a)
stream)
where
{-# INLINE go #-}
go :: SPEC -> s -> s -> m (b, Stream m (Array a))
go !SPEC
_ s
st !s
fs = do
Step s (Array a)
r <- State StreamK m (Array a) -> s -> m (Step s (Array a))
step forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st
case Step s (Array a)
r of
D.Yield (Array MutByteArray
contents Int
start Int
end) s
s ->
let fp :: Tuple' Int MutByteArray
fp = forall a b. a -> b -> Tuple' a b
Tuple' Int
end MutByteArray
contents
in SPEC
-> s
-> Tuple' Int MutByteArray
-> Int
-> s
-> m (b, Stream m (Array a))
goArray SPEC
SPEC s
s Tuple' Int MutByteArray
fp Int
start s
fs
D.Skip s
s -> SPEC -> s -> s -> m (b, Stream m (Array a))
go SPEC
SPEC s
s s
fs
Step s (Array a)
D.Stop -> do
b
b <- s -> m b
final s
fs
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, forall (m :: * -> *) a. Applicative m => Stream m a
D.nil)
goArray :: SPEC
-> s
-> Tuple' Int MutByteArray
-> Int
-> s
-> m (b, Stream m (Array a))
goArray !SPEC
_ s
s (Tuple' Int
end MutByteArray
_) !Int
cur !s
fs
| Int
cur forall a. Eq a => a -> a -> Bool
== Int
end = do
SPEC -> s -> s -> m (b, Stream m (Array a))
go SPEC
SPEC s
s s
fs
goArray !SPEC
_ s
st fp :: Tuple' Int MutByteArray
fp@(Tuple' Int
end MutByteArray
contents) !Int
cur !s
fs = do
a
x <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => Int -> MutByteArray -> IO a
peekByteIndex Int
cur MutByteArray
contents
Step s b
res <- s -> a -> m (Step s b)
fstep s
fs a
x
let next :: Int
next = INDEX_NEXT(cur,a)
case Step s b
res of
FL.Done b
b -> do
let arr :: Array a
arr = forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
next Int
end
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (b
b, forall (m :: * -> *) a.
Applicative m =>
a -> Stream m a -> Stream m a
D.cons forall {a}. Array a
arr (forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State StreamK m (Array a) -> s -> m (Step s (Array a))
step s
st))
FL.Partial s
fs1 -> SPEC
-> s
-> Tuple' Int MutByteArray
-> Int
-> s
-> m (b, Stream m (Array a))
goArray SPEC
SPEC s
st Tuple' Int MutByteArray
fp Int
next s
fs1
{-# INLINE_NORMAL foldBreakK #-}
foldBreakK :: forall m a b. (MonadIO m, Unbox a) =>
Fold m a b -> K.StreamK m (Array a) -> m (b, K.StreamK m (Array a))
foldBreakK :: forall (m :: * -> *) a b.
(MonadIO m, Unbox a) =>
Fold m a b -> StreamK m (Array a) -> m (b, StreamK m (Array a))
foldBreakK (FL.Fold s -> a -> m (Step s b)
fstep m (Step s b)
initial s -> m b
_ s -> m b
final) StreamK m (Array a)
stream = do
Step s b
res <- m (Step s b)
initial
case Step s b
res of
FL.Partial s
fs -> forall {a}. s -> StreamK m (Array a) -> m (b, StreamK m (Array a))
go s
fs StreamK m (Array a)
stream
FL.Done b
fb -> forall (m :: * -> *) a. Monad m => a -> m a
return (b
fb, StreamK m (Array a)
stream)
where
{-# INLINE go #-}
go :: s -> StreamK m (Array a) -> m (b, StreamK m (Array a))
go !s
fs StreamK m (Array a)
st = do
let stop :: m (b, StreamK m a)
stop = (, forall (m :: * -> *) a. StreamK m a
K.nil) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
final s
fs
single :: Array a -> m (b, StreamK m (Array a))
single Array a
a = forall {a}.
Array a -> StreamK m (Array a) -> m (b, StreamK m (Array a))
yieldk Array a
a forall (m :: * -> *) a. StreamK m a
K.nil
yieldk :: Array a -> StreamK m (Array a) -> m (b, StreamK m (Array a))
yieldk (Array MutByteArray
contents Int
start Int
end) StreamK m (Array a)
r =
let fp :: Tuple' Int MutByteArray
fp = forall a b. a -> b -> Tuple' a b
Tuple' Int
end MutByteArray
contents
in s
-> StreamK m (Array a)
-> Tuple' Int MutByteArray
-> Int
-> m (b, StreamK m (Array a))
goArray s
fs StreamK m (Array a)
r Tuple' Int MutByteArray
fp Int
start
in forall (m :: * -> *) a r.
State StreamK m a
-> (a -> StreamK m a -> m r)
-> (a -> m r)
-> m r
-> StreamK m a
-> m r
K.foldStream forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState forall {a}.
Array a -> StreamK m (Array a) -> m (b, StreamK m (Array a))
yieldk forall {a}. Array a -> m (b, StreamK m (Array a))
single forall {m :: * -> *} {a}. m (b, StreamK m a)
stop StreamK m (Array a)
st
goArray :: s
-> StreamK m (Array a)
-> Tuple' Int MutByteArray
-> Int
-> m (b, StreamK m (Array a))
goArray !s
fs StreamK m (Array a)
st (Tuple' Int
end MutByteArray
_) !Int
cur
| Int
cur forall a. Eq a => a -> a -> Bool
== Int
end = do
s -> StreamK m (Array a) -> m (b, StreamK m (Array a))
go s
fs StreamK m (Array a)
st
goArray !s
fs StreamK m (Array a)
st fp :: Tuple' Int MutByteArray
fp@(Tuple' Int
end MutByteArray
contents) !Int
cur = do
a
x <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => Int -> MutByteArray -> IO a
peekByteIndex Int
cur MutByteArray
contents
Step s b
res <- s -> a -> m (Step s b)
fstep s
fs a
x
let next :: Int
next = INDEX_NEXT(cur,a)
case Step s b
res of
FL.Done b
b -> do
let arr :: Array a
arr = forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
next Int
end
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (b
b, forall a (m :: * -> *). a -> StreamK m a -> StreamK m a
K.cons forall {a}. Array a
arr StreamK m (Array a)
st)
FL.Partial s
fs1 -> s
-> StreamK m (Array a)
-> Tuple' Int MutByteArray
-> Int
-> m (b, StreamK m (Array a))
goArray s
fs1 StreamK m (Array a)
st Tuple' Int MutByteArray
fp Int
next
{-# INLINE_NORMAL foldBreak #-}
foldBreak ::
(MonadIO m, Unbox a)
=> Fold m a b
-> StreamK m (A.Array a)
-> m (b, StreamK m (A.Array a))
foldBreak :: forall (m :: * -> *) a b.
(MonadIO m, Unbox a) =>
Fold m a b -> StreamK m (Array a) -> m (b, StreamK m (Array a))
foldBreak = forall (m :: * -> *) a b.
(MonadIO m, Unbox a) =>
Fold m a b -> StreamK m (Array a) -> m (b, StreamK m (Array a))
foldBreakK
{-# INLINE takeArrayListRev #-}
takeArrayListRev :: forall a. Unbox a => Int -> [Array a] -> [Array a]
takeArrayListRev :: forall a. Unbox a => Int -> [Array a] -> [Array a]
takeArrayListRev = forall a. Unbox a => Int -> [Array a] -> [Array a]
go
where
go :: Int -> [Array a] -> [Array a]
go Int
_ [] = []
go Int
n [Array a]
_ | Int
n forall a. Ord a => a -> a -> Bool
<= Int
0 = []
go Int
n (Array a
x:[Array a]
xs) =
let len :: Int
len = forall a. Unbox a => Array a -> Int
Array.length Array a
x
in if Int
n forall a. Ord a => a -> a -> Bool
> Int
len
then Array a
x forall a. a -> [a] -> [a]
: Int -> [Array a] -> [Array a]
go (Int
n forall a. Num a => a -> a -> a
- Int
len) [Array a]
xs
else if Int
n forall a. Eq a => a -> a -> Bool
== Int
len
then [Array a
x]
else let !(Array MutByteArray
contents Int
_ Int
end) = Array a
x
!start :: Int
start = Int
end forall a. Num a => a -> a -> a
- (Int
n forall a. Num a => a -> a -> a
* SIZE_OF(a))
in [forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
start Int
end]
{-# INLINE splitAtArrayListRev #-}
splitAtArrayListRev ::
forall a. Unbox a => Int -> [Array a] -> ([Array a],[Array a])
splitAtArrayListRev :: forall a. Unbox a => Int -> [Array a] -> ([Array a], [Array a])
splitAtArrayListRev Int
n [Array a]
ls
| Int
n forall a. Ord a => a -> a -> Bool
<= Int
0 = ([], [Array a]
ls)
| Bool
otherwise = Int -> [Array a] -> ([Array a], [Array a])
go Int
n [Array a]
ls
where
go :: Int -> [Array a] -> ([Array a], [Array a])
go :: Int -> [Array a] -> ([Array a], [Array a])
go Int
_ [] = ([], [])
go Int
m (Array a
x:[Array a]
xs) =
let len :: Int
len = forall a. Unbox a => Array a -> Int
Array.length Array a
x
([Array a]
xs', [Array a]
xs'') = Int -> [Array a] -> ([Array a], [Array a])
go (Int
m forall a. Num a => a -> a -> a
- Int
len) [Array a]
xs
in if Int
m forall a. Ord a => a -> a -> Bool
> Int
len
then (Array a
xforall a. a -> [a] -> [a]
:[Array a]
xs', [Array a]
xs'')
else if Int
m forall a. Eq a => a -> a -> Bool
== Int
len
then ([Array a
x],[Array a]
xs)
else let !(Array MutByteArray
contents Int
start Int
end) = Array a
x
end1 :: Int
end1 = Int
end forall a. Num a => a -> a -> a
- (Int
m forall a. Num a => a -> a -> a
* SIZE_OF(a))
arr2 :: Array a
arr2 = forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
start Int
end1
arr1 :: Array a
arr1 = forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
end1 Int
end
in ([forall {a}. Array a
arr1], forall {a}. Array a
arr2forall a. a -> [a] -> [a]
:[Array a]
xs)
{-# INLINE spliceArraysRealloced #-}
spliceArraysRealloced :: forall m a. (MonadIO m, Unbox a)
=> Stream m (Array a) -> m (Array a)
spliceArraysRealloced :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m (Array a) -> m (Array a)
spliceArraysRealloced Stream m (Array a)
s = do
Maybe (Array a, Stream m (Array a))
res <- forall (m :: * -> *) a.
Monad m =>
Stream m a -> m (Maybe (a, Stream m a))
D.uncons Stream m (Array a)
s
case Maybe (Array a, Stream m (Array a))
res of
Just (Array a
a, Stream m (Array a)
strm) -> do
MutArray a
arr <-
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Stream m a -> m b
D.foldlM'
forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> MutArray a -> m (MutArray a)
MA.spliceExp
(forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a. Array a -> MutArray a
A.unsafeThaw Array a
a))
(forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Array a -> MutArray a
A.unsafeThaw Stream m (Array a)
strm)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. MutArray a -> Array a
A.unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> m (MutArray a)
MA.rightSize MutArray a
arr
Maybe (Array a, Stream m (Array a))
Nothing -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall {a}. Array a
A.nil
{-# INLINE toArray #-}
toArray :: (MonadIO m, Unbox a) => Stream m (Array a) -> m (Array a)
toArray :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m (Array a) -> m (Array a)
toArray = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m (Array a) -> m (Array a)
spliceArraysRealloced
{-# ANN type List NoSpecConstr #-}
newtype List a = List {forall a. List a -> [a]
getList :: [a]}
{-# INLINE_NORMAL parseBreakK #-}
parseBreakK ::
forall m a b. (MonadIO m, Unbox a)
=> PRD.Parser a m b
-> K.StreamK m (Array.Array a)
-> m (Either ParseError b, K.StreamK m (Array.Array a))
parseBreakK :: forall (m :: * -> *) a b.
(MonadIO m, Unbox a) =>
Parser a m b
-> StreamK m (Array a)
-> m (Either ParseError b, StreamK m (Array a))
parseBreakK (PRD.Parser s -> a -> m (Step s b)
pstep m (Initial s b)
initial s -> m (Step s b)
extract) StreamK m (Array a)
stream = do
Initial s b
res <- m (Initial s b)
initial
case Initial s b
res of
PRD.IPartial s
s -> s
-> StreamK m (Array a)
-> [a]
-> m (Either ParseError b, StreamK m (Array a))
go s
s StreamK m (Array a)
stream []
PRD.IDone b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right b
b, StreamK m (Array a)
stream)
PRD.IError String
err -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left (String -> ParseError
ParseError String
err), StreamK m (Array a)
stream)
where
go :: s
-> StreamK m (Array a)
-> [a]
-> m (Either ParseError b, StreamK m (Array a))
go !s
pst StreamK m (Array a)
st [a]
backBuf = do
let stop :: m (Either ParseError b, StreamK m (Array a))
stop = s -> [a] -> m (Either ParseError b, StreamK m (Array a))
goStop s
pst [a]
backBuf
single :: Array a -> m (Either ParseError b, StreamK m (Array a))
single Array a
a = Array a
-> StreamK m (Array a)
-> m (Either ParseError b, StreamK m (Array a))
yieldk Array a
a forall (m :: * -> *) a. StreamK m a
K.nil
yieldk :: Array a
-> StreamK m (Array a)
-> m (Either ParseError b, StreamK m (Array a))
yieldk Array a
arr StreamK m (Array a)
r = s
-> [a]
-> StreamK m (Array a)
-> Array a
-> m (Either ParseError b, StreamK m (Array a))
goArray s
pst [a]
backBuf StreamK m (Array a)
r Array a
arr
in forall (m :: * -> *) a r.
State StreamK m a
-> (a -> StreamK m a -> m r)
-> (a -> m r)
-> m r
-> StreamK m a
-> m r
K.foldStream forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState Array a
-> StreamK m (Array a)
-> m (Either ParseError b, StreamK m (Array a))
yieldk Array a -> m (Either ParseError b, StreamK m (Array a))
single m (Either ParseError b, StreamK m (Array a))
stop StreamK m (Array a)
st
goArray :: s
-> [a]
-> StreamK m (Array a)
-> Array a
-> m (Either ParseError b, StreamK m (Array a))
goArray !s
pst [a]
backBuf StreamK m (Array a)
st (Array MutByteArray
_ Int
cur Int
end) | Int
cur forall a. Eq a => a -> a -> Bool
== Int
end = s
-> StreamK m (Array a)
-> [a]
-> m (Either ParseError b, StreamK m (Array a))
go s
pst StreamK m (Array a)
st [a]
backBuf
goArray !s
pst [a]
backBuf StreamK m (Array a)
st (Array MutByteArray
contents Int
cur Int
end) = do
a
x <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => Int -> MutByteArray -> IO a
peekByteIndex Int
cur MutByteArray
contents
Step s b
pRes <- s -> a -> m (Step s b)
pstep s
pst a
x
let next :: Int
next = INDEX_NEXT(cur,a)
case Step s b
pRes of
PR.Partial Int
0 s
s ->
s
-> [a]
-> StreamK m (Array a)
-> Array a
-> m (Either ParseError b, StreamK m (Array a))
goArray s
s [] StreamK m (Array a)
st (forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
next Int
end)
PR.Partial Int
n s
s -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length (a
xforall a. a -> [a] -> [a]
:[a]
backBuf)) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
let src0 :: [a]
src0 = forall a. Int -> [a] -> [a]
Prelude.take Int
n (a
xforall a. a -> [a] -> [a]
:[a]
backBuf)
arr0 :: Array a
arr0 = forall a. Unbox a => Int -> [a] -> Array a
A.fromListN Int
n (forall a. [a] -> [a]
Prelude.reverse [a]
src0)
arr1 :: Array a
arr1 = forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
next Int
end
src :: Array a
src = Array a
arr0 forall a. Semigroup a => a -> a -> a
<> forall {a}. Array a
arr1
s
-> [a]
-> StreamK m (Array a)
-> Array a
-> m (Either ParseError b, StreamK m (Array a))
goArray s
s [] StreamK m (Array a)
st Array a
src
PR.Continue Int
0 s
s ->
s
-> [a]
-> StreamK m (Array a)
-> Array a
-> m (Either ParseError b, StreamK m (Array a))
goArray s
s (a
xforall a. a -> [a] -> [a]
:[a]
backBuf) StreamK m (Array a)
st (forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
next Int
end)
PR.Continue Int
n s
s -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length (a
xforall a. a -> [a] -> [a]
:[a]
backBuf)) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
let ([a]
src0, [a]
buf1) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
n (a
xforall a. a -> [a] -> [a]
:[a]
backBuf)
arr0 :: Array a
arr0 = forall a. Unbox a => Int -> [a] -> Array a
A.fromListN Int
n (forall a. [a] -> [a]
Prelude.reverse [a]
src0)
arr1 :: Array a
arr1 = forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
next Int
end
src :: Array a
src = Array a
arr0 forall a. Semigroup a => a -> a -> a
<> forall {a}. Array a
arr1
s
-> [a]
-> StreamK m (Array a)
-> Array a
-> m (Either ParseError b, StreamK m (Array a))
goArray s
s [a]
buf1 StreamK m (Array a)
st Array a
src
PR.Done Int
0 b
b -> do
let arr :: Array a
arr = forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
next Int
end
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right b
b, forall a (m :: * -> *). a -> StreamK m a -> StreamK m a
K.cons forall {a}. Array a
arr StreamK m (Array a)
st)
PR.Done Int
n b
b -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length (a
xforall a. a -> [a] -> [a]
:[a]
backBuf)) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
let src0 :: [a]
src0 = forall a. Int -> [a] -> [a]
Prelude.take Int
n (a
xforall a. a -> [a] -> [a]
:[a]
backBuf)
arr0 :: Array a
arr0 = forall a. Unbox a => Int -> [a] -> Array a
A.fromListN Int
n (forall a. [a] -> [a]
Prelude.reverse [a]
src0)
arr1 :: Array a
arr1 = forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
next Int
end
str :: StreamK m (Array a)
str = forall a (m :: * -> *). a -> StreamK m a -> StreamK m a
K.cons Array a
arr0 (forall a (m :: * -> *). a -> StreamK m a -> StreamK m a
K.cons forall {a}. Array a
arr1 StreamK m (Array a)
st)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right b
b, StreamK m (Array a)
str)
PR.Error String
err -> do
let n :: Int
n = forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length [a]
backBuf
arr0 :: Array a
arr0 = forall a. Unbox a => Int -> [a] -> Array a
A.fromListN Int
n (forall a. [a] -> [a]
Prelude.reverse [a]
backBuf)
arr1 :: Array a
arr1 = forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
cur Int
end
str :: StreamK m (Array a)
str = forall a (m :: * -> *). a -> StreamK m a -> StreamK m a
K.cons Array a
arr0 (forall a (m :: * -> *). a -> StreamK m a -> StreamK m a
K.cons forall {a}. Array a
arr1 StreamK m (Array a)
stream)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left (String -> ParseError
ParseError String
err), StreamK m (Array a)
str)
goExtract :: s -> [a] -> Array a -> m (Either ParseError b, StreamK m (Array a))
goExtract !s
pst [a]
backBuf (Array MutByteArray
_ Int
cur Int
end)
| Int
cur forall a. Eq a => a -> a -> Bool
== Int
end = s -> [a] -> m (Either ParseError b, StreamK m (Array a))
goStop s
pst [a]
backBuf
goExtract !s
pst [a]
backBuf (Array MutByteArray
contents Int
cur Int
end) = do
a
x <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => Int -> MutByteArray -> IO a
peekByteIndex Int
cur MutByteArray
contents
Step s b
pRes <- s -> a -> m (Step s b)
pstep s
pst a
x
let next :: Int
next = INDEX_NEXT(cur,a)
case Step s b
pRes of
PR.Partial Int
0 s
s ->
s -> [a] -> Array a -> m (Either ParseError b, StreamK m (Array a))
goExtract s
s [] (forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
next Int
end)
PR.Partial Int
n s
s -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length (a
xforall a. a -> [a] -> [a]
:[a]
backBuf)) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
let src0 :: [a]
src0 = forall a. Int -> [a] -> [a]
Prelude.take Int
n (a
xforall a. a -> [a] -> [a]
:[a]
backBuf)
arr0 :: Array a
arr0 = forall a. Unbox a => Int -> [a] -> Array a
A.fromListN Int
n (forall a. [a] -> [a]
Prelude.reverse [a]
src0)
arr1 :: Array a
arr1 = forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
next Int
end
src :: Array a
src = Array a
arr0 forall a. Semigroup a => a -> a -> a
<> forall {a}. Array a
arr1
s -> [a] -> Array a -> m (Either ParseError b, StreamK m (Array a))
goExtract s
s [] Array a
src
PR.Continue Int
0 s
s ->
s -> [a] -> Array a -> m (Either ParseError b, StreamK m (Array a))
goExtract s
s [a]
backBuf (forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
next Int
end)
PR.Continue Int
n s
s -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length (a
xforall a. a -> [a] -> [a]
:[a]
backBuf)) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
let ([a]
src0, [a]
buf1) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
n (a
xforall a. a -> [a] -> [a]
:[a]
backBuf)
arr0 :: Array a
arr0 = forall a. Unbox a => Int -> [a] -> Array a
A.fromListN Int
n (forall a. [a] -> [a]
Prelude.reverse [a]
src0)
arr1 :: Array a
arr1 = forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
next Int
end
src :: Array a
src = Array a
arr0 forall a. Semigroup a => a -> a -> a
<> forall {a}. Array a
arr1
s -> [a] -> Array a -> m (Either ParseError b, StreamK m (Array a))
goExtract s
s [a]
buf1 Array a
src
PR.Done Int
0 b
b -> do
let arr :: Array a
arr = forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
next Int
end
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right b
b, forall a (m :: * -> *). a -> StreamK m a
K.fromPure forall {a}. Array a
arr)
PR.Done Int
n b
b -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length [a]
backBuf) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
let src0 :: [a]
src0 = forall a. Int -> [a] -> [a]
Prelude.take Int
n [a]
backBuf
arr0 :: Array a
arr0 = forall a. Unbox a => Int -> [a] -> Array a
A.fromListN Int
n (forall a. [a] -> [a]
Prelude.reverse [a]
src0)
arr1 :: Array a
arr1 = forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
next Int
end
str :: StreamK m (Array a)
str = forall a (m :: * -> *). a -> StreamK m a -> StreamK m a
K.cons Array a
arr0 (forall a (m :: * -> *). a -> StreamK m a
K.fromPure forall {a}. Array a
arr1)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right b
b, forall {m :: * -> *}. StreamK m (Array a)
str)
PR.Error String
err -> do
let n :: Int
n = forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length [a]
backBuf
arr0 :: Array a
arr0 = forall a. Unbox a => Int -> [a] -> Array a
A.fromListN Int
n (forall a. [a] -> [a]
Prelude.reverse [a]
backBuf)
arr1 :: Array a
arr1 = forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
contents Int
cur Int
end
str :: StreamK m (Array a)
str = forall a (m :: * -> *). a -> StreamK m a -> StreamK m a
K.cons Array a
arr0 (forall a (m :: * -> *). a -> StreamK m a -> StreamK m a
K.cons forall {a}. Array a
arr1 StreamK m (Array a)
stream)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left (String -> ParseError
ParseError String
err), StreamK m (Array a)
str)
{-# INLINE goStop #-}
goStop :: s -> [a] -> m (Either ParseError b, StreamK m (Array a))
goStop !s
pst [a]
backBuf = do
Step s b
pRes <- s -> m (Step s b)
extract s
pst
case Step s b
pRes of
PR.Partial Int
_ s
_ -> forall a. (?callStack::CallStack) => String -> a
error String
"Bug: parseBreak: Partial in extract"
PR.Continue Int
0 s
s ->
s -> [a] -> m (Either ParseError b, StreamK m (Array a))
goStop s
s [a]
backBuf
PR.Continue Int
n s
s -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length [a]
backBuf) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
let ([a]
src0, [a]
buf1) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
n [a]
backBuf
arr :: Array a
arr = forall a. Unbox a => Int -> [a] -> Array a
A.fromListN Int
n (forall a. [a] -> [a]
Prelude.reverse [a]
src0)
s -> [a] -> Array a -> m (Either ParseError b, StreamK m (Array a))
goExtract s
s [a]
buf1 Array a
arr
PR.Done Int
0 b
b ->
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right b
b, forall (m :: * -> *) a. StreamK m a
K.nil)
PR.Done Int
n b
b -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length [a]
backBuf) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
let src0 :: [a]
src0 = forall a. Int -> [a] -> [a]
Prelude.take Int
n [a]
backBuf
arr0 :: Array a
arr0 = forall a. Unbox a => Int -> [a] -> Array a
A.fromListN Int
n (forall a. [a] -> [a]
Prelude.reverse [a]
src0)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right b
b, forall a (m :: * -> *). a -> StreamK m a
K.fromPure Array a
arr0)
PR.Error String
err -> do
let n :: Int
n = forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length [a]
backBuf
arr0 :: Array a
arr0 = forall a. Unbox a => Int -> [a] -> Array a
A.fromListN Int
n (forall a. [a] -> [a]
Prelude.reverse [a]
backBuf)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left (String -> ParseError
ParseError String
err), forall a (m :: * -> *). a -> StreamK m a
K.fromPure Array a
arr0)
{-# INLINE_NORMAL parseBreak #-}
parseBreak ::
(MonadIO m, Unbox a)
=> PR.Parser a m b
-> StreamK m (A.Array a)
-> m (Either ParseError b, StreamK m (A.Array a))
parseBreak :: forall (m :: * -> *) a b.
(MonadIO m, Unbox a) =>
Parser a m b
-> StreamK m (Array a)
-> m (Either ParseError b, StreamK m (Array a))
parseBreak = forall (m :: * -> *) a b.
(MonadIO m, Unbox a) =>
Parser a m b
-> StreamK m (Array a)
-> m (Either ParseError b, StreamK m (Array a))
parseBreakK
{-# INLINE_NORMAL runArrayParserDBreak #-}
runArrayParserDBreak ::
forall m a b. (MonadIO m, Unbox a)
=> PRD.Parser (Array a) m b
-> D.Stream m (Array.Array a)
-> m (Either ParseError b, D.Stream m (Array.Array a))
runArrayParserDBreak :: forall (m :: * -> *) a b.
(MonadIO m, Unbox a) =>
Parser (Array a) m b
-> Stream m (Array a)
-> m (Either ParseError b, Stream m (Array a))
runArrayParserDBreak
(PRD.Parser s -> Array a -> m (Step s b)
pstep m (Initial s b)
initial s -> m (Step s b)
extract)
stream :: Stream m (Array a)
stream@(D.Stream State StreamK m (Array a) -> s -> m (Step s (Array a))
step s
state) = do
Initial s b
res <- m (Initial s b)
initial
case Initial s b
res of
PRD.IPartial s
s -> SPEC
-> s
-> List (Array a)
-> s
-> m (Either ParseError b, Stream m (Array a))
go SPEC
SPEC s
state (forall a. [a] -> List a
List []) s
s
PRD.IDone b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right b
b, Stream m (Array a)
stream)
PRD.IError String
err -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left (String -> ParseError
ParseError String
err), Stream m (Array a)
stream)
where
go :: SPEC
-> s
-> List (Array a)
-> s
-> m (Either ParseError b, Stream m (Array a))
go SPEC
_ s
st List (Array a)
backBuf !s
pst = do
Step s (Array a)
r <- State StreamK m (Array a) -> s -> m (Step s (Array a))
step forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st
case Step s (Array a)
r of
D.Yield Array a
x s
s -> SPEC
-> [Array a]
-> s
-> List (Array a)
-> s
-> m (Either ParseError b, Stream m (Array a))
gobuf SPEC
SPEC [Array a
x] s
s List (Array a)
backBuf s
pst
D.Skip s
s -> SPEC
-> s
-> List (Array a)
-> s
-> m (Either ParseError b, Stream m (Array a))
go SPEC
SPEC s
s List (Array a)
backBuf s
pst
Step s (Array a)
D.Stop -> forall {m :: * -> *}.
Applicative m =>
List (Array a) -> s -> m (Either ParseError b, Stream m (Array a))
goStop List (Array a)
backBuf s
pst
gobuf :: SPEC
-> [Array a]
-> s
-> List (Array a)
-> s
-> m (Either ParseError b, Stream m (Array a))
gobuf !SPEC
_ [] s
s List (Array a)
backBuf !s
pst = SPEC
-> s
-> List (Array a)
-> s
-> m (Either ParseError b, Stream m (Array a))
go SPEC
SPEC s
s List (Array a)
backBuf s
pst
gobuf !SPEC
_ (Array a
x:[Array a]
xs) s
s List (Array a)
backBuf !s
pst = do
Step s b
pRes <- s -> Array a -> m (Step s b)
pstep s
pst Array a
x
case Step s b
pRes of
PR.Partial Int
0 s
pst1 ->
SPEC
-> [Array a]
-> s
-> List (Array a)
-> s
-> m (Either ParseError b, Stream m (Array a))
gobuf SPEC
SPEC [Array a]
xs s
s (forall a. [a] -> List a
List []) s
pst1
PR.Partial Int
n s
pst1 -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert
(Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)))
(forall (m :: * -> *) a. Monad m => a -> m a
return ())
let src0 :: [Array a]
src0 = forall a. Unbox a => Int -> [Array a] -> [Array a]
takeArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ [Array a]
xs
SPEC
-> [Array a]
-> s
-> List (Array a)
-> s
-> m (Either ParseError b, Stream m (Array a))
gobuf SPEC
SPEC [Array a]
src s
s (forall a. [a] -> List a
List []) s
pst1
PR.Continue Int
0 s
pst1 ->
SPEC
-> [Array a]
-> s
-> List (Array a)
-> s
-> m (Either ParseError b, Stream m (Array a))
gobuf SPEC
SPEC [Array a]
xs s
s (forall a. [a] -> List a
List (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)) s
pst1
PR.Continue Int
n s
pst1 -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert
(Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)))
(forall (m :: * -> *) a. Monad m => a -> m a
return ())
let ([Array a]
src0, [Array a]
buf1) = forall a. Unbox a => Int -> [Array a] -> ([Array a], [Array a])
splitAtArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ [Array a]
xs
SPEC
-> [Array a]
-> s
-> List (Array a)
-> s
-> m (Either ParseError b, Stream m (Array a))
gobuf SPEC
SPEC [Array a]
src s
s (forall a. [a] -> List a
List [Array a]
buf1) s
pst1
PR.Done Int
0 b
b -> do
let str :: Stream m (Array a)
str = forall (m :: * -> *) a.
Monad m =>
Stream m a -> Stream m a -> Stream m a
D.append (forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
D.fromList [Array a]
xs) (forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State StreamK m (Array a) -> s -> m (Step s (Array a))
step s
s)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right b
b, Stream m (Array a)
str)
PR.Done Int
n b
b -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert
(Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)))
(forall (m :: * -> *) a. Monad m => a -> m a
return ())
let src0 :: [Array a]
src0 = forall a. Unbox a => Int -> [Array a] -> [Array a]
takeArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ [Array a]
xs
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right b
b, forall (m :: * -> *) a.
Monad m =>
Stream m a -> Stream m a -> Stream m a
D.append (forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
D.fromList [Array a]
src) (forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State StreamK m (Array a) -> s -> m (Step s (Array a))
step s
s))
PR.Error String
err -> do
let src0 :: [Array a]
src0 = Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ Array a
xforall a. a -> [a] -> [a]
:[Array a]
xs
strm :: Stream m (Array a)
strm = forall (m :: * -> *) a.
Monad m =>
Stream m a -> Stream m a -> Stream m a
D.append (forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
D.fromList [Array a]
src) (forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State StreamK m (Array a) -> s -> m (Step s (Array a))
step s
s)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left (String -> ParseError
ParseError String
err), Stream m (Array a)
strm)
goExtract :: SPEC
-> [Array a]
-> List (Array a)
-> s
-> m (Either ParseError b, Stream m (Array a))
goExtract SPEC
_ [] List (Array a)
backBuf !s
pst = List (Array a) -> s -> m (Either ParseError b, Stream m (Array a))
goStop List (Array a)
backBuf s
pst
goExtract SPEC
_ (Array a
x:[Array a]
xs) List (Array a)
backBuf !s
pst = do
Step s b
pRes <- s -> Array a -> m (Step s b)
pstep s
pst Array a
x
case Step s b
pRes of
PR.Partial Int
0 s
pst1 ->
SPEC
-> [Array a]
-> List (Array a)
-> s
-> m (Either ParseError b, Stream m (Array a))
goExtract SPEC
SPEC [Array a]
xs (forall a. [a] -> List a
List []) s
pst1
PR.Partial Int
n s
pst1 -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert
(Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)))
(forall (m :: * -> *) a. Monad m => a -> m a
return ())
let src0 :: [Array a]
src0 = forall a. Unbox a => Int -> [Array a] -> [Array a]
takeArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ [Array a]
xs
SPEC
-> [Array a]
-> List (Array a)
-> s
-> m (Either ParseError b, Stream m (Array a))
goExtract SPEC
SPEC [Array a]
src (forall a. [a] -> List a
List []) s
pst1
PR.Continue Int
0 s
pst1 ->
SPEC
-> [Array a]
-> List (Array a)
-> s
-> m (Either ParseError b, Stream m (Array a))
goExtract SPEC
SPEC [Array a]
xs (forall a. [a] -> List a
List (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)) s
pst1
PR.Continue Int
n s
pst1 -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert
(Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)))
(forall (m :: * -> *) a. Monad m => a -> m a
return ())
let ([Array a]
src0, [Array a]
buf1) = forall a. Unbox a => Int -> [Array a] -> ([Array a], [Array a])
splitAtArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ [Array a]
xs
SPEC
-> [Array a]
-> List (Array a)
-> s
-> m (Either ParseError b, Stream m (Array a))
goExtract SPEC
SPEC [Array a]
src (forall a. [a] -> List a
List [Array a]
buf1) s
pst1
PR.Done Int
0 b
b ->
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right b
b, forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
D.fromList [Array a]
xs)
PR.Done Int
n b
b -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert
(Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)))
(forall (m :: * -> *) a. Monad m => a -> m a
return ())
let src0 :: [Array a]
src0 = forall a. Unbox a => Int -> [Array a] -> [Array a]
takeArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:forall a. List a -> [a]
getList List (Array a)
backBuf)
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ [Array a]
xs
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right b
b, forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
D.fromList [Array a]
src)
PR.Error String
err -> do
let src0 :: [Array a]
src0 = forall a. List a -> [a]
getList List (Array a)
backBuf
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ Array a
xforall a. a -> [a] -> [a]
:[Array a]
xs
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left (String -> ParseError
ParseError String
err), forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
D.fromList [Array a]
src)
{-# INLINE goStop #-}
goStop :: List (Array a) -> s -> m (Either ParseError b, Stream m (Array a))
goStop List (Array a)
backBuf s
pst = do
Step s b
pRes <- s -> m (Step s b)
extract s
pst
case Step s b
pRes of
PR.Partial Int
_ s
_ -> forall a. (?callStack::CallStack) => String -> a
error String
"Bug: runArrayParserDBreak: Partial in extract"
PR.Continue Int
0 s
pst1 ->
List (Array a) -> s -> m (Either ParseError b, Stream m (Array a))
goStop List (Array a)
backBuf s
pst1
PR.Continue Int
n s
pst1 -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert
(Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length (forall a. List a -> [a]
getList List (Array a)
backBuf)))
(forall (m :: * -> *) a. Monad m => a -> m a
return ())
let ([Array a]
src0, [Array a]
buf1) = forall a. Unbox a => Int -> [Array a] -> ([Array a], [Array a])
splitAtArrayListRev Int
n (forall a. List a -> [a]
getList List (Array a)
backBuf)
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0
SPEC
-> [Array a]
-> List (Array a)
-> s
-> m (Either ParseError b, Stream m (Array a))
goExtract SPEC
SPEC [Array a]
src (forall a. [a] -> List a
List [Array a]
buf1) s
pst1
PR.Done Int
0 b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right b
b, forall (m :: * -> *) a. Applicative m => Stream m a
D.nil)
PR.Done Int
n b
b -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert
(Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length (forall a. List a -> [a]
getList List (Array a)
backBuf)))
(forall (m :: * -> *) a. Monad m => a -> m a
return ())
let src0 :: [Array a]
src0 = forall a. Unbox a => Int -> [Array a] -> [Array a]
takeArrayListRev Int
n (forall a. List a -> [a]
getList List (Array a)
backBuf)
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right b
b, forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
D.fromList [Array a]
src)
PR.Error String
err -> do
let src0 :: [Array a]
src0 = forall a. List a -> [a]
getList List (Array a)
backBuf
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left (String -> ParseError
ParseError String
err), forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
D.fromList [Array a]
src)
{-# INLINE runArrayFold #-}
runArrayFold :: (MonadIO m, Unbox a) =>
ChunkFold m a b -> StreamK m (A.Array a) -> m (Either ParseError b)
runArrayFold :: forall (m :: * -> *) a b.
(MonadIO m, Unbox a) =>
ChunkFold m a b -> StreamK m (Array a) -> m (Either ParseError b)
runArrayFold (ChunkFold Parser (Array a) m b
p) StreamK m (Array a)
s = forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a b.
(MonadIO m, Unbox a) =>
Parser (Array a) m b
-> Stream m (Array a)
-> m (Either ParseError b, Stream m (Array a))
runArrayParserDBreak Parser (Array a) m b
p (forall (m :: * -> *) a. Applicative m => StreamK m a -> Stream m a
toStream StreamK m (Array a)
s)
{-# INLINE runArrayFoldBreak #-}
runArrayFoldBreak :: (MonadIO m, Unbox a) =>
ChunkFold m a b -> StreamK m (A.Array a) -> m (Either ParseError b, StreamK m (A.Array a))
runArrayFoldBreak :: forall (m :: * -> *) a b.
(MonadIO m, Unbox a) =>
ChunkFold m a b
-> StreamK m (Array a)
-> m (Either ParseError b, StreamK m (Array a))
runArrayFoldBreak (ChunkFold Parser (Array a) m b
p) StreamK m (Array a)
s =
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second forall (m :: * -> *) a. Monad m => Stream m a -> StreamK m a
fromStream forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a b.
(MonadIO m, Unbox a) =>
Parser (Array a) m b
-> Stream m (Array a)
-> m (Either ParseError b, Stream m (Array a))
runArrayParserDBreak Parser (Array a) m b
p (forall (m :: * -> *) a. Applicative m => StreamK m a -> Stream m a
toStream StreamK m (Array a)
s)
{-# ANN type ParseChunksState Fuse #-}
data ParseChunksState x inpBuf st pst =
ParseChunksInit inpBuf st
| ParseChunksInitBuf inpBuf
| ParseChunksInitLeftOver inpBuf
| ParseChunksStream st inpBuf !pst
| ParseChunksStop inpBuf !pst
| ParseChunksBuf inpBuf st inpBuf !pst
| inpBuf inpBuf !pst
| ParseChunksYield x (ParseChunksState x inpBuf st pst)
{-# INLINE_NORMAL runArrayFoldManyD #-}
runArrayFoldManyD
:: (Monad m, Unbox a)
=> ChunkFold m a b
-> D.Stream m (Array a)
-> D.Stream m (Either ParseError b)
runArrayFoldManyD :: forall (m :: * -> *) a b.
(Monad m, Unbox a) =>
ChunkFold m a b
-> Stream m (Array a) -> Stream m (Either ParseError b)
runArrayFoldManyD
(ChunkFold (PRD.Parser s -> Array a -> m (Step s b)
pstep m (Initial s b)
initial s -> m (Step s b)
extract)) (D.Stream State StreamK m (Array a) -> s -> m (Step s (Array a))
step s
state) =
forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {m :: * -> *} {a}.
State StreamK m a
-> ParseChunksState (Either ParseError b) [Array a] s s
-> m (Step
(ParseChunksState (Either ParseError b) [Array a] s s)
(Either ParseError b))
stepOuter (forall x inpBuf st pst.
inpBuf -> st -> ParseChunksState x inpBuf st pst
ParseChunksInit [] s
state)
where
{-# INLINE_LATE stepOuter #-}
stepOuter :: State StreamK m a
-> ParseChunksState (Either ParseError b) [Array a] s s
-> m (Step
(ParseChunksState (Either ParseError b) [Array a] s s)
(Either ParseError b))
stepOuter State StreamK m a
gst (ParseChunksInit [] s
st) = do
Step s (Array a)
r <- State StreamK m (Array a) -> s -> m (Step s (Array a))
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
case Step s (Array a)
r of
D.Yield Array a
x s
s -> do
Initial s b
res <- m (Initial s b)
initial
case Initial s b
res of
PRD.IPartial s
ps ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksBuf [Array a
x] s
s [] s
ps
PRD.IDone b
pb -> do
let next :: ParseChunksState x [Array a] s pst
next = forall x inpBuf st pst.
inpBuf -> st -> ParseChunksState x inpBuf st pst
ParseChunksInit [Array a
x] s
s
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield (forall a b. b -> Either a b
Right b
pb) forall {x} {pst}. ParseChunksState x [Array a] s pst
next
PRD.IError String
err -> do
let next :: ParseChunksState x [a] st pst
next = forall x inpBuf st pst. inpBuf -> ParseChunksState x inpBuf st pst
ParseChunksInitLeftOver []
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip
forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield (forall a b. a -> Either a b
Left (String -> ParseError
ParseError String
err)) forall {x} {a} {st} {pst}. ParseChunksState x [a] st pst
next
D.Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> st -> ParseChunksState x inpBuf st pst
ParseChunksInit [] s
s
Step s (Array a)
D.Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
D.Stop
stepOuter State StreamK m a
_ (ParseChunksInit [Array a]
src s
st) = do
Initial s b
res <- m (Initial s b)
initial
case Initial s b
res of
PRD.IPartial s
ps ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksBuf [Array a]
src s
st [] s
ps
PRD.IDone b
pb ->
let next :: ParseChunksState x [Array a] s pst
next = forall x inpBuf st pst.
inpBuf -> st -> ParseChunksState x inpBuf st pst
ParseChunksInit [Array a]
src s
st
in forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield (forall a b. b -> Either a b
Right b
pb) forall {x} {pst}. ParseChunksState x [Array a] s pst
next
PRD.IError String
err -> do
let next :: ParseChunksState x [a] st pst
next = forall x inpBuf st pst. inpBuf -> ParseChunksState x inpBuf st pst
ParseChunksInitLeftOver []
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip
forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield (forall a b. a -> Either a b
Left (String -> ParseError
ParseError String
err)) forall {x} {a} {st} {pst}. ParseChunksState x [a] st pst
next
stepOuter State StreamK m a
_ (ParseChunksInitBuf [Array a]
src) = do
Initial s b
res <- m (Initial s b)
initial
case Initial s b
res of
PRD.IPartial s
ps ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksExtract [Array a]
src [] s
ps
PRD.IDone b
pb ->
let next :: ParseChunksState x [Array a] st pst
next = forall x inpBuf st pst. inpBuf -> ParseChunksState x inpBuf st pst
ParseChunksInitBuf [Array a]
src
in forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield (forall a b. b -> Either a b
Right b
pb) forall {x} {st} {pst}. ParseChunksState x [Array a] st pst
next
PRD.IError String
err -> do
let next :: ParseChunksState x [a] st pst
next = forall x inpBuf st pst. inpBuf -> ParseChunksState x inpBuf st pst
ParseChunksInitLeftOver []
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip
forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield (forall a b. a -> Either a b
Left (String -> ParseError
ParseError String
err)) forall {x} {a} {st} {pst}. ParseChunksState x [a] st pst
next
stepOuter State StreamK m a
_ (ParseChunksInitLeftOver [Array a]
_) = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
D.Stop
stepOuter State StreamK m a
gst (ParseChunksStream s
st [Array a]
backBuf s
pst) = do
Step s (Array a)
r <- State StreamK m (Array a) -> s -> m (Step s (Array a))
step (forall (t :: (* -> *) -> * -> *) (m :: * -> *) a (n :: * -> *) b.
State t m a -> State t n b
adaptState State StreamK m a
gst) s
st
case Step s (Array a)
r of
D.Yield Array a
x s
s -> do
Step s b
pRes <- s -> Array a -> m (Step s b)
pstep s
pst Array a
x
case Step s b
pRes of
PR.Partial Int
0 s
pst1 ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksStream s
s [] s
pst1
PR.Partial Int
n s
pst1 -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert
(Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)))
(forall (m :: * -> *) a. Monad m => a -> m a
return ())
let src0 :: [Array a]
src0 = forall a. Unbox a => Int -> [Array a] -> [Array a]
takeArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksBuf [Array a]
src s
s [] s
pst1
PR.Continue Int
0 s
pst1 ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksStream s
s (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf) s
pst1
PR.Continue Int
n s
pst1 -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert
(Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)))
(forall (m :: * -> *) a. Monad m => a -> m a
return ())
let ([Array a]
src0, [Array a]
buf1) = forall a. Unbox a => Int -> [Array a] -> ([Array a], [Array a])
splitAtArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksBuf [Array a]
src s
s [Array a]
buf1 s
pst1
PR.Done Int
0 b
b -> do
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$
forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield (forall a b. b -> Either a b
Right b
b) (forall x inpBuf st pst.
inpBuf -> st -> ParseChunksState x inpBuf st pst
ParseChunksInit [] s
s)
PR.Done Int
n b
b -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert
(Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)))
(forall (m :: * -> *) a. Monad m => a -> m a
return ())
let src0 :: [Array a]
src0 = forall a. Unbox a => Int -> [Array a] -> [Array a]
takeArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0
next :: ParseChunksState x [Array a] s pst
next = forall x inpBuf st pst.
inpBuf -> st -> ParseChunksState x inpBuf st pst
ParseChunksInit [Array a]
src s
s
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip
forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield (forall a b. b -> Either a b
Right b
b) forall {x} {pst}. ParseChunksState x [Array a] s pst
next
PR.Error String
err -> do
let next :: ParseChunksState x [a] st pst
next = forall x inpBuf st pst. inpBuf -> ParseChunksState x inpBuf st pst
ParseChunksInitLeftOver []
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip
forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield (forall a b. a -> Either a b
Left (String -> ParseError
ParseError String
err)) forall {x} {a} {st} {pst}. ParseChunksState x [a] st pst
next
D.Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksStream s
s [Array a]
backBuf s
pst
Step s (Array a)
D.Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksStop [Array a]
backBuf s
pst
stepOuter State StreamK m a
_ (ParseChunksBuf [] s
s [Array a]
buf s
pst) =
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksStream s
s [Array a]
buf s
pst
stepOuter State StreamK m a
_ (ParseChunksBuf (Array a
x:[Array a]
xs) s
s [Array a]
backBuf s
pst) = do
Step s b
pRes <- s -> Array a -> m (Step s b)
pstep s
pst Array a
x
case Step s b
pRes of
PR.Partial Int
0 s
pst1 ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksBuf [Array a]
xs s
s [] s
pst1
PR.Partial Int
n s
pst1 -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf))) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
let src0 :: [Array a]
src0 = forall a. Unbox a => Int -> [Array a] -> [Array a]
takeArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ [Array a]
xs
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksBuf [Array a]
src s
s [] s
pst1
PR.Continue Int
0 s
pst1 ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksBuf [Array a]
xs s
s (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf) s
pst1
PR.Continue Int
n s
pst1 -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf))) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
let ([Array a]
src0, [Array a]
buf1) = forall a. Unbox a => Int -> [Array a] -> ([Array a], [Array a])
splitAtArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ [Array a]
xs
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> st -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksBuf [Array a]
src s
s [Array a]
buf1 s
pst1
PR.Done Int
0 b
b ->
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip
forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield (forall a b. b -> Either a b
Right b
b) (forall x inpBuf st pst.
inpBuf -> st -> ParseChunksState x inpBuf st pst
ParseChunksInit [Array a]
xs s
s)
PR.Done Int
n b
b -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf))) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
let src0 :: [Array a]
src0 = forall a. Unbox a => Int -> [Array a] -> [Array a]
takeArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ [Array a]
xs
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip
forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield (forall a b. b -> Either a b
Right b
b) (forall x inpBuf st pst.
inpBuf -> st -> ParseChunksState x inpBuf st pst
ParseChunksInit [Array a]
src s
s)
PR.Error String
err -> do
let next :: ParseChunksState x [a] st pst
next = forall x inpBuf st pst. inpBuf -> ParseChunksState x inpBuf st pst
ParseChunksInitLeftOver []
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip
forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield (forall a b. a -> Either a b
Left (String -> ParseError
ParseError String
err)) forall {x} {a} {st} {pst}. ParseChunksState x [a] st pst
next
stepOuter State StreamK m a
_ (ParseChunksExtract [] [Array a]
buf s
pst) =
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksStop [Array a]
buf s
pst
stepOuter State StreamK m a
_ (ParseChunksExtract (Array a
x:[Array a]
xs) [Array a]
backBuf s
pst) = do
Step s b
pRes <- s -> Array a -> m (Step s b)
pstep s
pst Array a
x
case Step s b
pRes of
PR.Partial Int
0 s
pst1 ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksExtract [Array a]
xs [] s
pst1
PR.Partial Int
n s
pst1 -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf))) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
let src0 :: [Array a]
src0 = forall a. Unbox a => Int -> [Array a] -> [Array a]
takeArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ [Array a]
xs
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksExtract [Array a]
src [] s
pst1
PR.Continue Int
0 s
pst1 ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksExtract [Array a]
xs (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf) s
pst1
PR.Continue Int
n s
pst1 -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf))) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
let ([Array a]
src0, [Array a]
buf1) = forall a. Unbox a => Int -> [Array a] -> ([Array a], [Array a])
splitAtArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ [Array a]
xs
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksExtract [Array a]
src [Array a]
buf1 s
pst1
PR.Done Int
0 b
b ->
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip
forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield (forall a b. b -> Either a b
Right b
b) (forall x inpBuf st pst. inpBuf -> ParseChunksState x inpBuf st pst
ParseChunksInitBuf [Array a]
xs)
PR.Done Int
n b
b -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf))) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
let src0 :: [Array a]
src0 = forall a. Unbox a => Int -> [Array a] -> [Array a]
takeArrayListRev Int
n (Array a
xforall a. a -> [a] -> [a]
:[Array a]
backBuf)
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0 forall a. [a] -> [a] -> [a]
++ [Array a]
xs
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip
forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield (forall a b. b -> Either a b
Right b
b) (forall x inpBuf st pst. inpBuf -> ParseChunksState x inpBuf st pst
ParseChunksInitBuf [Array a]
src)
PR.Error String
err -> do
let next :: ParseChunksState x [a] st pst
next = forall x inpBuf st pst. inpBuf -> ParseChunksState x inpBuf st pst
ParseChunksInitLeftOver []
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip
forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield (forall a b. a -> Either a b
Left (String -> ParseError
ParseError String
err)) forall {x} {a} {st} {pst}. ParseChunksState x [a] st pst
next
stepOuter State StreamK m a
_ (ParseChunksStop [Array a]
backBuf s
pst) = do
Step s b
pRes <- s -> m (Step s b)
extract s
pst
case Step s b
pRes of
PR.Partial Int
_ s
_ -> forall a. (?callStack::CallStack) => String -> a
error String
"runArrayFoldManyD: Partial in extract"
PR.Continue Int
0 s
pst1 ->
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksStop [Array a]
backBuf s
pst1
PR.Continue Int
n s
pst1 -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length [Array a]
backBuf)) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
let ([Array a]
src0, [Array a]
buf1) = forall a. Unbox a => Int -> [Array a] -> ([Array a], [Array a])
splitAtArrayListRev Int
n [Array a]
backBuf
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
inpBuf -> inpBuf -> pst -> ParseChunksState x inpBuf st pst
ParseChunksExtract [Array a]
src [Array a]
buf1 s
pst1
PR.Done Int
0 b
b ->
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip
forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield (forall a b. b -> Either a b
Right b
b) (forall x inpBuf st pst. inpBuf -> ParseChunksState x inpBuf st pst
ParseChunksInitLeftOver [])
PR.Done Int
n b
b -> do
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
n forall a. Ord a => a -> a -> Bool
<= forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a b. (a -> b) -> [a] -> [b]
map forall a. Unbox a => Array a -> Int
Array.length [Array a]
backBuf)) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
let src0 :: [Array a]
src0 = forall a. Unbox a => Int -> [Array a] -> [Array a]
takeArrayListRev Int
n [Array a]
backBuf
src :: [Array a]
src = forall a. [a] -> [a]
Prelude.reverse [Array a]
src0
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip
forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield (forall a b. b -> Either a b
Right b
b) (forall x inpBuf st pst. inpBuf -> ParseChunksState x inpBuf st pst
ParseChunksInitBuf [Array a]
src)
PR.Error String
err -> do
let next :: ParseChunksState x [a] st pst
next = forall x inpBuf st pst. inpBuf -> ParseChunksState x inpBuf st pst
ParseChunksInitLeftOver []
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
D.Skip
forall a b. (a -> b) -> a -> b
$ forall x inpBuf st pst.
x
-> ParseChunksState x inpBuf st pst
-> ParseChunksState x inpBuf st pst
ParseChunksYield (forall a b. a -> Either a b
Left (String -> ParseError
ParseError String
err)) forall {x} {a} {st} {pst}. ParseChunksState x [a] st pst
next
stepOuter State StreamK m a
_ (ParseChunksYield Either ParseError b
a ParseChunksState (Either ParseError b) [Array a] s s
next) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield Either ParseError b
a ParseChunksState (Either ParseError b) [Array a] s s
next
{-# INLINE runArrayFoldMany #-}
runArrayFoldMany
:: (Monad m, Unbox a)
=> ChunkFold m a b
-> StreamK m (Array a)
-> StreamK m (Either ParseError b)
runArrayFoldMany :: forall (m :: * -> *) a b.
(Monad m, Unbox a) =>
ChunkFold m a b
-> StreamK m (Array a) -> StreamK m (Either ParseError b)
runArrayFoldMany ChunkFold m a b
p StreamK m (Array a)
m = forall (m :: * -> *) a. Monad m => Stream m a -> StreamK m a
fromStream forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
(Monad m, Unbox a) =>
ChunkFold m a b
-> Stream m (Array a) -> Stream m (Either ParseError b)
runArrayFoldManyD ChunkFold m a b
p (forall (m :: * -> *) a. Applicative m => StreamK m a -> Stream m a
toStream StreamK m (Array a)
m)