Skip to content

Commit 003786c

Browse files
authored
[Parser] Do not eagerly lex IDs (#6542)
Lex them on demand instead to avoid wasted work.
1 parent eccf9f9 commit 003786c

File tree

2 files changed

+23
-43
lines changed

2 files changed

+23
-43
lines changed

src/parser/lexer.cpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,21 +1015,6 @@ std::optional<std::string_view> Token::getString() const {
10151015
return {};
10161016
}
10171017

1018-
std::optional<std::string_view> Token::getID() const {
1019-
if (auto* tok = std::get_if<IdTok>(&data)) {
1020-
if (tok->str) {
1021-
return std::string_view(*tok->str);
1022-
}
1023-
if (tok->isStr) {
1024-
// Remove '$' and quotes.
1025-
return span.substr(2, span.size() - 3);
1026-
}
1027-
// Remove '$'.
1028-
return span.substr(1);
1029-
}
1030-
return {};
1031-
}
1032-
10331018
void Lexer::skipSpace() {
10341019
while (true) {
10351020
if (auto ctx = annotation(next())) {
@@ -1069,6 +1054,26 @@ bool Lexer::takeRParen() {
10691054
return false;
10701055
}
10711056

1057+
std::optional<Name> Lexer::takeID() {
1058+
if (curr) {
1059+
return std::nullopt;
1060+
}
1061+
if (auto result = ident(next())) {
1062+
index += result->span.size();
1063+
advance();
1064+
if (result->str) {
1065+
return Name(*result->str);
1066+
}
1067+
if (result->isStr) {
1068+
// Remove '$' and quotes.
1069+
return Name(result->span.substr(2, result->span.size() - 3));
1070+
}
1071+
// Remove '$'.
1072+
return Name(result->span.substr(1));
1073+
}
1074+
return std::nullopt;
1075+
}
1076+
10721077
std::optional<std::string_view> Lexer::takeKeyword() {
10731078
if (curr) {
10741079
return std::nullopt;
@@ -1123,9 +1128,7 @@ std::optional<uint32_t> Lexer::takeAlign() {
11231128
void Lexer::lexToken() {
11241129
// TODO: Ensure we're getting the longest possible match.
11251130
Token tok;
1126-
if (auto t = ident(next())) {
1127-
tok = Token{t->span, IdTok{t->isStr, t->str}};
1128-
} else if (auto t = integer(next())) {
1131+
if (auto t = integer(next())) {
11291132
tok = Token{t->span, IntTok{t->n, t->sign}};
11301133
} else if (auto t = float_(next())) {
11311134
tok = Token{t->span, FloatTok{t->nanPayload, t->d}};
@@ -1186,8 +1189,6 @@ std::ostream& operator<<(std::ostream& os, const TextPos& pos) {
11861189
return os << pos.line << ":" << pos.col;
11871190
}
11881191

1189-
std::ostream& operator<<(std::ostream& os, const IdTok&) { return os << "id"; }
1190-
11911192
std::ostream& operator<<(std::ostream& os, const IntTok& tok) {
11921193
return os << (tok.sign == Pos ? "+" : tok.sign == Neg ? "-" : "") << tok.n;
11931194
}

src/parser/lexer.h

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,6 @@ struct TextPos {
4545
// Tokens
4646
// ======
4747

48-
struct IdTok {
49-
// Whether this ID has `$"..."` format
50-
bool isStr;
51-
52-
// If the ID is a string ID and contains escapes, this is its contents.
53-
std::optional<std::string> str;
54-
55-
bool operator==(const IdTok&) const { return true; }
56-
friend std::ostream& operator<<(std::ostream&, const IdTok&);
57-
};
58-
5948
enum Sign { NoSign, Pos, Neg };
6049

6150
struct IntTok {
@@ -88,7 +77,7 @@ struct StringTok {
8877
};
8978

9079
struct Token {
91-
using Data = std::variant<IdTok, IntTok, FloatTok, StringTok>;
80+
using Data = std::variant<IntTok, FloatTok, StringTok>;
9281
std::string_view span;
9382
Data data;
9483

@@ -102,7 +91,6 @@ struct Token {
10291
std::optional<double> getF64() const;
10392
std::optional<float> getF32() const;
10493
std::optional<std::string_view> getString() const;
105-
std::optional<std::string_view> getID() const;
10694

10795
bool operator==(const Token&) const;
10896
friend std::ostream& operator<<(std::ostream& os, const Token&);
@@ -164,16 +152,7 @@ struct Lexer {
164152
}
165153
}
166154

167-
std::optional<Name> takeID() {
168-
if (curr) {
169-
if (auto id = curr->getID()) {
170-
advance();
171-
// See comment on takeName.
172-
return Name(std::string(*id));
173-
}
174-
}
175-
return {};
176-
}
155+
std::optional<Name> takeID();
177156

178157
std::optional<std::string_view> takeKeyword();
179158
bool takeKeyword(std::string_view expected);

0 commit comments

Comments
 (0)