From 5cfcace1d301088457fe1f6794f498be6fea5986 Mon Sep 17 00:00:00 2001 From: Dmitri Plotnikov Date: Thu, 3 Sep 2026 16:30:02 -0700 Subject: [PATCH] Simplify SavePosition/RestorePosition functions PiperOrigin-RevId: 975984463 --- parser/internal/lexer.cc | 2 -- parser/internal/lexer.h | 28 ++++++--------------------- parser/internal/lexer_test.cc | 3 ++- parser/internal/pratt_parser_worker.h | 2 +- 4 files changed, 9 insertions(+), 26 deletions(-) diff --git a/parser/internal/lexer.cc b/parser/internal/lexer.cc index db08b644f..897a63399 100644 --- a/parser/internal/lexer.cc +++ b/parser/internal/lexer.cc @@ -163,8 +163,6 @@ std::string_view TokenTypeToString(TokenType type) { Token Lexer::Lex() { int32_t start = GetPosition(); if (ABSL_PREDICT_FALSE(position_ >= content_.size())) { - at_end_ = true; - done_ = true; return MakeToken(TokenType::kEnd, start, start); } char32_t c = content_.at(position_); diff --git a/parser/internal/lexer.h b/parser/internal/lexer.h index c35dc08e2..502be627a 100644 --- a/parser/internal/lexer.h +++ b/parser/internal/lexer.h @@ -140,13 +140,6 @@ class Lexer final { std::numeric_limits::max())); } - struct Position final { - int32_t position = 0; - bool at_end = false; - bool done = false; - LexerError error; - }; - Lexer(const Lexer&) = delete; Lexer(Lexer&&) = delete; Lexer& operator=(const Lexer&) = delete; @@ -165,15 +158,13 @@ class Lexer final { [[nodiscard]] int32_t GetPosition() const { return position_; } - [[nodiscard]] Position SavePosition() const { - return Position{position_, at_end_, done_, error_}; - } + [[nodiscard]] int32_t SavePosition() const { return position_; } - void RestorePosition(const Position& position) { - position_ = position.position; - at_end_ = position.at_end; - done_ = position.done; - error_ = position.error; + void RestorePosition(int32_t position) { + ABSL_DCHECK_GE(position, 0); + ABSL_DCHECK_LE(position, static_cast(content_.size())); + position_ = position; + error_ = LexerError{}; } private: @@ -201,9 +192,6 @@ class Lexer final { } [[nodiscard]] Token MakeToken(TokenType type, int32_t start, int32_t end) { - if (ABSL_PREDICT_FALSE(at_end_)) { - AtEndTokenCreated(); - } return Token{.type = type, .start = start, .end = end}; } @@ -214,8 +202,6 @@ class Lexer final { return Token{.type = TokenType::kError, .start = start, .end = end}; } - void AtEndTokenCreated() { done_ = true; } - // Consumes characters up to and including the first occurrence of character // `c` without interpreting backslashes as escapes. Returns true if `c` was // found and consumed; false if end of input was reached. @@ -294,8 +280,6 @@ class Lexer final { cel::SourceContentView content_; int32_t position_ = 0; - bool at_end_ = false; - bool done_ = false; LexerError error_; }; diff --git a/parser/internal/lexer_test.cc b/parser/internal/lexer_test.cc index ebe0011d3..9cdfbd45d 100644 --- a/parser/internal/lexer_test.cc +++ b/parser/internal/lexer_test.cc @@ -14,6 +14,7 @@ #include "parser/internal/lexer.h" +#include #include #include #include @@ -506,7 +507,7 @@ TEST(LexerPositionTest, SaveAndRestorePosition) { EXPECT_EQ(tok2.type, TokenType::kWhitespace); // Save position before '+' - Lexer::Position saved = lexer.SavePosition(); + int32_t saved = lexer.SavePosition(); Token tok3 = lexer.Lex(); EXPECT_EQ(tok3.type, TokenType::kPlus); diff --git a/parser/internal/pratt_parser_worker.h b/parser/internal/pratt_parser_worker.h index e1608a3a2..2cfe84b79 100644 --- a/parser/internal/pratt_parser_worker.h +++ b/parser/internal/pratt_parser_worker.h @@ -1222,7 +1222,7 @@ int PrattParserWorker::CountGroupingParentheses() { } // Save lexer position to restore after scanning ahead. - const Lexer::Position saved_pos = lexer_.SavePosition(); + const int32_t saved_pos = lexer_.SavePosition(); auto restore_lexer = absl::MakeCleanup( [this, saved_pos] { lexer_.RestorePosition(saved_pos); });