make parser monadic
This commit is contained in:
@@ -101,6 +101,7 @@ import Debug.Trace as Debug
|
|||||||
%name parseModule mod
|
%name parseModule mod
|
||||||
%name parseModuleFields modAsFields
|
%name parseModuleFields modAsFields
|
||||||
%name parseScript script
|
%name parseScript script
|
||||||
|
%monad { Either String }
|
||||||
%tokentype { Lexeme }
|
%tokentype { Lexeme }
|
||||||
|
|
||||||
%token
|
%token
|
||||||
@@ -1355,8 +1356,8 @@ data ModuleField =
|
|||||||
| MFData DataSegment
|
| MFData DataSegment
|
||||||
deriving(Show, Eq, Generic, NFData)
|
deriving(Show, Eq, Generic, NFData)
|
||||||
|
|
||||||
happyError (Lexeme _ EOF : []) = error $ "Error occuried during parsing phase at the end of file"
|
happyError (Lexeme _ EOF : []) = Left $ "Error occuried during parsing phase at the end of file"
|
||||||
happyError (Lexeme (AlexPn abs line col) tok : tokens) = error $
|
happyError (Lexeme (AlexPn abs line col) tok : tokens) = Left $
|
||||||
"Error occuried during parsing phase. " ++
|
"Error occuried during parsing phase. " ++
|
||||||
"Line " ++ show line ++ ", " ++
|
"Line " ++ show line ++ ", " ++
|
||||||
"Column " ++ show col ++ ", " ++
|
"Column " ++ show col ++ ", " ++
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ runScript onAssertFail script = do
|
|||||||
buildModule :: ModuleDef -> (Maybe Ident, Struct.Module)
|
buildModule :: ModuleDef -> (Maybe Ident, Struct.Module)
|
||||||
buildModule (RawModDef ident m) = (ident, m)
|
buildModule (RawModDef ident m) = (ident, m)
|
||||||
buildModule (TextModDef ident textRep) =
|
buildModule (TextModDef ident textRep) =
|
||||||
let Right m = Parser.parseModule <$> Lexer.scanner (TLEncoding.encodeUtf8 textRep) in
|
let Right m = Lexer.scanner (TLEncoding.encodeUtf8 textRep) >>= Parser.parseModule in
|
||||||
(ident, m)
|
(ident, m)
|
||||||
buildModule (BinaryModDef ident binaryRep) =
|
buildModule (BinaryModDef ident binaryRep) =
|
||||||
let Right m = Binary.decodeModuleLazy binaryRep in
|
let Right m = Binary.decodeModuleLazy binaryRep in
|
||||||
@@ -199,7 +199,7 @@ runScript onAssertFail script = do
|
|||||||
++ show (getFailureString reason)
|
++ show (getFailureString reason)
|
||||||
in onAssertFail msg assert
|
in onAssertFail msg assert
|
||||||
runAssert st assert@(AssertMalformed (TextModDef _ textRep) failureString) =
|
runAssert st assert@(AssertMalformed (TextModDef _ textRep) failureString) =
|
||||||
case DeepSeq.force $ Parser.parseModule <$> Lexer.scanner (TLEncoding.encodeUtf8 textRep) of
|
case DeepSeq.force $ Lexer.scanner (TLEncoding.encodeUtf8 textRep) >>= Parser.parseModule of
|
||||||
Right _ -> onAssertFail ("Module parsing should fail with failure string " ++ show failureString) assert
|
Right _ -> onAssertFail ("Module parsing should fail with failure string " ++ show failureString) assert
|
||||||
Left _ -> return ()
|
Left _ -> return ()
|
||||||
runAssert st assert@(AssertMalformed (BinaryModDef ident binaryRep) failureString) =
|
runAssert st assert@(AssertMalformed (BinaryModDef ident binaryRep) failureString) =
|
||||||
|
|||||||
+2
-2
@@ -27,7 +27,7 @@ isRight _ = False
|
|||||||
compile :: String -> IO ()
|
compile :: String -> IO ()
|
||||||
compile file = do
|
compile file = do
|
||||||
content <- LBS.readFile $ "tests/samples/" ++ file
|
content <- LBS.readFile $ "tests/samples/" ++ file
|
||||||
let Right mod = Parser.parseModule <$> Lexer.scanner content
|
let Right mod = Lexer.scanner content >>= Parser.parseModule
|
||||||
LBS.writeFile ("tests/runnable/" ++ file) $ Binary.dumpModuleLazy mod
|
LBS.writeFile ("tests/runnable/" ++ file) $ Binary.dumpModuleLazy mod
|
||||||
-- to run: python -m SimpleHTTPServer 8081 && open http://localhost:8081/tests/runnable
|
-- to run: python -m SimpleHTTPServer 8081 && open http://localhost:8081/tests/runnable
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ main = do
|
|||||||
-- let files = ["data.wast"]
|
-- let files = ["data.wast"]
|
||||||
scriptTestCases <- (`mapM` files) $ \file -> do
|
scriptTestCases <- (`mapM` files) $ \file -> do
|
||||||
content <- LBS.readFile $ "tests/samples/" ++ file
|
content <- LBS.readFile $ "tests/samples/" ++ file
|
||||||
let Right script = Parser.parseScript <$> Lexer.scanner content
|
let Right script = Lexer.scanner content >>= Parser.parseScript
|
||||||
return $ testCase file $ do
|
return $ testCase file $ do
|
||||||
Script.runScript (\msg assert -> assertFailure ("Failed assert: " ++ msg ++ ". Assert " ++ show assert)) script
|
Script.runScript (\msg assert -> assertFailure ("Failed assert: " ++ msg ++ ". Assert " ++ show assert)) script
|
||||||
defaultMain $ testGroup "Wasm Core Test Suit" scriptTestCases
|
defaultMain $ testGroup "Wasm Core Test Suit" scriptTestCases
|
||||||
|
|||||||
Reference in New Issue
Block a user