{-# LANGUAGE UnboxedTuples #-}

-- | Basic parser building blocks.

module FlatParse.Basic.Base
  (
  -- * Bytewise
    eof
  , take
  , take#
  , takeUnsafe#
  , takeRest
  , skip
  , skip#
  , skipBack
  , skipBack#
  , atSkip#
  , atSkipUnsafe#

  -- * Combinators
  , branch
  , notFollowedBy
  , chainl
  , chainr
  , lookahead
  , ensure
  , ensure#
  , withEnsure
  , withEnsure1
  , withEnsure#
  , isolate
  , isolate#
  , isolateUnsafe#

  -- ** Non-specific (TODO)
  , skipMany
  , skipSome

  -- * Errors and failures
  , failed
  , try
  , err
  , fails
  , cut
  , cutting
  , optional
  , optional_
  , withOption
  ) where

import Prelude hiding ( take )

import FlatParse.Basic.Parser
import qualified FlatParse.Common.Assorted as Common

import GHC.Exts
import qualified Data.ByteString as B
import qualified Data.ByteString.Internal as B
import GHC.ForeignPtr ( ForeignPtr(..) )
import qualified Control.Applicative

-- | The failing parser. By default, parser choice `(<|>)` arbitrarily backtracks
--   on parser failure.
failed :: ParserT st e a
failed :: forall (st :: ZeroBitType) e a. ParserT st e a
failed = forall (f :: * -> *) a. Alternative f => f a
Control.Applicative.empty
{-# inline failed #-}

-- | Throw a parsing error. By default, parser choice `(<|>)` can't backtrack
--   on parser error. Use `try` to convert an error to a recoverable failure.
err :: e -> ParserT st e a
err :: forall e (st :: ZeroBitType) a. e -> ParserT st e a
err e
e = forall (st :: ZeroBitType) e a.
(ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a)
-> ParserT st e a
ParserT \ForeignPtrContents
_fp Addr#
_eob Addr#
_s st
st -> forall (st :: ZeroBitType) e a. st -> e -> Res# st e a
Err# st
st e
e
{-# inline err #-}

-- | Convert a parsing error into failure.
try :: ParserT st e a -> ParserT st e a
try :: forall (st :: ZeroBitType) e a. ParserT st e a -> ParserT st e a
try (ParserT ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
f) = forall (st :: ZeroBitType) e a.
(ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a)
-> ParserT st e a
ParserT \ForeignPtrContents
fp Addr#
eob Addr#
s st
st -> case ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
f ForeignPtrContents
fp Addr#
eob Addr#
s st
st of
  Err# st
st' e
_ -> forall (st :: ZeroBitType) e a. st -> Res# st e a
Fail# st
st'
  Res# st e a
x          -> Res# st e a
x
{-# inline try #-}

-- | Convert a parsing failure to a success.
fails :: ParserT st e a -> ParserT st e ()
fails :: forall (st :: ZeroBitType) e a. ParserT st e a -> ParserT st e ()
fails (ParserT ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
f) = forall (st :: ZeroBitType) e a.
(ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a)
-> ParserT st e a
ParserT \ForeignPtrContents
fp Addr#
eob Addr#
s st
st ->
  case ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
f ForeignPtrContents
fp Addr#
eob Addr#
s st
st of
    OK#   st
st' a
_ Addr#
_ -> forall (st :: ZeroBitType) e a. st -> Res# st e a
Fail# st
st'
    Fail# st
st'     -> forall (st :: ZeroBitType) a e. st -> a -> Addr# -> Res# st e a
OK#   st
st' () Addr#
s
    Err#  st
st' e
e   -> forall (st :: ZeroBitType) e a. st -> e -> Res# st e a
Err#  st
st' e
e
{-# inline fails #-}

-- | Convert a parsing failure to an error.
cut :: ParserT st e a -> e -> ParserT st e a
cut :: forall (st :: ZeroBitType) e a.
ParserT st e a -> e -> ParserT st e a
cut (ParserT ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
f) e
e = forall (st :: ZeroBitType) e a.
(ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a)
-> ParserT st e a
ParserT \ForeignPtrContents
fp Addr#
eob Addr#
s st
st -> case ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
f ForeignPtrContents
fp Addr#
eob Addr#
s st
st of
  Fail# st
st' -> forall (st :: ZeroBitType) e a. st -> e -> Res# st e a
Err# st
st' e
e
  Res# st e a
x         -> Res# st e a
x
{-# inline cut #-}

-- | Run the parser, if we get a failure, throw the given error, but if we get an error, merge the
--   inner and the newly given errors using the @e -> e -> e@ function. This can be useful for
--   implementing parsing errors which may propagate hints or accummulate contextual information.
cutting :: ParserT st e a -> e -> (e -> e -> e) -> ParserT st e a
cutting :: forall (st :: ZeroBitType) e a.
ParserT st e a -> e -> (e -> e -> e) -> ParserT st e a
cutting (ParserT ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
f) e
e e -> e -> e
merge = forall (st :: ZeroBitType) e a.
(ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a)
-> ParserT st e a
ParserT \ForeignPtrContents
fp Addr#
eob Addr#
s st
st -> case ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
f ForeignPtrContents
fp Addr#
eob Addr#
s st
st of
  Fail# st
st'    -> forall (st :: ZeroBitType) e a. st -> e -> Res# st e a
Err# st
st' e
e
  Err#  st
st' e
e' -> forall (st :: ZeroBitType) e a. st -> e -> Res# st e a
Err# st
st' forall a b. (a -> b) -> a -> b
$! e -> e -> e
merge e
e' e
e
  Res# st e a
x            -> Res# st e a
x
{-# inline cutting #-}

-- | Convert a parsing failure to a `Maybe`. If possible, use `withOption`
--   instead.
optional :: ParserT st e a -> ParserT st e (Maybe a)
optional :: forall (st :: ZeroBitType) e a.
ParserT st e a -> ParserT st e (Maybe a)
optional ParserT st e a
p = (forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParserT st e a
p) forall (st :: ZeroBitType) e a.
ParserT st e a -> ParserT st e a -> ParserT st e a
<|> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
{-# inline optional #-}

-- | Convert a parsing failure to a `()`.
optional_ :: ParserT st e a -> ParserT st e ()
optional_ :: forall (st :: ZeroBitType) e a. ParserT st e a -> ParserT st e ()
optional_ ParserT st e a
p = (() forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParserT st e a
p) forall (st :: ZeroBitType) e a.
ParserT st e a -> ParserT st e a -> ParserT st e a
<|> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
{-# inline optional_ #-}

-- | CPS'd version of `optional`. This is usually more efficient, since it gets
--   rid of the extra `Maybe` allocation.
withOption :: ParserT st e a -> (a -> ParserT st e r) -> ParserT st e r -> ParserT st e r
withOption :: forall (st :: ZeroBitType) e a r.
ParserT st e a
-> (a -> ParserT st e r) -> ParserT st e r -> ParserT st e r
withOption (ParserT ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
f) a -> ParserT st e r
just (ParserT ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e r
nothing) = forall (st :: ZeroBitType) e a.
(ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a)
-> ParserT st e a
ParserT \ForeignPtrContents
fp Addr#
eob Addr#
s st
st ->
    case ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
f ForeignPtrContents
fp Addr#
eob Addr#
s st
st of
      OK#   st
st' a
a Addr#
s -> forall (st :: ZeroBitType) e a.
ParserT st e a
-> ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
runParserT# (a -> ParserT st e r
just a
a) ForeignPtrContents
fp Addr#
eob Addr#
s st
st'
      Fail# st
st'     -> ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e r
nothing ForeignPtrContents
fp Addr#
eob Addr#
s st
st'
      Err#  st
st' e
e   -> forall (st :: ZeroBitType) e a. st -> e -> Res# st e a
Err# st
st' e
e
{-# inline withOption #-}

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

-- | Succeed if the input is empty.
eof :: ParserT st e ()
eof :: forall (st :: ZeroBitType) e. ParserT st e ()
eof = forall (st :: ZeroBitType) e a.
(ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a)
-> ParserT st e a
ParserT \ForeignPtrContents
fp Addr#
eob Addr#
s st
st -> case Addr# -> Addr# -> Int#
eqAddr# Addr#
eob Addr#
s of
  Int#
1# -> forall (st :: ZeroBitType) a e. st -> a -> Addr# -> Res# st e a
OK#   st
st () Addr#
s
  Int#
_  -> forall (st :: ZeroBitType) e a. st -> Res# st e a
Fail# st
st
{-# inline eof #-}

-- | Save the parsing state, then run a parser, then restore the state.
lookahead :: ParserT st e a -> ParserT st e a
lookahead :: forall (st :: ZeroBitType) e a. ParserT st e a -> ParserT st e a
lookahead (ParserT ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
f) = forall (st :: ZeroBitType) e a.
(ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a)
-> ParserT st e a
ParserT \ForeignPtrContents
fp Addr#
eob Addr#
s st
st ->
  case ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
f ForeignPtrContents
fp Addr#
eob Addr#
s st
st of
    OK# st
st' a
a Addr#
_ -> forall (st :: ZeroBitType) a e. st -> a -> Addr# -> Res# st e a
OK# st
st' a
a Addr#
s
    Res# st e a
x           -> Res# st e a
x
{-# inline lookahead #-}

-- | @isolate n p@ runs the parser @p@ isolated to the next @n@ bytes.
--   All isolated bytes must be consumed.
--
-- Throws a runtime error if given a negative integer.
isolate :: Int -> ParserT st e a -> ParserT st e a
isolate :: forall (st :: ZeroBitType) e a.
Int -> ParserT st e a -> ParserT st e a
isolate = forall r. (Int# -> r) -> Int -> r
Common.withIntUnwrap# forall (st :: ZeroBitType) e a.
Int# -> ParserT st e a -> ParserT st e a
isolate#
{-# inline isolate #-}

-- | @isolate# n# p@ runs the parser @p@ isolated to the next @n#@ bytes.
--   All isolated bytes must be consumed.
--
-- Throws a runtime error if given a negative integer.
isolate# :: Int# -> ParserT st e a -> ParserT st e a
isolate# :: forall (st :: ZeroBitType) e a.
Int# -> ParserT st e a -> ParserT st e a
isolate# Int#
n# ParserT st e a
p = forall r. Int# -> r -> r
Common.withPosInt# Int#
n# (forall (st :: ZeroBitType) e a.
Int# -> ParserT st e a -> ParserT st e a
isolateUnsafe# Int#
n# ParserT st e a
p)
{-# inline isolate# #-}

-- | @isolateUnsafe# n# p@ runs the parser @p@ isolated to the next @n#@ bytes.
--   All isolated bytes must be consumed.
--
-- Undefined behaviour if given a negative integer.
isolateUnsafe# :: Int# -> ParserT st e a -> ParserT st e a
isolateUnsafe# :: forall (st :: ZeroBitType) e a.
Int# -> ParserT st e a -> ParserT st e a
isolateUnsafe# Int#
n# (ParserT ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
p) =
    forall (st :: ZeroBitType) e a.
Int# -> ParserT st e a -> ParserT st e a
withEnsure# Int#
n# forall a b. (a -> b) -> a -> b
$ forall (st :: ZeroBitType) e a.
(ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a)
-> ParserT st e a
ParserT \ForeignPtrContents
fp Addr#
eob Addr#
s st
st ->
        let s' :: Addr#
s' = Addr# -> Int# -> Addr#
plusAddr# Addr#
s Int#
n#
        in  case ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
p ForeignPtrContents
fp Addr#
s' Addr#
s st
st of
              OK# st
st' a
a Addr#
s'' ->
                case Addr# -> Addr# -> Int#
eqAddr# Addr#
s' Addr#
s'' of
                  Int#
1# -> forall (st :: ZeroBitType) a e. st -> a -> Addr# -> Res# st e a
OK#   st
st' a
a Addr#
s''
                  Int#
_  -> forall (st :: ZeroBitType) e a. st -> Res# st e a
Fail# st
st'
              Res# st e a
x -> Res# st e a
x
{-# inline isolateUnsafe# #-}

-- | An analogue of the list `foldl` function: first parse a @b@, then parse zero or more @a@-s,
--   and combine the results in a left-nested way by the @b -> a -> b@ function. Note: this is not
--   the usual `chainl` function from the parsec libraries!
chainl :: (b -> a -> b) -> ParserT st e b -> ParserT st e a -> ParserT st e b
chainl :: forall b a (st :: ZeroBitType) e.
(b -> a -> b) -> ParserT st e b -> ParserT st e a -> ParserT st e b
chainl b -> a -> b
f ParserT st e b
start ParserT st e a
elem = ParserT st e b
start forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> ParserT st e b
go where
  go :: b -> ParserT st e b
go b
b = do {!a
a <- ParserT st e a
elem; b -> ParserT st e b
go forall a b. (a -> b) -> a -> b
$! b -> a -> b
f b
b a
a} forall (st :: ZeroBitType) e a.
ParserT st e a -> ParserT st e a -> ParserT st e a
<|> forall (f :: * -> *) a. Applicative f => a -> f a
pure b
b
{-# inline chainl #-}

-- | An analogue of the list `foldr` function: parse zero or more @a@-s, terminated by a @b@, and
--   combine the results in a right-nested way using the @a -> b -> b@ function. Note: this is not
--   the usual `chainr` function from the parsec libraries!
chainr :: (a -> b -> b) -> ParserT st e a -> ParserT st e b -> ParserT st e b
chainr :: forall a b (st :: ZeroBitType) e.
(a -> b -> b) -> ParserT st e a -> ParserT st e b -> ParserT st e b
chainr a -> b -> b
f (ParserT ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
elem) (ParserT ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e b
end) = forall (st :: ZeroBitType) e a.
(ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a)
-> ParserT st e a
ParserT ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e b
go where
  go :: ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e b
go ForeignPtrContents
fp Addr#
eob Addr#
s st
st = case ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
elem ForeignPtrContents
fp Addr#
eob Addr#
s st
st of
    OK#   st
st' a
a Addr#
s -> case ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e b
go ForeignPtrContents
fp Addr#
eob Addr#
s st
st' of
      OK# st
st'' b
b Addr#
s -> let !b' :: b
b' = a -> b -> b
f a
a b
b in forall (st :: ZeroBitType) a e. st -> a -> Addr# -> Res# st e a
OK# st
st'' b
b' Addr#
s
      Res# st e b
x            -> Res# st e b
x
    Fail# st
st'     -> ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e b
end ForeignPtrContents
fp Addr#
eob Addr#
s st
st'
    Err#  st
st' e
e   -> forall (st :: ZeroBitType) e a. st -> e -> Res# st e a
Err# st
st' e
e
{-# inline chainr #-}

-- | Branch on a parser: if the first argument succeeds, continue with the second, else with the third.
--   This can produce slightly more efficient code than `(<|>)`. Moreover, `branch` does not
--   backtrack from the true/false cases.
branch :: ParserT st e a -> ParserT st e b -> ParserT st e b -> ParserT st e b
branch :: forall (st :: ZeroBitType) e a b.
ParserT st e a
-> ParserT st e b -> ParserT st e b -> ParserT st e b
branch ParserT st e a
pa ParserT st e b
pt ParserT st e b
pf = forall (st :: ZeroBitType) e a.
(ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a)
-> ParserT st e a
ParserT \ForeignPtrContents
fp Addr#
eob Addr#
s st
st -> case forall (st :: ZeroBitType) e a.
ParserT st e a
-> ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
runParserT# ParserT st e a
pa ForeignPtrContents
fp Addr#
eob Addr#
s st
st of
  OK#   st
st' a
_ Addr#
s -> forall (st :: ZeroBitType) e a.
ParserT st e a
-> ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
runParserT# ParserT st e b
pt ForeignPtrContents
fp Addr#
eob Addr#
s st
st'
  Fail# st
st'     -> forall (st :: ZeroBitType) e a.
ParserT st e a
-> ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
runParserT# ParserT st e b
pf ForeignPtrContents
fp Addr#
eob Addr#
s st
st'
  Err#  st
st' e
e   -> forall (st :: ZeroBitType) e a. st -> e -> Res# st e a
Err# st
st' e
e
{-# inline branch #-}

-- | Succeed if the first parser succeeds and the second one fails.
notFollowedBy :: ParserT st e a -> ParserT st e b -> ParserT st e a
notFollowedBy :: forall (st :: ZeroBitType) e a b.
ParserT st e a -> ParserT st e b -> ParserT st e a
notFollowedBy ParserT st e a
p1 ParserT st e b
p2 = ParserT st e a
p1 forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall (st :: ZeroBitType) e a. ParserT st e a -> ParserT st e ()
fails ParserT st e b
p2
{-# inline notFollowedBy #-}

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

-- | Assert that there are at least @n@ bytes remaining.
--
-- Undefined behaviour if given a negative integer.
ensure :: Int -> ParserT st e ()
ensure :: forall (st :: ZeroBitType) e. Int -> ParserT st e ()
ensure = forall r. (Int# -> r) -> Int -> r
Common.withIntUnwrap# forall (st :: ZeroBitType) e. Int# -> ParserT st e ()
ensure#
{-# inline ensure #-}

-- | Assert that there are at least @n#@ bytes remaining.
--
-- Undefined behaviour if given a negative integer.
ensure# :: Int# -> ParserT st e ()
ensure# :: forall (st :: ZeroBitType) e. Int# -> ParserT st e ()
ensure# Int#
n# = forall (st :: ZeroBitType) e a.
Int# -> ParserT st e a -> ParserT st e a
withEnsure# Int#
n# (forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
{-# inline ensure# #-}

-- TODO: András: why do we need withEnsure-s?
-- There's no unboxing to be improved.

-- | Assert that there are at least @n#@ bytes remaining (CPS).
--
-- Undefined behaviour if given a negative integer.
withEnsure :: Int -> ParserT st e r -> ParserT st e r
withEnsure :: forall (st :: ZeroBitType) e a.
Int -> ParserT st e a -> ParserT st e a
withEnsure = forall r. (Int# -> r) -> Int -> r
Common.withIntUnwrap# forall (st :: ZeroBitType) e a.
Int# -> ParserT st e a -> ParserT st e a
withEnsure#
{-# inline withEnsure #-}

-- | Assert that there is at least 1 byte remaining (CPS).
--
-- Undefined behaviour if given a negative integer.
withEnsure1 :: ParserT st e r -> ParserT st e r
withEnsure1 :: forall (st :: ZeroBitType) e a. ParserT st e a -> ParserT st e a
withEnsure1 (ParserT ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e r
p) = forall (st :: ZeroBitType) e a.
(ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a)
-> ParserT st e a
ParserT \ForeignPtrContents
fp Addr#
eob Addr#
s st
st ->
    case Addr# -> Addr# -> Int#
eqAddr# Addr#
eob Addr#
s of
      Int#
0# -> ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e r
p ForeignPtrContents
fp Addr#
eob Addr#
s st
st
      Int#
_  -> forall (st :: ZeroBitType) e a. st -> Res# st e a
Fail# st
st
{-# inline withEnsure1 #-}

-- | Assert that there are at least @n#@ bytes remaining (CPS).
--
-- Undefined behaviour if given a negative integer.
withEnsure# :: Int# -> ParserT st e r -> ParserT st e r
withEnsure# :: forall (st :: ZeroBitType) e a.
Int# -> ParserT st e a -> ParserT st e a
withEnsure# Int#
n# (ParserT ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e r
p) = forall (st :: ZeroBitType) e a.
(ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a)
-> ParserT st e a
ParserT \ForeignPtrContents
fp Addr#
eob Addr#
s st
st ->
    case Int#
n# Int# -> Int# -> Int#
<=# Addr# -> Addr# -> Int#
minusAddr# Addr#
eob Addr#
s of
      Int#
1# -> ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e r
p ForeignPtrContents
fp Addr#
eob Addr#
s st
st
      Int#
_  -> forall (st :: ZeroBitType) e a. st -> Res# st e a
Fail# st
st
{-# inline withEnsure# #-}

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

-- | Read the given number of bytes as a 'ByteString'.
--
-- Throws a runtime error if given a negative integer.
--
-- This does no copying. The 'B.ByteString' returned is a "slice" of the input,
-- and will keep it alive. To avoid this, use 'B.copy' on the output.
take :: Int -> ParserT st e B.ByteString
take :: forall (st :: ZeroBitType) e. Int -> ParserT st e ByteString
take (I# Int#
n#) = forall (st :: ZeroBitType) e. Int# -> ParserT st e ByteString
take# Int#
n#
{-# inline take #-}

-- | Read @n#@ bytes as a 'ByteString'. Fails if fewer than @n#@ bytes are
--   available.
--
-- Throws a runtime error if given a negative integer.
--
-- This does no copying. The 'B.ByteString' returned is a "slice" of the input,
-- and will keep it alive. To avoid this, use 'B.copy' on the output.
take# :: Int# -> ParserT st e B.ByteString
take# :: forall (st :: ZeroBitType) e. Int# -> ParserT st e ByteString
take# Int#
n# = forall r. Int# -> r -> r
Common.withPosInt# Int#
n# (forall (st :: ZeroBitType) e. Int# -> ParserT st e ByteString
takeUnsafe# Int#
n#)
{-# inline take# #-}

-- | Read @n#@ bytes as a 'ByteString'. Fails if fewer than @n#@ bytes are
--   available.
--
-- Undefined behaviour if given a negative integer.
--
-- This does no copying. The 'B.ByteString' returned is a "slice" of the input,
-- and will keep it alive. To avoid this, use 'B.copy' on the output.
takeUnsafe# :: Int# -> ParserT st e B.ByteString
takeUnsafe# :: forall (st :: ZeroBitType) e. Int# -> ParserT st e ByteString
takeUnsafe# Int#
n# = forall (st :: ZeroBitType) e a.
(ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a)
-> ParserT st e a
ParserT \ForeignPtrContents
fp Addr#
eob Addr#
s st
st ->
    case Int#
n# Int# -> Int# -> Int#
<=# Addr# -> Addr# -> Int#
minusAddr# Addr#
eob Addr#
s of
      Int#
1# -> forall (st :: ZeroBitType) a e. st -> a -> Addr# -> Res# st e a
OK#   st
st (ForeignPtr Word8 -> Int -> Int -> ByteString
B.PS (forall a. Addr# -> ForeignPtrContents -> ForeignPtr a
ForeignPtr Addr#
s ForeignPtrContents
fp) Int
0 (Int# -> Int
I# Int#
n#)) (Addr# -> Int# -> Addr#
plusAddr# Addr#
s Int#
n#)
      Int#
_  -> forall (st :: ZeroBitType) e a. st -> Res# st e a
Fail# st
st
{-# inline takeUnsafe# #-}

-- | Consume the rest of the input. May return the empty bytestring.
--
-- This does no copying. The 'B.ByteString' returned is a "slice" of the input,
-- and will keep it alive. To avoid this, use 'B.copy' on the output.
takeRest :: ParserT st e B.ByteString
takeRest :: forall (st :: ZeroBitType) e. ParserT st e ByteString
takeRest = forall (st :: ZeroBitType) e a.
(ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a)
-> ParserT st e a
ParserT \ForeignPtrContents
fp Addr#
eob Addr#
s st
st ->
  let n# :: Int#
n# = Addr# -> Addr# -> Int#
minusAddr# Addr#
eob Addr#
s
  in  forall (st :: ZeroBitType) a e. st -> a -> Addr# -> Res# st e a
OK# st
st (ForeignPtr Word8 -> Int -> Int -> ByteString
B.PS (forall a. Addr# -> ForeignPtrContents -> ForeignPtr a
ForeignPtr Addr#
s ForeignPtrContents
fp) Int
0 (Int# -> Int
I# Int#
n#)) Addr#
eob
{-# inline takeRest #-}

-- | Skip forward @n@ bytes. Fails if fewer than @n@ bytes are available.
--
-- Throws a runtime error if given a negative integer.
skip :: Int -> ParserT st e ()
skip :: forall (st :: ZeroBitType) e. Int -> ParserT st e ()
skip (I# Int#
n#) = forall (st :: ZeroBitType) e. Int# -> ParserT st e ()
skip# Int#
n#
{-# inline skip #-}

-- | Skip forward @n#@ bytes. Fails if fewer than @n#@ bytes are available.
--
-- Throws a runtime error if given a negative integer.
skip# :: Int# -> ParserT st e ()
skip# :: forall (st :: ZeroBitType) e. Int# -> ParserT st e ()
skip# Int#
n# = forall (st :: ZeroBitType) e a.
Int# -> ParserT st e a -> ParserT st e a
atSkip# Int#
n# (forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
{-# inline skip# #-}

-- | Go back @i@ bytes in the input. Takes a positive integer.
--
-- Extremely unsafe. Makes no checks. Almost certainly a Bad Idea.
skipBack :: Int -> ParserT st e ()
skipBack :: forall (st :: ZeroBitType) e. Int -> ParserT st e ()
skipBack = forall r. (Int# -> r) -> Int -> r
Common.withIntUnwrap# forall (st :: ZeroBitType) e. Int# -> ParserT st e ()
skipBack#
{-# inline skipBack #-}

-- | Go back @n#@ bytes. Takes a positive integer.
--
-- Extremely unsafe. Makes no checks. Almost certainly a Bad Idea.
skipBack# :: Int# -> ParserT st e ()
skipBack# :: forall (st :: ZeroBitType) e. Int# -> ParserT st e ()
skipBack# Int#
n# = forall (st :: ZeroBitType) e a.
(ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a)
-> ParserT st e a
ParserT \ForeignPtrContents
fp Addr#
eob Addr#
s st
st ->
    forall (st :: ZeroBitType) a e. st -> a -> Addr# -> Res# st e a
OK# st
st () (Addr# -> Int# -> Addr#
plusAddr# Addr#
s (Int# -> Int#
negateInt# Int#
n#))
{-# inline skipBack# #-}

-- | Skip forward @n#@ bytes and run the given parser. Fails if fewer than @n#@
--   bytes are available.
--
-- Throws a runtime error if given a negative integer.
atSkip# :: Int# -> ParserT st e a -> ParserT st e a
atSkip# :: forall (st :: ZeroBitType) e a.
Int# -> ParserT st e a -> ParserT st e a
atSkip# Int#
n# ParserT st e a
p = forall r. Int# -> r -> r
Common.withPosInt# Int#
n# (forall (st :: ZeroBitType) e a.
Int# -> ParserT st e a -> ParserT st e a
atSkipUnsafe# Int#
n# ParserT st e a
p)
{-# inline atSkip# #-}

-- | Skip forward @n@ bytes and run the given parser. Fails if fewer than @n@
--   bytes are available.
--
-- Undefined behaviour if given a negative integer.
atSkipUnsafe# :: Int# -> ParserT st e r -> ParserT st e r
atSkipUnsafe# :: forall (st :: ZeroBitType) e a.
Int# -> ParserT st e a -> ParserT st e a
atSkipUnsafe# Int#
n# (ParserT ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e r
p) =
    forall (st :: ZeroBitType) e a.
Int# -> ParserT st e a -> ParserT st e a
withEnsure# Int#
n# forall a b. (a -> b) -> a -> b
$ forall (st :: ZeroBitType) e a.
(ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a)
-> ParserT st e a
ParserT \ForeignPtrContents
fp Addr#
eob Addr#
s st
st ->
        ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e r
p ForeignPtrContents
fp Addr#
eob (Addr# -> Int# -> Addr#
plusAddr# Addr#
s Int#
n#) st
st
{-# inline atSkipUnsafe# #-}

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

-- | Skip a parser zero or more times.
skipMany :: ParserT st e a -> ParserT st e ()
skipMany :: forall (st :: ZeroBitType) e a. ParserT st e a -> ParserT st e ()
skipMany (ParserT ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
f) = forall (st :: ZeroBitType) e a.
(ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a)
-> ParserT st e a
ParserT ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e ()
go where
  go :: ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e ()
go ForeignPtrContents
fp Addr#
eob Addr#
s st
st = case ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e a
f ForeignPtrContents
fp Addr#
eob Addr#
s st
st of
    OK# st
st a
a Addr#
s -> ForeignPtrContents -> Addr# -> Addr# -> st -> Res# st e ()
go ForeignPtrContents
fp Addr#
eob Addr#
s st
st
    Fail# st
st   -> forall (st :: ZeroBitType) a e. st -> a -> Addr# -> Res# st e a
OK# st
st () Addr#
s
    Err# st
st e
e  -> forall (st :: ZeroBitType) e a. st -> e -> Res# st e a
Err# st
st e
e
{-# inline skipMany #-}

-- TODO identical to one from parser-combinators
-- | Skip a parser one or more times.
skipSome :: ParserT st e a -> ParserT st e ()
skipSome :: forall (st :: ZeroBitType) e a. ParserT st e a -> ParserT st e ()
skipSome ParserT st e a
p = ParserT st e a
p forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (st :: ZeroBitType) e a. ParserT st e a -> ParserT st e ()
skipMany ParserT st e a
p
{-# inline skipSome #-}