-- | @pipes@ utilities for incrementally running @attoparsec@-based parsers.
--
-- This module assumes familiarity with @pipes-parse@, you can learn about it in
-- "Pipes.Parse.Tutorial".

{-# LANGUAGE BangPatterns       #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleInstances  #-}
{-# LANGUAGE RankNTypes         #-}

module Pipes.Attoparsec (
    -- * Parsing
      parse
    , parsed

    -- ** Including input length
    --
    -- $lengths
    , parseL
    , parsedL

    -- * Utils
    , isEndOfParserInput

    -- * Types
    , ParserInput
    , ParsingError(..)
    ) where

import           Control.Exception                (Exception)
import qualified Control.Monad.Trans.State.Strict as S
import qualified Data.Attoparsec.ByteString
import qualified Data.Attoparsec.Text
import           Data.Attoparsec.Types            (IResult (..))
import qualified Data.Attoparsec.Types            as Attoparsec
import           Data.ByteString                  (ByteString)
import qualified Data.ByteString
import           Data.Data                        (Data, Typeable)
import           Data.Text                        (Text)
import qualified Data.Text
import           Pipes
import qualified Pipes.Parse                      as Pipes (Parser)

--------------------------------------------------------------------------------

-- | Convert an @attoparsec@ 'Attoparsec.Parser' to a @pipes-parse@
-- 'Pipes.Parser'.
--
-- This 'Pipes.Parser' is compatible with the tools from "Pipes.Parse".
--
-- It returns 'Nothing' if the underlying 'Producer' is exhausted, otherwise
-- it attempts to run the given attoparsec 'Attoparsec.Parser' on the underlying
-- 'Producer', possibly failing with 'ParsingError'.
parse
  :: (Monad m, ParserInput a)
  => Attoparsec.Parser a b                            -- ^ Attoparsec parser
  -> Pipes.Parser a m (Maybe (Either ParsingError b)) -- ^ Pipes parser
parse :: forall (m :: * -> *) a b.
(Monad m, ParserInput a) =>
Parser a b -> Parser a m (Maybe (Either ParsingError b))
parse Parser a b
parser = forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
S.StateT forall a b. (a -> b) -> a -> b
$ \Producer a m x
p0 -> do
    Either x (a, Producer a m x)
x <- forall (m :: * -> *) a r.
(Monad m, Eq a, Monoid a) =>
Producer a m r -> m (Either r (a, Producer a m r))
nextSkipEmpty Producer a m x
p0
    case Either x (a, Producer a m x)
x of
      Left x
r       -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Maybe a
Nothing, forall (m :: * -> *) a. Monad m => a -> m a
return x
r)
      Right (a
a,Producer a m x
p1) -> forall {m :: * -> *} {a} {a} {b}.
(Monad m, Eq a, Monoid a) =>
(Proxy X () () a m a -> Proxy X () () a m a)
-> IResult a b
-> Proxy X () () a m a
-> m (Maybe (Either ParsingError b), Proxy X () () a m a)
step (forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>) (forall a b. ParserInput a => Parser a b -> a -> IResult a b
_parse Parser a b
parser a
a) Producer a m x
p1
  where
    step :: (Proxy X () () a m a -> Proxy X () () a m a)
-> IResult a b
-> Proxy X () () a m a
-> m (Maybe (Either ParsingError b), Proxy X () () a m a)
step Proxy X () () a m a -> Proxy X () () a m a
diffP IResult a b
res Proxy X () () a m a
p0 = case IResult a b
res of
      Fail a
_ [String]
c String
m -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just (forall a b. a -> Either a b
Left ([String] -> String -> ParsingError
ParsingError [String]
c String
m)), Proxy X () () a m a -> Proxy X () () a m a
diffP Proxy X () () a m a
p0)
      Done a
a b
b   -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just (forall a b. b -> Either a b
Right b
b), forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Proxy X () () a m a
p0)
      Partial a -> IResult a b
k  -> do
        Either a (a, Proxy X () () a m a)
x <- forall (m :: * -> *) a r.
(Monad m, Eq a, Monoid a) =>
Producer a m r -> m (Either r (a, Producer a m r))
nextSkipEmpty Proxy X () () a m a
p0
        case Either a (a, Proxy X () () a m a)
x of
          Left a
e -> (Proxy X () () a m a -> Proxy X () () a m a)
-> IResult a b
-> Proxy X () () a m a
-> m (Maybe (Either ParsingError b), Proxy X () () a m a)
step Proxy X () () a m a -> Proxy X () () a m a
diffP (a -> IResult a b
k forall a. Monoid a => a
mempty) (forall (m :: * -> *) a. Monad m => a -> m a
return a
e)
          Right (a
a,Proxy X () () a m a
p1) -> (Proxy X () () a m a -> Proxy X () () a m a)
-> IResult a b
-> Proxy X () () a m a
-> m (Maybe (Either ParsingError b), Proxy X () () a m a)
step (Proxy X () () a m a -> Proxy X () () a m a
diffP forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>)) (a -> IResult a b
k a
a) Proxy X () () a m a
p1
{-# INLINABLE parse #-}


-- | Convert a producer of 'ParserInput' to a producer of parsed values.
--
-- This producer returns 'Right' when end-of-input is reached successfully,
-- otherwise it returns a 'ParsingError' and the leftovers including
-- the malformed input that couldn't be parsed. You can use 'Pipes.Lift.errorP'
-- to promote the 'Either' return value to an 'Control.Monad.Trans.Error.ErrorT'
-- monad transformer.
parsed
  :: (Monad m, ParserInput a)
  => Attoparsec.Parser a b  -- ^ Attoparsec parser
  -> Producer a m r         -- ^ Raw input
  -> Producer b m (Either (ParsingError, Producer a m r) r)
parsed :: forall (m :: * -> *) a b r.
(Monad m, ParserInput a) =>
Parser a b
-> Producer a m r
-> Producer b m (Either (ParsingError, Producer a m r) r)
parsed Parser a b
parser = forall {m :: * -> *} {r} {x'} {x}.
Monad m =>
Producer a m r
-> Proxy x' x () b m (Either (ParsingError, Producer a m r) r)
go
  where
    go :: Producer a m r
-> Proxy x' x () b m (Either (ParsingError, Producer a m r) r)
go Producer a m r
p0 = do
      Either r (a, Producer a m r)
x <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (m :: * -> *) a r.
(Monad m, Eq a, Monoid a) =>
Producer a m r -> m (Either r (a, Producer a m r))
nextSkipEmpty Producer a m r
p0)
      case Either r (a, Producer a m r)
x of
        Left r
r       -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right r
r)
        Right (a
a,Producer a m r
p1) -> (Producer a m r -> Producer a m r)
-> IResult a b
-> Producer a m r
-> Proxy x' x () b m (Either (ParsingError, Producer a m r) r)
step (forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>) (forall a b. ParserInput a => Parser a b -> a -> IResult a b
_parse Parser a b
parser a
a) Producer a m r
p1
    step :: (Producer a m r -> Producer a m r)
-> IResult a b
-> Producer a m r
-> Proxy x' x () b m (Either (ParsingError, Producer a m r) r)
step Producer a m r -> Producer a m r
diffP IResult a b
res Producer a m r
p0 = case IResult a b
res of
      Fail a
_ [String]
c String
m -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left ([String] -> String -> ParsingError
ParsingError [String]
c String
m, Producer a m r -> Producer a m r
diffP Producer a m r
p0))
      Done a
a b
b   -> forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield b
b forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer a m r
-> Proxy x' x () b m (Either (ParsingError, Producer a m r) r)
go (forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer a m r
p0)
      Partial a -> IResult a b
k  -> do
        Either r (a, Producer a m r)
x <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (m :: * -> *) a r.
(Monad m, Eq a, Monoid a) =>
Producer a m r -> m (Either r (a, Producer a m r))
nextSkipEmpty Producer a m r
p0)
        case Either r (a, Producer a m r)
x of
          Left r
e -> (Producer a m r -> Producer a m r)
-> IResult a b
-> Producer a m r
-> Proxy x' x () b m (Either (ParsingError, Producer a m r) r)
step Producer a m r -> Producer a m r
diffP (a -> IResult a b
k forall a. Monoid a => a
mempty) (forall (m :: * -> *) a. Monad m => a -> m a
return r
e)
          Right (a
a,Producer a m r
p1) -> (Producer a m r -> Producer a m r)
-> IResult a b
-> Producer a m r
-> Proxy x' x () b m (Either (ParsingError, Producer a m r) r)
step (Producer a m r -> Producer a m r
diffP forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>)) (a -> IResult a b
k a
a) Producer a m r
p1
{-# INLINABLE parsed #-}

--------------------------------------------------------------------------------
-- $lengths
-- Like the functions above, but these also provide information about
-- the length of input consumed in order to fully parse each value.
--------------------------------------------------------------------------------

-- | Like 'parse', but also returns the length of input consumed to parse the
-- value.
parseL
    :: (Monad m, ParserInput a)
    => Attoparsec.Parser a b                           -- ^ Attoparsec parser
    -> Pipes.Parser a m (Maybe (Either ParsingError (Int, b))) -- ^ Pipes parser
parseL :: forall (m :: * -> *) a b.
(Monad m, ParserInput a) =>
Parser a b -> Parser a m (Maybe (Either ParsingError (Int, b)))
parseL Parser a b
parser = forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
S.StateT forall a b. (a -> b) -> a -> b
$ \Producer a m x
p0 -> do
    Either x (a, Producer a m x)
x <- forall (m :: * -> *) a r.
(Monad m, Eq a, Monoid a) =>
Producer a m r -> m (Either r (a, Producer a m r))
nextSkipEmpty Producer a m x
p0
    case Either x (a, Producer a m x)
x of
      Left x
r       -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Maybe a
Nothing, forall (m :: * -> *) a. Monad m => a -> m a
return x
r)
      Right (a
a,Producer a m x
p1) -> forall {m :: * -> *} {a} {a} {b}.
(Monad m, ParserInput a) =>
(Proxy X () () a m a -> Proxy X () () a m a)
-> IResult a b
-> Proxy X () () a m a
-> Int
-> m (Maybe (Either ParsingError (Int, b)), Proxy X () () a m a)
step (forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>) (forall a b. ParserInput a => Parser a b -> a -> IResult a b
_parse Parser a b
parser a
a) Producer a m x
p1 (forall a. ParserInput a => a -> Int
_length a
a)
  where
    step :: (Proxy X () () a m a -> Proxy X () () a m a)
-> IResult a b
-> Proxy X () () a m a
-> Int
-> m (Maybe (Either ParsingError (Int, b)), Proxy X () () a m a)
step Proxy X () () a m a -> Proxy X () () a m a
diffP IResult a b
res Proxy X () () a m a
p0 !Int
len = case IResult a b
res of
      Fail a
_ [String]
c String
m -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just (forall a b. a -> Either a b
Left ([String] -> String -> ParsingError
ParsingError [String]
c String
m)), Proxy X () () a m a -> Proxy X () () a m a
diffP Proxy X () () a m a
p0)
      Done a
a b
b   -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just (forall a b. b -> Either a b
Right (Int
len forall a. Num a => a -> a -> a
- forall a. ParserInput a => a -> Int
_length a
a, b
b)), forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Proxy X () () a m a
p0)
      Partial a -> IResult a b
k  -> do
        Either a (a, Proxy X () () a m a)
x <- forall (m :: * -> *) a r.
(Monad m, Eq a, Monoid a) =>
Producer a m r -> m (Either r (a, Producer a m r))
nextSkipEmpty Proxy X () () a m a
p0
        case Either a (a, Proxy X () () a m a)
x of
          Left a
e -> (Proxy X () () a m a -> Proxy X () () a m a)
-> IResult a b
-> Proxy X () () a m a
-> Int
-> m (Maybe (Either ParsingError (Int, b)), Proxy X () () a m a)
step Proxy X () () a m a -> Proxy X () () a m a
diffP (a -> IResult a b
k forall a. Monoid a => a
mempty) (forall (m :: * -> *) a. Monad m => a -> m a
return a
e) Int
len
          Right (a
a,Proxy X () () a m a
p1) -> (Proxy X () () a m a -> Proxy X () () a m a)
-> IResult a b
-> Proxy X () () a m a
-> Int
-> m (Maybe (Either ParsingError (Int, b)), Proxy X () () a m a)
step (Proxy X () () a m a -> Proxy X () () a m a
diffP forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>)) (a -> IResult a b
k a
a) Proxy X () () a m a
p1 (Int
len forall a. Num a => a -> a -> a
+ forall a. ParserInput a => a -> Int
_length a
a)
{-# INLINABLE parseL #-}


-- | Like 'parsed', except this tags each parsed value with the length of input
-- consumed to parse the value.
parsedL
    :: (Monad m, ParserInput a)
    => Attoparsec.Parser a b    -- ^ Attoparsec parser
    -> Producer a m r           -- ^ Raw input
    -> Producer (Int, b) m (Either (ParsingError, Producer a m r) r)
parsedL :: forall (m :: * -> *) a b r.
(Monad m, ParserInput a) =>
Parser a b
-> Producer a m r
-> Producer (Int, b) m (Either (ParsingError, Producer a m r) r)
parsedL Parser a b
parser = forall {m :: * -> *} {r} {x'} {x}.
Monad m =>
Producer a m r
-> Proxy
     x' x () (Int, b) m (Either (ParsingError, Producer a m r) r)
go
  where
    go :: Producer a m r
-> Proxy
     x' x () (Int, b) m (Either (ParsingError, Producer a m r) r)
go Producer a m r
p0 = do
      Either r (a, Producer a m r)
x <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (m :: * -> *) a r.
(Monad m, Eq a, Monoid a) =>
Producer a m r -> m (Either r (a, Producer a m r))
nextSkipEmpty Producer a m r
p0)
      case Either r (a, Producer a m r)
x of
        Left r
r       -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right r
r)
        Right (a
a,Producer a m r
p1) -> (Producer a m r -> Producer a m r)
-> IResult a b
-> Producer a m r
-> Int
-> Proxy
     x' x () (Int, b) m (Either (ParsingError, Producer a m r) r)
step (forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>) (forall a b. ParserInput a => Parser a b -> a -> IResult a b
_parse Parser a b
parser a
a) Producer a m r
p1 (forall a. ParserInput a => a -> Int
_length a
a)
    step :: (Producer a m r -> Producer a m r)
-> IResult a b
-> Producer a m r
-> Int
-> Proxy
     x' x () (Int, b) m (Either (ParsingError, Producer a m r) r)
step Producer a m r -> Producer a m r
diffP IResult a b
res Producer a m r
p0 !Int
len = case IResult a b
res of
      Fail a
_ [String]
c String
m -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left ([String] -> String -> ParsingError
ParsingError [String]
c String
m, Producer a m r -> Producer a m r
diffP Producer a m r
p0))
      Done a
a b
b   -> forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield (Int
len forall a. Num a => a -> a -> a
- forall a. ParserInput a => a -> Int
_length a
a, b
b) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer a m r
-> Proxy
     x' x () (Int, b) m (Either (ParsingError, Producer a m r) r)
go (forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer a m r
p0)
      Partial a -> IResult a b
k  -> do
        Either r (a, Producer a m r)
x <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall (m :: * -> *) a r.
(Monad m, Eq a, Monoid a) =>
Producer a m r -> m (Either r (a, Producer a m r))
nextSkipEmpty Producer a m r
p0)
        case Either r (a, Producer a m r)
x of
          Left r
e -> (Producer a m r -> Producer a m r)
-> IResult a b
-> Producer a m r
-> Int
-> Proxy
     x' x () (Int, b) m (Either (ParsingError, Producer a m r) r)
step Producer a m r -> Producer a m r
diffP (a -> IResult a b
k forall a. Monoid a => a
mempty) (forall (m :: * -> *) a. Monad m => a -> m a
return r
e) Int
len
          Right (a
a,Producer a m r
p1) -> (Producer a m r -> Producer a m r)
-> IResult a b
-> Producer a m r
-> Int
-> Proxy
     x' x () (Int, b) m (Either (ParsingError, Producer a m r) r)
step (Producer a m r -> Producer a m r
diffP forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>)) (a -> IResult a b
k a
a) Producer a m r
p1 (Int
len forall a. Num a => a -> a -> a
+ forall a. ParserInput a => a -> Int
_length a
a)
{-# INLINABLE parsedL #-}

--------------------------------------------------------------------------------

-- | Like 'Pipes.Parse.isEndOfInput', except that it also consumes and discards
-- leading empty chunks.
isEndOfParserInput :: (Monad m, ParserInput a) => Pipes.Parser a m Bool
isEndOfParserInput :: forall (m :: * -> *) a. (Monad m, ParserInput a) => Parser a m Bool
isEndOfParserInput = forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
S.StateT forall a b. (a -> b) -> a -> b
$ \Producer a m x
p0 -> do
    Either x (a, Producer a m x)
x <- forall (m :: * -> *) a r.
(Monad m, Eq a, Monoid a) =>
Producer a m r -> m (Either r (a, Producer a m r))
nextSkipEmpty Producer a m x
p0
    case Either x (a, Producer a m x)
x of
       Left x
r        -> forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
True,  forall (m :: * -> *) a. Monad m => a -> m a
return x
r)
       Right (a
a, Producer a m x
p1) -> forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
False, forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Producer a m x
p1)
{-# INLINABLE isEndOfParserInput #-}

--------------------------------------------------------------------------------

-- | A class for valid @attoparsec@ input types
class (Eq a, Monoid a) => ParserInput a where
    _parse  :: Attoparsec.Parser a b -> a -> IResult a b
    _length :: a -> Int

-- | Strict 'ByteString'.
instance ParserInput ByteString where
    _parse :: forall b. Parser ByteString b -> ByteString -> IResult ByteString b
_parse  = forall b. Parser ByteString b -> ByteString -> IResult ByteString b
Data.Attoparsec.ByteString.parse
    {-# INLINE _parse #-}
    _length :: ByteString -> Int
_length = ByteString -> Int
Data.ByteString.length
    {-# INLINE _length #-}

-- | Strict 'Text'.
instance ParserInput Text where
    _parse :: forall b. Parser Text b -> Text -> IResult Text b
_parse  = forall b. Parser Text b -> Text -> IResult Text b
Data.Attoparsec.Text.parse
    {-# INLINE _parse #-}
    _length :: Text -> Int
_length = Text -> Int
Data.Text.length
    {-# INLINE _length #-}

--------------------------------------------------------------------------------

-- | A parsing error report, as provided by Attoparsec's 'Fail'.
data ParsingError = ParsingError
    { ParsingError -> [String]
peContexts :: [String]  -- ^ Contexts where the parsing error occurred.
    , ParsingError -> String
peMessage  ::  String   -- ^ Parsing error description message.
    } deriving (Int -> ParsingError -> ShowS
[ParsingError] -> ShowS
ParsingError -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ParsingError] -> ShowS
$cshowList :: [ParsingError] -> ShowS
show :: ParsingError -> String
$cshow :: ParsingError -> String
showsPrec :: Int -> ParsingError -> ShowS
$cshowsPrec :: Int -> ParsingError -> ShowS
Show, ReadPrec [ParsingError]
ReadPrec ParsingError
Int -> ReadS ParsingError
ReadS [ParsingError]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ParsingError]
$creadListPrec :: ReadPrec [ParsingError]
readPrec :: ReadPrec ParsingError
$creadPrec :: ReadPrec ParsingError
readList :: ReadS [ParsingError]
$creadList :: ReadS [ParsingError]
readsPrec :: Int -> ReadS ParsingError
$creadsPrec :: Int -> ReadS ParsingError
Read, ParsingError -> ParsingError -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ParsingError -> ParsingError -> Bool
$c/= :: ParsingError -> ParsingError -> Bool
== :: ParsingError -> ParsingError -> Bool
$c== :: ParsingError -> ParsingError -> Bool
Eq, Typeable ParsingError
ParsingError -> DataType
ParsingError -> Constr
(forall b. Data b => b -> b) -> ParsingError -> ParsingError
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> ParsingError -> u
forall u. (forall d. Data d => d -> u) -> ParsingError -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ParsingError -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ParsingError -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ParsingError -> m ParsingError
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ParsingError -> m ParsingError
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ParsingError
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ParsingError -> c ParsingError
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ParsingError)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ParsingError)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ParsingError -> m ParsingError
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ParsingError -> m ParsingError
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ParsingError -> m ParsingError
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> ParsingError -> m ParsingError
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ParsingError -> m ParsingError
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> ParsingError -> m ParsingError
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> ParsingError -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> ParsingError -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> ParsingError -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> ParsingError -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ParsingError -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> ParsingError -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ParsingError -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> ParsingError -> r
gmapT :: (forall b. Data b => b -> b) -> ParsingError -> ParsingError
$cgmapT :: (forall b. Data b => b -> b) -> ParsingError -> ParsingError
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ParsingError)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c ParsingError)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ParsingError)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c ParsingError)
dataTypeOf :: ParsingError -> DataType
$cdataTypeOf :: ParsingError -> DataType
toConstr :: ParsingError -> Constr
$ctoConstr :: ParsingError -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ParsingError
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c ParsingError
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ParsingError -> c ParsingError
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> ParsingError -> c ParsingError
Data, Typeable)

instance Exception ParsingError

--------------------------------------------------------------------------------
-- Internal stuff

-- | Like 'Pipes.next', except it skips leading 'mempty' chunks.
nextSkipEmpty
  :: (Monad m, Eq a, Monoid a)
  => Producer a m r
  -> m (Either r (a, Producer a m r))
nextSkipEmpty :: forall (m :: * -> *) a r.
(Monad m, Eq a, Monoid a) =>
Producer a m r -> m (Either r (a, Producer a m r))
nextSkipEmpty = forall (m :: * -> *) a r.
(Monad m, Eq a, Monoid a) =>
Producer a m r -> m (Either r (a, Producer a m r))
go where
    go :: Producer a m r -> m (Either r (a, Producer a m r))
go Producer a m r
p0 = do
      Either r (a, Producer a m r)
x <- forall (m :: * -> *) a r.
Monad m =>
Producer a m r -> m (Either r (a, Producer a m r))
next Producer a m r
p0
      case Either r (a, Producer a m r)
x of
         Left  r
_        -> forall (m :: * -> *) a. Monad m => a -> m a
return Either r (a, Producer a m r)
x
         Right (a
a,Producer a m r
p1)
          | a
a forall a. Eq a => a -> a -> Bool
== forall a. Monoid a => a
mempty -> Producer a m r -> m (Either r (a, Producer a m r))
go Producer a m r
p1
          | Bool
otherwise   -> forall (m :: * -> *) a. Monad m => a -> m a
return Either r (a, Producer a m r)
x
{-# INLINABLE nextSkipEmpty #-}