Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
Use a boolean flag for "Is last token an Equal"?
Browse files Browse the repository at this point in the history
Cloning is expensive especially for every token.
  • Loading branch information
dhruvmanila committed Jul 21, 2023
1 parent e16b845 commit 67ee8a4
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ pub struct Lexer<T: Iterator<Item = char>> {
pending: Vec<Spanned>,
// The current location.
location: TextSize,
// The last emitted token.
last_emitted: Option<Tok>,
// Is the last token an equal sign?
last_token_is_equal: bool,
// Lexer mode.
mode: Mode,
}
Expand Down Expand Up @@ -235,7 +235,7 @@ where
pending: Vec::with_capacity(5),
location: start,
window: CharWindow::new(input),
last_emitted: None,
last_token_is_equal: false,
mode,
};
// Fill the window.
Expand Down Expand Up @@ -948,10 +948,7 @@ where
}
}
'%' => {
if self.mode == Mode::Jupyter
&& self.nesting == 0
&& matches!(self.last_emitted, Some(Tok::Equal))
{
if self.mode == Mode::Jupyter && self.nesting == 0 && self.last_token_is_equal {
self.lex_and_emit_magic_command();
} else {
let tok_start = self.get_pos();
Expand Down Expand Up @@ -1035,10 +1032,7 @@ where
}
}
'!' => {
if self.mode == Mode::Jupyter
&& self.nesting == 0
&& matches!(self.last_emitted, Some(Tok::Equal))
{
if self.mode == Mode::Jupyter && self.nesting == 0 && self.last_token_is_equal {
self.lex_and_emit_magic_command();
} else {
let tok_start = self.get_pos();
Expand Down Expand Up @@ -1309,7 +1303,7 @@ where

// Helper function to emit a lexed token to the queue of tokens.
fn emit(&mut self, spanned: Spanned) {
self.last_emitted = Some(spanned.0.clone());
self.last_token_is_equal = matches!(spanned.0, Tok::Equal);
self.pending.push(spanned);
}
}
Expand Down

0 comments on commit 67ee8a4

Please sign in to comment.