Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions parser/internal/lexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand Down
28 changes: 6 additions & 22 deletions parser/internal/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,6 @@ class Lexer final {
std::numeric_limits<int32_t>::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;
Expand All @@ -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<int32_t>(content_.size()));
position_ = position;
error_ = LexerError{};
}

private:
Expand Down Expand Up @@ -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};
}

Expand All @@ -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.
Expand Down Expand Up @@ -294,8 +280,6 @@ class Lexer final {

cel::SourceContentView content_;
int32_t position_ = 0;
bool at_end_ = false;
bool done_ = false;
LexerError error_;
};

Expand Down
3 changes: 2 additions & 1 deletion parser/internal/lexer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "parser/internal/lexer.h"

#include <cstdint>
#include <string>
#include <string_view>
#include <utility>
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion parser/internal/pratt_parser_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ int PrattParserWorker<ExprNode>::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); });

Expand Down
Loading