module Raaz.Core.Parse.Applicative
( Parser, parseWidth, parseError, runParser
, unsafeRunParser
, parse, parseStorable
, parseVector, parseStorableVector
, unsafeParseVector, unsafeParseStorableVector
, parseByteString
, skip
) where
import Data.ByteString (ByteString)
import Data.Vector.Generic (Vector, generateM)
import Foreign.Ptr (castPtr)
import Foreign.Storable (Storable, peek, peekElemOff)
import Prelude hiding ( length )
import System.IO.Unsafe (unsafePerformIO)
import Raaz.Core.MonoidalAction
import Raaz.Core.Types.Endian
import Raaz.Core.Types.Pointer
import Raaz.Core.Util.ByteString (createFrom, length, withByteString)
type BytesMonoid = BYTES Int
type ParseAction = FieldM IO Pointer
type Parser = TwistRF ParseAction BytesMonoid
makeParser :: LengthUnit l => l -> (Pointer -> IO a) -> Parser a
makeParser l action = TwistRF (liftToFieldM action) $ inBytes l
skip :: LengthUnit u => u -> Parser ()
skip = flip makeParser $ const $ return ()
parseError :: String -> Parser a
parseError msg = makeParser (0 :: BYTES Int) $ \ _ -> fail msg
parseWidth :: Parser a -> BYTES Int
parseWidth = twistMonoidValue
runParser :: Parser a -> ByteString -> Maybe a
runParser pr bs
| length bs < parseWidth pr = Nothing
| otherwise = Just $ unsafePerformIO $ withByteString bs $ unsafeRunParser pr
unsafeRunParser :: Parser a -> Pointer -> IO a
unsafeRunParser = runFieldM . twistFunctorValue
undefParse :: Parser a -> a
undefParse _ = undefined
parseStorable :: Storable a => Parser a
parseStorable = pa
where pa = makeParser (sizeOf $ undefParse pa) (peek . castPtr)
parse :: EndianStore a => Parser a
parse = pa
where pa = makeParser (sizeOf $ undefParse pa) (load . castPtr)
parseByteString :: LengthUnit l => l -> Parser ByteString
parseByteString l = makeParser l $ createFrom l
unsafeParseStorableVector :: (Storable a, Vector v a) => Int -> Parser (v a)
unsafeParseStorableVector n = pvec
where pvec = makeParser width $ \ cptr -> generateM n (getA cptr)
width = fromIntegral n * sizeOf (undefA pvec)
undefA :: (Storable a, Vector v a)=> Parser (v a) -> a
undefA _ = undefined
getA = peekElemOff . castPtr
unsafeParseVector :: (EndianStore a, Vector v a) => Int -> Parser (v a)
unsafeParseVector n = pvec
where pvec = makeParser width $ \ cptr -> generateM n (loadFromIndex (castPtr cptr))
width = fromIntegral n * sizeOf (undefA pvec)
undefA :: (EndianStore a, Vector v a)=> Parser (v a) -> a
undefA _ = undefined
parseStorableVector :: (Storable a, Vector v a) => Int -> Parser (v a)
parseStorableVector n | n < 0 = parseError $ "parseStorableVector on " ++ show n
| otherwise = unsafeParseStorableVector n
parseVector :: (EndianStore a, Vector v a) => Int -> Parser (v a)
parseVector n | n < 0 = parseError $ "parseVector on " ++ show n
| otherwise = unsafeParseVector n