module Data.Attoparsec.ByteString.Streaming
(Message
, parse
, parsed
)
where
import qualified Data.ByteString as B
import qualified Data.Attoparsec.ByteString as A
import qualified Data.Attoparsec.Internal.Types as T
import Data.Attoparsec.ByteString
hiding (IResult(..), Result, eitherResult, maybeResult,
parse, parseWith, parseTest)
import Streaming hiding (concats, unfold)
import Streaming.Internal (Stream (..))
import Data.ByteString.Streaming
import Data.ByteString.Streaming.Internal
import Data.Monoid
type Message = ([String], String)
parse :: Monad m
=> A.Parser a
-> ByteString m x -> m (Either a Message, ByteString m x)
parse :: Parser a -> ByteString m x -> m (Either a Message, ByteString m x)
parse Parser a
parser ByteString m x
bs = do
(Either Message a
e,ByteString m x
rest) <- Parser a -> ByteString m x -> m (Either Message a, ByteString m x)
forall (m :: * -> *) a x.
Monad m =>
Parser a -> ByteString m x -> m (Either Message a, ByteString m x)
apply Parser a
parser ByteString m x
bs
(Either a Message, ByteString m x)
-> m (Either a Message, ByteString m x)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Message -> Either a Message)
-> (a -> Either a Message) -> Either Message a -> Either a Message
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Message -> Either a Message
forall a b. b -> Either a b
Right a -> Either a Message
forall a b. a -> Either a b
Left Either Message a
e, ByteString m x
rest)
{-#INLINE parse #-}
apply :: Monad m
=> A.Parser a
-> ByteString m x -> m (Either Message a, ByteString m x)
apply :: Parser a -> ByteString m x -> m (Either Message a, ByteString m x)
apply Parser a
parser = ByteString m x -> m (Either Message a, ByteString m x)
forall (m :: * -> *) a.
Monad m =>
ByteStream m a -> m (Either Message a, ByteStream m a)
begin where
begin :: ByteStream m a -> m (Either Message a, ByteStream m a)
begin ByteStream m a
p0 = case ByteStream m a
p0 of
Go m (ByteStream m a)
m -> m (ByteStream m a)
m m (ByteStream m a)
-> (ByteStream m a -> m (Either Message a, ByteStream m a))
-> m (Either Message a, ByteStream m a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteStream m a -> m (Either Message a, ByteStream m a)
begin
Empty a
r -> (ByteStream m a -> ByteStream m a)
-> IResult ByteString a
-> ByteStream m a
-> m (Either Message a, ByteStream m a)
forall (m :: * -> *) a b.
Monad m =>
(ByteStream m a -> ByteStream m a)
-> IResult ByteString b
-> ByteStream m a
-> m (Either Message b, ByteStream m a)
step ByteStream m a -> ByteStream m a
forall a. a -> a
id (Parser a -> ByteString -> IResult ByteString a
forall a. Parser a -> ByteString -> Result a
A.parse Parser a
parser ByteString
forall a. Monoid a => a
mempty) (a -> ByteStream m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
r)
Chunk ByteString
bs ByteStream m a
p1 -> if ByteString -> Bool
B.null ByteString
bs
then ByteStream m a -> m (Either Message a, ByteStream m a)
begin ByteStream m a
p1
else (ByteStream m a -> ByteStream m a)
-> IResult ByteString a
-> ByteStream m a
-> m (Either Message a, ByteStream m a)
forall (m :: * -> *) a b.
Monad m =>
(ByteStream m a -> ByteStream m a)
-> IResult ByteString b
-> ByteStream m a
-> m (Either Message b, ByteStream m a)
step (ByteString -> ByteStream m ()
forall (m :: * -> *). ByteString -> ByteStream m ()
chunk ByteString
bs ByteStream m () -> ByteStream m a -> ByteStream m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>) (Parser a -> ByteString -> IResult ByteString a
forall a. Parser a -> ByteString -> Result a
A.parse Parser a
parser ByteString
bs) ByteStream m a
p1
step :: (ByteStream m a -> ByteStream m a)
-> IResult ByteString b
-> ByteStream m a
-> m (Either Message b, ByteStream m a)
step ByteStream m a -> ByteStream m a
diff IResult ByteString b
res ByteStream m a
p0 = case IResult ByteString b
res of
T.Fail ByteString
_ [String]
c String
m -> (Either Message b, ByteStream m a)
-> m (Either Message b, ByteStream m a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Message -> Either Message b
forall a b. a -> Either a b
Left ([String]
c,String
m), ByteStream m a -> ByteStream m a
diff ByteStream m a
p0)
T.Done ByteString
a b
b -> (Either Message b, ByteStream m a)
-> m (Either Message b, ByteStream m a)
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Either Message b
forall a b. b -> Either a b
Right b
b, ByteString -> ByteStream m ()
forall (m :: * -> *). ByteString -> ByteStream m ()
chunk ByteString
a ByteStream m () -> ByteStream m a -> ByteStream m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ByteStream m a
p0)
T.Partial ByteString -> IResult ByteString b
k -> do
let clean :: ByteStream m a -> m (Either Message b, ByteStream m a)
clean ByteStream m a
p = case ByteStream m a
p of
Go m (ByteStream m a)
m -> m (ByteStream m a)
m m (ByteStream m a)
-> (ByteStream m a -> m (Either Message b, ByteStream m a))
-> m (Either Message b, ByteStream m a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteStream m a -> m (Either Message b, ByteStream m a)
clean
Empty a
r -> (ByteStream m a -> ByteStream m a)
-> IResult ByteString b
-> ByteStream m a
-> m (Either Message b, ByteStream m a)
step ByteStream m a -> ByteStream m a
diff (ByteString -> IResult ByteString b
k ByteString
forall a. Monoid a => a
mempty) (a -> ByteStream m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
r)
Chunk ByteString
bs ByteStream m a
p1 | ByteString -> Bool
B.null ByteString
bs -> ByteStream m a -> m (Either Message b, ByteStream m a)
clean ByteStream m a
p1
| Bool
otherwise -> (ByteStream m a -> ByteStream m a)
-> IResult ByteString b
-> ByteStream m a
-> m (Either Message b, ByteStream m a)
step (ByteStream m a -> ByteStream m a
diff (ByteStream m a -> ByteStream m a)
-> (ByteStream m a -> ByteStream m a)
-> ByteStream m a
-> ByteStream m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> ByteStream m ()
forall (m :: * -> *). ByteString -> ByteStream m ()
chunk ByteString
bs ByteStream m () -> ByteStream m a -> ByteStream m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>)) (ByteString -> IResult ByteString b
k ByteString
bs) ByteStream m a
p1
ByteStream m a -> m (Either Message b, ByteStream m a)
clean ByteStream m a
p0
{-#INLINABLE apply #-}
parsed
:: Monad m
=> A.Parser a
-> ByteString m r
-> Stream (Of a) m (Either (Message, ByteString m r) r)
parsed :: Parser a
-> ByteString m r
-> Stream (Of a) m (Either (Message, ByteString m r) r)
parsed Parser a
parser = ByteString m r
-> Stream (Of a) m (Either (Message, ByteString m r) r)
forall (m :: * -> *) a.
Monad m =>
ByteStream m a
-> Stream (Of a) m (Either (Message, ByteStream m a) a)
begin
where
begin :: ByteStream m a
-> Stream (Of a) m (Either (Message, ByteStream m a) a)
begin ByteStream m a
p0 = case ByteStream m a
p0 of
Go m (ByteStream m a)
m -> m (ByteStream m a) -> Stream (Of a) m (ByteStream m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (ByteStream m a)
m Stream (Of a) m (ByteStream m a)
-> (ByteStream m a
-> Stream (Of a) m (Either (Message, ByteStream m a) a))
-> Stream (Of a) m (Either (Message, ByteStream m a) a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteStream m a
-> Stream (Of a) m (Either (Message, ByteStream m a) a)
begin
Empty a
r -> Either (Message, ByteStream m a) a
-> Stream (Of a) m (Either (Message, ByteStream m a) a)
forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return (a -> Either (Message, ByteStream m a) a
forall a b. b -> Either a b
Right a
r)
Chunk ByteString
bs ByteStream m a
p1 | ByteString -> Bool
B.null ByteString
bs -> ByteStream m a
-> Stream (Of a) m (Either (Message, ByteStream m a) a)
begin ByteStream m a
p1
| Bool
otherwise -> (ByteStream m a -> ByteStream m a)
-> Result a
-> ByteStream m a
-> Stream (Of a) m (Either (Message, ByteStream m a) a)
step (ByteString -> ByteStream m ()
forall (m :: * -> *). ByteString -> ByteStream m ()
chunk ByteString
bs ByteStream m () -> ByteStream m a -> ByteStream m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>) (Parser a -> ByteString -> Result a
forall a. Parser a -> ByteString -> Result a
A.parse Parser a
parser ByteString
bs) ByteStream m a
p1
step :: (ByteStream m a -> ByteStream m a)
-> Result a
-> ByteStream m a
-> Stream (Of a) m (Either (Message, ByteStream m a) a)
step ByteStream m a -> ByteStream m a
diffP Result a
res ByteStream m a
p0 = case Result a
res of
A.Fail ByteString
_ [String]
c String
m -> Either (Message, ByteStream m a) a
-> Stream (Of a) m (Either (Message, ByteStream m a) a)
forall (f :: * -> *) (m :: * -> *) r. r -> Stream f m r
Return ((Message, ByteStream m a) -> Either (Message, ByteStream m a) a
forall a b. a -> Either a b
Left (([String]
c,String
m), ByteStream m a -> ByteStream m a
diffP ByteStream m a
p0))
A.Done ByteString
bs a
a | ByteString -> Bool
B.null ByteString
bs -> Of a (Stream (Of a) m (Either (Message, ByteStream m a) a))
-> Stream (Of a) m (Either (Message, ByteStream m a) a)
forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a a
-> Stream (Of a) m (Either (Message, ByteStream m a) a)
-> Of a (Stream (Of a) m (Either (Message, ByteStream m a) a))
forall a b. a -> b -> Of a b
:> ByteStream m a
-> Stream (Of a) m (Either (Message, ByteStream m a) a)
begin ByteStream m a
p0)
| Bool
otherwise -> Of a (Stream (Of a) m (Either (Message, ByteStream m a) a))
-> Stream (Of a) m (Either (Message, ByteStream m a) a)
forall (f :: * -> *) (m :: * -> *) r.
f (Stream f m r) -> Stream f m r
Step (a
a a
-> Stream (Of a) m (Either (Message, ByteStream m a) a)
-> Of a (Stream (Of a) m (Either (Message, ByteStream m a) a))
forall a b. a -> b -> Of a b
:> ByteStream m a
-> Stream (Of a) m (Either (Message, ByteStream m a) a)
begin (ByteString -> ByteStream m ()
forall (m :: * -> *). ByteString -> ByteStream m ()
chunk ByteString
bs ByteStream m () -> ByteStream m a -> ByteStream m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ByteStream m a
p0))
A.Partial ByteString -> Result a
k -> do
Either a (ByteString, ByteStream m a)
x <- m (Either a (ByteString, ByteStream m a))
-> Stream (Of a) m (Either a (ByteString, ByteStream m a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ByteStream m a -> m (Either a (ByteString, ByteStream m a))
forall (m :: * -> *) r.
Monad m =>
ByteStream m r -> m (Either r (ByteString, ByteStream m r))
nextChunk ByteStream m a
p0)
case Either a (ByteString, ByteStream m a)
x of
Left a
e -> (ByteStream m a -> ByteStream m a)
-> Result a
-> ByteStream m a
-> Stream (Of a) m (Either (Message, ByteStream m a) a)
step ByteStream m a -> ByteStream m a
diffP (ByteString -> Result a
k ByteString
forall a. Monoid a => a
mempty) (a -> ByteStream m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
e)
Right (ByteString
bs,ByteStream m a
p1) | ByteString -> Bool
B.null ByteString
bs -> (ByteStream m a -> ByteStream m a)
-> Result a
-> ByteStream m a
-> Stream (Of a) m (Either (Message, ByteStream m a) a)
step ByteStream m a -> ByteStream m a
diffP Result a
res ByteStream m a
p1
| Bool
otherwise -> (ByteStream m a -> ByteStream m a)
-> Result a
-> ByteStream m a
-> Stream (Of a) m (Either (Message, ByteStream m a) a)
step (ByteStream m a -> ByteStream m a
diffP (ByteStream m a -> ByteStream m a)
-> (ByteStream m a -> ByteStream m a)
-> ByteStream m a
-> ByteStream m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> ByteStream m ()
forall (m :: * -> *). ByteString -> ByteStream m ()
chunk ByteString
bs ByteStream m () -> ByteStream m a -> ByteStream m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>)) (ByteString -> Result a
k ByteString
bs) ByteStream m a
p1
{-# INLINABLE parsed #-}