From c183dc9ce6b6e14581078ba42ff1824f922234ca Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Wed, 24 Apr 2024 11:07:28 -0700 Subject: [Parser] Use the new parser in wasm-shell and wasm-as (#6529) Updating just one or the other of these tools would cause the tests spec/import-after-*.fail.wast to fail, since only the updated tool would correctly fail to parse its contents. To avoid this, update both tools at once. (The tests erroneously pass before this change because check.py does not ensure that .fail.wast tests fail, only that failing tests end in .fail.wast.) In wasm-shell, to minimize the diff, only use the new parser to parse modules and instructions. Continue using the legacy parsing based on s-expressions for the other wast commands. Updating the parsing of the other commands to use `Lexer` instead of `SExpressionParser` is left as future work. The boundary between the two parsing styles is somewhat hacky, but it is worth it to enable incremental development. Update the tests to fix incorrect wast rejected by the new parser. Many of the spec/old_* tests use non-standard forms from before Wasm MVP was standardized, so fixing them would have been onerous. All of these tests have non-old_* variants, so simply delete them. --- src/parser/wat-parser.cpp | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'src/parser/wat-parser.cpp') diff --git a/src/parser/wat-parser.cpp b/src/parser/wat-parser.cpp index 7f6dd2975..cc7d87540 100644 --- a/src/parser/wat-parser.cpp +++ b/src/parser/wat-parser.cpp @@ -99,17 +99,11 @@ void propagateDebugLocations(Module& wasm) { runner.run(); } -// ================ -// Parser Functions -// ================ - -} // anonymous namespace - -Result<> parseModule(Module& wasm, std::string_view input) { +Result<> doParseModule(Module& wasm, Lexer& input, bool allowExtra) { // Parse module-level declarations. ParseDeclsCtx decls(input, wasm); CHECK_ERR(module(decls)); - if (!decls.in.empty()) { + if (!allowExtra && !decls.in.empty()) { return decls.in.err("Unexpected tokens after module"); } @@ -222,8 +216,27 @@ Result<> parseModule(Module& wasm, std::string_view input) { } propagateDebugLocations(wasm); + input = decls.in; return Ok{}; } +} // anonymous namespace + +Result<> parseModule(Module& wasm, std::string_view in) { + Lexer lexer(in); + return doParseModule(wasm, lexer, false); +} + +Result<> parseModule(Module& wasm, Lexer& lexer) { + return doParseModule(wasm, lexer, true); +} + +Result parseExpression(Module& wasm, Lexer& lexer) { + ParseDefsCtx ctx(lexer, wasm, {}, {}, {}, {}, {}); + auto e = expr(ctx); + CHECK_ERR(e); + return *e; +} + } // namespace wasm::WATParser -- cgit v1.2.3