Skip to content

Commit a534bd7

Browse files
author
Julian Wollersberger
committed
Move the eat_*_digits() methods to literals.rs.
1 parent 59583ac commit a534bd7

File tree

3 files changed

+74
-74
lines changed

3 files changed

+74
-74
lines changed

compiler/rustc_lexer/src/cursor.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,11 @@ impl<'a> Cursor<'a> {
8181

8282
Some(c)
8383
}
84+
85+
/// Eats symbols while predicate returns true or until the end of file is reached.
86+
pub(crate) fn eat_while(&mut self, mut predicate: impl FnMut(char) -> bool) {
87+
while predicate(self.first()) && !self.is_eof() {
88+
self.bump();
89+
}
90+
}
8491
}

compiler/rustc_lexer/src/lib.rs

Lines changed: 9 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ mod tests;
3131
use self::TokenKind::*;
3232
use crate::cursor::Cursor;
3333
use crate::literals::{
34-
double_quoted_string, lifetime_or_char, number, raw_double_quoted_string, single_quoted_string,
35-
LiteralKind,
34+
double_quoted_string, eat_literal_suffix, lifetime_or_char, number, raw_double_quoted_string,
35+
single_quoted_string, LiteralKind,
3636
};
3737

3838
/// Parsed token.
@@ -272,7 +272,7 @@ impl Cursor<'_> {
272272
let (n_hashes, err) = raw_double_quoted_string(self, 1);
273273
let suffix_start = self.len_consumed();
274274
if err.is_none() {
275-
self.eat_literal_suffix();
275+
eat_literal_suffix(self);
276276
}
277277
let kind = LiteralKind::RawStr { n_hashes, err };
278278
Literal { kind, suffix_start }
@@ -287,7 +287,7 @@ impl Cursor<'_> {
287287
let terminated = single_quoted_string(self);
288288
let suffix_start = self.len_consumed();
289289
if terminated {
290-
self.eat_literal_suffix();
290+
eat_literal_suffix(self);
291291
}
292292
let kind = LiteralKind::Byte { terminated };
293293
Literal { kind, suffix_start }
@@ -297,7 +297,7 @@ impl Cursor<'_> {
297297
let terminated = double_quoted_string(self);
298298
let suffix_start = self.len_consumed();
299299
if terminated {
300-
self.eat_literal_suffix();
300+
eat_literal_suffix(self);
301301
}
302302
let kind = LiteralKind::ByteStr { terminated };
303303
Literal { kind, suffix_start }
@@ -307,7 +307,7 @@ impl Cursor<'_> {
307307
let (n_hashes, err) = raw_double_quoted_string(self, 2);
308308
let suffix_start = self.len_consumed();
309309
if err.is_none() {
310-
self.eat_literal_suffix();
310+
eat_literal_suffix(self);
311311
}
312312
let kind = LiteralKind::RawByteStr { n_hashes, err };
313313
Literal { kind, suffix_start }
@@ -323,7 +323,7 @@ impl Cursor<'_> {
323323
c @ '0'..='9' => {
324324
let literal_kind = number(self, c);
325325
let suffix_start = self.len_consumed();
326-
self.eat_literal_suffix();
326+
eat_literal_suffix(self);
327327
TokenKind::Literal { kind: literal_kind, suffix_start }
328328
}
329329

@@ -363,7 +363,7 @@ impl Cursor<'_> {
363363
let terminated = double_quoted_string(self);
364364
let suffix_start = self.len_consumed();
365365
if terminated {
366-
self.eat_literal_suffix();
366+
eat_literal_suffix(self);
367367
}
368368
let kind = LiteralKind::Str { terminated };
369369
Literal { kind, suffix_start }
@@ -448,56 +448,7 @@ impl Cursor<'_> {
448448
Ident
449449
}
450450

451-
fn eat_decimal_digits(&mut self) -> bool {
452-
let mut has_digits = false;
453-
loop {
454-
match self.first() {
455-
'_' => {
456-
self.bump();
457-
}
458-
'0'..='9' => {
459-
has_digits = true;
460-
self.bump();
461-
}
462-
_ => break,
463-
}
464-
}
465-
has_digits
466-
}
467-
468-
fn eat_hexadecimal_digits(&mut self) -> bool {
469-
let mut has_digits = false;
470-
loop {
471-
match self.first() {
472-
'_' => {
473-
self.bump();
474-
}
475-
'0'..='9' | 'a'..='f' | 'A'..='F' => {
476-
has_digits = true;
477-
self.bump();
478-
}
479-
_ => break,
480-
}
481-
}
482-
has_digits
483-
}
484-
485-
/// Eats the float exponent. Returns true if at least one digit was met,
486-
/// and returns false otherwise.
487-
fn eat_float_exponent(&mut self) -> bool {
488-
debug_assert!(self.prev() == 'e' || self.prev() == 'E');
489-
if self.first() == '-' || self.first() == '+' {
490-
self.bump();
491-
}
492-
self.eat_decimal_digits()
493-
}
494-
495-
// Eats the suffix of the literal, e.g. "_u8".
496-
fn eat_literal_suffix(&mut self) {
497-
self.eat_identifier();
498-
}
499-
500-
// Eats the identifier.
451+
/// Eats one identifier.
501452
fn eat_identifier(&mut self) {
502453
if !is_id_start(self.first()) {
503454
return;
@@ -506,11 +457,4 @@ impl Cursor<'_> {
506457

507458
self.eat_while(is_id_continue);
508459
}
509-
510-
/// Eats symbols while predicate returns true or until the end of file is reached.
511-
fn eat_while(&mut self, mut predicate: impl FnMut(char) -> bool) {
512-
while predicate(self.first()) && !self.is_eof() {
513-
self.bump();
514-
}
515-
}
516460
}

compiler/rustc_lexer/src/literals.rs

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,21 @@ pub(crate) fn number(cursor: &mut Cursor, first_digit: char) -> LiteralKind {
6060
'b' => {
6161
base = Base::Binary;
6262
cursor.bump();
63-
cursor.eat_decimal_digits()
63+
eat_decimal_digits(cursor)
6464
}
6565
'o' => {
6666
base = Base::Octal;
6767
cursor.bump();
68-
cursor.eat_decimal_digits()
68+
eat_decimal_digits(cursor)
6969
}
7070
'x' => {
7171
base = Base::Hexadecimal;
7272
cursor.bump();
73-
cursor.eat_hexadecimal_digits()
73+
eat_hexadecimal_digits(cursor)
7474
}
7575
// Not a base prefix.
7676
'0'..='9' | '_' | '.' | 'e' | 'E' => {
77-
cursor.eat_decimal_digits();
77+
eat_decimal_digits(cursor);
7878
true
7979
}
8080
// Just a 0.
@@ -87,7 +87,7 @@ pub(crate) fn number(cursor: &mut Cursor, first_digit: char) -> LiteralKind {
8787
}
8888
} else {
8989
// No base prefix, parse number in the usual way.
90-
cursor.eat_decimal_digits();
90+
eat_decimal_digits(cursor);
9191
};
9292

9393
match cursor.first() {
@@ -100,11 +100,11 @@ pub(crate) fn number(cursor: &mut Cursor, first_digit: char) -> LiteralKind {
100100
cursor.bump();
101101
let mut empty_exponent = false;
102102
if cursor.first().is_digit(10) {
103-
cursor.eat_decimal_digits();
103+
eat_decimal_digits(cursor);
104104
match cursor.first() {
105105
'e' | 'E' => {
106106
cursor.bump();
107-
empty_exponent = !cursor.eat_float_exponent();
107+
empty_exponent = !eat_float_exponent(cursor);
108108
}
109109
_ => (),
110110
}
@@ -113,13 +113,57 @@ pub(crate) fn number(cursor: &mut Cursor, first_digit: char) -> LiteralKind {
113113
}
114114
'e' | 'E' => {
115115
cursor.bump();
116-
let empty_exponent = !cursor.eat_float_exponent();
116+
let empty_exponent = !eat_float_exponent(cursor);
117117
LiteralKind::Float { base, empty_exponent }
118118
}
119119
_ => LiteralKind::Int { base, empty_int: false },
120120
}
121121
}
122122

123+
pub(crate) fn eat_decimal_digits(cursor: &mut Cursor) -> bool {
124+
let mut has_digits = false;
125+
loop {
126+
match cursor.first() {
127+
'_' => {
128+
cursor.bump();
129+
}
130+
'0'..='9' => {
131+
has_digits = true;
132+
cursor.bump();
133+
}
134+
_ => break,
135+
}
136+
}
137+
has_digits
138+
}
139+
140+
pub(crate) fn eat_hexadecimal_digits(cursor: &mut Cursor) -> bool {
141+
let mut has_digits = false;
142+
loop {
143+
match cursor.first() {
144+
'_' => {
145+
cursor.bump();
146+
}
147+
'0'..='9' | 'a'..='f' | 'A'..='F' => {
148+
has_digits = true;
149+
cursor.bump();
150+
}
151+
_ => break,
152+
}
153+
}
154+
has_digits
155+
}
156+
157+
/// Eats the float exponent. Returns true if at least one digit was met,
158+
/// and returns false otherwise.
159+
fn eat_float_exponent(cursor: &mut Cursor) -> bool {
160+
debug_assert!(cursor.prev() == 'e' || cursor.prev() == 'E');
161+
if cursor.first() == '-' || cursor.first() == '+' {
162+
cursor.bump();
163+
}
164+
eat_decimal_digits(cursor)
165+
}
166+
123167
pub(crate) fn lifetime_or_char(cursor: &mut Cursor) -> TokenKind {
124168
debug_assert!(cursor.prev() == '\'');
125169

@@ -137,7 +181,7 @@ pub(crate) fn lifetime_or_char(cursor: &mut Cursor) -> TokenKind {
137181
let terminated = single_quoted_string(cursor);
138182
let suffix_start = cursor.len_consumed();
139183
if terminated {
140-
cursor.eat_literal_suffix();
184+
eat_literal_suffix(cursor);
141185
}
142186
let kind = LiteralKind::Char { terminated };
143187
return TokenKind::Literal { kind, suffix_start };
@@ -307,3 +351,8 @@ fn raw_string_unvalidated(cursor: &mut Cursor, prefix_len: usize) -> (usize, Opt
307351
}
308352
}
309353
}
354+
355+
/// Eats the suffix of a literal, e.g. "_u8".
356+
pub(crate) fn eat_literal_suffix(cursor: &mut Cursor) {
357+
cursor.eat_identifier();
358+
}

0 commit comments

Comments
 (0)