mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-31 00:44:19 +00:00
Refactor lexer functions
This commit is contained in:
parent
1fe79331f3
commit
6ae42e249b
1 changed files with 17 additions and 25 deletions
|
@ -293,27 +293,26 @@ where
|
||||||
/// Numeric lexing. The feast can start!
|
/// Numeric lexing. The feast can start!
|
||||||
fn lex_number(&mut self) -> LexResult {
|
fn lex_number(&mut self) -> LexResult {
|
||||||
let start_pos = self.get_pos();
|
let start_pos = self.get_pos();
|
||||||
if self.window[0] == Some('0') {
|
match (self.window[0], self.window[1]) {
|
||||||
if matches!(self.window[1], Some('x' | 'X')) {
|
(Some('0'), Some('x' | 'X')) => {
|
||||||
// Hex! (0xdeadbeef)
|
// Hex! (0xdeadbeef)
|
||||||
self.next_char();
|
self.next_char();
|
||||||
self.next_char();
|
self.next_char();
|
||||||
self.lex_number_radix(start_pos, 16)
|
self.lex_number_radix(start_pos, 16)
|
||||||
} else if matches!(self.window[1], Some('o' | 'O')) {
|
}
|
||||||
|
(Some('0'), Some('o' | 'O')) => {
|
||||||
// Octal style! (0o377)
|
// Octal style! (0o377)
|
||||||
self.next_char();
|
self.next_char();
|
||||||
self.next_char();
|
self.next_char();
|
||||||
self.lex_number_radix(start_pos, 8)
|
self.lex_number_radix(start_pos, 8)
|
||||||
} else if matches!(self.window[1], Some('b' | 'B')) {
|
}
|
||||||
|
(Some('0'), Some('b' | 'B')) => {
|
||||||
// Binary! (0b_1110_0101)
|
// Binary! (0b_1110_0101)
|
||||||
self.next_char();
|
self.next_char();
|
||||||
self.next_char();
|
self.next_char();
|
||||||
self.lex_number_radix(start_pos, 2)
|
self.lex_number_radix(start_pos, 2)
|
||||||
} else {
|
|
||||||
self.lex_normal_number()
|
|
||||||
}
|
}
|
||||||
} else {
|
_ => self.lex_normal_number(),
|
||||||
self.lex_normal_number()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -439,10 +438,9 @@ where
|
||||||
fn take_number(&mut self, radix: u32) -> Option<char> {
|
fn take_number(&mut self, radix: u32) -> Option<char> {
|
||||||
let take_char = Lexer::<T>::is_digit_of_radix(self.window[0], radix);
|
let take_char = Lexer::<T>::is_digit_of_radix(self.window[0], radix);
|
||||||
|
|
||||||
if take_char {
|
match take_char {
|
||||||
Some(self.next_char().unwrap())
|
true => Some(self.next_char().unwrap()),
|
||||||
} else {
|
_ => None,
|
||||||
None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -459,12 +457,9 @@ where
|
||||||
|
|
||||||
/// Test if we face '[eE][-+]?[0-9]+'
|
/// Test if we face '[eE][-+]?[0-9]+'
|
||||||
fn at_exponent(&self) -> bool {
|
fn at_exponent(&self) -> bool {
|
||||||
match self.window[0] {
|
match (self.window[0], self.window[1]) {
|
||||||
Some('e' | 'E') => match self.window[1] {
|
(Some('e' | 'E'), Some('+' | '-')) => matches!(self.window[2], Some('0'..='9')),
|
||||||
Some('+' | '-') => matches!(self.window[2], Some('0'..='9')),
|
(Some('e' | 'E'), Some('0'..='9')) => true,
|
||||||
Some('0'..='9') => true,
|
|
||||||
_ => false,
|
|
||||||
},
|
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -705,13 +700,10 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_identifier_continuation(&self) -> bool {
|
fn is_identifier_continuation(&self) -> bool {
|
||||||
if let Some(c) = self.window[0] {
|
match self.window[0] {
|
||||||
match c {
|
Some('_' | '0'..='9') => true,
|
||||||
'_' | '0'..='9' => true,
|
Some(c) => is_xid_continue(c),
|
||||||
c => is_xid_continue(c),
|
_ => false,
|
||||||
}
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue