Skip to content

Commit 9a60754

Browse files
committed
make suggested changes
- factor out code that determines whether the string beggining with `e_` is an exponent or the start of the suffix - reduce use of mutable state
1 parent ec2246b commit 9a60754

File tree

1 file changed

+50
-49
lines changed
  • compiler/rustc_lexer/src

1 file changed

+50
-49
lines changed

compiler/rustc_lexer/src/lib.rs

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -659,53 +659,30 @@ impl Cursor<'_> {
659659
// might have stuff after the ., and if it does, it needs to start
660660
// with a number
661661
self.bump();
662+
if !self.first().is_ascii_digit() {
663+
return (Float { base, empty_exponent: false }, None);
664+
}
665+
self.eat_decimal_digits();
666+
662667
let mut empty_exponent = false;
663-
let mut suffix_start = None;
664-
if self.first().is_ascii_digit() {
665-
self.eat_decimal_digits();
666-
// This will be the start of the suffix if there is no exponent
667-
suffix_start = Some(self.pos_within_token());
668-
match (self.first(), self.second()) {
669-
('e' | 'E', '_') => {
670-
// check if series of `_` is ended by a digit. If yes
671-
// include it in the number as exponent. If no include
672-
// it in suffix.
673-
self.bump();
674-
while matches!(self.first(), '_') {
675-
self.bump();
676-
}
677-
// If we find a digit, then the exponential was valid
678-
// so the suffix will start at the cursor as usual.
679-
if self.first().is_ascii_digit() {
680-
self.eat_decimal_digits();
681-
suffix_start = None;
682-
}
683-
}
684-
('e' | 'E', '0'..='9' | '+' | '-') => {
685-
// Definitely an exponent (which still can be empty).
686-
self.bump();
687-
empty_exponent = !self.eat_float_exponent();
688-
suffix_start = None;
689-
}
690-
_ => (),
668+
let suffix_start = match (self.first(), self.second()) {
669+
('e' | 'E', '_') => self.eat_underscore_exponent(),
670+
('e' | 'E', '0'..='9' | '+' | '-') => {
671+
// Definitely an exponent (which still can be empty).
672+
self.bump();
673+
empty_exponent = !self.eat_float_exponent();
674+
None
691675
}
692-
}
676+
_ => Some(self.pos_within_token()),
677+
};
693678
(Float { base, empty_exponent }, suffix_start)
694679
}
695680
('e' | 'E', '_') => {
696-
// See above block for similar approach.
697-
let non_exponent_suffix_start = self.pos_within_token();
698-
self.bump();
699-
while matches!(self.first(), '_') {
700-
self.bump();
701-
}
702-
if self.first().is_ascii_digit() {
703-
self.eat_decimal_digits();
704-
(Float { base, empty_exponent: false }, None)
681+
if let Some(suffix_start) = self.eat_underscore_exponent() {
682+
// The suffix begins at `e`, meaning the number is an integer.
683+
(Int { base, empty_int: false }, Some(suffix_start))
705684
} else {
706-
// No digit means the suffix begins at `e`, meaning the number
707-
// is an integer.
708-
(Int { base, empty_int: false }, Some(non_exponent_suffix_start))
685+
(Float { base, empty_exponent: false }, None)
709686
}
710687
}
711688
('e' | 'E', '0'..='9' | '+' | '-') => {
@@ -718,6 +695,35 @@ impl Cursor<'_> {
718695
}
719696
}
720697

698+
/// Try to find and eat an exponent
699+
///
700+
/// Assumes the first character is `e`/`E` and second is `_`, and consumes
701+
/// `e`/`E` followed by all consecutive `_`s.
702+
///
703+
/// Returns `Some` if no exponent was found. In this case, the suffix is partially
704+
/// consumed, and began at the return value.
705+
fn eat_underscore_exponent(&mut self) -> Option<u32> {
706+
debug_assert!(matches!(self.first(), 'e' | 'E'));
707+
debug_assert!(matches!(self.second(), '_'));
708+
let suffix_start = self.pos_within_token();
709+
710+
// check if series of `_` is ended by a digit. If yes
711+
// include it in the number as exponent. If no include
712+
// it in suffix.
713+
self.bump();
714+
while matches!(self.first(), '_') {
715+
self.bump();
716+
}
717+
// If we find a digit, then the exponential was valid
718+
// so the suffix will start at the cursor as usual.
719+
if self.first().is_ascii_digit() {
720+
self.eat_decimal_digits();
721+
None
722+
} else {
723+
Some(suffix_start)
724+
}
725+
}
726+
721727
fn lifetime_or_char(&mut self) -> TokenKind {
722728
debug_assert!(self.prev() == '\'');
723729

@@ -1009,23 +1015,18 @@ impl Cursor<'_> {
10091015
}
10101016

10111017
/// Eats the suffix of the literal, e.g. "u8".
1012-
///
1013-
/// Returns `true` if anything was eaten.
1014-
fn eat_literal_suffix(&mut self) -> bool {
1018+
fn eat_literal_suffix(&mut self) {
10151019
self.eat_identifier()
10161020
}
10171021

10181022
/// Eats the identifier. Note: succeeds on `_`, which isn't a valid
10191023
/// identifier.
1020-
///
1021-
/// Returns `true` if anything was eaten.
1022-
fn eat_identifier(&mut self) -> bool {
1024+
fn eat_identifier(&mut self) {
10231025
if !is_id_start(self.first()) {
1024-
return false;
1026+
return;
10251027
}
10261028
self.bump();
10271029

10281030
self.eat_while(is_id_continue);
1029-
true
10301031
}
10311032
}

0 commit comments

Comments
 (0)