{-| Element-agnostic parsing utilities for @pipes@

    See "Pipes.Parse.Tutorial" for an extended tutorial
-}

{-# LANGUAGE RankNTypes #-}

module Pipes.Parse (
    -- * Parsing
    -- $parsing
      Parser
    , draw
    , skip
    , drawAll
    , skipAll
    , unDraw
    , peek
    , isEndOfInput
    , foldAll
    , foldAllM

    -- * Parsing Lenses
    -- $parsinglenses
    , span
    , splitAt
    , groupBy
    , group

    -- * Utilities
    , toParser
    , toParser_
    , parsed
    , parsed_
    , parseForever
    , parseForever_

    -- * Re-exports
    -- $reexports
    , module Control.Monad.Trans.Class
    , module Control.Monad.Trans.State.Strict
    , module Pipes
    ) where

import Control.Monad (join, forever, liftM)
import Control.Monad.Trans.Class (lift)
import qualified Control.Monad.Trans.State.Strict as S
import Control.Monad.Trans.State.Strict (
    StateT(StateT, runStateT), evalStateT, execStateT )
import Data.Functor.Constant (Constant(Constant, getConstant))
import Data.Foldable (forM_)
import Pipes.Internal (unsafeHoist, closed)
import Pipes (Producer, yield, next)
import Pipes as NoReexport

import Prelude hiding (span, splitAt)

{- $parsing
    @pipes-parse@ handles end-of-input and pushback by storing a 'Producer' in
    a 'StateT' layer.

    Connect 'Parser's to 'Producer's using either 'runStateT', 'evalStateT', or
    'execStateT':

> runStateT  :: Parser a m r -> Producer a m x -> m (r, Producer a m x)
> evalStateT :: Parser a m r -> Producer a m x -> m  r
> execStateT :: Parser a m r -> Producer a m x -> m    (Producer a m x)
>                                                       ^^^^^^^^^^^^^^
>                                                          Leftovers
-}

-- | A 'Parser' is an action that reads from and writes to a stored 'Producer'
type Parser a m r = forall x . StateT (Producer a m x) m r

{-| Draw one element from the underlying 'Producer', returning 'Nothing' if the
    'Producer' is empty
-}
draw :: Monad m => Parser a m (Maybe a)
draw :: Parser a m (Maybe a)
draw = do
    Producer a m x
p <- StateT (Producer a m x) m (Producer a m x)
forall (m :: * -> *) s. Monad m => StateT s m s
S.get
    Either x (a, Producer a m x)
x <- m (Either x (a, Producer a m x))
-> StateT (Producer a m x) m (Either x (a, Producer a m x))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Producer a m x -> m (Either x (a, Producer a m x))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer a m x
p)
    case Either x (a, Producer a m x)
x of
        Left   x
r      -> do
            Producer a m x -> StateT (Producer a m x) m ()
forall (m :: * -> *) s. Monad m => s -> StateT s m ()
S.put (x -> Producer a m x
forall (m :: * -> *) a. Monad m => a -> m a
return x
r)
            Maybe a -> StateT (Producer a m x) m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
        Right (a
a, Producer a m x
p') -> do
            Producer a m x -> StateT (Producer a m x) m ()
forall (m :: * -> *) s. Monad m => s -> StateT s m ()
S.put Producer a m x
p'
            Maybe a -> StateT (Producer a m x) m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
{-# INLINABLE draw #-}

{-| Skip one element from the underlying 'Producer', returning 'True' if
    successful or 'False' if the 'Producer' is empty

> skip = fmap isJust draw
-}
skip :: Monad m => Parser a m Bool
skip :: Parser a m Bool
skip = do
    Maybe a
x <- StateT (Producer a m x) m (Maybe a)
forall (m :: * -> *) a. Monad m => Parser a m (Maybe a)
draw
    Bool -> StateT (Producer a m x) m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> StateT (Producer a m x) m Bool)
-> Bool -> StateT (Producer a m x) m Bool
forall a b. (a -> b) -> a -> b
$ case Maybe a
x of
        Maybe a
Nothing -> Bool
False
        Just a
_  -> Bool
True
{-# INLINABLE skip #-}

{-| Draw all elements from the underlying 'Producer'

    Note that 'drawAll' is not an idiomatic use of @pipes-parse@, but I provide
    it for simple testing purposes.  Idiomatic @pipes-parse@ style consumes the
    elements immediately as they are generated instead of loading all elements
    into memory.  For example, you can use 'foldAll' or 'foldAllM' for this
    purpose.
-}
drawAll :: Monad m => Parser a m [a]
drawAll :: Parser a m [a]
drawAll = ([a] -> [a]) -> StateT (Producer a m x) m [a]
forall (m :: * -> *) a c x.
Monad m =>
([a] -> c) -> StateT (Producer a m x) m c
go [a] -> [a]
forall a. a -> a
id
  where
    go :: ([a] -> c) -> StateT (Producer a m x) m c
go [a] -> c
diffAs = do
        Maybe a
x <- StateT (Producer a m x) m (Maybe a)
forall (m :: * -> *) a. Monad m => Parser a m (Maybe a)
draw
        case Maybe a
x of
            Maybe a
Nothing -> c -> StateT (Producer a m x) m c
forall (m :: * -> *) a. Monad m => a -> m a
return ([a] -> c
diffAs [])
            Just a
a  -> ([a] -> c) -> StateT (Producer a m x) m c
go ([a] -> c
diffAs ([a] -> c) -> ([a] -> [a]) -> [a] -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
aa -> [a] -> [a]
forall a. a -> [a] -> [a]
:))
{-# INLINABLE drawAll #-}

-- | Drain all elements from the underlying 'Producer'
skipAll :: Monad m => Parser a m ()
skipAll :: Parser a m ()
skipAll = StateT (Producer a m x) m ()
forall a x. StateT (Producer a m x) m ()
go
  where
    go :: StateT (Producer a m x) m ()
go = do
        Maybe a
x <- StateT (Producer a m x) m (Maybe a)
forall (m :: * -> *) a. Monad m => Parser a m (Maybe a)
draw
        case Maybe a
x  of
            Maybe a
Nothing -> () -> StateT (Producer a m x) m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
            Just a
_  -> StateT (Producer a m x) m ()
go
{-# INLINABLE skipAll #-}

-- | Push back an element onto the underlying 'Producer'
unDraw :: Monad m => a -> Parser a m ()
unDraw :: a -> Parser a m ()
unDraw a
a = (Proxy X () () a m x -> Proxy X () () a m x)
-> StateT (Proxy X () () a m x) m ()
forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
S.modify (a -> Proxy X () () a m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a Proxy X () () a m () -> Proxy X () () a m x -> Proxy X () () a m x
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>)
{-# INLINABLE unDraw #-}

{-| 'peek' checks the first element of the stream, but uses 'unDraw' to push the
    element back so that it is available for the next 'draw' command.

> peek = do
>     x <- draw
>     case x of
>         Nothing -> return ()
>         Just a  -> unDraw a
>     return x
-}
peek :: Monad m => Parser a m (Maybe a)
peek :: Parser a m (Maybe a)
peek = do
    Maybe a
x <- StateT (Producer a m x) m (Maybe a)
forall (m :: * -> *) a. Monad m => Parser a m (Maybe a)
draw
    Maybe a
-> (a -> StateT (Producer a m x) m ())
-> StateT (Producer a m x) m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe a
x ((a -> StateT (Producer a m x) m ())
 -> StateT (Producer a m x) m ())
-> (a -> StateT (Producer a m x) m ())
-> StateT (Producer a m x) m ()
forall a b. (a -> b) -> a -> b
$ \a
a -> a -> Parser a m ()
forall (m :: * -> *) a. Monad m => a -> Parser a m ()
unDraw a
a
    Maybe a -> StateT (Producer a m x) m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
x
{-# INLINABLE peek #-}

{-| Check if the underlying 'Producer' is empty

> isEndOfInput = fmap isNothing peek
-}
isEndOfInput :: Monad m => Parser a m Bool
isEndOfInput :: Parser a m Bool
isEndOfInput = do
    Maybe a
x <- StateT (Producer a m x) m (Maybe a)
forall (m :: * -> *) a. Monad m => Parser a m (Maybe a)
peek
    Bool -> StateT (Producer a m x) m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (case Maybe a
x of
        Maybe a
Nothing -> Bool
True
        Just a
_  -> Bool
False )
{-# INLINABLE isEndOfInput #-}

{-| Fold all input values

> Control.Foldl.purely foldAll :: Monad m => Fold a b -> Parser a m b
-}
foldAll
    :: Monad m
    => (x -> a -> x)
    -- ^ Step function
    -> x
    -- ^ Initial accumulator
    -> (x -> b)
    -- ^ Extraction function
    -> Parser a m b
foldAll :: (x -> a -> x) -> x -> (x -> b) -> Parser a m b
foldAll x -> a -> x
step x
begin x -> b
done = x -> StateT (Producer a m x) m b
forall (m :: * -> *) x. Monad m => x -> StateT (Producer a m x) m b
go x
begin
  where
    go :: x -> StateT (Producer a m x) m b
go x
x = do
        Maybe a
ea <- StateT (Producer a m x) m (Maybe a)
forall (m :: * -> *) a. Monad m => Parser a m (Maybe a)
draw
        case Maybe a
ea of
            Maybe a
Nothing -> b -> StateT (Producer a m x) m b
forall (m :: * -> *) a. Monad m => a -> m a
return (x -> b
done x
x)
            Just a
a  -> x -> StateT (Producer a m x) m b
go (x -> StateT (Producer a m x) m b)
-> x -> StateT (Producer a m x) m b
forall a b. (a -> b) -> a -> b
$! x -> a -> x
step x
x a
a
{-# INLINABLE foldAll #-}

{-| Fold all input values monadically

> Control.Foldl.impurely foldAllM :: Monad m => FoldM a m b -> Parser a m b
-}
foldAllM
    :: Monad m
    => (x -> a -> m x)
    -- ^ Step function
    -> m x
    -- ^ Initial accumulator
    -> (x -> m b)
    -- ^ Extraction function
    -> Parser a m b
foldAllM :: (x -> a -> m x) -> m x -> (x -> m b) -> Parser a m b
foldAllM x -> a -> m x
step m x
begin x -> m b
done = do
    x
x0 <- m x -> StateT (Producer a m x) m x
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m x
begin
    x -> StateT (Producer a m x) m b
forall x. x -> StateT (Producer a m x) m b
go x
x0
  where
    go :: x -> StateT (Producer a m x) m b
go x
x = do
        Maybe a
ea <- StateT (Producer a m x) m (Maybe a)
forall (m :: * -> *) a. Monad m => Parser a m (Maybe a)
draw
        case Maybe a
ea of
            Maybe a
Nothing -> m b -> StateT (Producer a m x) m b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (x -> m b
done x
x)
            Just a
a  -> do
                x
x' <- m x -> StateT (Producer a m x) m x
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (x -> a -> m x
step x
x a
a)
                x -> StateT (Producer a m x) m b
go (x -> StateT (Producer a m x) m b)
-> x -> StateT (Producer a m x) m b
forall a b. (a -> b) -> a -> b
$! x
x'
{-# INLINABLE foldAllM #-}

{- $parsinglenses
    Connect lenses to 'Producer's using ('Lens.Family.^.') or
    'Lens.Family.view':

> (^.) :: Producer a m x
>      -> Lens' (Producer a m x) (Producer b m y)
>      -> Producer b m y

    Connect lenses to 'Parser's using 'Lens.Family.State.Strict.zoom':

> zoom :: Lens' (Producer a m x) (Producer b m y)
>      -> Parser b m r
>      -> Parser a m r

    Connect lenses to each other using ('.') (i.e. function composition):

> (.) :: Lens' (Producer a m x) (Producer b m y)
>     -> Lens' (Producer b m y) (Producer c m z)
>     -> Lens' (Producer a m y) (Producer c m z)
-}

type Lens' a b = forall f . (Functor f) => (b -> f b) -> a -> f a

{-| 'span' is an improper lens that splits the 'Producer' into two 'Producer's,
    where the outer 'Producer' is the longest consecutive group of elements that
    satisfy the predicate
-}
span
    :: Monad m
    => (a -> Bool) -> Lens' (Producer a m x) (Producer a m (Producer a m x))
span :: (a -> Bool)
-> Lens' (Producer a m x) (Producer a m (Producer a m x))
span a -> Bool
predicate Producer a m (Producer a m x) -> f (Producer a m (Producer a m x))
k Producer a m x
p0 = (Producer a m (Producer a m x) -> Producer a m x)
-> f (Producer a m (Producer a m x)) -> f (Producer a m x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Producer a m (Producer a m x) -> Producer a m x
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Producer a m (Producer a m x) -> f (Producer a m (Producer a m x))
k (Producer a m x -> Producer a m (Producer a m x)
forall (m :: * -> *) b x' x.
Monad m =>
Producer a m b -> Proxy x' x () a m (Producer a m b)
to Producer a m x
p0))
  where
--  to :: Monad m => Producer a m x -> Producer a m (Producer a m x)
    to :: Producer a m b -> Proxy x' x () a m (Producer a m b)
to Producer a m b
p = do
        Either b (a, Producer a m b)
x <- m (Either b (a, Producer a m b))
-> Proxy x' x () a m (Either b (a, Producer a m b))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Producer a m b -> m (Either b (a, Producer a m b))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer a m b
p)
        case Either b (a, Producer a m b)
x of
            Left   b
r      -> Producer a m b -> Proxy x' x () a m (Producer a m b)
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Producer a m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
r)
            Right (a
a, Producer a m b
p') ->
                if a -> Bool
predicate a
a
                then do
                    a -> Proxy x' x () a m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a
                    Producer a m b -> Proxy x' x () a m (Producer a m b)
to Producer a m b
p'
                else Producer a m b -> Proxy x' x () a m (Producer a m b)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Proxy X () () a m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a Proxy X () () a m () -> Producer a m b -> Producer a m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer a m b
p')
{-# INLINABLE span #-}

{-| 'splitAt' is an improper lens that splits a 'Producer' into two 'Producer's
    after a fixed number of elements
-}
splitAt
    :: Monad m
    => Int -> Lens' (Producer a m x) (Producer a m (Producer a m x))
splitAt :: Int -> Lens' (Producer a m x) (Producer a m (Producer a m x))
splitAt Int
n0 Producer a m (Producer a m x) -> f (Producer a m (Producer a m x))
k Producer a m x
p0 = (Producer a m (Producer a m x) -> Producer a m x)
-> f (Producer a m (Producer a m x)) -> f (Producer a m x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Producer a m (Producer a m x) -> Producer a m x
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Producer a m (Producer a m x) -> f (Producer a m (Producer a m x))
k (Int -> Producer a m x -> Producer a m (Producer a m x)
forall t (m :: * -> *) a a x' x.
(Ord t, Num t, Monad m) =>
t -> Producer a m a -> Proxy x' x () a m (Producer a m a)
to Int
n0 Producer a m x
p0))
  where
--  to :: Monad m => Int -> Producer a m x -> Producer a m (Producer a m x)
    to :: t -> Producer a m a -> Proxy x' x () a m (Producer a m a)
to t
n Producer a m a
p =
        if t
n t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0
        then Producer a m a -> Proxy x' x () a m (Producer a m a)
forall (m :: * -> *) a. Monad m => a -> m a
return Producer a m a
p
        else do
            Either a (a, Producer a m a)
x <- m (Either a (a, Producer a m a))
-> Proxy x' x () a m (Either a (a, Producer a m a))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Producer a m a -> m (Either a (a, Producer a m a))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer a m a
p)
            case Either a (a, Producer a m a)
x of
                Left   a
r      -> Producer a m a -> Proxy x' x () a m (Producer a m a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Producer a m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
r)
                Right (a
a, Producer a m a
p') -> do
                    a -> Proxy x' x () a m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a
                    t -> Producer a m a -> Proxy x' x () a m (Producer a m a)
to (t
n t -> t -> t
forall a. Num a => a -> a -> a
- t
1) Producer a m a
p'
{-# INLINABLE splitAt #-}

(^.) :: a -> ((b -> Constant b b) -> a -> Constant b a) -> b
a
a ^. :: a -> ((b -> Constant b b) -> a -> Constant b a) -> b
^. (b -> Constant b b) -> a -> Constant b a
lens = Constant b a -> b
forall a k (b :: k). Constant a b -> a
getConstant ((b -> Constant b b) -> a -> Constant b a
lens b -> Constant b b
forall k a (b :: k). a -> Constant a b
Constant a
a)

{-| 'groupBy' splits a 'Producer' into two 'Producer's after the first group of
     elements that are equal according to the equality predicate
-}
groupBy
    :: Monad m
    => (a -> a -> Bool)
    -> Lens' (Producer a m x) (Producer a m (Producer a m x))
groupBy :: (a -> a -> Bool)
-> Lens' (Producer a m x) (Producer a m (Producer a m x))
groupBy a -> a -> Bool
equals Producer a m (Producer a m x) -> f (Producer a m (Producer a m x))
k Producer a m x
p0 = (Producer a m (Producer a m x) -> Producer a m x)
-> f (Producer a m (Producer a m x)) -> f (Producer a m x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Producer a m (Producer a m x) -> Producer a m x
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Producer a m (Producer a m x) -> f (Producer a m (Producer a m x))
k (Producer a m x -> Producer a m (Producer a m x)
forall (m :: * -> *) b.
Monad m =>
Producer a m b -> Proxy X () () a m (Producer a m b)
to Producer a m x
p0))
  where
--  to :: Monad m => Producer a m r -> Producer a m (Producer a m x)
    to :: Producer a m b -> Proxy X () () a m (Producer a m b)
to Producer a m b
p = do
        Either b (a, Producer a m b)
x <- m (Either b (a, Producer a m b))
-> Proxy X () () a m (Either b (a, Producer a m b))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Producer a m b -> m (Either b (a, Producer a m b))
forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer a m b
p)
        case Either b (a, Producer a m b)
x of
            Left   b
r      -> Producer a m b -> Proxy X () () a m (Producer a m b)
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Producer a m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
r)
            Right (a
a, Producer a m b
p') -> (a -> Proxy X () () a m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a Proxy X () () a m () -> Producer a m b -> Producer a m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer a m b
p') Producer a m b
-> ((Proxy X () () a m (Producer a m b)
     -> Constant
          (Proxy X () () a m (Producer a m b))
          (Proxy X () () a m (Producer a m b)))
    -> Producer a m b
    -> Constant (Proxy X () () a m (Producer a m b)) (Producer a m b))
-> Proxy X () () a m (Producer a m b)
forall a b. a -> ((b -> Constant b b) -> a -> Constant b a) -> b
^. (a -> Bool)
-> Lens' (Producer a m b) (Proxy X () () a m (Producer a m b))
forall (m :: * -> *) a x.
Monad m =>
(a -> Bool)
-> Lens' (Producer a m x) (Producer a m (Producer a m x))
span (a -> a -> Bool
equals a
a)
{-# INLINABLE groupBy #-}

-- | Like 'groupBy', where the equality predicate is ('==')
group
    :: (Monad m, Eq a) => Lens' (Producer a m x) (Producer a m (Producer a m x))
group :: Lens' (Producer a m x) (Producer a m (Producer a m x))
group = (a -> a -> Bool)
-> Lens' (Producer a m x) (Producer a m (Producer a m x))
forall (m :: * -> *) a x.
Monad m =>
(a -> a -> Bool)
-> Lens' (Producer a m x) (Producer a m (Producer a m x))
groupBy a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==)
{-# INLINABLE group #-}

{-| Convert a 'Consumer' to a 'Parser'

    'Nothing' signifies end of input
-}
toParser :: Monad m => Consumer (Maybe a) m r -> Parser a m r
toParser :: Consumer (Maybe a) m r -> Parser a m r
toParser Consumer (Maybe a) m r
consumer = Effect (StateT (Producer a m x) m) r -> StateT (Producer a m x) m r
forall (m :: * -> *) r. Monad m => Effect m r -> m r
runEffect (StateT (Producer a m x) m (Maybe a)
-> Proxy X () () X (StateT (Producer a m x) m) (Maybe a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift StateT (Producer a m x) m (Maybe a)
forall (m :: * -> *) a. Monad m => Parser a m (Maybe a)
draw Proxy X () () X (StateT (Producer a m x) m) (Maybe a)
-> Proxy () (Maybe a) () X (StateT (Producer a m x) m) r
-> Effect (StateT (Producer a m x) m) r
forall (m :: * -> *) a' a y' y b c.
Functor m =>
Proxy a' a y' y m b -> Proxy () b y' y m c -> Proxy a' a y' y m c
>~ (forall x. m x -> StateT (Producer a m x) m x)
-> Consumer (Maybe a) m r
-> Proxy () (Maybe a) () X (StateT (Producer a m x) m) r
forall (m :: * -> *) (n :: * -> *) a' a b' b r.
Functor m =>
(forall x. m x -> n x)
-> Proxy a' a b' b m r -> Proxy a' a b' b n r
unsafeHoist forall x. m x -> StateT (Producer a m x) m x
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift Consumer (Maybe a) m r
consumer)
{-# INLINABLE toParser #-}

-- | Convert a never-ending 'Consumer' to a 'Parser'
toParser_ :: Monad m => Consumer a m X -> Parser a m ()
toParser_ :: Consumer a m X -> Parser a m ()
toParser_ Consumer a m X
consumer = (Proxy X () () a m x -> m ((), Proxy X () () a m x))
-> StateT (Proxy X () () a m x) m ()
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT ((Proxy X () () a m x -> m ((), Proxy X () () a m x))
 -> StateT (Proxy X () () a m x) m ())
-> (Proxy X () () a m x -> m ((), Proxy X () () a m x))
-> StateT (Proxy X () () a m x) m ()
forall a b. (a -> b) -> a -> b
$ \Proxy X () () a m x
producer -> do
    x
r <- Effect m x -> m x
forall (m :: * -> *) r. Monad m => Effect m r -> m r
runEffect (Proxy X () () a m x
producer Proxy X () () a m x -> Proxy () a () X m x -> Effect m x
forall (m :: * -> *) a' a b r c' c.
Functor m =>
Proxy a' a () b m r -> Proxy () b c' c m r -> Proxy a' a c' c m r
>-> (X -> x) -> Consumer a m X -> Proxy () a () X m x
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap X -> x
forall a. X -> a
closed Consumer a m X
consumer)
    ((), Proxy X () () a m x) -> m ((), Proxy X () () a m x)
forall (m :: * -> *) a. Monad m => a -> m a
return ((), x -> Proxy X () () a m x
forall (m :: * -> *) a. Monad m => a -> m a
return x
r)
{-# INLINABLE toParser_ #-}


{-| Run a `Parser` repeatedly on a `Producer`, `yield`ing each `Right result

    Returns the remainder of the `Producer` when the `Parser` returns `Left`
-}
parsed
    :: Monad m
    => Parser a m (Either e b)
    -> Producer a m r -> Producer b m (e, Producer a m r)
parsed :: Parser a m (Either e b)
-> Producer a m r -> Producer b m (e, Producer a m r)
parsed Parser a m (Either e b)
parser = Producer a m r -> Producer b m (e, Producer a m r)
forall x x' x.
Producer a m x -> Proxy x' x () b m (e, Producer a m x)
go
  where
    go :: Producer a m x -> Proxy x' x () b m (e, Producer a m x)
go Producer a m x
p = do
        (Either e b
x, Producer a m x
p') <- m (Either e b, Producer a m x)
-> Proxy x' x () b m (Either e b, Producer a m x)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (StateT (Producer a m x) m (Either e b)
-> Producer a m x -> m (Either e b, Producer a m x)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT (Producer a m x) m (Either e b)
Parser a m (Either e b)
parser Producer a m x
p)
        case Either e b
x of
            Left  e
r -> (e, Producer a m x) -> Proxy x' x () b m (e, Producer a m x)
forall (m :: * -> *) a. Monad m => a -> m a
return (e
r, Producer a m x
p')
            Right b
b -> do
                b -> Proxy x' x () b m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield b
b
                Producer a m x -> Proxy x' x () b m (e, Producer a m x)
go Producer a m x
p'
{-# INLINABLE parsed #-}

{-| Run a `Parser` repeatedly on a `Producer`, `yield`ing each `Just` result

    Returns the remainder of the `Producer` when the `Parser` returns `Nothing`
-}
parsed_
    :: Monad m
    => Parser a m (Maybe b)
    -> Producer a m r
    -> Producer b m (Producer a m r)
parsed_ :: Parser a m (Maybe b)
-> Producer a m r -> Producer b m (Producer a m r)
parsed_ Parser a m (Maybe b)
parser Producer a m r
p = do
    ((), Producer a m r
p') <- Parser a m (Either () b)
-> Producer a m r -> Producer b m ((), Producer a m r)
forall (m :: * -> *) a e b r.
Monad m =>
Parser a m (Either e b)
-> Producer a m r -> Producer b m (e, Producer a m r)
parsed Parser a m (Either () b)
parser' Producer a m r
p
    Producer a m r -> Producer b m (Producer a m r)
forall (m :: * -> *) a. Monad m => a -> m a
return Producer a m r
p'
  where
    parser' :: StateT (Producer a m x) m (Either () b)
parser' = do
        Maybe b
x <- StateT (Producer a m x) m (Maybe b)
Parser a m (Maybe b)
parser
        Either () b -> StateT (Producer a m x) m (Either () b)
forall (m :: * -> *) a. Monad m => a -> m a
return (case Maybe b
x of
            Maybe b
Nothing -> () -> Either () b
forall a b. a -> Either a b
Left ()
            Just b
b  -> b -> Either () b
forall a b. b -> Either a b
Right b
b )
{-# INLINABLE parsed_ #-}

-- | Convert a 'Parser' to a 'Pipe' by running it repeatedly on the input
parseForever ::
  Monad m =>
  (forall n. Monad n => Parser a n (Either r b)) ->
  Pipe a b m r
parseForever :: (forall (n :: * -> *). Monad n => Parser a n (Either r b))
-> Pipe a b m r
parseForever forall (n :: * -> *). Monad n => Parser a n (Either r b)
parse = Producer a (Proxy () a () b m) Any -> Pipe a b m r
forall (m :: * -> *) x' x x.
Functor m =>
Producer a (Proxy x' x () b m) x -> Proxy x' x () b m r
go (Proxy X () () a (Proxy () a () b m) ()
-> Producer a (Proxy () a () b m) Any
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (Proxy () a () b m a -> Proxy X () () a (Proxy () a () b m) a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift Proxy () a () b m a
forall (m :: * -> *) a. Functor m => Consumer' a m a
await Proxy X () () a (Proxy () a () b m) a
-> (a -> Proxy X () () a (Proxy () a () b m) ())
-> Proxy X () () a (Proxy () a () b m) ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Proxy X () () a (Proxy () a () b m) ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield))
  where go :: Producer a (Proxy x' x () b m) x -> Proxy x' x () b m r
go Producer a (Proxy x' x () b m) x
prod = do (Either r b
b, Producer a (Proxy x' x () b m) x
prod') <- StateT
  (Producer a (Proxy x' x () b m) x) (Proxy x' x () b m) (Either r b)
-> Producer a (Proxy x' x () b m) x
-> Proxy x' x () b m (Either r b, Producer a (Proxy x' x () b m) x)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT
  (Producer a (Proxy x' x () b m) x) (Proxy x' x () b m) (Either r b)
forall (n :: * -> *). Monad n => Parser a n (Either r b)
parse Producer a (Proxy x' x () b m) x
prod
                     (r -> Proxy x' x () b m r)
-> (b -> Proxy x' x () b m r) -> Either r b -> Proxy x' x () b m r
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either r -> Proxy x' x () b m r
forall (m :: * -> *) a. Monad m => a -> m a
return ((Proxy x' x () b m () -> Proxy x' x () b m r -> Proxy x' x () b m r
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer a (Proxy x' x () b m) x -> Proxy x' x () b m r
go Producer a (Proxy x' x () b m) x
prod') (Proxy x' x () b m () -> Proxy x' x () b m r)
-> (b -> Proxy x' x () b m ()) -> b -> Proxy x' x () b m r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Proxy x' x () b m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield) Either r b
b
{-# DEPRECATED parseForever "Use `parsed` instead" #-}

-- | Variant of `parseForever` for parsers which return a Maybe
-- instead of an Either
parseForever_ ::
  Monad m =>
  (forall n. Monad n => Parser a n (Maybe b)) ->
  Pipe a b m ()
parseForever_ :: (forall (n :: * -> *). Monad n => Parser a n (Maybe b))
-> Pipe a b m ()
parseForever_ forall (n :: * -> *). Monad n => Parser a n (Maybe b)
parse = (forall (n :: * -> *). Monad n => Parser a n (Either () b))
-> Pipe a b m ()
forall (m :: * -> *) a r b.
Monad m =>
(forall (n :: * -> *). Monad n => Parser a n (Either r b))
-> Pipe a b m r
parseForever ((Maybe b -> Either () b)
-> StateT (Producer a n x) n (Maybe b)
-> StateT (Producer a n x) n (Either () b)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (Either () b -> (b -> Either () b) -> Maybe b -> Either () b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> Either () b
forall a b. a -> Either a b
Left ()) b -> Either () b
forall a b. b -> Either a b
Right) StateT (Producer a n x) n (Maybe b)
forall (n :: * -> *). Monad n => Parser a n (Maybe b)
parse)
{-# DEPRECATED parseForever_ "Use `parsed_` instead" #-}

{- $reexports
    "Control.Monad.Trans.Class" re-exports 'lift'.

    "Control.Monad.Trans.State.Strict" re-exports 'StateT', 'runStateT',
    'evalStateT', and 'execStateT'.

    "Pipes" re-exports 'Producer', 'yield', and 'next'.
-}