Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions check.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def run_spec_test(wast):

def run_opt_test(wast):
# check optimization validation
cmd = shared.WASM_OPT + [wast, '-O', '-all', '-q']
cmd = shared.WASM_OPT + [wast, '-O', '-all', '-q', '--new-wat-parser']
support.run_command(cmd)

def check_expected(actual, expected):
Expand All @@ -213,7 +213,7 @@ def check_expected(actual, expected):
try:
actual = run_spec_test(wast)
except Exception as e:
if ('wasm-validator error' in str(e) or 'parse exception' in str(e)) and '.fail.' in base:
if ('wasm-validator error' in str(e) or 'error: ' in str(e)) and '.fail.' in base:
print('<< test failed as expected >>')
continue # don't try all the binary format stuff TODO
else:
Expand Down
12 changes: 5 additions & 7 deletions src/parser/contexts.h
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ struct ParseDeclsCtx : NullTypeParserCtx, NullInstrParserCtx {
return Ok{};
}

ParseDeclsCtx(std::string_view in, Module& wasm) : in(in), wasm(wasm) {}
ParseDeclsCtx(Lexer& in, Module& wasm) : in(in), wasm(wasm) {}

void addFuncType(SignatureT) {}
void addContType(ContinuationT) {}
Expand Down Expand Up @@ -1049,9 +1049,7 @@ struct ParseTypeDefsCtx : TypeParserCtx<ParseTypeDefsCtx> {
// The index of the subtype definition we are parsing.
Index index = 0;

ParseTypeDefsCtx(std::string_view in,
TypeBuilder& builder,
const IndexMap& typeIndices)
ParseTypeDefsCtx(Lexer& in, TypeBuilder& builder, const IndexMap& typeIndices)
: TypeParserCtx<ParseTypeDefsCtx>(typeIndices), in(in), builder(builder),
names(builder.size()) {}

Expand Down Expand Up @@ -1121,7 +1119,7 @@ struct ParseImplicitTypeDefsCtx : TypeParserCtx<ParseImplicitTypeDefsCtx> {
// Map signatures to the first defined heap type they match.
std::unordered_map<Signature, HeapType> sigTypes;

ParseImplicitTypeDefsCtx(std::string_view in,
ParseImplicitTypeDefsCtx(Lexer& in,
std::vector<HeapType>& types,
std::unordered_map<Index, HeapType>& implicitTypes,
const IndexMap& typeIndices)
Expand Down Expand Up @@ -1192,7 +1190,7 @@ struct ParseModuleTypesCtx : TypeParserCtx<ParseModuleTypesCtx>,
Index index = 0;

ParseModuleTypesCtx(
std::string_view in,
Lexer& in,
Module& wasm,
const std::vector<HeapType>& types,
const std::unordered_map<Index, HeapType>& implicitTypes,
Expand Down Expand Up @@ -1397,7 +1395,7 @@ struct ParseDefsCtx : TypeParserCtx<ParseDefsCtx> {
}

ParseDefsCtx(
std::string_view in,
Lexer& in,
Module& wasm,
const std::vector<HeapType>& types,
const std::unordered_map<Index, HeapType>& implicitTypes,
Expand Down
3 changes: 2 additions & 1 deletion src/parser/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,13 @@ extern Name srcAnnotationKind;

struct Lexer {
private:
std::string_view buffer;
size_t index = 0;
std::optional<Token> curr;
std::vector<Annotation> annotations;

public:
std::string_view buffer;

Lexer(std::string_view buffer) : buffer(buffer) { setIndex(0); }

size_t getIndex() const { return index; }
Expand Down
29 changes: 21 additions & 8 deletions src/parser/wat-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down Expand Up @@ -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<Expression*> parseExpression(Module& wasm, Lexer& lexer) {
ParseDefsCtx ctx(lexer, wasm, {}, {}, {}, {}, {});
auto e = expr(ctx);
CHECK_ERR(e);
return *e;
}

} // namespace wasm::WATParser
7 changes: 7 additions & 0 deletions src/parser/wat-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <string_view>

#include "parser/lexer.h"
#include "support/result.h"
#include "wasm.h"

Expand All @@ -27,6 +28,12 @@ namespace wasm::WATParser {
// Parse a single WAT module.
Result<> parseModule(Module& wasm, std::string_view in);

// Parse a single WAT module that may have other things after it, as in a wast
// file.
Result<> parseModule(Module& wasm, Lexer& lexer);

Result<Expression*> parseExpression(Module& wasm, Lexer& lexer);

} // namespace wasm::WATParser

#endif // parser_wat_parser_h
18 changes: 4 additions & 14 deletions src/tools/wasm-as.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
// wasm2asm console tool
//

#include "parser/wat-parser.h"
#include "support/colors.h"
#include "support/file.h"
#include "wasm-io.h"
#include "wasm-s-parser.h"
#include "wasm-validator.h"

#include "tool-options.h"
Expand Down Expand Up @@ -109,19 +109,9 @@ int main(int argc, const char* argv[]) {
Module wasm;
options.applyFeatures(wasm);

try {
if (options.debug) {
std::cerr << "s-parsing..." << std::endl;
}
SExpressionParser parser(const_cast<char*>(input.c_str()));
Element& root = *parser.root;
if (options.debug) {
std::cerr << "w-parsing..." << std::endl;
}
SExpressionWasmBuilder builder(wasm, *root[0], options.profile);
} catch (ParseException& p) {
p.dump(std::cerr);
Fatal() << "error in parsing input";
auto parsed = WATParser::parseModule(wasm, input);
if (auto* err = parsed.getErr()) {
Fatal() << err->msg;
}

if (options.extra["validate"] != "none") {
Expand Down
Loading