diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/emscripten-helpers.cc | 5 | ||||
-rw-r--r-- | src/test-wast-parser.cc | 3 | ||||
-rw-r--r-- | src/tools/spectest-interp.cc | 4 | ||||
-rw-r--r-- | src/tools/wast2json.cc | 4 | ||||
-rw-r--r-- | src/tools/wat-desugar.cc | 4 | ||||
-rw-r--r-- | src/tools/wat2wasm.cc | 4 | ||||
-rw-r--r-- | src/wabt.post.js | 13 | ||||
-rw-r--r-- | src/wast-lexer.cc | 34 | ||||
-rw-r--r-- | src/wast-parser.cc | 6 |
9 files changed, 43 insertions, 34 deletions
diff --git a/src/emscripten-helpers.cc b/src/emscripten-helpers.cc index cd050f6d..03d5ad80 100644 --- a/src/emscripten-helpers.cc +++ b/src/emscripten-helpers.cc @@ -96,9 +96,10 @@ void wabt_destroy_features(wabt::Features* f) { wabt::WastLexer* wabt_new_wast_buffer_lexer(const char* filename, const void* data, - size_t size) { + size_t size, + wabt::Errors* errors) { std::unique_ptr<wabt::WastLexer> lexer = - wabt::WastLexer::CreateBufferLexer(filename, data, size); + wabt::WastLexer::CreateBufferLexer(filename, data, size, errors); return lexer.release(); } diff --git a/src/test-wast-parser.cc b/src/test-wast-parser.cc index 69b5cf67..60fc601d 100644 --- a/src/test-wast-parser.cc +++ b/src/test-wast-parser.cc @@ -34,8 +34,9 @@ std::string repeat(std::string s, size_t count) { } Errors ParseInvalidModule(std::string text) { - auto lexer = WastLexer::CreateBufferLexer("test", text.c_str(), text.size()); Errors errors; + auto lexer = + WastLexer::CreateBufferLexer("test", text.c_str(), text.size(), &errors); std::unique_ptr<Module> module; Features features; WastParseOptions options(features); diff --git a/src/tools/spectest-interp.cc b/src/tools/spectest-interp.cc index af50bfb2..aae11347 100644 --- a/src/tools/spectest-interp.cc +++ b/src/tools/spectest-interp.cc @@ -1382,9 +1382,9 @@ wabt::Result CommandRunner::ReadInvalidTextModule( const std::string& header) { std::vector<uint8_t> file_data; wabt::Result result = ReadFile(module_filename, &file_data); - std::unique_ptr<WastLexer> lexer = WastLexer::CreateBufferLexer( - module_filename, file_data.data(), file_data.size()); Errors errors; + std::unique_ptr<WastLexer> lexer = WastLexer::CreateBufferLexer( + module_filename, file_data.data(), file_data.size(), &errors); if (Succeeded(result)) { std::unique_ptr<wabt::Module> module; WastParseOptions options(s_features); diff --git a/src/tools/wast2json.cc b/src/tools/wast2json.cc index bdc4a674..4b92e279 100644 --- a/src/tools/wast2json.cc +++ b/src/tools/wast2json.cc @@ -104,13 +104,13 @@ int ProgramMain(int argc, char** argv) { std::vector<uint8_t> file_data; Result result = ReadFile(s_infile, &file_data); + Errors errors; std::unique_ptr<WastLexer> lexer = WastLexer::CreateBufferLexer( - s_infile, file_data.data(), file_data.size()); + s_infile, file_data.data(), file_data.size(), &errors); if (Failed(result)) { WABT_FATAL("unable to read file: %s\n", s_infile); } - Errors errors; std::unique_ptr<Script> script; WastParseOptions parse_wast_options(s_features); result = ParseWastScript(lexer.get(), &script, &errors, &parse_wast_options); diff --git a/src/tools/wat-desugar.cc b/src/tools/wat-desugar.cc index 69a5dd54..e1e27adc 100644 --- a/src/tools/wat-desugar.cc +++ b/src/tools/wat-desugar.cc @@ -90,10 +90,10 @@ int ProgramMain(int argc, char** argv) { WABT_FATAL("unable to read %s\n", s_infile); } + Errors errors; std::unique_ptr<WastLexer> lexer(WastLexer::CreateBufferLexer( - s_infile, file_data.data(), file_data.size())); + s_infile, file_data.data(), file_data.size(), &errors)); - Errors errors; std::unique_ptr<Script> script; WastParseOptions parse_wast_options(s_features); result = ParseWastScript(lexer.get(), &script, &errors, &parse_wast_options); diff --git a/src/tools/wat2wasm.cc b/src/tools/wat2wasm.cc index eab17ebf..fedd88a6 100644 --- a/src/tools/wat2wasm.cc +++ b/src/tools/wat2wasm.cc @@ -133,13 +133,13 @@ int ProgramMain(int argc, char** argv) { std::vector<uint8_t> file_data; Result result = ReadFile(s_infile, &file_data); + Errors errors; std::unique_ptr<WastLexer> lexer = WastLexer::CreateBufferLexer( - s_infile, file_data.data(), file_data.size()); + s_infile, file_data.data(), file_data.size(), &errors); if (Failed(result)) { WABT_FATAL("unable to read file: %s\n", s_infile); } - Errors errors; std::unique_ptr<Module> module; WastParseOptions parse_wast_options(s_features); result = ParseWatModule(lexer.get(), &module, &errors, &parse_wast_options); diff --git a/src/wabt.post.js b/src/wabt.post.js index c71e9d03..0d67b34b 100644 --- a/src/wabt.post.js +++ b/src/wabt.post.js @@ -110,11 +110,12 @@ FEATURES.forEach(function(feature) { /// Lexer -function Lexer(filename, buffer) { +function Lexer(filename, buffer, errors) { this.filenameObj = allocateCString(filename); this.bufferObj = allocateBuffer(buffer); this.addr = Module._wabt_new_wast_buffer_lexer( - this.filenameObj.addr, this.bufferObj.addr, this.bufferObj.size); + this.filenameObj.addr, this.bufferObj.addr, this.bufferObj.size, + errors.addr); } Lexer.prototype = Object.create(Object.prototype); @@ -159,10 +160,9 @@ OutputBuffer.prototype.destroy = function() { /// Errors -function Errors(kind, lexer) { +function Errors(kind) { this.kind = kind; this.addr = Module._wabt_new_errors(); - this.lexer = lexer; } Errors.prototype = Object.create(Object.prototype); @@ -194,8 +194,9 @@ Errors.prototype.destroy = function() { /// parseWat function parseWat(filename, buffer, options) { - var lexer = new Lexer(filename, buffer); - var errors = new Errors('text', lexer); + var errors = new Errors('text'); + var lexer = new Lexer(filename, buffer, errors); + errors.lexer = lexer; var features = new Features(options || {}); try { diff --git a/src/wast-lexer.cc b/src/wast-lexer.cc index 7a62e4a0..cc3d2751 100644 --- a/src/wast-lexer.cc +++ b/src/wast-lexer.cc @@ -22,11 +22,8 @@ #include "wabt/config.h" #include "wabt/lexer-source.h" -#include "wabt/wast-parser.h" -#define ERROR(...) \ - if (parser) \ - parser->Error(GetLocation(), __VA_ARGS__) +#define ERROR(...) Error(GetLocation(), __VA_ARGS__) namespace wabt { @@ -37,7 +34,8 @@ namespace { } // namespace WastLexer::WastLexer(std::unique_ptr<LexerSource> source, - std::string_view filename) + std::string_view filename, + Errors* errors) : source_(std::move(source)), filename_(filename), line_(1), @@ -45,17 +43,20 @@ WastLexer::WastLexer(std::unique_ptr<LexerSource> source, buffer_end_(buffer_ + source_->size()), line_start_(buffer_), token_start_(buffer_), - cursor_(buffer_) {} + cursor_(buffer_), + errors_(errors) {} // static std::unique_ptr<WastLexer> WastLexer::CreateBufferLexer( std::string_view filename, const void* data, - size_t size) { - return MakeUnique<WastLexer>(MakeUnique<LexerSource>(data, size), filename); + size_t size, + Errors* errors) { + return MakeUnique<WastLexer>(MakeUnique<LexerSource>(data, size), filename, + errors); } -Token WastLexer::GetToken(WastParser* parser) { +Token WastLexer::GetToken() { while (true) { token_start_ = cursor_; switch (PeekChar()) { @@ -64,7 +65,7 @@ Token WastLexer::GetToken(WastParser* parser) { case '(': if (MatchString("(;")) { - if (ReadBlockComment(parser)) { + if (ReadBlockComment()) { continue; } return BareToken(TokenType::Eof); @@ -103,7 +104,7 @@ Token WastLexer::GetToken(WastParser* parser) { continue; case '"': - return GetStringToken(parser); + return GetStringToken(); case '+': case '-': @@ -234,7 +235,7 @@ void WastLexer::Newline() { line_start_ = cursor_; } -bool WastLexer::ReadBlockComment(WastParser* parser) { +bool WastLexer::ReadBlockComment() { int nesting = 1; while (true) { switch (ReadChar()) { @@ -294,7 +295,7 @@ void WastLexer::ReadWhitespace() { } } -Token WastLexer::GetStringToken(WastParser* parser) { +Token WastLexer::GetStringToken() { const char* saved_token_start = token_start_; bool has_error = false; bool in_string = true; @@ -472,7 +473,7 @@ WastLexer::ReservedChars WastLexer::ReadReservedChars() { ret = ReservedChars::Id; } } else if (peek == '"') { - GetStringToken(nullptr); + GetStringToken(); ret = ReservedChars::Some; } else { break; @@ -608,4 +609,9 @@ Token WastLexer::GetReservedToken() { return TextToken(TokenType::Reserved); } +void WastLexer::Error(Location loc, const char* format, ...) { + WABT_SNPRINTF_ALLOCA(buffer, length, format); + errors_->emplace_back(ErrorLevel::Error, loc, buffer); +} + } // namespace wabt diff --git a/src/wast-parser.cc b/src/wast-parser.cc index a3cc7a66..f3fe5e25 100644 --- a/src/wast-parser.cc +++ b/src/wast-parser.cc @@ -584,7 +584,7 @@ void WastParser::Error(Location loc, const char* format, ...) { Token WastParser::GetToken() { if (tokens_.empty()) { - tokens_.push_back(lexer_->GetToken(this)); + tokens_.push_back(lexer_->GetToken()); } return tokens_.front(); } @@ -595,7 +595,7 @@ Location WastParser::GetLocation() { TokenType WastParser::Peek(size_t n) { while (tokens_.size() <= n) { - Token cur = lexer_->GetToken(this); + Token cur = lexer_->GetToken(); if (cur.token_type() != TokenType::LparAnn) { tokens_.push_back(cur); } else { @@ -613,7 +613,7 @@ TokenType WastParser::Peek(size_t n) { } int indent = 1; while (indent > 0) { - cur = lexer_->GetToken(this); + cur = lexer_->GetToken(); switch (cur.token_type()) { case TokenType::Lpar: case TokenType::LparAnn: |