mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:24 +00:00
Add eat_char2
for the lexer (#6968)
## Summary This PR adds a new helper method on the `Cursor` called `eat_char2` which is similar to `eat_char` but accepts 2 characters instead of 1. It'll `bump` the cursor twice if both characters are found on lookahead. ## Test Plan `cargo test`
This commit is contained in:
parent
715d86dae9
commit
4d49d5e845
2 changed files with 14 additions and 13 deletions
|
@ -515,13 +515,7 @@ impl<'source> Lexer<'source> {
|
||||||
|
|
||||||
// If the next two characters are also the quote character, then we have a triple-quoted
|
// If the next two characters are also the quote character, then we have a triple-quoted
|
||||||
// string; consume those two characters and ensure that we require a triple-quote to close
|
// string; consume those two characters and ensure that we require a triple-quote to close
|
||||||
let triple_quoted = if self.cursor.first() == quote && self.cursor.second() == quote {
|
let triple_quoted = self.cursor.eat_char2(quote, quote);
|
||||||
self.cursor.bump();
|
|
||||||
self.cursor.bump();
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
};
|
|
||||||
|
|
||||||
let value_start = self.offset();
|
let value_start = self.offset();
|
||||||
|
|
||||||
|
@ -544,9 +538,7 @@ impl<'source> Lexer<'source> {
|
||||||
}
|
}
|
||||||
Some(c) if c == quote => {
|
Some(c) if c == quote => {
|
||||||
if triple_quoted {
|
if triple_quoted {
|
||||||
if self.cursor.first() == quote && self.cursor.second() == quote {
|
if self.cursor.eat_char2(quote, quote) {
|
||||||
self.cursor.bump();
|
|
||||||
self.cursor.bump();
|
|
||||||
break self.offset() - TextSize::new(3);
|
break self.offset() - TextSize::new(3);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -918,9 +910,7 @@ impl<'source> Lexer<'source> {
|
||||||
'.' => {
|
'.' => {
|
||||||
if self.cursor.first().is_ascii_digit() {
|
if self.cursor.first().is_ascii_digit() {
|
||||||
self.lex_decimal_number('.')?
|
self.lex_decimal_number('.')?
|
||||||
} else if self.cursor.first() == '.' && self.cursor.second() == '.' {
|
} else if self.cursor.eat_char2('.', '.') {
|
||||||
self.cursor.bump();
|
|
||||||
self.cursor.bump();
|
|
||||||
Tok::Ellipsis
|
Tok::Ellipsis
|
||||||
} else {
|
} else {
|
||||||
Tok::Dot
|
Tok::Dot
|
||||||
|
|
|
@ -85,6 +85,17 @@ impl<'a> Cursor<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super) fn eat_char2(&mut self, c1: char, c2: char) -> bool {
|
||||||
|
let mut chars = self.chars.clone();
|
||||||
|
if chars.next() == Some(c1) && chars.next() == Some(c2) {
|
||||||
|
self.bump();
|
||||||
|
self.bump();
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(super) fn eat_if<F>(&mut self, mut predicate: F) -> Option<char>
|
pub(super) fn eat_if<F>(&mut self, mut predicate: F) -> Option<char>
|
||||||
where
|
where
|
||||||
F: FnMut(char) -> bool,
|
F: FnMut(char) -> bool,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue