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

Commit 67ee8a4

Browse files
committed
Use a boolean flag for "Is last token an Equal"?
Cloning is expensive especially for every token.
1 parent e16b845 commit 67ee8a4

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

parser/src/lexer.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ pub struct Lexer<T: Iterator<Item = char>> {
175175
pending: Vec<Spanned>,
176176
// The current location.
177177
location: TextSize,
178-
// The last emitted token.
179-
last_emitted: Option<Tok>,
178+
// Is the last token an equal sign?
179+
last_token_is_equal: bool,
180180
// Lexer mode.
181181
mode: Mode,
182182
}
@@ -235,7 +235,7 @@ where
235235
pending: Vec::with_capacity(5),
236236
location: start,
237237
window: CharWindow::new(input),
238-
last_emitted: None,
238+
last_token_is_equal: false,
239239
mode,
240240
};
241241
// Fill the window.
@@ -948,10 +948,7 @@ where
948948
}
949949
}
950950
'%' => {
951-
if self.mode == Mode::Jupyter
952-
&& self.nesting == 0
953-
&& matches!(self.last_emitted, Some(Tok::Equal))
954-
{
951+
if self.mode == Mode::Jupyter && self.nesting == 0 && self.last_token_is_equal {
955952
self.lex_and_emit_magic_command();
956953
} else {
957954
let tok_start = self.get_pos();
@@ -1035,10 +1032,7 @@ where
10351032
}
10361033
}
10371034
'!' => {
1038-
if self.mode == Mode::Jupyter
1039-
&& self.nesting == 0
1040-
&& matches!(self.last_emitted, Some(Tok::Equal))
1041-
{
1035+
if self.mode == Mode::Jupyter && self.nesting == 0 && self.last_token_is_equal {
10421036
self.lex_and_emit_magic_command();
10431037
} else {
10441038
let tok_start = self.get_pos();
@@ -1309,7 +1303,7 @@ where
13091303

13101304
// Helper function to emit a lexed token to the queue of tokens.
13111305
fn emit(&mut self, spanned: Spanned) {
1312-
self.last_emitted = Some(spanned.0.clone());
1306+
self.last_token_is_equal = matches!(spanned.0, Tok::Equal);
13131307
self.pending.push(spanned);
13141308
}
13151309
}

0 commit comments

Comments
 (0)