{-# LANGUAGE UnboxedTuples, MagicHash, RecordWildCards, TypeApplications #-}
module Parsley.Internal.Backend.Machine.Types.Input (
Input(off), Input#(..),
mkInput, fromInput, toInput,
forcePos, updatePos, updateOffset,
chooseInput
) where
import Parsley.Internal.Backend.Machine.InputRep (StaRep, DynRep)
import Parsley.Internal.Backend.Machine.InputOps (DynOps, asSta, asDyn)
import Parsley.Internal.Backend.Machine.Types.Input.Offset (Offset(offset), mkOffset, moveN)
import Parsley.Internal.Backend.Machine.Types.Input.Pos (StaPos, DynPos, toDynPos, fromDynPos, fromStaPos, force, update)
import Parsley.Internal.Backend.Machine.Types.InputCharacteristic (InputCharacteristic(..))
import Parsley.Internal.Common.Utils (Code)
import Parsley.Internal.Core.CharPred (CharPred)
import Parsley.Internal.Core.CombinatorAST (PosSelector)
data Input o = Input {
forall o. Input o -> Offset o
off :: {-# UNPACK #-} !(Offset o),
forall o. Input o -> StaPos
pos :: {-# UNPACK #-} !StaPos
}
data Input# o = Input# {
forall o. Input# o -> Code (DynRep o)
off# :: !(Code (DynRep o)),
forall o. Input# o -> DynPos
pos# :: !DynPos
}
mkInput :: StaRep o -> (Word, Word) -> Input o
mkInput :: forall o. StaRep o -> (Word, Word) -> Input o
mkInput StaRep o
off = forall o. Offset o -> StaPos -> Input o
Input (forall o. StaRep o -> Word -> Offset o
mkOffset StaRep o
off Word
0) forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word, Word) -> StaPos
fromStaPos
fromInput :: forall o. DynOps o => Input o -> Input# o
fromInput :: forall o. DynOps o => Input o -> Input# o
fromInput Input{Offset o
StaPos
pos :: StaPos
off :: Offset o
pos :: forall o. Input o -> StaPos
off :: forall o. Input o -> Offset o
..} = forall o. Code (DynRep o) -> DynPos -> Input# o
Input# (forall input. DynOps input => StaRep input -> Code (DynRep input)
asDyn @o (forall o. Offset o -> StaRep o
offset Offset o
off)) (StaPos -> DynPos
toDynPos StaPos
pos)
toInput :: forall o. DynOps o => Word -> Input# o -> Input o
toInput :: forall o. DynOps o => Word -> Input# o -> Input o
toInput Word
u Input#{DynPos
Code (DynRep o)
pos# :: DynPos
off# :: Code (DynRep o)
pos# :: forall o. Input# o -> DynPos
off# :: forall o. Input# o -> Code (DynRep o)
..} = forall o. Offset o -> StaPos -> Input o
Input (forall o. StaRep o -> Word -> Offset o
mkOffset (forall input. DynOps input => Code (DynRep input) -> StaRep input
asSta @o Code (DynRep o)
off#) Word
u) (DynPos -> StaPos
fromDynPos DynPos
pos#)
updateOffset :: Offset o -> Input o -> Input o
updateOffset :: forall o. Offset o -> Input o -> Input o
updateOffset Offset o
off Input o
inp = Input o
inp { off :: Offset o
off = Offset o
off }
forcePos :: Input o -> PosSelector -> (Code Int -> Input o -> Code r) -> Code r
forcePos :: forall o r.
Input o -> PosSelector -> (Code Int -> Input o -> Code r) -> Code r
forcePos Input o
input PosSelector
sel Code Int -> Input o -> Code r
k = forall r.
StaPos -> PosSelector -> (Code Int -> StaPos -> Code r) -> Code r
force (forall o. Input o -> StaPos
pos Input o
input) PosSelector
sel (\Code Int
dp StaPos
sp -> Code Int -> Input o -> Code r
k Code Int
dp (Input o
input { pos :: StaPos
pos = StaPos
sp }))
updatePos :: Input o -> Code Char -> CharPred -> Input o
updatePos :: forall o. Input o -> Code Char -> CharPred -> Input o
updatePos Input o
input Code Char
c CharPred
p = Input o
input { pos :: StaPos
pos = StaPos -> Code Char -> CharPred -> StaPos
update (forall o. Input o -> StaPos
pos Input o
input) Code Char
c CharPred
p }
chooseInput :: forall o. DynOps o => InputCharacteristic -> Word -> Input o -> Input# o -> Input o
chooseInput :: forall o.
DynOps o =>
InputCharacteristic -> Word -> Input o -> Input# o -> Input o
chooseInput (AlwaysConsumes (Just Word
n)) Word
_ Input o
inp Input# o
inp# = Input o
inp { off :: Offset o
off = forall o. Word -> Offset o -> StaRep o -> Offset o
moveN Word
n (forall o. Input o -> Offset o
off Input o
inp) (forall input. DynOps input => Code (DynRep input) -> StaRep input
asSta @o (forall o. Input# o -> Code (DynRep o)
off# Input# o
inp#)), pos :: StaPos
pos = DynPos -> StaPos
fromDynPos (forall o. Input# o -> DynPos
pos# Input# o
inp#) }
chooseInput InputCharacteristic
NeverConsumes Word
_ Input o
inp Input# o
_inp# = Input o
inp
chooseInput (AlwaysConsumes Maybe Word
Nothing) Word
u Input o
_inp Input# o
inp# = forall o. DynOps o => Word -> Input# o -> Input o
toInput Word
u Input# o
inp#
chooseInput InputCharacteristic
MayConsume Word
u Input o
_inp Input# o
inp# = forall o. DynOps o => Word -> Input# o -> Input o
toInput Word
u Input# o
inp#