-- |
-- Module      :  Data.Attoparsec.Internal
-- Copyright   :  Bryan O'Sullivan 2007-2015
-- License     :  BSD3
--
-- Maintainer  :  bos@serpentine.com
-- Stability   :  experimental
-- Portability :  unknown
--
-- Simple, efficient parser combinators, loosely based on the Parsec
-- library.

module Data.Attoparsec.Internal
    ( compareResults
    , DirChunk
    , prompt
    , demandInput
    , demandInput_
    , wantInput
    , endOfInput
    , atEnd
    , satisfyElem
    , concatReverse
    ) where

#if !MIN_VERSION_base(4,8,0)
import Control.Applicative ((<$>))
import Data.Monoid (Monoid, mconcat)
#endif
import Debug.TraceEmbrace
import Data.Attoparsec.Internal.Types
import Data.ByteString (ByteString)
import Data.Tagged (Tagged(..))
import Data.Text (Text)
import Prelude hiding (succ)

-- | Compare two 'IResult' values for equality.
--
-- If both 'IResult's are 'Partial', the result will be 'Nothing', as
-- they are incomplete and hence their equality cannot be known.
-- (This is why there is no 'Eq' instance for 'IResult'.)
compareResults :: (Eq i, Eq r) => IResult i r -> IResult i r -> Maybe Bool
compareResults :: forall i r.
(Eq i, Eq r) =>
IResult i r -> IResult i r -> Maybe Bool
compareResults (Fail i
t0 [String]
ctxs0 String
msg0) (Fail i
t1 [String]
ctxs1 String
msg1) =
    Bool -> Maybe Bool
forall a. a -> Maybe a
Just (i
t0 i -> i -> Bool
forall a. Eq a => a -> a -> Bool
== i
t1 Bool -> Bool -> Bool
&& [String]
ctxs0 [String] -> [String] -> Bool
forall a. Eq a => a -> a -> Bool
== [String]
ctxs1 Bool -> Bool -> Bool
&& String
msg0 String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
msg1)
compareResults (Done i
t0 r
r0) (Done i
t1 r
r1) =
    Bool -> Maybe Bool
forall a. a -> Maybe a
Just (i
t0 i -> i -> Bool
forall a. Eq a => a -> a -> Bool
== i
t1 Bool -> Bool -> Bool
&& r
r0 r -> r -> Bool
forall a. Eq a => a -> a -> Bool
== r
r1)
compareResults (Partial i -> IResult i r
_) (Partial i -> IResult i r
_) = Maybe Bool
forall a. Maybe a
Nothing
compareResults IResult i r
_ IResult i r
_ = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False

-- | Ask for input.  If we receive any, pass the augmented input to a
-- success continuation, otherwise to a failure continuation.
prompt :: forall t d r. (Show t, DirChunk d t)
       => DirState d t -> DirPos d -> More
       -> (DirState d t -> DirPos d -> More -> IResult t r)
       -> (DirState d t -> DirPos d -> More -> IResult t r)
       -> IResult t r
prompt :: forall t (d :: Dir) r.
(Show t, DirChunk d t) =>
DirState d t
-> DirPos d
-> More
-> (DirState d t -> DirPos d -> More -> IResult t r)
-> (DirState d t -> DirPos d -> More -> IResult t r)
-> IResult t r
prompt DirState d t
t DirPos d
pos More
_more DirState d t -> DirPos d -> More -> IResult t r
lose DirState d t -> DirPos d -> More -> IResult t r
succ = (t -> IResult t r) -> IResult t r
forall i r. (i -> IResult i r) -> IResult i r
Partial ((t -> IResult t r) -> IResult t r)
-> (t -> IResult t r) -> IResult t r
forall a b. (a -> b) -> a -> b
$ \t
s ->
  if t -> Bool
forall c. Chunk c => c -> Bool
nullChunk (t -> Bool) -> t -> Bool
forall a b. (a -> b) -> a -> b
$ $(tr "/t pos s") t
s
  then DirState d t -> DirPos d -> More -> IResult t r
lose ($(tr "/t pos s") DirState d t
t) DirPos d
pos More
Complete
  else
    let
      pos' :: DirPos d
pos' = DirPos d -> t -> DirPos d
forall (d :: Dir) c. DirChunk d c => DirPos d -> c -> DirPos d
shiftPositionOnBufferExtend DirPos d
pos t
s
      t' :: DirState d t
t' = DirState d t -> Tagged d t -> DirState d t
forall (d :: Dir) c.
DirChunk d c =>
DirState d c -> Tagged d c -> DirState d c
pappendChunk DirState d t
t (forall {k} (s :: k) b. b -> Tagged s b
forall (s :: Dir) b. b -> Tagged s b
Tagged @d t
s)
    in
      DirState d t -> DirPos d -> More -> IResult t r
succ ($(tw "pappendChunk/t t' pos pos'")  DirState d t
t') DirPos d
pos' More
Incomplete
{-# SPECIALIZE prompt :: State ByteString -> Pos -> More
                      -> (State ByteString -> Pos -> More
                          -> IResult ByteString r)
                      -> (State ByteString -> Pos -> More
                          -> IResult ByteString r)
                      -> IResult ByteString r #-}
{-# SPECIALIZE prompt :: DirState Backward ByteString -> DirPos Backward -> More
                      -> (DirState Backward ByteString -> DirPos Backward -> More
                          -> IResult ByteString r)
                      -> (DirState Backward ByteString -> DirPos Backward -> More
                          -> IResult ByteString r)
                      -> IResult ByteString r #-}
{-# SPECIALIZE prompt :: State Text -> Pos -> More
                      -> (State Text -> Pos -> More -> IResult Text r)
                      -> (State Text -> Pos -> More -> IResult Text r)
                      -> IResult Text r #-}

-- | Immediately demand more input via a 'Partial' continuation
-- result.
demandInput :: (Show t, DirChunk d t) => DirParser d t ()
demandInput :: forall t (d :: Dir). (Show t, DirChunk d t) => DirParser d t ()
demandInput = (forall r.
 DirState d t
 -> DirPos d
 -> More
 -> DirFailure d t (DirState d t) r
 -> DirSuccess d t (DirState d t) () r
 -> IResult t r)
-> DirParser d t ()
forall (d :: Dir) i a.
(forall r.
 DirState d i
 -> DirPos d
 -> More
 -> DirFailure d i (DirState d i) r
 -> DirSuccess d i (DirState d i) a r
 -> IResult i r)
-> DirParser d i a
Parser ((forall r.
  DirState d t
  -> DirPos d
  -> More
  -> DirFailure d t (DirState d t) r
  -> DirSuccess d t (DirState d t) () r
  -> IResult t r)
 -> DirParser d t ())
-> (forall r.
    DirState d t
    -> DirPos d
    -> More
    -> DirFailure d t (DirState d t) r
    -> DirSuccess d t (DirState d t) () r
    -> IResult t r)
-> DirParser d t ()
forall a b. (a -> b) -> a -> b
$ \DirState d t
t DirPos d
pos More
more DirFailure d t (DirState d t) r
lose DirSuccess d t (DirState d t) () r
succ ->
  case $(tw "case/t pos more") More
more of
    More
Complete -> DirFailure d t (DirState d t) r
lose ($(tr "Complete not enough/t pos") DirState d t
t) DirPos d
pos More
more [] String
"not enough input"
    More
_ -> let lose' :: DirState d t -> DirPos d -> More -> IResult t r
lose' DirState d t
_ DirPos d
pos' More
more' = DirFailure d t (DirState d t) r
lose ($(tr "lose'/pos pos' t") DirState d t
t) DirPos d
pos' More
more' [] String
"not enough input"
             succ' :: DirState d t -> DirPos d -> More -> IResult t r
succ' DirState d t
t' DirPos d
pos' More
more' = DirSuccess d t (DirState d t) () r
succ ($(tr "succ'/pos pos' t t'")  DirState d t
t') DirPos d
pos' More
more' ()
         in DirState d t
-> DirPos d
-> More
-> (DirState d t -> DirPos d -> More -> IResult t r)
-> (DirState d t -> DirPos d -> More -> IResult t r)
-> IResult t r
forall t (d :: Dir) r.
(Show t, DirChunk d t) =>
DirState d t
-> DirPos d
-> More
-> (DirState d t -> DirPos d -> More -> IResult t r)
-> (DirState d t -> DirPos d -> More -> IResult t r)
-> IResult t r
prompt DirState d t
t DirPos d
pos More
more DirState d t -> DirPos d -> More -> IResult t r
lose' DirState d t -> DirPos d -> More -> IResult t r
succ'
{-# SPECIALIZE demandInput :: Parser ByteString () #-}
{-# SPECIALIZE demandInput :: DirParser Backward ByteString () #-}
{-# SPECIALIZE demandInput :: Parser Text () #-}

-- | Immediately demand more input via a 'Partial' continuation
-- result.  Return the new input.
demandInput_ :: forall d t. DirChunk d t => DirParser d t t
demandInput_ :: forall (d :: Dir) t. DirChunk d t => DirParser d t t
demandInput_ = (forall r.
 DirState d t
 -> DirPos d
 -> More
 -> DirFailure d t (DirState d t) r
 -> DirSuccess d t (DirState d t) t r
 -> IResult t r)
-> DirParser d t t
forall (d :: Dir) i a.
(forall r.
 DirState d i
 -> DirPos d
 -> More
 -> DirFailure d i (DirState d i) r
 -> DirSuccess d i (DirState d i) a r
 -> IResult i r)
-> DirParser d i a
Parser ((forall r.
  DirState d t
  -> DirPos d
  -> More
  -> DirFailure d t (DirState d t) r
  -> DirSuccess d t (DirState d t) t r
  -> IResult t r)
 -> DirParser d t t)
-> (forall r.
    DirState d t
    -> DirPos d
    -> More
    -> DirFailure d t (DirState d t) r
    -> DirSuccess d t (DirState d t) t r
    -> IResult t r)
-> DirParser d t t
forall a b. (a -> b) -> a -> b
$ \DirState d t
t DirPos d
pos More
more DirFailure d t (DirState d t) r
lose DirSuccess d t (DirState d t) t r
succ ->
  case More
more of
    More
Complete -> DirFailure d t (DirState d t) r
lose ($(tw "Complete lose/t pos") DirState d t
t) DirPos d
pos More
more [] String
"not enough input"
    More
_ -> (t -> IResult t r) -> IResult t r
forall i r. (i -> IResult i r) -> IResult i r
Partial ((t -> IResult t r) -> IResult t r)
-> (t -> IResult t r) -> IResult t r
forall a b. (a -> b) -> a -> b
$ \t
s ->
         if t -> Bool
forall c. Chunk c => c -> Bool
nullChunk t
s
         then DirFailure d t (DirState d t) r
lose ($(tw "lose/t pos") DirState d t
t) DirPos d
pos More
Complete [] String
"not enough input"
         else
           let
             t' :: DirState d t
t' = $(tw "pappendChunk/pos'") (DirState d t -> DirState d t) -> DirState d t -> DirState d t
forall a b. (a -> b) -> a -> b
$ DirState d t -> Tagged d t -> DirState d t
forall (d :: Dir) c.
DirChunk d c =>
DirState d c -> Tagged d c -> DirState d c
pappendChunk DirState d t
t (forall {k} (s :: k) b. b -> Tagged s b
forall (s :: Dir) b. b -> Tagged s b
Tagged @d t
s)
             pos' :: DirPos d
pos' = DirPos d -> t -> DirPos d
forall (d :: Dir) c. DirChunk d c => DirPos d -> c -> DirPos d
shiftPositionOnBufferExtend DirPos d
pos t
s
           in
             DirSuccess d t (DirState d t) t r
succ DirState d t
t' DirPos d
pos' More
more t
s
{-# SPECIALIZE demandInput_ :: Parser ByteString ByteString #-}
{-# SPECIALIZE demandInput_ :: DirParser Backward ByteString ByteString #-}
{-# SPECIALIZE demandInput_ :: Parser Text Text #-}

-- | This parser always succeeds.  It returns 'True' if any input is
-- available either immediately or on demand, and 'False' if the end
-- of all input has been reached.
wantInput :: forall t d . (Show t, DirChunk d t) => DirParser d t Bool
wantInput :: forall t (d :: Dir). (Show t, DirChunk d t) => DirParser d t Bool
wantInput = (forall r.
 DirState d t
 -> DirPos d
 -> More
 -> DirFailure d t (DirState d t) r
 -> DirSuccess d t (DirState d t) Bool r
 -> IResult t r)
-> DirParser d t Bool
forall (d :: Dir) i a.
(forall r.
 DirState d i
 -> DirPos d
 -> More
 -> DirFailure d i (DirState d i) r
 -> DirSuccess d i (DirState d i) a r
 -> IResult i r)
-> DirParser d i a
Parser ((forall r.
  DirState d t
  -> DirPos d
  -> More
  -> DirFailure d t (DirState d t) r
  -> DirSuccess d t (DirState d t) Bool r
  -> IResult t r)
 -> DirParser d t Bool)
-> (forall r.
    DirState d t
    -> DirPos d
    -> More
    -> DirFailure d t (DirState d t) r
    -> DirSuccess d t (DirState d t) Bool r
    -> IResult t r)
-> DirParser d t Bool
forall a b. (a -> b) -> a -> b
$ \DirState d t
t DirPos d
pos More
more DirFailure d t (DirState d t) r
_lose DirSuccess d t (DirState d t) Bool r
succ ->
  case () of
    ()
_ | t -> DirPos d -> DirState d t -> Bool
forall (d :: Dir) c.
DirChunk d c =>
c -> DirPos d -> DirState d c -> Bool
notAtBufferEnd (t
forall a. HasCallStack => a
undefined :: t) DirPos d
pos DirState d t
t -> DirSuccess d t (DirState d t) Bool r
succ DirState d t
t DirPos d
pos More
more Bool
True
      | More
more More -> More -> Bool
forall a. Eq a => a -> a -> Bool
== More
Complete -> DirSuccess d t (DirState d t) Bool r
succ DirState d t
t DirPos d
pos More
more Bool
False
      | Bool
otherwise       -> let lose' :: DirState d t -> DirPos d -> More -> IResult t r
lose' DirState d t
t' DirPos d
pos' More
more' = DirSuccess d t (DirState d t) Bool r
succ DirState d t
t' DirPos d
pos' More
more' Bool
False
                               succ' :: DirState d t -> DirPos d -> More -> IResult t r
succ' DirState d t
t' DirPos d
pos' More
more' = DirSuccess d t (DirState d t) Bool r
succ DirState d t
t' DirPos d
pos' More
more' Bool
True
                           in DirState d t
-> DirPos d
-> More
-> (DirState d t -> DirPos d -> More -> IResult t r)
-> (DirState d t -> DirPos d -> More -> IResult t r)
-> IResult t r
forall t (d :: Dir) r.
(Show t, DirChunk d t) =>
DirState d t
-> DirPos d
-> More
-> (DirState d t -> DirPos d -> More -> IResult t r)
-> (DirState d t -> DirPos d -> More -> IResult t r)
-> IResult t r
prompt DirState d t
t DirPos d
pos More
more DirState d t -> DirPos d -> More -> IResult t r
lose' DirState d t -> DirPos d -> More -> IResult t r
succ'
{-# INLINE wantInput #-}

-- | Match only if all input has been consumed.
endOfInput :: forall t d. (Show t, DirChunk d t) => DirParser d t ()
endOfInput :: forall t (d :: Dir). (Show t, DirChunk d t) => DirParser d t ()
endOfInput = (forall r.
 DirState d t
 -> DirPos d
 -> More
 -> DirFailure d t (DirState d t) r
 -> DirSuccess d t (DirState d t) () r
 -> IResult t r)
-> DirParser d t ()
forall (d :: Dir) i a.
(forall r.
 DirState d i
 -> DirPos d
 -> More
 -> DirFailure d i (DirState d i) r
 -> DirSuccess d i (DirState d i) a r
 -> IResult i r)
-> DirParser d i a
Parser ((forall r.
  DirState d t
  -> DirPos d
  -> More
  -> DirFailure d t (DirState d t) r
  -> DirSuccess d t (DirState d t) () r
  -> IResult t r)
 -> DirParser d t ())
-> (forall r.
    DirState d t
    -> DirPos d
    -> More
    -> DirFailure d t (DirState d t) r
    -> DirSuccess d t (DirState d t) () r
    -> IResult t r)
-> DirParser d t ()
forall a b. (a -> b) -> a -> b
$ \DirState d t
t DirPos d
pos More
more DirFailure d t (DirState d t) r
lose DirSuccess d t (DirState d t) () r
succ ->
  case () of
    ()
_| t -> DirPos d -> DirState d t -> Bool
forall (d :: Dir) c.
DirChunk d c =>
c -> DirPos d -> DirState d c -> Bool
notAtBufferEnd (t
forall a. HasCallStack => a
undefined :: t) DirPos d
pos DirState d t
t -> DirFailure d t (DirState d t) r
lose DirState d t
t DirPos d
pos More
more [] String
"endOfInput"
     | More
more More -> More -> Bool
forall a. Eq a => a -> a -> Bool
== More
Complete -> DirSuccess d t (DirState d t) () r
succ DirState d t
t DirPos d
pos More
more ()
     | Bool
otherwise ->
       let lose' :: DirFailure d t (DirState d t) r
lose' DirState d t
t' DirPos d
pos' More
more' [String]
_ctx String
_msg = DirSuccess d t (DirState d t) () r
succ DirState d t
t' DirPos d
pos' More
more' ()
           succ' :: DirSuccess d t (DirState d t) () r
succ' DirState d t
t' DirPos d
pos' More
more' ()
_a = DirFailure d t (DirState d t) r
lose DirState d t
t' DirPos d
pos' More
more' [] String
"endOfInput"
       in  DirParser d t ()
-> forall r.
   DirState d t
   -> DirPos d
   -> More
   -> DirFailure d t (DirState d t) r
   -> DirSuccess d t (DirState d t) () r
   -> IResult t r
forall (d :: Dir) i a.
DirParser d i a
-> forall r.
   DirState d i
   -> DirPos d
   -> More
   -> DirFailure d i (DirState d i) r
   -> DirSuccess d i (DirState d i) a r
   -> IResult i r
runParser DirParser d t ()
forall t (d :: Dir). (Show t, DirChunk d t) => DirParser d t ()
demandInput DirState d t
t DirPos d
pos More
more DirFailure d t (DirState d t) r
lose' DirSuccess d t (DirState d t) () r
succ'
{-# SPECIALIZE endOfInput :: Parser ByteString () #-}
{-# SPECIALIZE endOfInput :: DirParser Backward ByteString () #-}
{-# SPECIALIZE endOfInput :: Parser Text () #-}

-- | Return an indication of whether the end of input has been
-- reached.
atEnd :: (Show t, DirChunk d t) => DirParser d t Bool
atEnd :: forall t (d :: Dir). (Show t, DirChunk d t) => DirParser d t Bool
atEnd = Bool -> Bool
not (Bool -> Bool) -> DirParser d t Bool -> DirParser d t Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DirParser d t Bool
forall t (d :: Dir). (Show t, DirChunk d t) => DirParser d t Bool
wantInput
{-# INLINE atEnd #-}

satisfySuspended :: forall d t r . (Show t, DirChunk d t)
                 => (DirChunkElem d t -> Bool)
                 -> DirState d t -> DirPos d -> More
                 -> DirFailure d t (DirState d t) r
                 -> DirSuccess d t (DirState d t) (DirChunkElem d t) r
                 -> IResult t r
satisfySuspended :: forall (d :: Dir) t r.
(Show t, DirChunk d t) =>
(DirChunkElem d t -> Bool)
-> DirState d t
-> DirPos d
-> More
-> DirFailure d t (DirState d t) r
-> DirSuccess d t (DirState d t) (DirChunkElem d t) r
-> IResult t r
satisfySuspended DirChunkElem d t -> Bool
p DirState d t
t DirPos d
pos More
more DirFailure d t (DirState d t) r
lose DirSuccess d t (DirState d t) (DirChunkElem d t) r
succ =
    DirParser d t (DirChunkElem d t)
-> forall r.
   DirState d t
   -> DirPos d
   -> More
   -> DirFailure d t (DirState d t) r
   -> DirSuccess d t (DirState d t) (DirChunkElem d t) r
   -> IResult t r
forall (d :: Dir) i a.
DirParser d i a
-> forall r.
   DirState d i
   -> DirPos d
   -> More
   -> DirFailure d i (DirState d i) r
   -> DirSuccess d i (DirState d i) a r
   -> IResult i r
runParser (DirParser d t ()
forall t (d :: Dir). (Show t, DirChunk d t) => DirParser d t ()
demandInput DirParser d t ()
-> DirParser d t (DirChunkElem d t)
-> DirParser d t (DirChunkElem d t)
forall a b. DirParser d t a -> DirParser d t b -> DirParser d t b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> DirParser d t (DirChunkElem d t)
go) DirState d t
t DirPos d
pos More
more DirFailure d t (DirState d t) r
lose DirSuccess d t (DirState d t) (DirChunkElem d t) r
succ
  where go :: DirParser d t (DirChunkElem d t)
go = (forall r.
 DirState d t
 -> DirPos d
 -> More
 -> DirFailure d t (DirState d t) r
 -> DirSuccess d t (DirState d t) (DirChunkElem d t) r
 -> IResult t r)
-> DirParser d t (DirChunkElem d t)
forall (d :: Dir) i a.
(forall r.
 DirState d i
 -> DirPos d
 -> More
 -> DirFailure d i (DirState d i) r
 -> DirSuccess d i (DirState d i) a r
 -> IResult i r)
-> DirParser d i a
Parser ((forall r.
  DirState d t
  -> DirPos d
  -> More
  -> DirFailure d t (DirState d t) r
  -> DirSuccess d t (DirState d t) (DirChunkElem d t) r
  -> IResult t r)
 -> DirParser d t (DirChunkElem d t))
-> (forall r.
    DirState d t
    -> DirPos d
    -> More
    -> DirFailure d t (DirState d t) r
    -> DirSuccess d t (DirState d t) (DirChunkElem d t) r
    -> IResult t r)
-> DirParser d t (DirChunkElem d t)
forall a b. (a -> b) -> a -> b
$ \DirState d t
t' DirPos d
pos' More
more' DirFailure d t (DirState d t) r
lose' DirSuccess d t (DirState d t) (DirChunkElem d t) r
succ' ->
          case t -> DirPos d -> DirState d t -> Maybe (DirChunkElem d t, Int)
forall (d :: Dir) c.
DirChunk d c =>
c -> DirPos d -> DirState d c -> Maybe (DirChunkElem d c, Int)
bufferElemAt (t
forall a. HasCallStack => a
undefined :: t) DirPos d
pos' DirState d t
t' of
            Just (DirChunkElem d t
e, Int
l) | DirChunkElem d t -> Bool
p DirChunkElem d t
e -> DirSuccess d t (DirState d t) (DirChunkElem d t) r
succ' DirState d t
t' (DirPos d
pos' DirPos d -> DirPos d -> DirPos d
forall a. Num a => a -> a -> a
+ (DirPos d -> DirPos d
forall (d :: Dir). DirectedPlus d => DirPos d -> DirPos d
there (Int -> DirPos d
forall (d :: Dir). Int -> DirPos d
Pos Int
l))) More
more' DirChunkElem d t
e
                        | Bool
otherwise -> DirFailure d t (DirState d t) r
lose' DirState d t
t' DirPos d
pos' More
more' [] String
"satisfyElem"
            Maybe (DirChunkElem d t, Int)
Nothing -> DirParser d t (DirChunkElem d t)
-> forall r.
   DirState d t
   -> DirPos d
   -> More
   -> DirFailure d t (DirState d t) r
   -> DirSuccess d t (DirState d t) (DirChunkElem d t) r
   -> IResult t r
forall (d :: Dir) i a.
DirParser d i a
-> forall r.
   DirState d i
   -> DirPos d
   -> More
   -> DirFailure d i (DirState d i) r
   -> DirSuccess d i (DirState d i) a r
   -> IResult i r
runParser (DirParser d t ()
forall t (d :: Dir). (Show t, DirChunk d t) => DirParser d t ()
demandInput DirParser d t ()
-> DirParser d t (DirChunkElem d t)
-> DirParser d t (DirChunkElem d t)
forall a b. DirParser d t a -> DirParser d t b -> DirParser d t b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> DirParser d t (DirChunkElem d t)
go) DirState d t
t' DirPos d
pos' More
more' DirFailure d t (DirState d t) r
lose' DirSuccess d t (DirState d t) (DirChunkElem d t) r
succ'
{-# SPECIALIZE satisfySuspended :: (ChunkElem ByteString -> Bool)
                                -> State ByteString -> Pos -> More
                                -> Failure ByteString (State ByteString) r
                                -> Success ByteString (State ByteString)
                                           (ChunkElem ByteString) r
                                -> IResult ByteString r #-}
{-# SPECIALIZE satisfySuspended :: (ChunkElem ByteString -> Bool)
                                -> DirState Backward ByteString -> DirPos Backward -> More
                                -> DirFailure Backward ByteString (DirState Backward ByteString) r
                                -> DirSuccess Backward ByteString (DirState Backward ByteString)
                                           (ChunkElem ByteString) r
                                -> IResult ByteString r #-}
{-# SPECIALIZE satisfySuspended :: (ChunkElem Text -> Bool)
                                -> State Text -> Pos -> More
                                -> Failure Text (State Text) r
                                -> Success Text (State Text)
                                           (ChunkElem Text) r
                                -> IResult Text r #-}

-- | The parser @satisfyElem p@ succeeds for any chunk element for which the
-- predicate @p@ returns 'True'. Returns the element that is
-- actually parsed.
satisfyElem :: forall t d. (Show t, DirChunk d t)
            => (DirChunkElem d t -> Bool) -> DirParser d t (DirChunkElem d t)
satisfyElem :: forall t (d :: Dir).
(Show t, DirChunk d t) =>
(DirChunkElem d t -> Bool) -> DirParser d t (DirChunkElem d t)
satisfyElem DirChunkElem d t -> Bool
p = (forall r.
 DirState d t
 -> DirPos d
 -> More
 -> DirFailure d t (DirState d t) r
 -> DirSuccess d t (DirState d t) (DirChunkElem d t) r
 -> IResult t r)
-> DirParser d t (DirChunkElem d t)
forall (d :: Dir) i a.
(forall r.
 DirState d i
 -> DirPos d
 -> More
 -> DirFailure d i (DirState d i) r
 -> DirSuccess d i (DirState d i) a r
 -> IResult i r)
-> DirParser d i a
Parser ((forall r.
  DirState d t
  -> DirPos d
  -> More
  -> DirFailure d t (DirState d t) r
  -> DirSuccess d t (DirState d t) (DirChunkElem d t) r
  -> IResult t r)
 -> DirParser d t (DirChunkElem d t))
-> (forall r.
    DirState d t
    -> DirPos d
    -> More
    -> DirFailure d t (DirState d t) r
    -> DirSuccess d t (DirState d t) (DirChunkElem d t) r
    -> IResult t r)
-> DirParser d t (DirChunkElem d t)
forall a b. (a -> b) -> a -> b
$ \DirState d t
t DirPos d
pos More
more DirFailure d t (DirState d t) r
lose DirSuccess d t (DirState d t) (DirChunkElem d t) r
succ ->
    case t -> DirPos d -> DirState d t -> Maybe (DirChunkElem d t, Int)
forall (d :: Dir) c.
DirChunk d c =>
c -> DirPos d -> DirState d c -> Maybe (DirChunkElem d c, Int)
bufferElemAt (t
forall a. HasCallStack => a
undefined :: t) DirPos d
pos DirState d t
t of
      Just (DirChunkElem d t
e, Int
l) | DirChunkElem d t -> Bool
p DirChunkElem d t
e -> DirSuccess d t (DirState d t) (DirChunkElem d t) r
succ DirState d t
t (DirPos d
pos DirPos d -> DirPos d -> DirPos d
forall a. Num a => a -> a -> a
+ (DirPos d -> DirPos d
forall (d :: Dir). DirectedPlus d => DirPos d -> DirPos d
there (Int -> DirPos d
forall (d :: Dir). Int -> DirPos d
Pos Int
l))) More
more DirChunkElem d t
e
                  | Bool
otherwise -> DirFailure d t (DirState d t) r
lose DirState d t
t DirPos d
pos More
more [] String
"satisfyElem"
      Maybe (DirChunkElem d t, Int)
Nothing -> (DirChunkElem d t -> Bool)
-> DirState d t
-> DirPos d
-> More
-> DirFailure d t (DirState d t) r
-> DirSuccess d t (DirState d t) (DirChunkElem d t) r
-> IResult t r
forall (d :: Dir) t r.
(Show t, DirChunk d t) =>
(DirChunkElem d t -> Bool)
-> DirState d t
-> DirPos d
-> More
-> DirFailure d t (DirState d t) r
-> DirSuccess d t (DirState d t) (DirChunkElem d t) r
-> IResult t r
satisfySuspended DirChunkElem d t -> Bool
p DirState d t
t DirPos d
pos More
more DirFailure d t (DirState d t) r
lose DirSuccess d t (DirState d t) (DirChunkElem d t) r
succ
{-# INLINE satisfyElem #-}