module Ptr.Parse
where
import Ptr.Prelude hiding (peek, take)
import qualified Ptr.PokeAndPeek as A
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Short.Internal as E
import qualified Ptr.Prelude as C
import qualified Ptr.IO as D
newtype Parse output =
Parse (Int -> Ptr Word8 -> forall result. (Int -> IO result) -> (Text -> IO result) -> (output -> Int -> Ptr Word8 -> IO result) -> IO result)
deriving instance Functor Parse
instance Applicative Parse where
pure x =
Parse (\ availableAmount ptr _ _ succeed -> succeed x availableAmount ptr)
(<*>) (Parse left) (Parse right) =
Parse $ \ !availableAmount !ptr failWithEOI failWithMessage succeed ->
left availableAmount ptr failWithEOI failWithMessage $ \ leftOutput !leftAvailableAmount !leftPtr ->
right leftAvailableAmount leftPtr failWithEOI failWithMessage $ \ rightOutput !rightAvailableAmount !rightPtr ->
succeed (leftOutput rightOutput) rightAvailableAmount rightPtr
instance Alternative Parse where
empty =
Parse (\ _ _ failWithEOI _ _ -> failWithEOI 0)
(<|>) (Parse left) (Parse right) =
Parse $ \ !availableAmount !ptr failWithEOI failWithMessage succeed ->
left availableAmount ptr
(\ _ -> right availableAmount ptr failWithEOI failWithMessage succeed)
failWithMessage succeed
instance Monad Parse where
return = pure
(>>=) (Parse left) rightK =
Parse $ \ !availableAmount !ptr failWithEOI failWithMessage succeed ->
left availableAmount ptr failWithEOI failWithMessage $ \ leftOutput !leftAvailableAmount !leftPtr ->
case rightK leftOutput of
Parse right ->
right leftAvailableAmount leftPtr failWithEOI failWithMessage succeed
instance MonadPlus Parse where
mzero = empty
mplus = (<|>)
instance MonadIO Parse where
liftIO io =
Parse $ \ availableAmount ptr _ _ succeed -> io >>= \ output -> succeed output availableAmount ptr
fail :: Text -> Parse output
fail message =
Parse $ \ _ _ _ failWithMessage _ -> failWithMessage message
io :: Int -> (Ptr Word8 -> IO output) -> Parse output
io !requiredAmount ptrIO =
Parse $ \ !availableAmount !ptr failWithEOI failWithMessage succeed ->
if availableAmount >= requiredAmount
then do
!result <- ptrIO ptr
succeed result (availableAmount requiredAmount) (plusPtr ptr requiredAmount)
else failWithEOI (requiredAmount availableAmount)
mapInIO :: (output -> IO newOutput) -> Parse output -> Parse newOutput
mapInIO io (Parse parseIO) =
Parse $ \ !availableAmount !ptr failWithEOI failWithMessage succeed ->
parseIO availableAmount ptr failWithEOI failWithMessage
(\ output newAvailableAmount newPtr -> io output >>= \ newOutput -> succeed newOutput newAvailableAmount newPtr)
pokeAndPeek :: A.PokeAndPeek input output -> Parse output
pokeAndPeek (A.PokeAndPeek requiredAmount _ ptrIO) =
Parse $ \ !availableAmount !ptr failWithEOI failWithMessage succeed ->
if availableAmount >= requiredAmount
then do
!result <- ptrIO ptr
succeed result (availableAmount requiredAmount) (plusPtr ptr requiredAmount)
else failWithEOI (requiredAmount availableAmount)
limiting :: Int -> Parse output -> Parse output
limiting limitAmount (Parse io) =
Parse $ \ !availableAmount !ptr failWithEOI failWithMessage succeed ->
if availableAmount >= limitAmount
then io limitAmount ptr failWithEOI failWithMessage succeed
else failWithEOI (limitAmount availableAmount)
peekRemainders :: Parse ByteString
peekRemainders =
Parse $ \ !availableAmount !ptr failWithEOI failWithMessage succeed -> do
!bytes <- D.peekBytes ptr availableAmount
succeed bytes availableAmount ptr
word8 :: Parse Word8
word8 =
io 1 D.peekWord8
beWord16 :: Parse Word16
beWord16 =
io 2 D.peekBEWord16
beWord32 :: Parse Word32
beWord32 =
io 4 D.peekBEWord32
beWord64 :: Parse Word64
beWord64 =
io 8 D.peekBEWord64
bytes :: Int -> Parse ByteString
bytes amount =
io amount (\ ptr -> D.peekBytes ptr amount)
allBytes :: Parse ByteString
allBytes =
Parse $ \ !availableAmount !ptr failWithEOI failWithMessage succeed -> do
!bytes <- D.peekBytes ptr availableAmount
succeed bytes 0 (plusPtr ptr availableAmount)
nullTerminatedBytes :: Parse ByteString
nullTerminatedBytes =
Parse $ \ !availableAmount !ptr failWithEOI failWithMessage succeed -> do
!bytes <- B.packCString (castPtr ptr)
case succ (B.length bytes) of
consumedAmount -> if consumedAmount <= availableAmount
then succeed bytes (availableAmount consumedAmount) (plusPtr ptr consumedAmount)
else failWithEOI (consumedAmount availableAmount)
nullTerminatedShortByteString :: Parse ShortByteString
nullTerminatedShortByteString =
Parse $ \ !availableAmount !ptr failWithEOI failWithMessage succeed ->
D.peekNullTerminatedShortByteString ptr $ \ length create ->
if length <= availableAmount
then do
!result <- create
succeed result (availableAmount length) (plusPtr ptr length)
else failWithEOI (length availableAmount)
bytesWhile :: (Word8 -> Bool) -> Parse ByteString
bytesWhile predicate =
Parse $ \ !availableAmount !ptr failWithEOI failWithMessage succeed ->
let
iterate !availableAmount !unconsumedAmount !currentPtr =
if unconsumedAmount > 0
then do
byte <- C.peek currentPtr
if predicate byte
then iterate availableAmount (pred unconsumedAmount) (plusPtr currentPtr 1)
else do
bytes <- B.packCStringLen (castPtr ptr, availableAmount unconsumedAmount)
succeed bytes unconsumedAmount currentPtr
else failWithEOI 0
in iterate availableAmount availableAmount ptr
skipWhile :: (Word8 -> Bool) -> Parse ()
skipWhile predicate =
Parse $ \ !availableAmount !ptr failWithEOI failWithMessage succeed ->
let
iterate !availableAmount !unconsumedAmount !ptr =
if unconsumedAmount > 0
then do
byte <- C.peek ptr
if predicate byte
then iterate availableAmount (pred unconsumedAmount) (plusPtr ptr 1)
else succeed () unconsumedAmount ptr
else failWithEOI 0
in iterate availableAmount availableAmount ptr
foldWhile :: (Word8 -> Bool) -> (state -> Word8 -> state) -> state -> Parse state
foldWhile predicate step start =
Parse $ \ !availableAmount !ptr failWithEOI failWithMessage succeed ->
let
iterate !state !unconsumedAmount !ptr =
if unconsumedAmount > 0
then do
byte <- C.peek ptr
if predicate byte
then iterate (step state byte) (pred unconsumedAmount) (plusPtr ptr 1)
else succeed state unconsumedAmount ptr
else failWithEOI 0
in iterate start availableAmount ptr
unsignedASCIIIntegral :: Integral a => Parse a
unsignedASCIIIntegral =
foldWhile byteIsDigit step 0
where
byteIsDigit byte =
byte 48 <= 9
step !state !byte =
state * 10 + fromIntegral byte 48