From 96923f19eb0e7233fd1c7f438d3ebcf97690fd53 Mon Sep 17 00:00:00 2001 From: Thomas Lively <7121787+tlively@users.noreply.github.com> Date: Tue, 14 Jun 2022 18:46:42 -0700 Subject: [Parser][NFC] Small code cleanups (#4729) Apply cleanups suggested by aheejin in post-merge code review of previous parser PRs. --- src/wasm/wasm-io.cpp | 2 +- src/wasm/wat-lexer.cpp | 2 +- src/wasm/wat-parser.cpp | 12 ++++++------ src/wat-lexer.h | 16 ++++++++++------ src/wat-parser.h | 3 +-- 5 files changed, 19 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/wasm/wasm-io.cpp b/src/wasm/wasm-io.cpp index 55892975b..90f267e9b 100644 --- a/src/wasm/wasm-io.cpp +++ b/src/wasm/wasm-io.cpp @@ -24,12 +24,12 @@ // binary. // +#include "wasm-io.h" #include "support/debug.h" #include "wasm-binary.h" #include "wasm-s-parser.h" #include "wat-parser.h" -#include "wasm-io.h" namespace wasm { diff --git a/src/wasm/wat-lexer.cpp b/src/wasm/wat-lexer.cpp index 0d1dc2794..c7959295c 100644 --- a/src/wasm/wat-lexer.cpp +++ b/src/wasm/wat-lexer.cpp @@ -942,7 +942,7 @@ void Lexer::lexToken() { curr = {tok}; } -TextPos Lexer::position(const char* c) { +TextPos Lexer::position(const char* c) const { assert(size_t(c - buffer.data()) < buffer.size()); TextPos pos{1, 0}; for (const char* p = buffer.data(); p != c; ++p) { diff --git a/src/wasm/wat-parser.cpp b/src/wasm/wat-parser.cpp index aafb66019..caeccfd76 100644 --- a/src/wasm/wat-parser.cpp +++ b/src/wasm/wat-parser.cpp @@ -32,11 +32,11 @@ // definitions. This phase establishes the indices and names of each module // element so that subsequent phases can look them up. // -// The second phase, not yet implemented, parses type definitions to construct -// the types used in the module. This has to be its own phase because we have no -// way to refer to a type before it has been built along with all the other -// types, unlike for other module elements that can be referred to by name -// before their definitions have been parsed. +// The second phase parses type definitions to construct the types used in the +// module. This has to be its own phase because we have no way to refer to a +// type before it has been built along with all the other types, unlike for +// other module elements that can be referred to by name before their +// definitions have been parsed. // // The third phase, not yet implemented, further parses and constructs types // implicitly defined by type uses in functions, blocks, and call_indirect @@ -89,7 +89,7 @@ struct ParseInput { lexer.setIndex(index); } - bool empty() { return lexer == lexer.end(); } + bool empty() { return lexer.empty(); } std::optional peek() { if (!empty()) { diff --git a/src/wat-lexer.h b/src/wat-lexer.h index 30aa1e036..b41cbd8c1 100644 --- a/src/wat-lexer.h +++ b/src/wat-lexer.h @@ -166,7 +166,7 @@ public: Lexer(std::string_view buffer) : buffer(buffer) { setIndex(0); } - size_t getIndex() { return index; } + size_t getIndex() const { return index; } void setIndex(size_t i) { index = i; @@ -206,12 +206,16 @@ public: Lexer begin() { return *this; } - Lexer end() { return Lexer(); } + Lexer end() const { return Lexer(); } - TextPos position(const char* c); - TextPos position(size_t i) { return position(buffer.data() + i); } - TextPos position(std::string_view span) { return position(span.data()); } - TextPos position(Token tok) { return position(tok.span); } + bool empty() const { return *this == end(); } + + TextPos position(const char* c) const; + TextPos position(size_t i) const { return position(buffer.data() + i); } + TextPos position(std::string_view span) const { + return position(span.data()); + } + TextPos position(Token tok) const { return position(tok.span); } private: void skipSpace(); diff --git a/src/wat-parser.h b/src/wat-parser.h index 4bb9b62c8..7f92de9e4 100644 --- a/src/wat-parser.h +++ b/src/wat-parser.h @@ -17,7 +17,6 @@ #ifndef wasm_wat_parser_h #define wasm_wat_parser_h -#include #include #include "wasm.h" @@ -37,7 +36,7 @@ template struct Result { Result(Result& other) = default; Result(const Err& e) : val(std::in_place_type, e) {} - Result(Err&& e) : val(std::in_place_type, e) {} + Result(Err&& e) : val(std::in_place_type, std::move(e)) {} template Result(U&& u) : val(std::in_place_type, std::forward(u)) {} -- cgit v1.2.3