Skip to content

Commit

Permalink
Clone the entire stack for lexer checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed May 17, 2024
1 parent 6dad12f commit dcab2ef
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
9 changes: 2 additions & 7 deletions crates/ruff_python_parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ impl<'src> Lexer<'src> {
TokenKind::Comment
}

/// Lex a single `IPython` escape command.
/// Lex a single IPython escape command.
fn lex_ipython_escape_command(&mut self, escape_kind: IpyEscapeKind) -> TokenKind {
let mut value = String::new();

Expand Down Expand Up @@ -1341,11 +1341,6 @@ impl<'src> Lexer<'src> {
}

/// Restore the lexer to the given checkpoint.
///
/// # Panics
///
/// If the current indentation is less than the indentation at the checkpoint
/// If the lexer is out of any f-strings it was in at the time of checkpoint
pub(crate) fn rewind(&mut self, checkpoint: LexerCheckpoint<'src>) {
self.value = checkpoint.value;
self.current = checkpoint.current;
Expand Down Expand Up @@ -1554,7 +1549,7 @@ pub(crate) enum TokenValue {
/// and prefixes of the string
flags: AnyStringFlags,
},
/// Token value for `IPython` escape commands. These are recognized by the lexer
/// Token value for IPython escape commands. These are recognized by the lexer
/// only when the mode is [`Mode::Ipython`].
IpyEscapeCommand {
/// The magic command value.
Expand Down
12 changes: 5 additions & 7 deletions crates/ruff_python_parser/src/lexer/fstring.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ruff_python_ast::AnyStringFlags;

/// The context representing the current f-string that the lexer is in.
#[derive(Debug)]
#[derive(Clone, Debug)]
pub(crate) struct FStringContext {
flags: AnyStringFlags,

Expand Down Expand Up @@ -129,15 +129,13 @@ impl FStrings {
}

pub(crate) fn checkpoint(&self) -> FStringsCheckpoint {
FStringsCheckpoint(self.stack.len())
FStringsCheckpoint(self.stack.clone())
}

pub(crate) fn rewind(&mut self, checkpoint: FStringsCheckpoint) {
assert!(self.stack.len() <= checkpoint.0);

self.stack.truncate(checkpoint.0);
self.stack = checkpoint.0;
}
}

#[derive(Debug, Copy, Clone)]
pub(crate) struct FStringsCheckpoint(usize);
#[derive(Debug, Clone)]
pub(crate) struct FStringsCheckpoint(Vec<FStringContext>);
10 changes: 4 additions & 6 deletions crates/ruff_python_parser/src/lexer/indentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,16 @@ impl Indentations {
}

pub(crate) fn checkpoint(&self) -> IndentationsCheckpoint {
IndentationsCheckpoint(self.stack.len())
IndentationsCheckpoint(self.stack.clone())
}

pub(crate) fn rewind(&mut self, checkpoint: IndentationsCheckpoint) {
assert!(self.stack.len() <= checkpoint.0);

self.stack.truncate(checkpoint.0);
self.stack = checkpoint.0;
}
}

#[derive(Debug, Copy, Clone)]
pub(crate) struct IndentationsCheckpoint(usize);
#[derive(Debug, Clone)]
pub(crate) struct IndentationsCheckpoint(Vec<Indentation>);

assert_eq_size!(Indentation, u64);

Expand Down

0 comments on commit dcab2ef

Please sign in to comment.