Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cursor offset for lexer checkpoint #11734

Merged
merged 1 commit into from
Jun 4, 2024
Merged
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
16 changes: 10 additions & 6 deletions crates/ruff_python_parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1338,13 +1338,13 @@ impl<'src> Lexer<'src> {
}

/// Creates a checkpoint to which the lexer can later return to using [`Self::rewind`].
pub(crate) fn checkpoint(&self) -> LexerCheckpoint<'src> {
pub(crate) fn checkpoint(&self) -> LexerCheckpoint {
LexerCheckpoint {
value: self.current_value.clone(),
current_kind: self.current_kind,
current_range: self.current_range,
current_flags: self.current_flags,
cursor: self.cursor.clone(),
cursor_offset: self.offset(),
state: self.state,
nesting: self.nesting,
indentations_checkpoint: self.indentations.checkpoint(),
Expand All @@ -1355,13 +1355,13 @@ impl<'src> Lexer<'src> {
}

/// Restore the lexer to the given checkpoint.
pub(crate) fn rewind(&mut self, checkpoint: LexerCheckpoint<'src>) {
pub(crate) fn rewind(&mut self, checkpoint: LexerCheckpoint) {
let LexerCheckpoint {
value,
current_kind,
current_range,
current_flags,
cursor,
cursor_offset,
state,
nesting,
indentations_checkpoint,
Expand All @@ -1370,6 +1370,10 @@ impl<'src> Lexer<'src> {
errors_position,
} = checkpoint;

let mut cursor = Cursor::new(self.source);
// We preserve the previous char using this method.
cursor.skip_bytes(cursor_offset.to_usize());

Comment on lines +1373 to +1376
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use Cursor::new(self.source[cursor_offset.to_usize()..] but that would always set the previous character to EOF.

self.current_value = value;
self.current_kind = current_kind;
self.current_range = current_range;
Expand Down Expand Up @@ -1700,12 +1704,12 @@ pub(crate) enum TokenValue {
},
}

pub(crate) struct LexerCheckpoint<'src> {
pub(crate) struct LexerCheckpoint {
value: TokenValue,
current_kind: TokenKind,
current_range: TextRange,
current_flags: TokenFlags,
cursor: Cursor<'src>,
cursor_offset: TextSize,
state: State,
nesting: u32,
indentations_checkpoint: IndentationsCheckpoint,
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff_python_parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ impl<'src> Parser<'src> {
}

/// Creates a checkpoint to which the parser can later return to using [`Self::rewind`].
fn checkpoint(&self) -> ParserCheckpoint<'src> {
fn checkpoint(&self) -> ParserCheckpoint {
ParserCheckpoint {
tokens: self.tokens.checkpoint(),
errors_position: self.errors.len(),
Expand All @@ -620,7 +620,7 @@ impl<'src> Parser<'src> {
}

/// Restore the parser to the given checkpoint.
fn rewind(&mut self, checkpoint: ParserCheckpoint<'src>) {
fn rewind(&mut self, checkpoint: ParserCheckpoint) {
let ParserCheckpoint {
tokens,
errors_position,
Expand All @@ -637,8 +637,8 @@ impl<'src> Parser<'src> {
}
}

struct ParserCheckpoint<'src> {
tokens: TokenSourceCheckpoint<'src>,
struct ParserCheckpoint {
tokens: TokenSourceCheckpoint,
errors_position: usize,
current_token_id: TokenId,
prev_token_end: TextSize,
Expand Down
8 changes: 4 additions & 4 deletions crates/ruff_python_parser/src/token_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<'src> TokenSource<'src> {
}

/// Creates a checkpoint to which the token source can later return to using [`Self::rewind`].
pub(crate) fn checkpoint(&self) -> TokenSourceCheckpoint<'src> {
pub(crate) fn checkpoint(&self) -> TokenSourceCheckpoint {
TokenSourceCheckpoint {
lexer_checkpoint: self.lexer.checkpoint(),
tokens_position: self.tokens.len(),
Expand All @@ -135,7 +135,7 @@ impl<'src> TokenSource<'src> {
}

/// Restore the token source to the given checkpoint.
pub(crate) fn rewind(&mut self, checkpoint: TokenSourceCheckpoint<'src>) {
pub(crate) fn rewind(&mut self, checkpoint: TokenSourceCheckpoint) {
let TokenSourceCheckpoint {
lexer_checkpoint,
tokens_position,
Expand Down Expand Up @@ -168,8 +168,8 @@ impl<'src> TokenSource<'src> {
}
}

pub(crate) struct TokenSourceCheckpoint<'src> {
lexer_checkpoint: LexerCheckpoint<'src>,
pub(crate) struct TokenSourceCheckpoint {
lexer_checkpoint: LexerCheckpoint,
tokens_position: usize,
comments_position: usize,
}
Expand Down
Loading