module Language.Parser.Ptera.TH.ParserLib (
    module Language.Parser.Ptera.Runner.Parser,
    module Data.Proxy,
    Parser,
    PteraTHArray,
    pteraTHTokenToTerminal,
    pteraTHArrayIndex,
    pteraTHArrayFromList,
    pteraTHLookupTable8,
    pteraTHLookupTable16,
    pteraTHLookupTable32,
    pteraTHUnsafeExtractReduceArgument,
    pteraTHUnsafeRunner,
    pteraTHAction,
    pteraTHActionTaskPure,
) where

import           Language.Parser.Ptera.Prelude

import qualified Data.Array                          as Array
import qualified Data.Bits                           as Bits
import           Data.Proxy                          (Proxy (..))
import qualified GHC.Prim                            as Prim
import qualified GHC.ST                              as ST
import qualified GHC.Types                           as Types
import qualified Language.Parser.Ptera.Data.HEnum    as HEnum
import qualified Language.Parser.Ptera.Runner        as Runner
import           Language.Parser.Ptera.Runner.Parser (ActionM (..), ActionTask,
                                                      AltKind (..),
                                                      GrammarToken (..),
                                                      ReduceArgument (..),
                                                      RunnerParser (..),
                                                      Trans (..), TransOp (..),
                                                      failAction, getAction,
                                                      modifyAction)
import qualified Unsafe.Coerce                       as Unsafe

type Parser = Runner.T

type PteraTHArray = Array.Array

pteraTHTokenToTerminal :: GrammarToken tokens elem => Proxy tokens -> elem -> Int
pteraTHTokenToTerminal :: forall tokens elem.
GrammarToken tokens elem =>
Proxy tokens -> elem -> Int
pteraTHTokenToTerminal Proxy tokens
p elem
t = forall k (as :: [k]). HEnum as -> Int
HEnum.unsafeHEnum do forall tokens elem.
GrammarToken tokens elem =>
Proxy tokens -> elem -> T (TokensTag tokens)
tokenToTerminal Proxy tokens
p elem
t

pteraTHArrayIndex :: PteraTHArray Int e -> Int -> e
pteraTHArrayIndex :: forall e. PteraTHArray Int e -> Int -> e
pteraTHArrayIndex PteraTHArray Int e
arr Int
i = PteraTHArray Int e
arr forall i e. Ix i => Array i e -> i -> e
Array.! Int
i

pteraTHArrayFromList :: Int -> [e] -> PteraTHArray Int e
pteraTHArrayFromList :: forall e. Int -> [e] -> PteraTHArray Int e
pteraTHArrayFromList Int
b [e]
l = forall i e. Ix i => (i, i) -> [e] -> Array i e
Array.listArray (Int
0, Int
b) [e]
l

pteraTHLookupTable8 :: Int -> Prim.Addr# -> Int -> Int -> Int
pteraTHLookupTable8 :: Int -> Addr# -> Int -> Int -> Int
pteraTHLookupTable8 Int
offset Addr#
table# Int
s Int
c = do
    let !(Types.I# Int#
i#) = Int
s forall a. Bits a => a -> Int -> a
`Bits.shiftL` Int
offset forall a. Num a => a -> a -> a
+ Int
c
    forall a. (forall s. ST s a) -> a
ST.runST do
        forall s a. STRep s a -> ST s a
ST.ST \State# s
s0# -> do
            let !(# State# s
s1#, Word8#
r# #) = forall d. Addr# -> Int# -> State# d -> (# State# d, Word8# #)
Prim.readWord8OffAddr# Addr#
table# Int#
i# State# s
s0#
            case Word# -> Int#
Prim.word2Int# do Word8# -> Word#
Prim.word8ToWord# Word8#
r# of
                Int#
255# -> (# State# s
s1#, Int
-1 #)
                Int#
ri#  -> (# State# s
s1#, Int# -> Int
Types.I# Int#
ri# #)

pteraTHLookupTable16 :: Int -> Prim.Addr# -> Int -> Int -> Int
pteraTHLookupTable16 :: Int -> Addr# -> Int -> Int -> Int
pteraTHLookupTable16 Int
offset Addr#
table# Int
s Int
c = do
    let !(Types.I# Int#
i#) = Int
s forall a. Bits a => a -> Int -> a
`Bits.shiftL` Int
offset forall a. Num a => a -> a -> a
+ Int
c
    forall a. (forall s. ST s a) -> a
ST.runST do
        forall s a. STRep s a -> ST s a
ST.ST \State# s
s0# -> do
            let !(# State# s
s1#, Word16#
r# #) = forall d. Addr# -> Int# -> State# d -> (# State# d, Word16# #)
Prim.readWord16OffAddr# Addr#
table# Int#
i# State# s
s0#
            case Word# -> Int#
Prim.word2Int# do Word16# -> Word#
Prim.word16ToWord# Word16#
r# of
                Int#
65535# -> (# State# s
s1#, Int
-1 #)
                Int#
ri#    -> (# State# s
s1#, Int# -> Int
Types.I# Int#
ri# #)

pteraTHLookupTable32 :: Int -> Prim.Addr# -> Int -> Int -> Int
pteraTHLookupTable32 :: Int -> Addr# -> Int -> Int -> Int
pteraTHLookupTable32 Int
offset Addr#
table# Int
s Int
c = do
    let !(Types.I# Int#
i#) = Int
s forall a. Bits a => a -> Int -> a
`Bits.shiftL` Int
offset forall a. Num a => a -> a -> a
+ Int
c
    forall a. (forall s. ST s a) -> a
ST.runST do
        forall s a. STRep s a -> ST s a
ST.ST \State# s
s0# -> do
            let !(# State# s
s1#, Int32#
r# #) = forall d. Addr# -> Int# -> State# d -> (# State# d, Int32# #)
Prim.readInt32OffAddr# Addr#
table# Int#
i# State# s
s0#
            (# State# s
s1#, Int# -> Int
Types.I# do Int32# -> Int#
Prim.int32ToInt# Int32#
r# #)

pteraTHUnsafeExtractReduceArgument :: ReduceArgument -> a
pteraTHUnsafeExtractReduceArgument :: forall a. ReduceArgument -> a
pteraTHUnsafeExtractReduceArgument = \case
    ReduceArgument a
x -> forall a b. a -> b
Unsafe.unsafeCoerce a
x

pteraTHUnsafeRunner :: RunnerParser ctx elem () -> Parser ctx rules elem initials
pteraTHUnsafeRunner :: forall ctx elem rules (initials :: [Symbol]).
RunnerParser ctx elem () -> Parser ctx rules elem initials
pteraTHUnsafeRunner RunnerParser ctx elem ()
p = forall ctx rules elem (initials :: [Symbol]).
T ctx elem () -> RunnerM ctx rules elem initials
Runner.UnsafeRunnerM RunnerParser ctx elem ()
p

pteraTHAction :: ([ReduceArgument] -> ActionTask ctx b) -> ActionM ctx
pteraTHAction :: forall ctx b. ([ReduceArgument] -> ActionTask ctx b) -> ActionM ctx
pteraTHAction [ReduceArgument] -> ActionTask ctx b
f = forall ctx.
([ReduceArgument] -> ActionTask ctx ReduceArgument) -> ActionM ctx
ActionM do \[ReduceArgument]
args -> forall a. a -> ReduceArgument
ReduceArgument forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [ReduceArgument] -> ActionTask ctx b
f [ReduceArgument]
args

pteraTHActionTaskPure :: a -> ActionTask ctx a
pteraTHActionTaskPure :: forall a ctx. a -> ActionTask ctx a
pteraTHActionTaskPure a
x = forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x