{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}

{-|
Parser supporting a custom reader environment, custom error types and an 'Int'
state. A common use case of the `Int` state is to keep track of column numbers
to implement indentation-sensitive parsers.
-}

module FlatParse.Stateful (

  -- * Parser types
    FP.Parser.ParserT(..)
  , FP.Parser.Parser, FP.Parser.ParserIO, FP.Parser.ParserST

  -- * Running parsers
  , Result(..)
  , runParser
  , runParserUtf8
  , runParserIO
  , runParserST
  , embedParserST

  -- ** Primitive result types
  , type FP.Parser.Res#
  , pattern FP.Parser.OK#, pattern FP.Parser.Err#, pattern FP.Parser.Fail#
  , type FP.Parser.ResI#

  -- * Embedding `ST` operations
  , liftST

  -- * Environment operations
  , ask
  , local

  -- * State operations
  , get
  , put
  , modify

  -- * UTF conversion
  , Common.strToUtf8
  , Common.utf8ToStr

  -- * Character predicates
  , Common.isDigit
  , Common.isLatinLetter
  , Common.isGreekLetter

  -- * Parsers
  -- ** Bytewise
  , FP.Base.eof
  , FP.Base.take
  , FP.Base.take#
  , FP.Base.takeUnsafe#
  , FP.Base.takeRest
  , FP.Base.skip
  , FP.Base.skip#
  , FP.Base.skipBack
  , FP.Base.skipBack#
  , FP.Base.atSkip#
  , FP.Base.atSkipUnsafe#

  , FP.Bytes.bytes
  , FP.Bytes.bytesUnsafe
  , byteString
  , anyCString
  , anyVarintProtobuf

  -- ** Combinators
  , (FP.Parser.<|>)
  , FP.Base.branch
  , FP.Base.notFollowedBy
  , FP.Base.chainl
  , FP.Base.chainr
  , FP.Base.lookahead
  , FP.Base.ensure
  , FP.Base.ensure#
  , FP.Base.withEnsure
  , FP.Base.withEnsure1
  , FP.Base.withEnsure#
  , FP.Base.isolate
  , isolateToNextNull
  , FP.Base.isolate#
  , FP.Base.isolateUnsafe#
  , FP.Switch.switch
  , FP.Switch.switchWithPost
  , FP.Switch.rawSwitchWithPost
  , Control.Applicative.many
  , FP.Base.skipMany
  , Control.Applicative.some
  , FP.Base.skipSome

  -- ** Errors and failures
  , Control.Applicative.empty
  , FP.Base.failed
  , FP.Base.try
  , FP.Base.err
  , FP.Base.withError
  , FP.Base.fails
  , FP.Base.cut
  , FP.Base.cutting
  , FP.Base.optional
  , FP.Base.optional_
  , FP.Base.withOption

  -- ** Positions
  , FlatParse.Common.Position.Pos(..)
  , FlatParse.Common.Position.endPos
  , FlatParse.Common.Position.addrToPos#
  , FlatParse.Common.Position.posToAddr#
  , FlatParse.Common.Position.Span(..)
  , FlatParse.Common.Position.unsafeSlice
  , getPos
  , setPos
  , spanOf
  , withSpan
  , byteStringOf
  , withByteString
  , inSpan
  , Basic.validPos
  , Basic.posLineCols
  , Basic.mkPos

  -- ** Text
  -- *** UTF-8
  , FP.Text.char, FP.Text.string
  , FP.Text.anyChar, FP.Text.skipAnyChar
  , FP.Text.satisfy, FP.Text.skipSatisfy
  , FP.Text.fusedSatisfy, FP.Text.skipFusedSatisfy
  , FP.Text.takeLine
  , FP.Text.takeRestString
  , Basic.linesUtf8

  -- *** ASCII
  , FP.Text.anyAsciiChar, FP.Text.skipAnyAsciiChar
  , FP.Text.satisfyAscii, FP.Text.skipSatisfyAscii

  -- *** ASCII-encoded numbers
  , FP.Text.anyAsciiDecimalWord
  , FP.Text.anyAsciiDecimalInt
  , FP.Text.anyAsciiDecimalInteger
  , FP.Text.anyAsciiHexWord
  , FP.Text.anyAsciiHexInt

  -- ** Machine integers
  , module FP.Integers

  -- ** Debugging parsers
  , FP.Text.traceLine
  , FP.Text.traceRest

  -- * Unsafe
  , unsafeSpanToByteString

  -- ** IO
  , unsafeLiftIO

  -- ** Parsers
  , module FP.Addr
  , anyCStringUnsafe

  ) where


import qualified FlatParse.Basic as Basic
import FlatParse.Stateful.Parser
import FlatParse.Stateful.Base
import FlatParse.Stateful.Integers
import FlatParse.Stateful.Addr
import FlatParse.Common.Position
import qualified FlatParse.Common.Assorted as Common
import qualified FlatParse.Common.Numbers  as Common

import qualified FlatParse.Stateful.Parser as FP.Parser
import qualified FlatParse.Stateful.Base as FP.Base
import qualified FlatParse.Stateful.Integers as FP.Integers
import qualified FlatParse.Stateful.Bytes as FP.Bytes
import qualified FlatParse.Stateful.Text as FP.Text
import qualified FlatParse.Stateful.Switch as FP.Switch
import qualified FlatParse.Stateful.Addr as FP.Addr

import qualified Control.Applicative
import GHC.IO (IO(..), unsafeIOToST)
import GHC.Exts
import GHC.ForeignPtr
import GHC.ST (ST(..))
import System.IO.Unsafe

import qualified Data.ByteString as B
import qualified Data.ByteString.Internal as B
import qualified Data.ByteString.Unsafe as B

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

-- | Higher-level boxed data type for parsing results.
data Result e a =
    OK a Int !(B.ByteString)  -- ^ Contains return value, last `Int` state, unconsumed input.
  | Fail                      -- ^ Recoverable-by-default failure.
  | Err !e                    -- ^ Unrecoverble-by-default error.
  deriving Int -> Result e a -> ShowS
[Result e a] -> ShowS
Result e a -> String
(Int -> Result e a -> ShowS)
-> (Result e a -> String)
-> ([Result e a] -> ShowS)
-> Show (Result e a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall e a. (Show a, Show e) => Int -> Result e a -> ShowS
forall e a. (Show a, Show e) => [Result e a] -> ShowS
forall e a. (Show a, Show e) => Result e a -> String
$cshowsPrec :: forall e a. (Show a, Show e) => Int -> Result e a -> ShowS
showsPrec :: Int -> Result e a -> ShowS
$cshow :: forall e a. (Show a, Show e) => Result e a -> String
show :: Result e a -> String
$cshowList :: forall e a. (Show a, Show e) => [Result e a] -> ShowS
showList :: [Result e a] -> ShowS
Show

instance Functor (Result e) where
  fmap :: forall a b. (a -> b) -> Result e a -> Result e b
fmap a -> b
f (OK a
a Int
s ByteString
n) = let !b :: b
b = a -> b
f a
a in b -> Int -> ByteString -> Result e b
forall e a. a -> Int -> ByteString -> Result e a
OK b
b Int
s ByteString
n
  fmap a -> b
f Result e a
r          = Result e a -> Result e b
forall a b. a -> b
unsafeCoerce# Result e a
r
  {-# inline fmap #-}
  <$ :: forall a b. a -> Result e b -> Result e a
(<$) a
a (OK b
_ Int
s ByteString
n) = a -> Int -> ByteString -> Result e a
forall e a. a -> Int -> ByteString -> Result e a
OK a
a Int
s ByteString
n
  (<$) a
_ Result e b
r          = Result e b -> Result e a
forall a b. a -> b
unsafeCoerce# Result e b
r
  {-# inline (<$) #-}

-- | Embed an IO action in a 'ParserT'. This is slightly safer than 'unsafePerformIO' because
-- it will sequenced correctly with respect to the surrounding actions, and its execution is guaranteed.
unsafeLiftIO :: IO a -> ParserT st r e a
unsafeLiftIO :: forall a (st :: ZeroBitType) r e. IO a -> ParserT st r e a
unsafeLiftIO IO a
io = (ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
forall (st :: ZeroBitType) r e a.
(ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
ParserT \ForeignPtrContents
fp !r
r Addr#
eob Addr#
s Int#
n st
st ->
                   let !a :: a
a = IO a -> a
forall a. IO a -> a
unsafePerformIO IO a
io
                   in st -> a -> Addr# -> Int# -> Res# st e a
forall (st :: ZeroBitType) a e.
st -> a -> Addr# -> Int# -> Res# st e a
OK# st
st a
a Addr#
s Int#
n
{-# inline unsafeLiftIO #-}

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

-- | Run a pure parser. The `Int` argument is the initial state.
runParser :: Parser r e a -> r -> Int -> B.ByteString -> Result e a
runParser :: forall r e a. Parser r e a -> r -> Int -> ByteString -> Result e a
runParser (ParserT ForeignPtrContents
-> r -> Addr# -> Addr# -> Int# -> PureMode -> Res# PureMode e a
f) !r
r (I# Int#
n) b :: ByteString
b@(B.PS (ForeignPtr Addr#
_ ForeignPtrContents
fp) Int
_ (I# Int#
len)) = IO (Result e a) -> Result e a
forall a. IO a -> a
unsafeDupablePerformIO (IO (Result e a) -> Result e a) -> IO (Result e a) -> Result e a
forall a b. (a -> b) -> a -> b
$
  ByteString -> (CString -> IO (Result e a)) -> IO (Result e a)
forall a. ByteString -> (CString -> IO a) -> IO a
B.unsafeUseAsCString ByteString
b \(Ptr Addr#
buf) -> do
    let end :: Addr#
end = Addr# -> Int# -> Addr#
plusAddr# Addr#
buf Int#
len
    Result e a -> IO (Result e a)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure case ForeignPtrContents
-> r -> Addr# -> Addr# -> Int# -> PureMode -> Res# PureMode e a
f ForeignPtrContents
fp r
r Addr#
end Addr#
buf Int#
n PureMode
forall {k} (a :: k). Proxy# a
proxy# of
      OK# PureMode
_st a
a Addr#
s Int#
n' -> let offset :: Int#
offset = Addr# -> Addr# -> Int#
minusAddr# Addr#
s Addr#
buf
                        in a -> Int -> ByteString -> Result e a
forall e a. a -> Int -> ByteString -> Result e a
OK a
a (Int# -> Int
I# Int#
n') (Int -> ByteString -> ByteString
B.drop (Int# -> Int
I# Int#
offset) ByteString
b)

      Err# PureMode
_st e
e ->  e -> Result e a
forall e a. e -> Result e a
Err e
e
      Fail# PureMode
_st  ->  Result e a
forall e a. Result e a
Fail
{-# noinline runParser #-}
-- We mark this as noinline to allow power users to safely do unsafe state token coercions.
-- Details are discussed in https://github.com/AndrasKovacs/flatparse/pull/34#issuecomment-1326999390

-- | Run a parser on a 'String', converting it to the corresponding UTF-8 bytes.
--   The `Int` argument is the initial state.
--
-- Reminder: @OverloadedStrings@ for 'B.ByteString' does not yield a valid UTF-8
-- encoding! For non-ASCII 'B.ByteString' literal input, use this wrapper or
-- convert your input using `strToUtf8`.
runParserUtf8 :: Parser r e a -> r -> Int -> String -> Result e a
runParserUtf8 :: forall r e a. Parser r e a -> r -> Int -> String -> Result e a
runParserUtf8 Parser r e a
pa r
r !Int
n String
s = Parser r e a -> r -> Int -> ByteString -> Result e a
forall r e a. Parser r e a -> r -> Int -> ByteString -> Result e a
runParser Parser r e a
pa r
r Int
n (String -> ByteString
Common.strToUtf8 String
s)

-- | Run an `ST`-based parser. The `Int` argument is the initial state.
runParserST :: ParserST s r e a -> r -> Int -> B.ByteString -> ST s (Result e a)
runParserST :: forall s r e a.
ParserST s r e a -> r -> Int -> ByteString -> ST s (Result e a)
runParserST ParserST s r e a
pst !r
r Int
i ByteString
buf = IO (Result e a) -> ST s (Result e a)
forall a s. IO a -> ST s a
unsafeIOToST (ParserIO r e a -> r -> Int -> ByteString -> IO (Result e a)
forall r e a.
ParserIO r e a -> r -> Int -> ByteString -> IO (Result e a)
runParserIO (ParserST s r e a -> ParserIO r e a
forall a b. a -> b
unsafeCoerce# ParserST s r e a
pst) r
r Int
i ByteString
buf)
{-# inlinable runParserST #-}

-- | Run an `IO`-based parser. The `Int` argument is the initial state.
runParserIO :: ParserIO r e a -> r -> Int -> B.ByteString -> IO (Result e a)
runParserIO :: forall r e a.
ParserIO r e a -> r -> Int -> ByteString -> IO (Result e a)
runParserIO (ParserT ForeignPtrContents
-> r -> Addr# -> Addr# -> Int# -> IOMode -> Res# IOMode e a
f) !r
r (I# Int#
n) b :: ByteString
b@(B.PS (ForeignPtr Addr#
_ ForeignPtrContents
fp) Int
_ (I# Int#
len)) = do
  ByteString -> (CString -> IO (Result e a)) -> IO (Result e a)
forall a. ByteString -> (CString -> IO a) -> IO a
B.unsafeUseAsCString ByteString
b \(Ptr Addr#
buf) -> do
    let end :: Addr#
end = Addr# -> Int# -> Addr#
plusAddr# Addr#
buf Int#
len
    (IOMode -> (# IOMode, Result e a #)) -> IO (Result e a)
forall a. (IOMode -> (# IOMode, a #)) -> IO a
IO \IOMode
st -> case ForeignPtrContents
-> r -> Addr# -> Addr# -> Int# -> IOMode -> Res# IOMode e a
f ForeignPtrContents
fp r
r Addr#
end Addr#
buf Int#
n IOMode
st of
      OK# IOMode
rw' a
a Addr#
s Int#
n' ->  let offset :: Int#
offset = Addr# -> Addr# -> Int#
minusAddr# Addr#
s Addr#
buf
                         in (# IOMode
rw', a -> Int -> ByteString -> Result e a
forall e a. a -> Int -> ByteString -> Result e a
OK a
a (Int# -> Int
I# Int#
n') (Int -> ByteString -> ByteString
B.drop (Int# -> Int
I# Int#
offset) ByteString
b) #)

      Err# IOMode
rw' e
e ->  (# IOMode
rw', e -> Result e a
forall e a. e -> Result e a
Err e
e #)
      Fail# IOMode
rw'  ->  (# IOMode
rw', Result e a
forall e a. Result e a
Fail #)
{-# inlinable runParserIO #-}

-- | Run a `ParserST` inside a pure parser.
embedParserST :: forall r e a. (forall s. ParserST s r e a) -> Parser r e a
embedParserST :: forall r e a. (forall s. ParserST s r e a) -> Parser r e a
embedParserST forall s. ParserST s r e a
f = ParserST RealWorld r e a -> Parser r e a
forall a b. a -> b
unsafeCoerce# (ParserST RealWorld r e a
forall s. ParserST s r e a
f :: ParserST RealWorld r e a)
{-# inline embedParserST #-}

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

-- | Run an `ST` action in a `ParserST`.
liftST :: ST s a -> ParserST s r e a
liftST :: forall s a r e. ST s a -> ParserST s r e a
liftST (ST STRep s a
f) = (ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> STMode s -> Res# (STMode s) e a)
-> ParserT (STMode s) r e a
forall (st :: ZeroBitType) r e a.
(ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
ParserT \ForeignPtrContents
fp !r
r Addr#
eob Addr#
s Int#
n STMode s
st -> case STRep s a
f STMode s
st of
  (# STMode s
st, a
a #) -> STMode s -> a -> Addr# -> Int# -> Res# (STMode s) e a
forall (st :: ZeroBitType) a e.
st -> a -> Addr# -> Int# -> Res# st e a
OK# STMode s
st a
a Addr#
s Int#
n
{-# inline liftST #-}

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

-- | Query the `Int` state.
get :: ParserT st r e Int
get :: forall (st :: ZeroBitType) r e. ParserT st r e Int
get = (ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e Int)
-> ParserT st r e Int
forall (st :: ZeroBitType) r e a.
(ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
ParserT \ForeignPtrContents
fp !r
r Addr#
eob Addr#
s Int#
n st
st -> st -> Int -> Addr# -> Int# -> Res# st e Int
forall (st :: ZeroBitType) a e.
st -> a -> Addr# -> Int# -> Res# st e a
OK# st
st (Int# -> Int
I# Int#
n) Addr#
s Int#
n
{-# inline get #-}

-- | Write the `Int` state.
put :: Int -> ParserT st r e ()
put :: forall (st :: ZeroBitType) r e. Int -> ParserT st r e ()
put (I# Int#
n) = (ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e ())
-> ParserT st r e ()
forall (st :: ZeroBitType) r e a.
(ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
ParserT \ForeignPtrContents
fp !r
r Addr#
eob Addr#
s Int#
_ st
st -> st -> () -> Addr# -> Int# -> Res# st e ()
forall (st :: ZeroBitType) a e.
st -> a -> Addr# -> Int# -> Res# st e a
OK# st
st () Addr#
s Int#
n
{-# inline put #-}

-- | Modify the `Int` state.
modify :: (Int -> Int) -> ParserT st r e ()
modify :: forall (st :: ZeroBitType) r e. (Int -> Int) -> ParserT st r e ()
modify Int -> Int
f = (ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e ())
-> ParserT st r e ()
forall (st :: ZeroBitType) r e a.
(ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
ParserT \ForeignPtrContents
fp !r
r Addr#
eob Addr#
s Int#
n st
st ->
  case Int -> Int
f (Int# -> Int
I# Int#
n) of
    I# Int#
n -> st -> () -> Addr# -> Int# -> Res# st e ()
forall (st :: ZeroBitType) a e.
st -> a -> Addr# -> Int# -> Res# st e a
OK# st
st () Addr#
s Int#
n
{-# inline modify #-}

-- | Query the environment.
ask :: ParserT st r e r
ask :: forall (st :: ZeroBitType) r e. ParserT st r e r
ask = (ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e r)
-> ParserT st r e r
forall (st :: ZeroBitType) r e a.
(ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
ParserT \ForeignPtrContents
fp !r
r Addr#
eob Addr#
s Int#
n st
st -> st -> r -> Addr# -> Int# -> Res# st e r
forall (st :: ZeroBitType) a e.
st -> a -> Addr# -> Int# -> Res# st e a
OK# st
st r
r Addr#
s Int#
n
{-# inline ask #-}

-- | Run a parser in a modified environment.
local :: (r -> r) -> ParserT st r e a -> ParserT st r e a
local :: forall r (st :: ZeroBitType) e a.
(r -> r) -> ParserT st r e a -> ParserT st r e a
local r -> r
f (ParserT ForeignPtrContents
-> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a
g) = (ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
forall (st :: ZeroBitType) r e a.
(ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
ParserT \ForeignPtrContents
fp !r
r Addr#
eob Addr#
s Int#
n st
st -> let !r' :: r
r' = r -> r
f r
r in ForeignPtrContents
-> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a
g ForeignPtrContents
fp r
r' Addr#
eob Addr#
s Int#
n st
st
{-# inline local #-}

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

-- | Parse a given `B.ByteString`. If the bytestring is statically known, consider using 'bytes' instead.
byteString :: B.ByteString -> ParserT st r e ()
byteString :: forall (st :: ZeroBitType) r e. ByteString -> ParserT st r e ()
byteString (B.PS (ForeignPtr Addr#
bs ForeignPtrContents
fcontent) Int
_ (I# Int#
len)) =

  let go64 :: Addr# -> Addr# -> Addr# -> Int# -> State# RealWorld -> Res# (State# RealWorld) e ()
      go64 :: forall e.
Addr# -> Addr# -> Addr# -> Int# -> IOMode -> Res# IOMode e ()
go64 Addr#
bs Addr#
bsend Addr#
s Int#
n IOMode
rw =
        let bs' :: Addr#
bs' = Addr# -> Int# -> Addr#
plusAddr# Addr#
bs Int#
8# in
        case Addr# -> Addr# -> Int#
gtAddr# Addr#
bs' Addr#
bsend of
          Int#
1# -> Addr# -> Addr# -> Addr# -> Int# -> IOMode -> Res# IOMode e ()
forall e.
Addr# -> Addr# -> Addr# -> Int# -> IOMode -> Res# IOMode e ()
go8 Addr#
bs Addr#
bsend Addr#
s Int#
n IOMode
rw
#if MIN_VERSION_base(4,17,0)
          Int#
_  -> case Word64# -> Word64# -> Int#
eqWord64# (Addr# -> Int# -> Word64#
indexWord64OffAddr# Addr#
bs Int#
0#) (Addr# -> Int# -> Word64#
indexWord64OffAddr# Addr#
s Int#
0#) of
#else
          _  -> case eqWord# (indexWord64OffAddr# bs 0#) (indexWord64OffAddr# s 0#) of
#endif
            Int#
1# -> Addr# -> Addr# -> Addr# -> Int# -> IOMode -> Res# IOMode e ()
forall e.
Addr# -> Addr# -> Addr# -> Int# -> IOMode -> Res# IOMode e ()
go64 Addr#
bs' Addr#
bsend (Addr# -> Int# -> Addr#
plusAddr# Addr#
s Int#
8#) Int#
n IOMode
rw
            Int#
_  -> IOMode -> Res# IOMode e ()
forall (st :: ZeroBitType) e a. st -> Res# st e a
Fail# IOMode
rw

      go8 :: Addr# -> Addr# -> Addr# -> Int# -> State# RealWorld -> Res# (State# RealWorld) e ()
      go8 :: forall e.
Addr# -> Addr# -> Addr# -> Int# -> IOMode -> Res# IOMode e ()
go8 Addr#
bs Addr#
bsend Addr#
s Int#
n IOMode
rw = case Addr# -> Addr# -> Int#
ltAddr# Addr#
bs Addr#
bsend of
#if MIN_VERSION_base(4,16,0)
        Int#
1# -> case Word8# -> Word8# -> Int#
eqWord8# (Addr# -> Int# -> Word8#
indexWord8OffAddr# Addr#
bs Int#
0#) (Addr# -> Int# -> Word8#
indexWord8OffAddr# Addr#
s Int#
0#) of
#else
        1# -> case eqWord# (indexWord8OffAddr# bs 0#) (indexWord8OffAddr# s 0#) of
#endif
          Int#
1# -> Addr# -> Addr# -> Addr# -> Int# -> IOMode -> Res# IOMode e ()
forall e.
Addr# -> Addr# -> Addr# -> Int# -> IOMode -> Res# IOMode e ()
go8 (Addr# -> Int# -> Addr#
plusAddr# Addr#
bs Int#
1#) Addr#
bsend (Addr# -> Int# -> Addr#
plusAddr# Addr#
s Int#
1#) Int#
n IOMode
rw
          Int#
_  -> IOMode -> Res# IOMode e ()
forall (st :: ZeroBitType) e a. st -> Res# st e a
Fail# IOMode
rw
        Int#
_  -> IOMode -> () -> Addr# -> Int# -> Res# IOMode e ()
forall (st :: ZeroBitType) a e.
st -> a -> Addr# -> Int# -> Res# st e a
OK# IOMode
rw () Addr#
s Int#
n

      go :: Addr# -> Addr# -> Addr# -> Int# -> State# RealWorld -> Res# (State# RealWorld) e ()
      go :: forall e.
Addr# -> Addr# -> Addr# -> Int# -> IOMode -> Res# IOMode e ()
go Addr#
bs Addr#
bsend Addr#
s Int#
n IOMode
rw = case Addr# -> Addr# -> Addr# -> Int# -> IOMode -> Res# IOMode e ()
forall e.
Addr# -> Addr# -> Addr# -> Int# -> IOMode -> Res# IOMode e ()
go64 Addr#
bs Addr#
bsend Addr#
s Int#
n IOMode
rw of
        (# IOMode
rw', ResI# e ()
res #) -> case ForeignPtrContents -> IOMode -> IOMode
forall a. a -> IOMode -> IOMode
touch# ForeignPtrContents
fcontent IOMode
rw' of
          IOMode
rw'' -> (# IOMode
rw'', ResI# e ()
res #)

  in (ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e ())
-> ParserT st r e ()
forall (st :: ZeroBitType) r e a.
(ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
ParserT \ForeignPtrContents
fp !r
r Addr#
eob Addr#
s Int#
n st
st ->
      case Int#
len Int# -> Int# -> Int#
<=# Addr# -> Addr# -> Int#
minusAddr# Addr#
eob Addr#
s of
           Int#
1# -> case (IOMode -> Res# IOMode e ()) -> Res# IOMode e ()
forall o. (IOMode -> o) -> o
runRW# (Addr# -> Addr# -> Addr# -> Int# -> IOMode -> Res# IOMode e ()
forall e.
Addr# -> Addr# -> Addr# -> Int# -> IOMode -> Res# IOMode e ()
go Addr#
bs (Addr# -> Int# -> Addr#
plusAddr# Addr#
bs Int#
len) Addr#
s Int#
n) of
             (# IOMode
rw, ResI# e ()
a #) -> (# st
st, ResI# e ()
a #)
           Int#
_  -> st -> Res# st e ()
forall (st :: ZeroBitType) e a. st -> Res# st e a
Fail# st
st
{-# inline byteString #-}

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

-- | Get the current position in the input.
getPos :: ParserT st r e Pos
getPos :: forall (st :: ZeroBitType) r e. ParserT st r e Pos
getPos = (ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e Pos)
-> ParserT st r e Pos
forall (st :: ZeroBitType) r e a.
(ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
ParserT \ForeignPtrContents
fp !r
r Addr#
eob Addr#
s Int#
n st
st -> st -> Pos -> Addr# -> Int# -> Res# st e Pos
forall (st :: ZeroBitType) a e.
st -> a -> Addr# -> Int# -> Res# st e a
OK# st
st (Addr# -> Addr# -> Pos
addrToPos# Addr#
eob Addr#
s) Addr#
s Int#
n
{-# inline getPos #-}

-- | Set the input position.
--
-- Warning: this can result in crashes if the position points outside the
-- current buffer. It is always safe to 'setPos' values which came from 'getPos'
-- with the current input.
setPos :: Pos -> ParserT st r e ()
setPos :: forall (st :: ZeroBitType) r e. Pos -> ParserT st r e ()
setPos Pos
s = (ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e ())
-> ParserT st r e ()
forall (st :: ZeroBitType) r e a.
(ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
ParserT \ForeignPtrContents
fp !r
r Addr#
eob Addr#
_ Int#
n st
st -> st -> () -> Addr# -> Int# -> Res# st e ()
forall (st :: ZeroBitType) a e.
st -> a -> Addr# -> Int# -> Res# st e a
OK# st
st () (Addr# -> Pos -> Addr#
posToAddr# Addr#
eob Pos
s) Int#
n
{-# inline setPos #-}

-- | Return the consumed span of a parser. Use `withSpan` if possible for better efficiency.
spanOf :: ParserT st r e a -> ParserT st r e Span
spanOf :: forall (st :: ZeroBitType) r e a.
ParserT st r e a -> ParserT st r e Span
spanOf (ParserT ForeignPtrContents
-> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a
f) = (ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e Span)
-> ParserT st r e Span
forall (st :: ZeroBitType) r e a.
(ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
ParserT \ForeignPtrContents
fp !r
r Addr#
eob Addr#
s Int#
n st
st -> case ForeignPtrContents
-> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a
f ForeignPtrContents
fp r
r Addr#
eob Addr#
s Int#
n st
st of
  OK# st
st' a
a Addr#
s' Int#
n -> st -> Span -> Addr# -> Int# -> Res# st e Span
forall (st :: ZeroBitType) a e.
st -> a -> Addr# -> Int# -> Res# st e a
OK# st
st' (Pos -> Pos -> Span
Span (Addr# -> Addr# -> Pos
addrToPos# Addr#
eob Addr#
s) (Addr# -> Addr# -> Pos
addrToPos# Addr#
eob Addr#
s')) Addr#
s' Int#
n
  Res# st e a
x              -> Res# st e a -> Res# st e Span
forall a b. a -> b
unsafeCoerce# Res# st e a
x
{-# inline spanOf #-}

-- | Bind the result together with the span of the result. CPS'd version of `spanOf`
--   for better unboxing.
withSpan :: ParserT st r e a -> (a -> Span -> ParserT st r e b) -> ParserT st r e b
withSpan :: forall (st :: ZeroBitType) r e a b.
ParserT st r e a
-> (a -> Span -> ParserT st r e b) -> ParserT st r e b
withSpan (ParserT ForeignPtrContents
-> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a
f) a -> Span -> ParserT st r e b
g = (ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e b)
-> ParserT st r e b
forall (st :: ZeroBitType) r e a.
(ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
ParserT \ForeignPtrContents
fp !r
r Addr#
eob Addr#
s Int#
n st
st -> case ForeignPtrContents
-> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a
f ForeignPtrContents
fp r
r Addr#
eob Addr#
s Int#
n st
st of
  OK# st
st' a
a Addr#
s' Int#
n -> ParserT st r e b
-> ForeignPtrContents
-> r
-> Addr#
-> Addr#
-> Int#
-> st
-> Res# st e b
forall (st :: ZeroBitType) r e a.
ParserT st r e a
-> ForeignPtrContents
-> r
-> Addr#
-> Addr#
-> Int#
-> st
-> Res# st e a
runParserT# (a -> Span -> ParserT st r e b
g a
a (Pos -> Pos -> Span
Span (Addr# -> Addr# -> Pos
addrToPos# Addr#
eob Addr#
s) (Addr# -> Addr# -> Pos
addrToPos# Addr#
eob Addr#
s'))) ForeignPtrContents
fp r
r Addr#
eob Addr#
s' Int#
n st
st'
  Res# st e a
x              -> Res# st e a -> Res# st e b
forall a b. a -> b
unsafeCoerce# Res# st e a
x
{-# inline withSpan #-}

-- | Return the `B.ByteString` consumed by a parser. Note: it's more efficient to use `spanOf` and
--   `withSpan` instead.
byteStringOf :: ParserT st r e a -> ParserT st r e B.ByteString
byteStringOf :: forall (st :: ZeroBitType) r e a.
ParserT st r e a -> ParserT st r e ByteString
byteStringOf (ParserT ForeignPtrContents
-> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a
f) = (ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e ByteString)
-> ParserT st r e ByteString
forall (st :: ZeroBitType) r e a.
(ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
ParserT \ForeignPtrContents
fp !r
r Addr#
eob Addr#
s Int#
n st
st -> case ForeignPtrContents
-> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a
f ForeignPtrContents
fp r
r Addr#
eob Addr#
s Int#
n st
st of
  OK# st
st' a
a Addr#
s' Int#
n -> st -> ByteString -> Addr# -> Int# -> Res# st e ByteString
forall (st :: ZeroBitType) a e.
st -> a -> Addr# -> Int# -> Res# st e a
OK# st
st' (ForeignPtr Word8 -> Int -> Int -> ByteString
B.PS (Addr# -> ForeignPtrContents -> ForeignPtr Word8
forall a. Addr# -> ForeignPtrContents -> ForeignPtr a
ForeignPtr Addr#
s ForeignPtrContents
fp) Int
0 (Int# -> Int
I# (Addr# -> Addr# -> Int#
minusAddr# Addr#
s' Addr#
s))) Addr#
s' Int#
n
  Res# st e a
x              -> Res# st e a -> Res# st e ByteString
forall a b. a -> b
unsafeCoerce# Res# st e a
x
{-# inline byteStringOf #-}

-- | CPS'd version of `byteStringOf`. Can be more efficient, because the result is more eagerly unboxed
--   by GHC. It's more efficient to use `spanOf` or `withSpan` instead.
withByteString :: ParserT st r e a -> (a -> B.ByteString -> ParserT st r e b) -> ParserT st r e b
withByteString :: forall (st :: ZeroBitType) r e a b.
ParserT st r e a
-> (a -> ByteString -> ParserT st r e b) -> ParserT st r e b
withByteString (ParserT ForeignPtrContents
-> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a
f) a -> ByteString -> ParserT st r e b
g = (ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e b)
-> ParserT st r e b
forall (st :: ZeroBitType) r e a.
(ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
ParserT \ForeignPtrContents
fp !r
r Addr#
eob Addr#
s Int#
n st
st -> case ForeignPtrContents
-> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a
f ForeignPtrContents
fp r
r Addr#
eob Addr#
s Int#
n st
st of
  OK# st
st' a
a Addr#
s' Int#
n -> ParserT st r e b
-> ForeignPtrContents
-> r
-> Addr#
-> Addr#
-> Int#
-> st
-> Res# st e b
forall (st :: ZeroBitType) r e a.
ParserT st r e a
-> ForeignPtrContents
-> r
-> Addr#
-> Addr#
-> Int#
-> st
-> Res# st e a
runParserT# (a -> ByteString -> ParserT st r e b
g a
a (ForeignPtr Word8 -> Int -> Int -> ByteString
B.PS (Addr# -> ForeignPtrContents -> ForeignPtr Word8
forall a. Addr# -> ForeignPtrContents -> ForeignPtr a
ForeignPtr Addr#
s ForeignPtrContents
fp) Int
0 (Int# -> Int
I# (Addr# -> Addr# -> Int#
minusAddr# Addr#
s' Addr#
s)))) ForeignPtrContents
fp r
r Addr#
eob Addr#
s' Int#
n st
st'
  Res# st e a
x              -> Res# st e a -> Res# st e b
forall a b. a -> b
unsafeCoerce# Res# st e a
x
{-# inline withByteString #-}

-- | Run a parser in a given input 'Span'.
--
-- The input position and the parser state is restored after the parser is
-- finished, so 'inSpan' does not consume input and has no side effect.
--
-- Warning: this operation may crash if the given span points outside the
-- current parsing buffer. It's always safe to use 'inSpan' if the 'Span' comes
-- from a previous 'withSpan' or 'spanOf' call on the current input.
inSpan :: Span -> ParserT st r e a -> ParserT st r e a
inSpan :: forall (st :: ZeroBitType) r e a.
Span -> ParserT st r e a -> ParserT st r e a
inSpan (Span Pos
s Pos
eob) (ParserT ForeignPtrContents
-> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a
f) = (ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
forall (st :: ZeroBitType) r e a.
(ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
ParserT \ForeignPtrContents
fp !r
r Addr#
eob' Addr#
s' Int#
n' st
st ->
  case ForeignPtrContents
-> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a
f ForeignPtrContents
fp r
r (Addr# -> Pos -> Addr#
posToAddr# Addr#
eob' Pos
eob) (Addr# -> Pos -> Addr#
posToAddr# Addr#
eob' Pos
s) Int#
n' st
st of
    OK# st
st' a
a Addr#
_ Int#
_ -> st -> a -> Addr# -> Int# -> Res# st e a
forall (st :: ZeroBitType) a e.
st -> a -> Addr# -> Int# -> Res# st e a
OK# st
st' a
a Addr#
s' Int#
n'
    Res# st e a
x             -> Res# st e a -> Res# st e a
forall a b. a -> b
unsafeCoerce# Res# st e a
x
{-# inline inSpan #-}

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

-- | Create a 'B.ByteString' from a 'Span'.
--
-- The result is invalid if the 'Span' points outside the current buffer, or if
-- the 'Span' start is greater than the end position.
unsafeSpanToByteString :: Span -> ParserT st r e B.ByteString
unsafeSpanToByteString :: forall (st :: ZeroBitType) r e. Span -> ParserT st r e ByteString
unsafeSpanToByteString (Span Pos
l Pos
r) =
  ParserT st r e ByteString -> ParserT st r e ByteString
forall (st :: ZeroBitType) r e a.
ParserT st r e a -> ParserT st r e a
lookahead (Pos -> ParserT st r e ()
forall (st :: ZeroBitType) r e. Pos -> ParserT st r e ()
setPos Pos
l ParserT st r e ()
-> ParserT st r e ByteString -> ParserT st r e ByteString
forall a b.
ParserT st r e a -> ParserT st r e b -> ParserT st r e b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParserT st r e () -> ParserT st r e ByteString
forall (st :: ZeroBitType) r e a.
ParserT st r e a -> ParserT st r e ByteString
byteStringOf (Pos -> ParserT st r e ()
forall (st :: ZeroBitType) r e. Pos -> ParserT st r e ()
setPos Pos
r))
{-# inline unsafeSpanToByteString #-}

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

-- | Isolate the given parser up to (excluding) the next null byte.
--
-- Like 'isolate', all isolated bytes must be consumed. The null byte is
-- consumed afterwards.
--
-- Useful for defining parsers for null-terminated data.
isolateToNextNull :: ParserT st r e a -> ParserT st r e a
isolateToNextNull :: forall (st :: ZeroBitType) r e a.
ParserT st r e a -> ParserT st r e a
isolateToNextNull (ParserT ForeignPtrContents
-> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a
p) = (ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
forall (st :: ZeroBitType) r e a.
(ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
ParserT \ForeignPtrContents
fp !r
r Addr#
eob Addr#
s Int#
n st
st -> ForeignPtrContents
-> r -> Int# -> Addr# -> Addr# -> st -> Addr# -> Res# st e a
go ForeignPtrContents
fp r
r Int#
n Addr#
eob Addr#
s st
st Addr#
s
  where
    goP :: ForeignPtrContents
-> r -> Int# -> Addr# -> Addr# -> st -> Res# st e a
goP ForeignPtrContents
fp r
r Int#
n Addr#
sNull Addr#
s0 st
st =
        case ForeignPtrContents
-> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a
p ForeignPtrContents
fp r
r Addr#
sNull Addr#
s0 Int#
n st
st of
          OK# st
st' a
a Addr#
s' Int#
n' ->
            case Addr# -> Addr# -> Int#
eqAddr# Addr#
s' Addr#
sNull of
              Int#
1# -> -- consumed up to null: skip null, return
                    st -> a -> Addr# -> Int# -> Res# st e a
forall (st :: ZeroBitType) a e.
st -> a -> Addr# -> Int# -> Res# st e a
OK#   st
st' a
a (Addr#
sNull Addr# -> Int# -> Addr#
`plusAddr#` Int#
1#) Int#
n'
              Int#
_  -> st -> Res# st e a
forall (st :: ZeroBitType) e a. st -> Res# st e a
Fail# st
st' -- didn't consume fully up to null: fail
          Res# st e a
x -> Res# st e a
x

    go8 :: ForeignPtrContents
-> r -> Int# -> Addr# -> Addr# -> st -> Addr# -> Res# st e a
go8 ForeignPtrContents
fp r
r Int#
n Addr#
eob Addr#
s0 st
st Addr#
s =
        case Addr# -> Addr# -> Int#
eqAddr# Addr#
eob Addr#
s of
          Int#
1# -> st -> Res# st e a
forall (st :: ZeroBitType) e a. st -> Res# st e a
Fail# st
st -- end of input, no null: fail
          Int#
_  ->
            let s' :: Addr#
s' = Addr#
s Addr# -> Int# -> Addr#
`plusAddr#` Int#
1# in
#if MIN_VERSION_base(4,16,0)
            -- below may be made clearer with ExtendedLiterals (GHC 9.8)
            case Word8# -> Word8# -> Int#
eqWord8# (Addr# -> Int# -> Word8#
indexWord8OffAddr# Addr#
s Int#
0#) (Word# -> Word8#
wordToWord8# Word#
0##) of
#else
            case eqWord# (indexWord8OffAddr# s 0#) 0## of
#endif
              Int#
1# -> ForeignPtrContents
-> r -> Int# -> Addr# -> Addr# -> st -> Res# st e a
goP ForeignPtrContents
fp r
r Int#
n Addr#
s   Addr#
s0 st
st    --     0x00: isolate, execute parser
              Int#
_  -> ForeignPtrContents
-> r -> Int# -> Addr# -> Addr# -> st -> Addr# -> Res# st e a
go8 ForeignPtrContents
fp r
r Int#
n Addr#
eob Addr#
s0 st
st Addr#
s' -- not 0x00: next please!

    {- The "find first null byte" algorithms used here are adapted from
       Hacker's Delight (2012) ch.6.

    We read a word (8 bytes) at a time for efficiency. The internal algorithm
    does byte indexing, thus endianness matters. We switch between indexing
    algorithms depending on compile-time native endianness. (The code
    surrounding the indexing is endian-independent, so we do this inline).
    -}
    go :: ForeignPtrContents
-> r -> Int# -> Addr# -> Addr# -> st -> Addr# -> Res# st e a
go ForeignPtrContents
fp r
r Int#
n Addr#
eob Addr#
s0 st
st Addr#
s =
        let sWord :: Addr#
sWord = Addr#
s Addr# -> Int# -> Addr#
`plusAddr#` Int#
8# in
        case Addr# -> Addr# -> Int#
gtAddr# Addr#
sWord Addr#
eob of
          Int#
1# -> -- <  8 bytes of input: revert to scanning byte by byte
            -- we _could_ operate on a word and simply ensure not to use the
            -- out-of-bounds data, which would be faster, but the act of
            -- reading could probably segfault
            ForeignPtrContents
-> r -> Int# -> Addr# -> Addr# -> st -> Addr# -> Res# st e a
go8 ForeignPtrContents
fp r
r Int#
n Addr#
eob Addr#
s0 st
st Addr#
s
          Int#
_  -> -- >= 8 bytes of input: use efficient 8-byte scanning
#ifdef WORDS_BIGENDIAN
            -- big-endian ("L->R"): find leftmost null byte
            let !x@(I# x#) = Common.zbytel'intermediate (I# (indexIntOffAddr# s 0#)) in
#else
            -- little-endian ("R->L"): find rightmost null byte
            let !x :: Int
x@(I# Int#
x#) = Int -> Int
forall a. (FiniteBits a, Num a) => a -> a
Common.zbyter'intermediate (Int# -> Int
I# (Addr# -> Int# -> Int#
indexIntOffAddr# Addr#
s Int#
0#)) in
#endif
            case Int#
x# Int# -> Int# -> Int#
==# Int#
0# of
              Int#
1# -> ForeignPtrContents
-> r -> Int# -> Addr# -> Addr# -> st -> Addr# -> Res# st e a
go ForeignPtrContents
fp r
r Int#
n Addr#
eob Addr#
s0 st
st Addr#
sWord -- no 0x00 in next word
              Int#
_  -> -- 0x00 somewhere in next word
#ifdef WORDS_BIGENDIAN
                let !(I# nullIdx#) = Common.zbytel'toIdx x in
#else
                let !(I# Int#
nullIdx#) = Int -> Int
forall a. (FiniteBits a, Num a) => a -> Int
Common.zbyter'toIdx Int
x in
                -- TO TEST BE ON LE: change above CPP to zbytel, uncomment below
                -- let !(I# nullIdx#) = Common.zbytel'toIdx (I# (word2Int# (byteSwap# (int2Word# x#)))) in
#endif
                let sNull :: Addr#
sNull = Addr#
s Addr# -> Int# -> Addr#
`plusAddr#` Int#
nullIdx# in
                ForeignPtrContents
-> r -> Int# -> Addr# -> Addr# -> st -> Res# st e a
goP ForeignPtrContents
fp r
r Int#
n Addr#
sNull Addr#
s0 st
st
{-# inline isolateToNextNull #-}

-- | Read a null-terminated bytestring (a C-style string).
--
-- Consumes the null terminator.
anyCString :: ParserT st r e B.ByteString
anyCString :: forall (st :: ZeroBitType) r e. ParserT st r e ByteString
anyCString = ParserT st r e ByteString -> ParserT st r e ByteString
forall (st :: ZeroBitType) r e a.
ParserT st r e a -> ParserT st r e a
isolateToNextNull ParserT st r e ByteString
forall (st :: ZeroBitType) r e. ParserT st r e ByteString
takeRest
{-# inline anyCString #-}

-- | Read a null-terminated bytestring (a C-style string), where the bytestring
--   is known to be null-terminated somewhere in the input.
--
-- Highly unsafe. Unless you have a guarantee that the string will be null
-- terminated before the input ends, use 'anyCString' instead. Honestly, I'm not
-- sure if this is a good function to define. But here it is.
--
-- Fails on GHC versions older than 9.0, since we make use of the
-- 'cstringLength#' primop introduced in GHC 9.0, and we aren't very useful
-- without it.
--
-- Consumes the null terminator.
anyCStringUnsafe :: ParserT st r e B.ByteString
{-# inline anyCStringUnsafe #-}
#if MIN_VERSION_base(4,15,0)
anyCStringUnsafe :: forall (st :: ZeroBitType) r e. ParserT st r e ByteString
anyCStringUnsafe = (ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e ByteString)
-> ParserT st r e ByteString
forall (st :: ZeroBitType) r e a.
(ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
ParserT \ForeignPtrContents
fp !r
r Addr#
eob Addr#
s Int#
n st
st ->
  case Addr# -> Addr# -> Int#
eqAddr# Addr#
eob Addr#
s of
    Int#
1# -> st -> Res# st e ByteString
forall (st :: ZeroBitType) e a. st -> Res# st e a
Fail# st
st
    Int#
_  -> let n# :: Int#
n#  = Addr# -> Int#
cstringLength# Addr#
s
              s'# :: Addr#
s'# = Addr# -> Int# -> Addr#
plusAddr# Addr#
s (Int#
n# Int# -> Int# -> Int#
+# Int#
1#)
           in st -> ByteString -> Addr# -> Int# -> Res# st e ByteString
forall (st :: ZeroBitType) a e.
st -> a -> Addr# -> Int# -> Res# st e a
OK# st
st (ForeignPtr Word8 -> Int -> Int -> ByteString
B.PS (Addr# -> ForeignPtrContents -> ForeignPtr Word8
forall a. Addr# -> ForeignPtrContents -> ForeignPtr a
ForeignPtr Addr#
s ForeignPtrContents
fp) Int
0 (Int# -> Int
I# Int#
n#)) Addr#
s'# Int#
n
#else
anyCStringUnsafe = error "Flatparse.Stateful.anyCStringUnsafe: requires GHC 9.0 / base-4.15, not available on this compiler"
#endif

-- | Read a protobuf-style varint into a positive 'Int'.
--
-- protobuf-style varints are byte-aligned. For each byte, the lower 7 bits are
-- data and the MSB indicates if there are further bytes. Once fully parsed, the
-- 7-bit payloads are concatenated and interpreted as a little-endian unsigned
-- integer.
--
-- Fails if the varint exceeds the positive 'Int' range.
--
-- Really, these are varnats. They also match with the LEB128 varint encoding.
--
-- protobuf encodes negatives in unsigned integers using zigzag encoding. See
-- the @fromZigzag@ family of functions for this functionality.
--
-- Further reading:
-- https://developers.google.com/protocol-buffers/docs/encoding#varints
anyVarintProtobuf :: ParserT st r e Int
anyVarintProtobuf :: forall (st :: ZeroBitType) r e. ParserT st r e Int
anyVarintProtobuf = (ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e Int)
-> ParserT st r e Int
forall (st :: ZeroBitType) r e a.
(ForeignPtrContents
 -> r -> Addr# -> Addr# -> Int# -> st -> Res# st e a)
-> ParserT st r e a
ParserT \ForeignPtrContents
fp !r
r Addr#
eob Addr#
s Int#
n st
st ->
    case Addr# -> Addr# -> (# (# #) | (# Int#, Addr#, Int# #) #)
Common.anyVarintProtobuf# Addr#
eob Addr#
s of
      (# (##) | #) -> st -> Res# st e Int
forall (st :: ZeroBitType) e a. st -> Res# st e a
Fail# st
st
      (# | (# Int#
w#, Addr#
s#, Int#
bits# #) #) ->
        case Int#
bits# Int# -> Int# -> Int#
># Int#
63# of
          Int#
0# -> st -> Int -> Addr# -> Int# -> Res# st e Int
forall (st :: ZeroBitType) a e.
st -> a -> Addr# -> Int# -> Res# st e a
OK# st
st (Int# -> Int
I# Int#
w#) Addr#
s# Int#
n
          Int#
_  -> st -> Res# st e Int
forall (st :: ZeroBitType) e a. st -> Res# st e a
Fail# st
st -- overflow
{-# inline anyVarintProtobuf #-}