Skip to content

Commit

Permalink
Rollup merge of #111584 - nnethercote:number-lexing-tweaks, r=matklad
Browse files Browse the repository at this point in the history
Number lexing tweaks

A couple of improvements to things that puzzled me when I was looking at this code.

r? `@matklad`
  • Loading branch information
matthiaskrgr authored May 15, 2023
2 parents 2f0b456 + e52794d commit 119b722
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions compiler/rustc_lexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,34 +582,38 @@ impl Cursor<'_> {
let mut base = Base::Decimal;
if first_digit == '0' {
// Attempt to parse encoding base.
let has_digits = match self.first() {
match self.first() {
'b' => {
base = Base::Binary;
self.bump();
self.eat_decimal_digits()
if !self.eat_decimal_digits() {
return Int { base, empty_int: true };
}
}
'o' => {
base = Base::Octal;
self.bump();
self.eat_decimal_digits()
if !self.eat_decimal_digits() {
return Int { base, empty_int: true };
}
}
'x' => {
base = Base::Hexadecimal;
self.bump();
self.eat_hexadecimal_digits()
if !self.eat_hexadecimal_digits() {
return Int { base, empty_int: true };
}
}
// Not a base prefix.
'0'..='9' | '_' | '.' | 'e' | 'E' => {
// Not a base prefix; consume additional digits.
'0'..='9' | '_' => {
self.eat_decimal_digits();
true
}

// Also not a base prefix; nothing more to do here.
'.' | 'e' | 'E' => {}

// Just a 0.
_ => return Int { base, empty_int: false },
};
// Base prefix was provided, but there were no digits
// after it, e.g. "0x".
if !has_digits {
return Int { base, empty_int: true };
}
} else {
// No base prefix, parse number in the usual way.
Expand Down

0 comments on commit 119b722

Please sign in to comment.