Skip to content

Commit cc0022a

Browse files
committed
Rename some things.
`Cursor` keeps track of the position within the current token. But it uses confusing names that don't make it clear that the "length consumed" is just within the current token. This commit renames things to make this clearer.
1 parent ceb25d1 commit cc0022a

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

compiler/rustc_lexer/src/cursor.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::str::Chars;
55
/// Next characters can be peeked via `first` method,
66
/// and position can be shifted forward via `bump` method.
77
pub struct Cursor<'a> {
8-
initial_len: usize,
8+
len_remaining: usize,
99
/// Iterator over chars. Slightly faster than a &str.
1010
chars: Chars<'a>,
1111
#[cfg(debug_assertions)]
@@ -17,7 +17,7 @@ pub(crate) const EOF_CHAR: char = '\0';
1717
impl<'a> Cursor<'a> {
1818
pub fn new(input: &'a str) -> Cursor<'a> {
1919
Cursor {
20-
initial_len: input.len(),
20+
len_remaining: input.len(),
2121
chars: input.chars(),
2222
#[cfg(debug_assertions)]
2323
prev: EOF_CHAR,
@@ -61,13 +61,13 @@ impl<'a> Cursor<'a> {
6161
}
6262

6363
/// Returns amount of already consumed symbols.
64-
pub(crate) fn len_consumed(&self) -> u32 {
65-
(self.initial_len - self.chars.as_str().len()) as u32
64+
pub(crate) fn pos_within_token(&self) -> u32 {
65+
(self.len_remaining - self.chars.as_str().len()) as u32
6666
}
6767

6868
/// Resets the number of bytes consumed to 0.
69-
pub(crate) fn reset_len_consumed(&mut self) {
70-
self.initial_len = self.chars.as_str().len();
69+
pub(crate) fn reset_pos_within_token(&mut self) {
70+
self.len_remaining = self.chars.as_str().len();
7171
}
7272

7373
/// Moves to the next character.

compiler/rustc_lexer/src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl Cursor<'_> {
315315
('#', c1) if is_id_start(c1) => self.raw_ident(),
316316
('#', _) | ('"', _) => {
317317
let res = self.raw_double_quoted_string(1);
318-
let suffix_start = self.len_consumed();
318+
let suffix_start = self.pos_within_token();
319319
if res.is_ok() {
320320
self.eat_literal_suffix();
321321
}
@@ -330,7 +330,7 @@ impl Cursor<'_> {
330330
('\'', _) => {
331331
self.bump();
332332
let terminated = self.single_quoted_string();
333-
let suffix_start = self.len_consumed();
333+
let suffix_start = self.pos_within_token();
334334
if terminated {
335335
self.eat_literal_suffix();
336336
}
@@ -340,7 +340,7 @@ impl Cursor<'_> {
340340
('"', _) => {
341341
self.bump();
342342
let terminated = self.double_quoted_string();
343-
let suffix_start = self.len_consumed();
343+
let suffix_start = self.pos_within_token();
344344
if terminated {
345345
self.eat_literal_suffix();
346346
}
@@ -350,7 +350,7 @@ impl Cursor<'_> {
350350
('r', '"') | ('r', '#') => {
351351
self.bump();
352352
let res = self.raw_double_quoted_string(2);
353-
let suffix_start = self.len_consumed();
353+
let suffix_start = self.pos_within_token();
354354
if res.is_ok() {
355355
self.eat_literal_suffix();
356356
}
@@ -367,7 +367,7 @@ impl Cursor<'_> {
367367
// Numeric literal.
368368
c @ '0'..='9' => {
369369
let literal_kind = self.number(c);
370-
let suffix_start = self.len_consumed();
370+
let suffix_start = self.pos_within_token();
371371
self.eat_literal_suffix();
372372
TokenKind::Literal { kind: literal_kind, suffix_start }
373373
}
@@ -406,7 +406,7 @@ impl Cursor<'_> {
406406
// String literal.
407407
'"' => {
408408
let terminated = self.double_quoted_string();
409-
let suffix_start = self.len_consumed();
409+
let suffix_start = self.pos_within_token();
410410
if terminated {
411411
self.eat_literal_suffix();
412412
}
@@ -419,8 +419,8 @@ impl Cursor<'_> {
419419
}
420420
_ => Unknown,
421421
};
422-
let res = Some(Token::new(token_kind, self.len_consumed()));
423-
self.reset_len_consumed();
422+
let res = Some(Token::new(token_kind, self.pos_within_token()));
423+
self.reset_pos_within_token();
424424
res
425425
}
426426

@@ -606,7 +606,7 @@ impl Cursor<'_> {
606606

607607
if !can_be_a_lifetime {
608608
let terminated = self.single_quoted_string();
609-
let suffix_start = self.len_consumed();
609+
let suffix_start = self.pos_within_token();
610610
if terminated {
611611
self.eat_literal_suffix();
612612
}
@@ -631,7 +631,7 @@ impl Cursor<'_> {
631631
if self.first() == '\'' {
632632
self.bump();
633633
let kind = Char { terminated: true };
634-
Literal { kind, suffix_start: self.len_consumed() }
634+
Literal { kind, suffix_start: self.pos_within_token() }
635635
} else {
636636
Lifetime { starts_with_number }
637637
}
@@ -712,7 +712,7 @@ impl Cursor<'_> {
712712

713713
fn raw_string_unvalidated(&mut self, prefix_len: u32) -> Result<u32, RawStrError> {
714714
debug_assert!(self.prev() == 'r');
715-
let start_pos = self.len_consumed();
715+
let start_pos = self.pos_within_token();
716716
let mut possible_terminator_offset = None;
717717
let mut max_hashes = 0;
718718

@@ -766,7 +766,7 @@ impl Cursor<'_> {
766766
// Keep track of possible terminators to give a hint about
767767
// where there might be a missing terminator
768768
possible_terminator_offset =
769-
Some(self.len_consumed() - start_pos - n_end_hashes + prefix_len);
769+
Some(self.pos_within_token() - start_pos - n_end_hashes + prefix_len);
770770
max_hashes = n_end_hashes;
771771
}
772772
}

0 commit comments

Comments
 (0)