summaryrefslogtreecommitdiff
path: root/src/wast-parser.cc
diff options
context:
space:
mode:
authorKeith Winstein <keithw@cs.stanford.edu>2023-06-07 13:25:52 -0700
committerGitHub <noreply@github.com>2023-06-07 13:25:52 -0700
commit7cedfca8dd7514e3485f47ca1e0ed81bd5bdcb9b (patch)
tree6b1a4e5898adf5b8883ca580f2b42a8e35ead7f5 /src/wast-parser.cc
parentaca6d6f508176bbb0be5dae087e12264d149c9e9 (diff)
downloadwabt-7cedfca8dd7514e3485f47ca1e0ed81bd5bdcb9b.tar.gz
wabt-7cedfca8dd7514e3485f47ca1e0ed81bd5bdcb9b.tar.bz2
wabt-7cedfca8dd7514e3485f47ca1e0ed81bd5bdcb9b.zip
WastParser: allow empty modules/scripts with warning (#2248)
Diffstat (limited to 'src/wast-parser.cc')
-rw-r--r--src/wast-parser.cc15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/wast-parser.cc b/src/wast-parser.cc
index 11d0d338..29da7b23 100644
--- a/src/wast-parser.cc
+++ b/src/wast-parser.cc
@@ -1174,13 +1174,15 @@ Result WastParser::ParseModule(std::unique_ptr<Module>* out_module) {
} else if (IsModuleField(PeekPair())) {
// Parse an inline module (i.e. one with no surrounding (module)).
CHECK_RESULT(ParseModuleFieldList(module.get()));
+ } else if (PeekMatch(TokenType::Eof)) {
+ errors_->emplace_back(ErrorLevel::Warning, GetLocation(), "empty module");
} else {
ConsumeIfLpar();
ErrorExpected({"a module field", "a module"});
}
EXPECT(Eof);
- if (errors_->size() == 0) {
+ if (!HasError()) {
*out_module = std::move(module);
return Result::Ok;
} else {
@@ -1203,13 +1205,15 @@ Result WastParser::ParseScript(std::unique_ptr<Script>* out_script) {
script->commands.emplace_back(std::move(command));
} else if (IsCommand(PeekPair())) {
CHECK_RESULT(ParseCommandList(script.get(), &script->commands));
+ } else if (PeekMatch(TokenType::Eof)) {
+ errors_->emplace_back(ErrorLevel::Warning, GetLocation(), "empty script");
} else {
ConsumeIfLpar();
ErrorExpected({"a module field", "a command"});
}
EXPECT(Eof);
- if (errors_->size() == 0) {
+ if (!HasError()) {
*out_script = std::move(script);
return Result::Ok;
} else {
@@ -3343,7 +3347,6 @@ Result WastParser::ParseModuleCommand(Script* script, CommandPtr* out_command) {
module->name = bsm->name;
module->loc = bsm->loc;
for (const auto& error : errors) {
- assert(error.error_level == ErrorLevel::Error);
if (error.loc.offset == kInvalidOffset) {
Error(bsm->loc, "error in binary module: %s", error.message.c_str());
} else {
@@ -3590,6 +3593,12 @@ void WastParser::CheckImportOrdering(Module* module) {
}
}
+bool WastParser::HasError() const {
+ return std::any_of(errors_->begin(), errors_->end(), [](const auto& x) {
+ return x.error_level == ErrorLevel::Error;
+ });
+}
+
Result ParseWatModule(WastLexer* lexer,
std::unique_ptr<Module>* out_module,
Errors* errors,