summaryrefslogtreecommitdiff
path: root/src/parser/lexer.h
diff options
context:
space:
mode:
authorThomas Lively <tlively@google.com>2024-04-25 21:19:46 -0700
committerGitHub <noreply@github.com>2024-04-25 21:19:46 -0700
commiteccf9f951262bf6909bf75e8865e09d0596dcc09 (patch)
tree873779152561a68fa30f2423508ae9a72d09e49f /src/parser/lexer.h
parent35560732b6a2c6960a6e72ea478bc0238a967c30 (diff)
downloadbinaryen-eccf9f951262bf6909bf75e8865e09d0596dcc09.tar.gz
binaryen-eccf9f951262bf6909bf75e8865e09d0596dcc09.tar.bz2
binaryen-eccf9f951262bf6909bf75e8865e09d0596dcc09.zip
[Parser] Do not eagerly lex keywords (#6541)
Lex them on demand instead to avoid wasted work.
Diffstat (limited to 'src/parser/lexer.h')
-rw-r--r--src/parser/lexer.h84
1 files changed, 5 insertions, 79 deletions
diff --git a/src/parser/lexer.h b/src/parser/lexer.h
index 10ba7c25a..f8f7f8b57 100644
--- a/src/parser/lexer.h
+++ b/src/parser/lexer.h
@@ -87,13 +87,8 @@ struct StringTok {
friend std::ostream& operator<<(std::ostream&, const StringTok&);
};
-struct KeywordTok {
- bool operator==(const KeywordTok&) const { return true; }
- friend std::ostream& operator<<(std::ostream&, const KeywordTok&);
-};
-
struct Token {
- using Data = std::variant<IdTok, IntTok, FloatTok, StringTok, KeywordTok>;
+ using Data = std::variant<IdTok, IntTok, FloatTok, StringTok>;
std::string_view span;
Data data;
@@ -101,13 +96,6 @@ struct Token {
// Token classification
// ====================
- std::optional<std::string_view> getKeyword() const {
- if (std::get_if<KeywordTok>(&data)) {
- return span;
- }
- return {};
- }
-
template<typename T> std::optional<T> getU() const;
template<typename T> std::optional<T> getS() const;
template<typename T> std::optional<T> getI() const;
@@ -187,77 +175,15 @@ public:
return {};
}
- std::optional<std::string_view> takeKeyword() {
- if (curr) {
- if (auto keyword = curr->getKeyword()) {
- advance();
- return *keyword;
- }
- }
- return {};
- }
+ std::optional<std::string_view> takeKeyword();
+ bool takeKeyword(std::string_view expected);
std::optional<std::string_view> peekKeyword() {
return Lexer(*this).takeKeyword();
}
- bool takeKeyword(std::string_view expected) {
- if (curr) {
- if (auto keyword = curr->getKeyword()) {
- if (*keyword == expected) {
- advance();
- return true;
- }
- }
- }
- return false;
- }
-
- std::optional<uint64_t> takeOffset() {
- using namespace std::string_view_literals;
- if (curr) {
- if (auto keyword = curr->getKeyword()) {
- if (keyword->substr(0, 7) != "offset="sv) {
- return {};
- }
- Lexer subLexer(keyword->substr(7));
- if (subLexer.empty()) {
- return {};
- }
- if (auto o = subLexer.curr->getU<uint64_t>()) {
- subLexer.advance();
- if (subLexer.empty()) {
- advance();
- return o;
- }
- }
- }
- }
- return std::nullopt;
- }
-
- std::optional<uint32_t> takeAlign() {
- using namespace std::string_view_literals;
- if (curr) {
- if (auto keyword = curr->getKeyword()) {
- if (keyword->substr(0, 6) != "align="sv) {
- return {};
- }
- Lexer subLexer(keyword->substr(6));
- if (subLexer.empty()) {
- return {};
- }
- if (auto a = subLexer.curr->getU<uint32_t>()) {
- subLexer.advance();
- if (subLexer.empty()) {
- advance();
- return a;
- }
- }
- }
- }
- return {};
- }
+ std::optional<uint64_t> takeOffset();
+ std::optional<uint32_t> takeAlign();
template<typename T> std::optional<T> takeU() {
if (curr) {