{-# language TypeApplications #-}
{-# language BlockArguments #-}
{-# language ScopedTypeVariables #-}
{-# language BangPatterns #-}
{-# language MagicHash #-}
module Text.Parsnip.Internal.Simple
( SimpleResult(..)
, relative
, absolute
) where

import Data.ByteString
import GHC.Prim
import GHC.Types
import Text.Parsnip.Internal.Parser
import Text.Parsnip.Internal.Private

--------------------------------------------------------------------------------
-- * Simple parsers
--------------------------------------------------------------------------------

data SimpleResult a
  = SimpleOK a {-# unpack #-} !Int
  | SimpleFail {-# unpack #-} !Int

relative :: forall s a. KnownBase s => (ByteString -> SimpleResult a) -> Parser s a
relative :: forall s a.
KnownBase s =>
(ByteString -> SimpleResult a) -> Parser s a
relative = case forall s. KnownBase s => Base s
reflectBase @s of
  !(Base Addr#
x ForeignPtrContents
g Addr#
q Addr#
r) -> \ByteString -> SimpleResult a
f -> (Addr# -> State# s -> Result s a) -> Parser s a
forall s a. (Addr# -> State# s -> Result s a) -> Parser s a
Parser \Addr#
p State# s
s -> case ByteString -> SimpleResult a
f (ByteString -> SimpleResult a) -> ByteString -> SimpleResult a
forall a b. (a -> b) -> a -> b
$ Addr# -> ForeignPtrContents -> Int# -> ByteString
mkBS (Addr#
x Addr# -> Int# -> Addr#
`plusAddr#` Addr# -> Addr# -> Int#
minusAddr# Addr#
p Addr#
q) ForeignPtrContents
g (Addr# -> Addr# -> Int#
minusAddr# Addr#
r Addr#
p) of
    SimpleOK a
a (I# Int#
i) -> a -> Addr# -> State# s -> Result s a
forall a s. a -> Addr# -> State# s -> Result s a
OK a
a (Addr# -> Int# -> Addr#
plusAddr# Addr#
p Int#
i) State# s
s
    SimpleFail (I# Int#
i) -> Addr# -> State# s -> Result s a
forall s a. Addr# -> State# s -> Result s a
Fail (Addr# -> Int# -> Addr#
plusAddr# Addr#
p Int#
i) State# s
s
{-# inline relative #-}

absolute :: forall s a. KnownBase s => (ByteString -> Int -> SimpleResult a) -> Parser s a
absolute :: forall s a.
KnownBase s =>
(ByteString -> Int -> SimpleResult a) -> Parser s a
absolute = case forall s. KnownBase s => Base s
reflectBase @s of
  !(Base Addr#
x ForeignPtrContents
g Addr#
q Addr#
r) -> \ByteString -> Int -> SimpleResult a
f -> (Addr# -> State# s -> Result s a) -> Parser s a
forall s a. (Addr# -> State# s -> Result s a) -> Parser s a
Parser \Addr#
p State# s
s -> case ByteString -> Int -> SimpleResult a
f (Addr# -> ForeignPtrContents -> Int# -> ByteString
mkBS Addr#
x ForeignPtrContents
g (Addr# -> Addr# -> Int#
minusAddr# Addr#
r Addr#
q)) (Int -> SimpleResult a) -> Int -> SimpleResult a
forall a b. (a -> b) -> a -> b
$ Int# -> Int
I# (Addr# -> Addr# -> Int#
minusAddr# Addr#
p Addr#
q) of
    SimpleOK a
a (I# Int#
i) -> a -> Addr# -> State# s -> Result s a
forall a s. a -> Addr# -> State# s -> Result s a
OK a
a (Addr# -> Int# -> Addr#
plusAddr# Addr#
p Int#
i) State# s
s
    SimpleFail (I# Int#
i) -> Addr# -> State# s -> Result s a
forall s a. Addr# -> State# s -> Result s a
Fail (Addr# -> Int# -> Addr#
plusAddr# Addr#
p Int#
i) State# s
s
{-# inline absolute #-}