Skip to content

Commit 36ec2e4

Browse files
authored
Merge pull request RustPython#4413 from harupy/more-generic-window-impl
Update `CharWindow` in `compiler/parser/src/lexer.rs` to allow slicing
2 parents 8f87dbc + 57b87c6 commit 36ec2e4

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

compiler/parser/src/lexer.rs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ where
124124
impl<T, const N: usize, Idx> Index<Idx> for CharWindow<T, N>
125125
where
126126
T: Iterator<Item = char>,
127-
Idx: SliceIndex<[Option<char>], Output = Option<char>>,
127+
Idx: SliceIndex<[Option<char>]>,
128128
{
129-
type Output = Option<char>;
129+
type Output = Idx::Output;
130130

131131
fn index(&self, index: Idx) -> &Self::Output {
132-
self.window.index(index)
132+
&self.window[index]
133133
}
134134
}
135135

@@ -199,12 +199,12 @@ where
199199
fn next(&mut self) -> Option<Self::Item> {
200200
// Collapse \r\n into \n
201201
loop {
202-
match (self.window[0], self.window[1]) {
203-
(Some('\r'), Some('\n')) => {
202+
match self.window[..2] {
203+
[Some('\r'), Some('\n')] => {
204204
// Windows EOL into \n
205205
self.shift();
206206
}
207-
(Some('\r'), _) => {
207+
[Some('\r'), _] => {
208208
// MAC EOL into \n
209209
self.window.change_first('\n');
210210
}
@@ -309,20 +309,20 @@ where
309309
/// Numeric lexing. The feast can start!
310310
fn lex_number(&mut self) -> LexResult {
311311
let start_pos = self.get_pos();
312-
match (self.window[0], self.window[1]) {
313-
(Some('0'), Some('x' | 'X')) => {
312+
match self.window[..2] {
313+
[Some('0'), Some('x' | 'X')] => {
314314
// Hex! (0xdeadbeef)
315315
self.next_char();
316316
self.next_char();
317317
self.lex_number_radix(start_pos, 16)
318318
}
319-
(Some('0'), Some('o' | 'O')) => {
319+
[Some('0'), Some('o' | 'O')] => {
320320
// Octal style! (0o377)
321321
self.next_char();
322322
self.next_char();
323323
self.lex_number_radix(start_pos, 8)
324324
}
325-
(Some('0'), Some('b' | 'B')) => {
325+
[Some('0'), Some('b' | 'B')] => {
326326
// Binary! (0b_1110_0101)
327327
self.next_char();
328328
self.next_char();
@@ -470,9 +470,9 @@ where
470470

471471
/// Test if we face '[eE][-+]?[0-9]+'
472472
fn at_exponent(&self) -> bool {
473-
match (self.window[0], self.window[1]) {
474-
(Some('e' | 'E'), Some('+' | '-')) => matches!(self.window[2], Some('0'..='9')),
475-
(Some('e' | 'E'), Some('0'..='9')) => true,
473+
match self.window[..2] {
474+
[Some('e' | 'E'), Some('+' | '-')] => matches!(self.window[2], Some('0'..='9')),
475+
[Some('e' | 'E'), Some('0'..='9')] => true,
476476
_ => false,
477477
}
478478
}
@@ -500,14 +500,13 @@ where
500500

501501
// If the next two characters are also the quote character, then we have a triple-quoted
502502
// string; consume those two characters and ensure that we require a triple-quote to close
503-
let triple_quoted =
504-
if self.window[0] == Some(quote_char) && self.window[1] == Some(quote_char) {
505-
self.next_char();
506-
self.next_char();
507-
true
508-
} else {
509-
false
510-
};
503+
let triple_quoted = if self.window[..2] == [Some(quote_char); 2] {
504+
self.next_char();
505+
self.next_char();
506+
true
507+
} else {
508+
false
509+
};
511510

512511
loop {
513512
match self.next_char() {
@@ -534,9 +533,7 @@ where
534533
// Look ahead at the next two characters; if we have two more
535534
// quote_chars, it's the end of the string; consume the remaining
536535
// closing quotes and break the loop
537-
if self.window[0] == Some(quote_char)
538-
&& self.window[1] == Some(quote_char)
539-
{
536+
if self.window[..2] == [Some(quote_char); 2] {
540537
self.next_char();
541538
self.next_char();
542539
break;
@@ -1094,7 +1091,7 @@ where
10941091
} else {
10951092
let tok_start = self.get_pos();
10961093
self.next_char();
1097-
if let (Some('.'), Some('.')) = (&self.window[0], &self.window[1]) {
1094+
if self.window[..2] == [Some('.'); 2] {
10981095
self.next_char();
10991096
self.next_char();
11001097
let tok_end = self.get_pos();

0 commit comments

Comments
 (0)