diff options
author | Thomas Lively <tlively@google.com> | 2024-02-20 13:08:37 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-20 13:08:37 -0800 |
commit | c0cdd267492956e9789148c8e478c467dd59d67b (patch) | |
tree | ce57c212b27345b4957c538d1c51ef2fbd3b8dac /src/parser/input.h | |
parent | a9f01c0c911afabc86dfc210f3ea596f1c35de6e (diff) | |
download | binaryen-c0cdd267492956e9789148c8e478c467dd59d67b.tar.gz binaryen-c0cdd267492956e9789148c8e478c467dd59d67b.tar.bz2 binaryen-c0cdd267492956e9789148c8e478c467dd59d67b.zip |
[Parser] Simplify the lexer interface (#6319)
The lexer was previously an iterator over tokens, but that expressivity is not
actually used in the parser. Instead, we have `input.h` that adapts the token
iterator interface into an iterface that is actually useful.
As a first step toward simplifying the lexer implementation to no longer be an
iterator over tokens, update its interface by moving the adaptation from input.h
to the lexer itself. This requires extensive changes to the lexer unit tests,
which will not have to change further when we actually simplify the lexer
implementation.
Diffstat (limited to 'src/parser/input.h')
-rw-r--r-- | src/parser/input.h | 69 |
1 files changed, 38 insertions, 31 deletions
diff --git a/src/parser/input.h b/src/parser/input.h index 6086ed1a5..f83f5a40a 100644 --- a/src/parser/input.h +++ b/src/parser/input.h @@ -41,40 +41,47 @@ struct ParseInput { bool empty() { return lexer.empty(); } - std::optional<Token> peek(); - bool takeLParen(); - bool takeRParen(); - bool takeUntilParen(); - std::optional<Name> takeID(); - std::optional<std::string_view> takeKeyword(); - bool takeKeyword(std::string_view expected); - std::optional<uint64_t> takeOffset(); - std::optional<uint32_t> takeAlign(); - std::optional<uint64_t> takeU64(); - std::optional<uint64_t> takeI64(); - std::optional<uint32_t> takeU32(); - std::optional<uint32_t> takeI32(); - std::optional<uint16_t> takeI16(); - std::optional<uint8_t> takeU8(); - std::optional<uint8_t> takeI8(); - std::optional<double> takeF64(); - std::optional<float> takeF32(); - std::optional<std::string> takeString(); - std::optional<Name> takeName(); - bool takeSExprStart(std::string_view expected); - bool peekSExprStart(std::string_view expected); + // TODO: Remove this useless layer of abstraction between the Lexer and + // Parser. + std::optional<Token> peek() { return lexer.peek(); } + bool takeLParen() { return lexer.takeLParen(); } + bool takeRParen() { return lexer.takeRParen(); } + bool takeUntilParen() { return lexer.takeUntilParen(); } + std::optional<Name> takeID() { return lexer.takeID(); } + std::optional<std::string_view> takeKeyword() { return lexer.takeKeyword(); } + bool takeKeyword(std::string_view expected) { + return lexer.takeKeyword(expected); + } + std::optional<uint64_t> takeOffset() { return lexer.takeOffset(); } + std::optional<uint32_t> takeAlign() { return lexer.takeAlign(); } + std::optional<uint64_t> takeU64() { return lexer.takeU64(); } + std::optional<uint64_t> takeI64() { return lexer.takeI64(); } + std::optional<uint32_t> takeU32() { return lexer.takeU32(); } + std::optional<uint32_t> takeI32() { return lexer.takeI32(); } + std::optional<uint16_t> takeI16() { return lexer.takeI16(); } + std::optional<uint8_t> takeU8() { return lexer.takeU8(); } + std::optional<uint8_t> takeI8() { return lexer.takeI8(); } + std::optional<double> takeF64() { return lexer.takeF64(); } + std::optional<float> takeF32() { return lexer.takeF32(); } + std::optional<std::string> takeString() { return lexer.takeString(); } + std::optional<Name> takeName() { return lexer.takeName(); } + bool takeSExprStart(std::string_view expected) { + return lexer.takeSExprStart(expected); + } + bool peekSExprStart(std::string_view expected) { + return lexer.peekSExprStart(expected); + } - Index getPos(); - [[nodiscard]] Err err(Index pos, std::string reason); - [[nodiscard]] Err err(std::string reason) { return err(getPos(), reason); } + Index getPos() { return lexer.getPos(); } -private: - template<typename T> std::optional<T> takeU(); - template<typename T> std::optional<T> takeS(); - template<typename T> std::optional<T> takeI(); -}; + [[nodiscard]] Err err(Index pos, std::string reason) { + std::stringstream msg; + msg << lexer.position(pos) << ": error: " << reason; + return Err{msg.str()}; + } -#include "input-impl.h" + [[nodiscard]] Err err(std::string reason) { return err(getPos(), reason); } +}; } // namespace wasm::WATParser |