Skip to content

Commit c0cdd26

Browse files
authored
[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.
1 parent a9f01c0 commit c0cdd26

File tree

4 files changed

+1084
-1751
lines changed

4 files changed

+1084
-1751
lines changed

src/parser/input-impl.h

Lines changed: 0 additions & 255 deletions
This file was deleted.

src/parser/input.h

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,40 +41,47 @@ struct ParseInput {
4141

4242
bool empty() { return lexer.empty(); }
4343

44-
std::optional<Token> peek();
45-
bool takeLParen();
46-
bool takeRParen();
47-
bool takeUntilParen();
48-
std::optional<Name> takeID();
49-
std::optional<std::string_view> takeKeyword();
50-
bool takeKeyword(std::string_view expected);
51-
std::optional<uint64_t> takeOffset();
52-
std::optional<uint32_t> takeAlign();
53-
std::optional<uint64_t> takeU64();
54-
std::optional<uint64_t> takeI64();
55-
std::optional<uint32_t> takeU32();
56-
std::optional<uint32_t> takeI32();
57-
std::optional<uint16_t> takeI16();
58-
std::optional<uint8_t> takeU8();
59-
std::optional<uint8_t> takeI8();
60-
std::optional<double> takeF64();
61-
std::optional<float> takeF32();
62-
std::optional<std::string> takeString();
63-
std::optional<Name> takeName();
64-
bool takeSExprStart(std::string_view expected);
65-
bool peekSExprStart(std::string_view expected);
44+
// TODO: Remove this useless layer of abstraction between the Lexer and
45+
// Parser.
46+
std::optional<Token> peek() { return lexer.peek(); }
47+
bool takeLParen() { return lexer.takeLParen(); }
48+
bool takeRParen() { return lexer.takeRParen(); }
49+
bool takeUntilParen() { return lexer.takeUntilParen(); }
50+
std::optional<Name> takeID() { return lexer.takeID(); }
51+
std::optional<std::string_view> takeKeyword() { return lexer.takeKeyword(); }
52+
bool takeKeyword(std::string_view expected) {
53+
return lexer.takeKeyword(expected);
54+
}
55+
std::optional<uint64_t> takeOffset() { return lexer.takeOffset(); }
56+
std::optional<uint32_t> takeAlign() { return lexer.takeAlign(); }
57+
std::optional<uint64_t> takeU64() { return lexer.takeU64(); }
58+
std::optional<uint64_t> takeI64() { return lexer.takeI64(); }
59+
std::optional<uint32_t> takeU32() { return lexer.takeU32(); }
60+
std::optional<uint32_t> takeI32() { return lexer.takeI32(); }
61+
std::optional<uint16_t> takeI16() { return lexer.takeI16(); }
62+
std::optional<uint8_t> takeU8() { return lexer.takeU8(); }
63+
std::optional<uint8_t> takeI8() { return lexer.takeI8(); }
64+
std::optional<double> takeF64() { return lexer.takeF64(); }
65+
std::optional<float> takeF32() { return lexer.takeF32(); }
66+
std::optional<std::string> takeString() { return lexer.takeString(); }
67+
std::optional<Name> takeName() { return lexer.takeName(); }
68+
bool takeSExprStart(std::string_view expected) {
69+
return lexer.takeSExprStart(expected);
70+
}
71+
bool peekSExprStart(std::string_view expected) {
72+
return lexer.peekSExprStart(expected);
73+
}
6674

67-
Index getPos();
68-
[[nodiscard]] Err err(Index pos, std::string reason);
69-
[[nodiscard]] Err err(std::string reason) { return err(getPos(), reason); }
75+
Index getPos() { return lexer.getPos(); }
7076

71-
private:
72-
template<typename T> std::optional<T> takeU();
73-
template<typename T> std::optional<T> takeS();
74-
template<typename T> std::optional<T> takeI();
75-
};
77+
[[nodiscard]] Err err(Index pos, std::string reason) {
78+
std::stringstream msg;
79+
msg << lexer.position(pos) << ": error: " << reason;
80+
return Err{msg.str()};
81+
}
7682

77-
#include "input-impl.h"
83+
[[nodiscard]] Err err(std::string reason) { return err(getPos(), reason); }
84+
};
7885

7986
} // namespace wasm::WATParser
8087

0 commit comments

Comments
 (0)