Skip to content

Commit 5efc3e7

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Simplify SavePosition/RestorePosition functions
PiperOrigin-RevId: 975984463
1 parent 04ffade commit 5efc3e7

4 files changed

Lines changed: 9 additions & 26 deletions

File tree

parser/internal/lexer.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,6 @@ std::string_view TokenTypeToString(TokenType type) {
163163
Token Lexer::Lex() {
164164
int32_t start = GetPosition();
165165
if (ABSL_PREDICT_FALSE(position_ >= content_.size())) {
166-
at_end_ = true;
167-
done_ = true;
168166
return MakeToken(TokenType::kEnd, start, start);
169167
}
170168
char32_t c = content_.at(position_);

parser/internal/lexer.h

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,6 @@ class Lexer final {
140140
std::numeric_limits<int32_t>::max()));
141141
}
142142

143-
struct Position final {
144-
int32_t position = 0;
145-
bool at_end = false;
146-
bool done = false;
147-
LexerError error;
148-
};
149-
150143
Lexer(const Lexer&) = delete;
151144
Lexer(Lexer&&) = delete;
152145
Lexer& operator=(const Lexer&) = delete;
@@ -165,15 +158,13 @@ class Lexer final {
165158

166159
[[nodiscard]] int32_t GetPosition() const { return position_; }
167160

168-
[[nodiscard]] Position SavePosition() const {
169-
return Position{position_, at_end_, done_, error_};
170-
}
161+
[[nodiscard]] int32_t SavePosition() const { return position_; }
171162

172-
void RestorePosition(const Position& position) {
173-
position_ = position.position;
174-
at_end_ = position.at_end;
175-
done_ = position.done;
176-
error_ = position.error;
163+
void RestorePosition(int32_t position) {
164+
ABSL_DCHECK_GE(position, 0);
165+
ABSL_DCHECK_LE(position, static_cast<int32_t>(content_.size()));
166+
position_ = position;
167+
error_ = LexerError{};
177168
}
178169

179170
private:
@@ -201,9 +192,6 @@ class Lexer final {
201192
}
202193

203194
[[nodiscard]] Token MakeToken(TokenType type, int32_t start, int32_t end) {
204-
if (ABSL_PREDICT_FALSE(at_end_)) {
205-
AtEndTokenCreated();
206-
}
207195
return Token{.type = type, .start = start, .end = end};
208196
}
209197

@@ -214,8 +202,6 @@ class Lexer final {
214202
return Token{.type = TokenType::kError, .start = start, .end = end};
215203
}
216204

217-
void AtEndTokenCreated() { done_ = true; }
218-
219205
// Consumes characters up to and including the first occurrence of character
220206
// `c` without interpreting backslashes as escapes. Returns true if `c` was
221207
// found and consumed; false if end of input was reached.
@@ -294,8 +280,6 @@ class Lexer final {
294280

295281
cel::SourceContentView content_;
296282
int32_t position_ = 0;
297-
bool at_end_ = false;
298-
bool done_ = false;
299283
LexerError error_;
300284
};
301285

parser/internal/lexer_test.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "parser/internal/lexer.h"
1616

17+
#include <cstdint>
1718
#include <string>
1819
#include <string_view>
1920
#include <utility>
@@ -506,7 +507,7 @@ TEST(LexerPositionTest, SaveAndRestorePosition) {
506507
EXPECT_EQ(tok2.type, TokenType::kWhitespace);
507508

508509
// Save position before '+'
509-
Lexer::Position saved = lexer.SavePosition();
510+
int32_t saved = lexer.SavePosition();
510511

511512
Token tok3 = lexer.Lex();
512513
EXPECT_EQ(tok3.type, TokenType::kPlus);

parser/internal/pratt_parser_worker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ int PrattParserWorker<ExprNode>::CountGroupingParentheses() {
12221222
}
12231223

12241224
// Save lexer position to restore after scanning ahead.
1225-
const Lexer::Position saved_pos = lexer_.SavePosition();
1225+
const int32_t saved_pos = lexer_.SavePosition();
12261226
auto restore_lexer = absl::MakeCleanup(
12271227
[this, saved_pos] { lexer_.RestorePosition(saved_pos); });
12281228

0 commit comments

Comments
 (0)