Skip to content

Commit 00ba73d

Browse files
authored
Merge pull request #4288 from yt2b/refactor_lexer
Refactor lexer functions
2 parents 3660ac2 + a79fb01 commit 00ba73d

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

compiler/parser/src/lexer.rs

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -293,27 +293,26 @@ where
293293
/// Numeric lexing. The feast can start!
294294
fn lex_number(&mut self) -> LexResult {
295295
let start_pos = self.get_pos();
296-
if self.window[0] == Some('0') {
297-
if matches!(self.window[1], Some('x' | 'X')) {
296+
match (self.window[0], self.window[1]) {
297+
(Some('0'), Some('x' | 'X')) => {
298298
// Hex! (0xdeadbeef)
299299
self.next_char();
300300
self.next_char();
301301
self.lex_number_radix(start_pos, 16)
302-
} else if matches!(self.window[1], Some('o' | 'O')) {
302+
}
303+
(Some('0'), Some('o' | 'O')) => {
303304
// Octal style! (0o377)
304305
self.next_char();
305306
self.next_char();
306307
self.lex_number_radix(start_pos, 8)
307-
} else if matches!(self.window[1], Some('b' | 'B')) {
308+
}
309+
(Some('0'), Some('b' | 'B')) => {
308310
// Binary! (0b_1110_0101)
309311
self.next_char();
310312
self.next_char();
311313
self.lex_number_radix(start_pos, 2)
312-
} else {
313-
self.lex_normal_number()
314314
}
315-
} else {
316-
self.lex_normal_number()
315+
_ => self.lex_normal_number(),
317316
}
318317
}
319318

@@ -439,11 +438,7 @@ where
439438
fn take_number(&mut self, radix: u32) -> Option<char> {
440439
let take_char = Lexer::<T>::is_digit_of_radix(self.window[0], radix);
441440

442-
if take_char {
443-
Some(self.next_char().unwrap())
444-
} else {
445-
None
446-
}
441+
take_char.then(|| self.next_char().unwrap())
447442
}
448443

449444
/// Test if a digit is of a certain radix.
@@ -459,12 +454,9 @@ where
459454

460455
/// Test if we face '[eE][-+]?[0-9]+'
461456
fn at_exponent(&self) -> bool {
462-
match self.window[0] {
463-
Some('e' | 'E') => match self.window[1] {
464-
Some('+' | '-') => matches!(self.window[2], Some('0'..='9')),
465-
Some('0'..='9') => true,
466-
_ => false,
467-
},
457+
match (self.window[0], self.window[1]) {
458+
(Some('e' | 'E'), Some('+' | '-')) => matches!(self.window[2], Some('0'..='9')),
459+
(Some('e' | 'E'), Some('0'..='9')) => true,
468460
_ => false,
469461
}
470462
}
@@ -705,13 +697,10 @@ where
705697
}
706698

707699
fn is_identifier_continuation(&self) -> bool {
708-
if let Some(c) = self.window[0] {
709-
match c {
710-
'_' | '0'..='9' => true,
711-
c => is_xid_continue(c),
712-
}
713-
} else {
714-
false
700+
match self.window[0] {
701+
Some('_' | '0'..='9') => true,
702+
Some(c) => is_xid_continue(c),
703+
_ => false,
715704
}
716705
}
717706

0 commit comments

Comments
 (0)