{-# OPTIONS_HADDOCK show-extensions #-}
{-# LANGUAGE CPP,
             ImplicitParams,
             MagicHash,
             TypeApplications,
             UnboxedTuples #-}
{-|
Module      : Parsley.Internal.Backend.Machine.InputOps
Description : Primitive operations for working with input.
License     : BSD-3-Clause
Maintainer  : Jamie Willis
Stability   : experimental

This module contains the primitive operations required by the
parsing machinery to work with input.

@since 1.0.0.0
-}
module Parsley.Internal.Backend.Machine.InputOps (
    InputPrep(..), PositionOps(..), LogOps(..),
    InputOps(..), more, next,
#if __GLASGOW_HASKELL__ <= 900
    word8ToWord#, word16ToWord#,
#endif
    InputDependant
  ) where

import Data.Array.Base                             (UArray(..), listArray)
import Data.ByteString.Internal                    (ByteString(..))
import Data.Text.Array                             (aBA{-, empty-})
import Data.Text.Internal                          (Text(..))
import Data.Text.Unsafe                            (iter, Iter(..){-, iter_, reverseIter_-})
import GHC.Exts                                    (Int(..), Char(..), TYPE, Int#)
import GHC.ForeignPtr                              (ForeignPtr(..))
import GHC.Prim                                    (indexWideCharArray#, indexWord16Array#, readWord8OffAddr#, word2Int#, chr#, touch#, realWorld#, plusAddr#, (+#), (-#))
#if __GLASGOW_HASKELL__ > 900
import GHC.Prim                                    (word16ToWord#, word8ToWord#)
#else
import GHC.Prim                                    (Word#)
#endif
import Parsley.Internal.Backend.Machine.InputRep   (Stream(..), CharList(..), Text16(..), Rep, UnpackedLazyByteString,
                                                    offWith, emptyUnpackedLazyByteString, intSame, intLess,
                                                    offsetText, offWithSame, offWithShiftRight, dropStream,
                                                    textShiftRight, textShiftLeft, byteStringShiftRight,
                                                    byteStringShiftLeft, max#)
import Parsley.Internal.Common.Utils               (Code)

import qualified Data.ByteString.Lazy.Internal as Lazy (ByteString(..))
--import qualified Data.Text                     as Text (length, index)


#if __GLASGOW_HASKELL__ <= 900
{-# INLINE word8ToWord# #-}
word8ToWord# :: Word# -> Word#
word8ToWord# :: Word# -> Word#
word8ToWord# Word#
x = Word#
x

{-# INLINE word16ToWord# #-}
word16ToWord# :: Word# -> Word#
word16ToWord# :: Word# -> Word#
word16ToWord# Word#
x = Word#
x
#endif

{- Auxillary Representation -}
{-|
Given some associated representation type, defines the operations
that work with a /closed over/ instance of that type. These are:

* @next@: extract the next character from the input (existence not included)
* @more@: query whether another character /can/ be read
* @init@: the initial state of the input.

@since 1.0.0.0
-}
type InputDependant (rep :: TYPE r) = (# {-next-} rep -> (# Char, rep #)
                                       , {-more-} rep -> Bool
                                       , {-init-} rep
                                       #)

{- Typeclasses -}
{-|
This class is responsible for converting the user's input into a form that
parsley can work with efficiently.

@since 1.0.0.0
-}
class InputPrep input where
  {-|
  Given the user's input to the parser, in its original form, this function
  distils it first into @`Rep` input@, which is parsley's internal representation,
  and then produces an `InputDependant` containing the core operations.

  @since 1.0.0.0
  -}
  prepare :: rep ~ Rep input => Code input -> Code (InputDependant rep)

{-|
Defines operations for manipulating offsets for regular use. These are not
tied to the original captured input but instead to the representation of its
offset.

@since 1.0.0.0
-}
class PositionOps (rep :: TYPE r) where
  {-|
  Compares two "input"s for equality. In reality this usually means an offset
  present in the @rep@.

  @since 1.0.0.0
  -}
  same :: Code rep -> Code rep -> Code Bool

  {-|
  Advances the input by several characters at a time (existence not included).
  This can be used to check if characters exist at a future point in the input
  in conjunction with `more`.

  @since 1.0.0.0
  -}
  shiftRight :: Code rep -> Code Int# -> Code rep

{-|
Defines operation used for debugging operations.

@since 1.0.0.0
-}
class LogOps (rep :: TYPE r) where
  {-|
  If possible, shifts the input back several characters.
  This is used to provide the previous input characters for the debugging combinator.

  @since 1.0.0.0
  -}
  shiftLeft :: Code rep -> Code Int# -> Code rep

  {-|
  Converts the represention of the input into an @Int@.

  @since 1.0.0.0
  -}
  offToInt  :: Code rep -> Code Int

{-|
This is a psuedo-typeclass, which depends directly on the values obtained from
`InputDependant`. Because this instance must depend on local information, it is
synthesised and passed around using @ImplicitParams@.

@since 1.0.0.0
-}
data InputOps (rep :: TYPE r) = InputOps { InputOps rep -> Code (rep -> Bool)
_more       :: Code (rep -> Bool)            -- ^ Does the input have any more characters?
                                         , InputOps rep -> Code (rep -> (# Char, rep #))
_next       :: Code (rep -> (# Char, rep #)) -- ^ Read the next character (without checking existence)
                                         }
{-|
Wraps around `InputOps` and `_more`.

Queries the input to see if another character may be consumed.

@since 1.4.0.0
-}
more :: forall r (rep :: TYPE r). (?ops :: InputOps rep) => Code rep -> Code Bool
more :: Code rep -> Code Bool
more Code rep
qo# = [|| $$(_more ?ops) $$(qo#) ||]

{-|
Wraps around `InputOps` and `_next`.

Given some input and a continuation that accepts new input and a character, it will read
a character off (without checking that it exists!) and feeds it and the remaining input
to the continuation.

@since 1.0.0.0
-}
next :: forall r (rep :: TYPE r) a. (?ops :: InputOps rep) => Code rep -> (Code Char -> Code rep -> Code a) -> Code a
next :: Code rep -> (Code Char -> Code rep -> Code a) -> Code a
next Code rep
ts Code Char -> Code rep -> Code a
k = [|| let !(# t, ts' #) = $$(_next ?ops) $$ts in $$(k [||t||] [||ts'||]) ||]

{- INSTANCES -}
-- InputPrep Instances
instance InputPrep [Char] where
  prepare :: Code [Char] -> Code (InputDependant rep)
prepare Code [Char]
input = Code (UArray Int Char) -> Code (InputDependant Int#)
forall input (rep :: TYPE (RepKind input)).
(InputPrep input, rep ~ Rep input) =>
Code input -> Code (InputDependant rep)
prepare @(UArray Int Char) [||listArray (0, length $$input-1) $$input||]

instance InputPrep (UArray Int Char) where
  prepare :: Code (UArray Int Char) -> Code (InputDependant rep)
prepare Code (UArray Int Char)
qinput = [||
      let !(UArray _ _ (I# size#) input#) = $$qinput
          next i# = (# C# (indexWideCharArray# input# i#), i# +# 1# #)
      in (# next, \qi -> $$(intLess [||qi||] [||size#||]), 0# #)
    ||]

instance InputPrep Text16 where
  prepare :: Code Text16 -> Code (InputDependant rep)
prepare Code Text16
qinput = [||
      let Text16 (Text arr (I# off#) (I# size#)) = $$qinput
          arr# = aBA arr
          next i# = (# C# (chr# (word2Int# (word16ToWord# (indexWord16Array# arr# i#)))), i# +# 1# #)
      in (# next, \qi -> $$(intLess [||qi||] [||size#||]), off# #)
    ||]

instance InputPrep ByteString where
  prepare :: Code ByteString -> Code (InputDependant rep)
prepare Code ByteString
qinput = [||
      let PS (ForeignPtr addr# final) (I# off#) (I# size#) = $$qinput
          next i# =
            case readWord8OffAddr# (addr# `plusAddr#` i#) 0# realWorld# of
              (# s', x #) -> case touch# final s' of
                _ -> (# C# (chr# (word2Int# (word8ToWord# x))), i# +# 1# #)
      in  (# next, \qi -> $$(intLess [||qi||] [||size#||]), off# #)
    ||]

instance InputPrep CharList where
  prepare :: Code CharList -> Code (InputDependant rep)
prepare Code CharList
qinput = [||
      let CharList input = $$qinput
          next :: (# Int#, [Char] #) -> (# Char, (# Int#, [Char] #) #)
          next (# i#, c:cs #) = (# c, (# i# +# 1#, cs #) #)
          more :: (# Int#, [Char] #) -> Bool
          more (# _, [] #) = False
          more _           = True
      in (# next, more, $$(offWith [||input||]) #)
    ||]

instance InputPrep Text where
  prepare :: Code Text -> Code (InputDependant rep)
prepare Code Text
qinput = [||
      let next t@(Text arr off unconsumed) = let !(Iter c d) = iter t 0 in (# c, Text arr (off + d) (unconsumed - d) #)
          more (Text _ _ unconsumed) = unconsumed > 0
      in (# next, more, $$qinput #)
    ||]

instance InputPrep Lazy.ByteString where
  prepare :: Code ByteString -> Code (InputDependant rep)
prepare Code ByteString
qinput = [||
      let next (# i#, addr#, final, off#, size#, cs #) =
            case readWord8OffAddr# addr# off# realWorld# of
              (# s', x #) -> case touch# final s' of
                _ -> (# C# (chr# (word2Int# (word8ToWord# x))),
                    if I# size# /= 1 then (# i# +# 1#, addr#, final, off# +# 1#, size# -# 1#, cs #)
                    else case cs of
                      Lazy.Chunk (PS (ForeignPtr addr'# final') (I# off'#) (I# size'#)) cs' ->
                        (# i# +# 1#, addr'#, final', off'#, size'#, cs' #)
                      Lazy.Empty -> $$(emptyUnpackedLazyByteString [||i# +# 1#||])
                  #)
          more :: UnpackedLazyByteString -> Bool
          more (# _, _, _, _, 0#, _ #) = False
          more (# _, _, _, _, _, _ #) = True

          initial :: UnpackedLazyByteString
          initial = case $$qinput of
            Lazy.Chunk (PS (ForeignPtr addr# final) (I# off#) (I# size#)) cs -> (# 0#, addr#, final, off#, size#, cs #)
            Lazy.Empty -> $$(emptyUnpackedLazyByteString [||0#||])
      in (# next, more, initial #)
    ||]

instance InputPrep Stream where
  prepare :: Code Stream -> Code (InputDependant rep)
prepare Code Stream
qinput = [||
      let next (# o#, c :> cs #) = (# c, (# o# +# 1#, cs #) #)
      in (# next, \_ -> True, $$(offWith qinput) #)
    ||]

shiftRightInt :: Code Int# -> Code Int# -> Code Int#
shiftRightInt :: Code Int# -> Code Int# -> Code Int#
shiftRightInt Code Int#
qo# Code Int#
qi# = [||$$(qo#) +# $$(qi#)||]

-- PositionOps Instances
instance PositionOps Int# where
  same :: Code Int# -> Code Int# -> Code Bool
same = Code Int# -> Code Int# -> Code Bool
intSame
  shiftRight :: Code Int# -> Code Int# -> Code Int#
shiftRight = Code Int# -> Code Int# -> Code Int#
shiftRightInt

instance PositionOps (# Int#, [Char] #) where
  same :: Code (# Int#, [Char] #) -> Code (# Int#, [Char] #) -> Code Bool
same = Code (# Int#, [Char] #) -> Code (# Int#, [Char] #) -> Code Bool
forall ts. Code (OffWith ts) -> Code (OffWith ts) -> Code Bool
offWithSame
  shiftRight :: Code (# Int#, [Char] #) -> Code Int# -> Code (# Int#, [Char] #)
shiftRight Code (# Int#, [Char] #)
qo# Code Int#
qi# = Code (Int -> [Char] -> [Char])
-> Code (# Int#, [Char] #) -> Code Int# -> Code (# Int#, [Char] #)
forall ts.
Code (Int -> ts -> ts)
-> Code (OffWith ts) -> Code Int# -> Code (OffWith ts)
offWithShiftRight [||drop||] Code (# Int#, [Char] #)
qo# Code Int#
qi#

instance PositionOps (# Int#, Stream #) where
  same :: Code (# Int#, Stream #) -> Code (# Int#, Stream #) -> Code Bool
same = Code (# Int#, Stream #) -> Code (# Int#, Stream #) -> Code Bool
forall ts. Code (OffWith ts) -> Code (OffWith ts) -> Code Bool
offWithSame
  shiftRight :: Code (# Int#, Stream #) -> Code Int# -> Code (# Int#, Stream #)
shiftRight Code (# Int#, Stream #)
qo# Code Int#
qi# = Code (Int -> Stream -> Stream)
-> Code (# Int#, Stream #) -> Code Int# -> Code (# Int#, Stream #)
forall ts.
Code (Int -> ts -> ts)
-> Code (OffWith ts) -> Code Int# -> Code (OffWith ts)
offWithShiftRight [||dropStream||] Code (# Int#, Stream #)
qo# Code Int#
qi#

instance PositionOps Text where
  same :: Code Text -> Code Text -> Code Bool
same Code Text
qt1 Code Text
qt2 = [||$$(offsetText qt1) == $$(offsetText qt2)||]
  shiftRight :: Code Text -> Code Int# -> Code Text
shiftRight Code Text
qo# Code Int#
qi# = [||textShiftRight $$(qo#) (I# $$(qi#))||]

instance PositionOps UnpackedLazyByteString where
  same :: Code UnpackedLazyByteString
-> Code UnpackedLazyByteString -> Code Bool
same Code UnpackedLazyByteString
qx# Code UnpackedLazyByteString
qy# = [||
      case $$(qx#) of
        (# i#, _, _, _, _, _ #) -> case $$(qy#) of
          (# j#, _, _, _, _, _ #) -> $$(intSame [||i#||] [||j#||])
    ||]
  shiftRight :: Code UnpackedLazyByteString
-> Code Int# -> Code UnpackedLazyByteString
shiftRight Code UnpackedLazyByteString
qo# Code Int#
qi# = [||byteStringShiftRight $$(qo#) $$(qi#)||]

-- LogOps Instances
instance LogOps Int# where
  shiftLeft :: Code Int# -> Code Int# -> Code Int#
shiftLeft Code Int#
qo# Code Int#
qi# = [||max# ($$(qo#) -# $$(qi#)) 0#||]
  offToInt :: Code Int# -> Code Int
offToInt Code Int#
qi# = [||I# $$(qi#)||]

instance LogOps (# Int#, ts #) where
  shiftLeft :: Code (# Int#, ts #) -> Code Int# -> Code (# Int#, ts #)
shiftLeft Code (# Int#, ts #)
qo# Code Int#
_ = Code (# Int#, ts #)
qo#
  offToInt :: Code (# Int#, ts #) -> Code Int
offToInt Code (# Int#, ts #)
qo# = [||case $$(qo#) of (# i#, _ #) -> I# i#||]

instance LogOps Text where
  shiftLeft :: Code Text -> Code Int# -> Code Text
shiftLeft Code Text
qo Code Int#
qi# = [||textShiftLeft $$qo (I# $$(qi#))||]
  offToInt :: Code Text -> Code Int
offToInt Code Text
qo = [||case $$qo of Text _ off _ -> div off 2||]

instance LogOps UnpackedLazyByteString where
  shiftLeft :: Code UnpackedLazyByteString
-> Code Int# -> Code UnpackedLazyByteString
shiftLeft Code UnpackedLazyByteString
qo# Code Int#
qi# = [||byteStringShiftLeft $$(qo#) $$(qi#)||]
  offToInt :: Code UnpackedLazyByteString -> Code Int
offToInt Code UnpackedLazyByteString
qo# = [||case $$(qo#) of (# i#, _, _, _, _, _ #) -> I# i# ||]

{- Old Instances -}
{-instance Input CacheText (Text, Stream) where
  prepare qinput = [||
      let (CacheText input) = $$qinput
          next (t@(Text arr off unconsumed), _) = let !(Iter c d) = iter t 0 in (# c, (Text arr (off+d) (unconsumed-d), nomore) #)
          more (Text _ _ unconsumed, _) = unconsumed > 0
          same (Text _ i _, _) (Text _ j _, _) = i == j
          (Text arr off unconsumed, _) << i = go i off unconsumed
            where
              go 0 off' unconsumed' = (Text arr off' unconsumed', nomore)
              go n off' unconsumed'
                | off' > 0 = let !d = reverseIter_ (Text arr off' unconsumed') 0 in go (n-1) (off'+d) (unconsumed'-d)
                | otherwise = (Text arr off' unconsumed', nomore)
          (Text arr off unconsumed, _) >> i = go i off unconsumed
            where
              go 0 off' unconsumed' = (Text arr off' unconsumed', nomore)
              go n off' unconsumed'
                | unconsumed' > 0 = let !d = iter_ (Text arr off' unconsumed') 0 in go (n-1) (off'+d) (unconsumed'-d)
                | otherwise = (Text arr off' unconsumed', nomore)
          toInt (Text arr off unconsumed, _) = div off 2
          box (# text, cache #) = (text, cache)
          unbox (text, cache) = (# text, cache #)
          newCRef (Text _ i _, _) = newSTRefU i
          readCRef ref = fmap (\i -> (Text empty i 0, nomore)) (readSTRefU ref)
          writeCRef ref (Text _ i _, _) = writeSTRefU ref i
      in PreparedInput next more same (input, nomore) box unbox newCRef readCRef writeCRef s(<<) (>>) toInt
    ||]

instance Input Lazy.ByteString (OffWith Lazy.ByteString) where
  prepare qinput = [||
      let next (OffWith i (Lazy.Chunk (PS ptr@(ForeignPtr addr# final) off@(I# off#) size) cs)) =
            case readWord8OffAddr# addr# off# realWorld# of
              (# s', x #) -> case touch# final s' of
                _ -> (# C# (chr# (word2Int# x)), OffWith (i+1) (if size == 1 then cs
                                                                else Lazy.Chunk (PS ptr (off+1) (size-1)) cs) #)
          more (OffWith _ Lazy.Empty) = False
          more _ = True
          ow@(OffWith _ (Lazy.Empty)) << _ = ow
          OffWith o (Lazy.Chunk (PS ptr off size) cs) << i =
            let d = min off i
            in OffWith (o - d) (Lazy.Chunk (PS ptr (off - d) (size + d)) cs)
          ow@(OffWith _ Lazy.Empty) >> _ = ow
          OffWith o (Lazy.Chunk (PS ptr off size) cs) >> i
            | i < size  = OffWith (o + i) (Lazy.Chunk (PS ptr (off + i) (size - i)) cs)
            | otherwise = OffWith (o + size) cs >> (i - size)
          readCRef ref = fmap (\i -> OffWith i Lazy.Empty) (readSTRefU ref)
      in PreparedInput next more offWithSame (offWith $$qinput) offWithBox offWithUnbox offWithNewORef readCRef offWithWriteORef (<<) (>>) offWithToInt
    ||]-}