feat: Parsing the constant magic flag

This commit is contained in:
vegowotenks 2025-07-11 18:40:25 +02:00
parent 6a4d4e5051
commit a4b5b06000
4 changed files with 26 additions and 9 deletions

View file

@ -1,6 +1,6 @@
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE InstanceSigs #-}
module Language.Java.Classfile.Extract (Extract(), bytes, runExtract) where
module Language.Java.Classfile.Extract (Extract(), bytes, runExtract, expectRaw, expectEqual) where
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy as ByteString
@ -47,6 +47,20 @@ bytes count = Extract $ \ input -> let
then Fail
else Done rest bs
expectRaw :: ByteString -> Extract ByteString
expectRaw e = Extract $ \ input -> let
(actual, rest) = ByteString.splitAt (ByteString.length e) input
in case actual == e of
True -> Done rest actual
False -> Fail
expectEqual :: Eq a => Extract a -> a -> Extract a
expectEqual (Extract computeActual) expected = Extract $ \ input -> case computeActual input of
Done rest actual -> if actual == expected
then Done rest actual
else Fail
Fail -> Fail
{- It seems I cannot define a lawful monad instance
instance Monad Extract where
(>>=) :: Extract a -> (a -> Extract b) -> Extract b