module Language.Parser.Ptera.TH.ParserLib (
    module Language.Parser.Ptera.Runner.Parser,
    module Data.Proxy,
    Parser,
    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

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

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

pteraTHArrayFromList :: Int -> [e] -> Array.Array Int e
pteraTHArrayFromList :: Int -> [e] -> Array Int e
pteraTHArrayFromList Int
b [e]
l = (Int, Int) -> [e] -> Array Int e
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 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`Bits.shiftL` Int
offset Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
c
    (forall s. ST s Int) -> Int
forall a. (forall s. ST s a) -> a
ST.runST do
        STRep s Int -> ST s Int
forall s a. STRep s a -> ST s a
ST.ST \State# s
s0# -> do
            let !(# State# s
s1#, Word#
r# #) = Addr# -> Int# -> State# s -> (# State# s, Word# #)
forall d. Addr# -> Int# -> State# d -> (# State# d, Word# #)
Prim.readWord8OffAddr# Addr#
table# Int#
i# State# s
s0#
            case Word#
r# of
                Word#
255## -> (# State# s
s1#, Int
-1 #)
                Word#
_     -> (# State# s
s1#, Int# -> Int
Types.I# do Word# -> Int#
Prim.word2Int# Word#
r# #)

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 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`Bits.shiftL` Int
offset Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
c
    (forall s. ST s Int) -> Int
forall a. (forall s. ST s a) -> a
ST.runST do
        STRep s Int -> ST s Int
forall s a. STRep s a -> ST s a
ST.ST \State# s
s0# -> do
            let !(# State# s
s1#, Word#
r# #) = Addr# -> Int# -> State# s -> (# State# s, Word# #)
forall d. Addr# -> Int# -> State# d -> (# State# d, Word# #)
Prim.readWord16OffAddr# Addr#
table# Int#
i# State# s
s0#
            case Word#
r# of
                Word#
65535## -> (# State# s
s1#, Int
-1 #)
                Word#
_       -> (# State# s
s1#, Int# -> Int
Types.I# do Word# -> Int#
Prim.word2Int# Word#
r# #)

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 Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`Bits.shiftL` Int
offset Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
c
    (forall s. ST s Int) -> Int
forall a. (forall s. ST s a) -> a
ST.runST do
        STRep s Int -> ST s Int
forall s a. STRep s a -> ST s a
ST.ST \State# s
s0# -> do
            let !(# State# s
s1#, Int#
r# #) = Addr# -> Int# -> State# s -> (# State# s, Int# #)
forall d. Addr# -> Int# -> State# d -> (# State# d, Int# #)
Prim.readInt32OffAddr# Addr#
table# Int#
i# State# s
s0#
            (# State# s
s1#, Int# -> Int
Types.I# Int#
r# #)

pteraTHUnsafeExtractReduceArgument :: ReduceArgument -> a
pteraTHUnsafeExtractReduceArgument :: ReduceArgument -> a
pteraTHUnsafeExtractReduceArgument (ReduceArgument a
x) = a -> a
forall a b. a -> b
Unsafe.unsafeCoerce a
x

pteraTHUnsafeRunner :: RunnerParser ctx elem () -> Parser ctx rules elem initials
pteraTHUnsafeRunner :: RunnerParser ctx elem () -> Parser ctx rules elem initials
pteraTHUnsafeRunner RunnerParser ctx elem ()
p = RunnerParser ctx elem () -> Parser ctx rules elem initials
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 :: ([ReduceArgument] -> ActionTask ctx b) -> ActionM ctx
pteraTHAction [ReduceArgument] -> ActionTask ctx b
f = ([ReduceArgument] -> ActionTask ctx ReduceArgument) -> ActionM ctx
forall ctx.
([ReduceArgument] -> ActionTask ctx ReduceArgument) -> ActionM ctx
ActionM do \[ReduceArgument]
args -> b -> ReduceArgument
forall a. a -> ReduceArgument
ReduceArgument (b -> ReduceArgument)
-> ActionTask ctx b -> ActionTask ctx 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 :: a -> ActionTask ctx a
pteraTHActionTaskPure a
x = a -> ActionTask ctx a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x