Skip to content

Commit 0825b35

Browse files
committed
librustc_lexer: Add methods "first" and "second" to the "Cursor"
1 parent d3d28a4 commit 0825b35

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

src/librustc_lexer/src/cursor.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ impl<'a> Cursor<'a> {
4545
self.chars().nth(n).unwrap_or(EOF_CHAR)
4646
}
4747

48+
/// Peeks the next symbol from the input stream without consuming it.
49+
pub(crate) fn first(&self) -> char {
50+
self.nth_char(0)
51+
}
52+
53+
/// Peeks the second symbol from the input stream without consuming it.
54+
pub(crate) fn second(&self) -> char {
55+
self.nth_char(1)
56+
}
57+
4858
/// Checks if there is nothing more to consume.
4959
pub(crate) fn is_eof(&self) -> bool {
5060
self.chars.as_str().is_empty()

src/librustc_lexer/src/lib.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl Cursor<'_> {
248248
let first_char = self.bump().unwrap();
249249
let token_kind = match first_char {
250250
// Slash, comment or block comment.
251-
'/' => match self.nth_char(0) {
251+
'/' => match self.first() {
252252
'/' => self.line_comment(),
253253
'*' => self.block_comment(),
254254
_ => Slash,
@@ -257,8 +257,8 @@ impl Cursor<'_> {
257257
// Whitespace sequence.
258258
c if is_whitespace(c) => self.whitespace(),
259259

260-
// Raw string literal or identifier.
261-
'r' => match (self.nth_char(0), self.nth_char(1)) {
260+
// Raw identifier, raw string literal or identifier.
261+
'r' => match (self.first(), self.second()) {
262262
('#', c1) if is_id_start(c1) => self.raw_ident(),
263263
('#', _) | ('"', _) => {
264264
let (n_hashes, started, terminated) = self.raw_double_quoted_string();
@@ -273,7 +273,7 @@ impl Cursor<'_> {
273273
},
274274

275275
// Byte literal, byte string literal, raw byte string literal or identifier.
276-
'b' => match (self.nth_char(0), self.nth_char(1)) {
276+
'b' => match (self.first(), self.second()) {
277277
('\'', _) => {
278278
self.bump();
279279
let terminated = self.single_quoted_string();
@@ -366,7 +366,7 @@ impl Cursor<'_> {
366366
}
367367

368368
fn line_comment(&mut self) -> TokenKind {
369-
debug_assert!(self.prev() == '/' && self.nth_char(0) == '/');
369+
debug_assert!(self.prev() == '/' && self.first() == '/');
370370
self.bump();
371371
loop {
372372
match self.nth_char(0) {
@@ -381,16 +381,16 @@ impl Cursor<'_> {
381381
}
382382

383383
fn block_comment(&mut self) -> TokenKind {
384-
debug_assert!(self.prev() == '/' && self.nth_char(0) == '*');
384+
debug_assert!(self.prev() == '/' && self.first() == '*');
385385
self.bump();
386386
let mut depth = 1usize;
387387
while let Some(c) = self.bump() {
388388
match c {
389-
'/' if self.nth_char(0) == '*' => {
389+
'/' if self.first() == '*' => {
390390
self.bump();
391391
depth += 1;
392392
}
393-
'*' if self.nth_char(0) == '/' => {
393+
'*' if self.first() == '/' => {
394394
self.bump();
395395
depth -= 1;
396396
if depth == 0 {
@@ -418,8 +418,8 @@ impl Cursor<'_> {
418418
fn raw_ident(&mut self) -> TokenKind {
419419
debug_assert!(
420420
self.prev() == 'r'
421-
&& self.nth_char(0) == '#'
422-
&& is_id_start(self.nth_char(1))
421+
&& self.first() == '#'
422+
&& is_id_start(self.second())
423423
);
424424
self.bump();
425425
self.bump();
@@ -442,7 +442,7 @@ impl Cursor<'_> {
442442
let mut base = Base::Decimal;
443443
if first_digit == '0' {
444444
// Attempt to parse encoding base.
445-
let has_digits = match self.nth_char(0) {
445+
let has_digits = match self.first() {
446446
'b' => {
447447
base = Base::Binary;
448448
self.bump();
@@ -476,20 +476,20 @@ impl Cursor<'_> {
476476
self.eat_decimal_digits();
477477
};
478478

479-
match self.nth_char(0) {
479+
match self.first() {
480480
// Don't be greedy if this is actually an
481481
// integer literal followed by field/method access or a range pattern
482482
// (`0..2` and `12.foo()`)
483-
'.' if self.nth_char(1) != '.'
484-
&& !is_id_start(self.nth_char(1)) =>
483+
'.' if self.second() != '.'
484+
&& !is_id_start(self.second()) =>
485485
{
486486
// might have stuff after the ., and if it does, it needs to start
487487
// with a number
488488
self.bump();
489489
let mut empty_exponent = false;
490-
if self.nth_char(0).is_digit(10) {
490+
if self.first().is_digit(10) {
491491
self.eat_decimal_digits();
492-
match self.nth_char(0) {
492+
match self.first() {
493493
'e' | 'E' => {
494494
self.bump();
495495
empty_exponent = self.float_exponent().is_err()
@@ -556,7 +556,7 @@ impl Cursor<'_> {
556556
// Parse until either quotes are terminated or error is detected.
557557
let mut first = true;
558558
loop {
559-
match self.nth_char(0) {
559+
match self.first() {
560560
// Probably beginning of the comment, which we don't want to include
561561
// to the error report.
562562
'/' if !first => break,
@@ -643,7 +643,7 @@ impl Cursor<'_> {
643643
fn eat_decimal_digits(&mut self) -> bool {
644644
let mut has_digits = false;
645645
loop {
646-
match self.nth_char(0) {
646+
match self.first() {
647647
'_' => {
648648
self.bump();
649649
}
@@ -660,7 +660,7 @@ impl Cursor<'_> {
660660
fn eat_hexadecimal_digits(&mut self) -> bool {
661661
let mut has_digits = false;
662662
loop {
663-
match self.nth_char(0) {
663+
match self.first() {
664664
'_' => {
665665
self.bump();
666666
}
@@ -676,7 +676,7 @@ impl Cursor<'_> {
676676

677677
fn float_exponent(&mut self) -> Result<(), ()> {
678678
debug_assert!(self.prev() == 'e' || self.prev() == 'E');
679-
if self.nth_char(0) == '-' || self.nth_char(0) == '+' {
679+
if self.first() == '-' || self.first() == '+' {
680680
self.bump();
681681
}
682682
if self.eat_decimal_digits() { Ok(()) } else { Err(()) }

0 commit comments

Comments
 (0)