Skip to content

Commit d4336a5

Browse files
author
Julian Wollersberger
committed
Inline some helper functions that are only used once, to lower the number of things I need to keep in my head.
And fix imports in tests.rs.
1 parent 629e161 commit d4336a5

File tree

3 files changed

+35
-45
lines changed

3 files changed

+35
-45
lines changed

compiler/rustc_lexer/src/lib.rs

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ pub mod unescape;
2828
#[cfg(test)]
2929
mod tests;
3030

31+
pub use crate::literals::{Base, LiteralKind, RawStrError};
32+
3133
use self::TokenKind::*;
3234
use crate::cursor::Cursor;
3335
use crate::literals::{
3436
double_quoted_string, eat_literal_suffix, lifetime_or_char, number, raw_double_quoted_string,
35-
single_quoted_string, LiteralKind,
37+
single_quoted_string,
3638
};
3739

3840
/// Parsed token.
@@ -165,12 +167,6 @@ pub fn strip_shebang(input: &str) -> Option<usize> {
165167
None
166168
}
167169

168-
/// Parses the first token from the provided input string.
169-
pub fn first_token(input: &str) -> Token {
170-
debug_assert!(!input.is_empty());
171-
advance_token(&mut Cursor::new(input))
172-
}
173-
174170
/// Creates an iterator that produces tokens from the input string.
175171
pub fn tokenize(mut input: &str) -> impl Iterator<Item = Token> + '_ {
176172
std::iter::from_fn(move || {
@@ -250,8 +246,11 @@ pub fn is_ident(string: &str) -> bool {
250246
}
251247
}
252248

253-
/// Parses a token from the input string.
254-
fn advance_token(cursor: &mut Cursor) -> Token {
249+
/// Parses the first token from the provided input string.
250+
pub fn first_token(input: &str) -> Token {
251+
debug_assert!(!input.is_empty());
252+
let cursor = &mut Cursor::new(input);
253+
255254
let first_char = cursor.bump().unwrap();
256255
let token_kind = match first_char {
257256
// Slash, comment or block comment.
@@ -262,11 +261,21 @@ fn advance_token(cursor: &mut Cursor) -> Token {
262261
},
263262

264263
// Whitespace sequence.
265-
c if is_whitespace(c) => whitespace(cursor),
264+
c if is_whitespace(c) => {
265+
cursor.bump_while(is_whitespace);
266+
Whitespace
267+
}
266268

267269
// Raw identifier, raw string literal or identifier.
268270
'r' => match (cursor.peek(), cursor.peek_second()) {
269-
('#', c1) if is_id_start(c1) => raw_ident(cursor),
271+
('#', c1) if is_id_start(c1) => {
272+
// Eat "#" symbol.
273+
cursor.bump();
274+
// Eat the identifier part of RawIdent.
275+
cursor.bump();
276+
ident(cursor);
277+
RawIdent
278+
}
270279
('#', _) | ('"', _) => {
271280
let (n_hashes, err) = raw_double_quoted_string(cursor, 1);
272281
let suffix_start = cursor.len_consumed();
@@ -425,34 +434,9 @@ fn block_comment(cursor: &mut Cursor) -> TokenKind {
425434
BlockComment { doc_style, terminated: depth == 0 }
426435
}
427436

428-
fn whitespace(cursor: &mut Cursor) -> TokenKind {
429-
debug_assert!(is_whitespace(cursor.prev()));
430-
cursor.bump_while(is_whitespace);
431-
Whitespace
432-
}
433-
434-
fn raw_ident(cursor: &mut Cursor) -> TokenKind {
435-
debug_assert!(cursor.prev() == 'r' && cursor.peek() == '#' && is_id_start(cursor.peek_second()));
436-
// Eat "#" symbol.
437-
cursor.bump();
438-
// Eat the identifier part of RawIdent.
439-
eat_identifier(cursor);
440-
RawIdent
441-
}
442-
443-
fn ident(cursor: &mut Cursor) -> TokenKind {
437+
/// Start is already eaten, eat the rest of identifier.
438+
pub(crate) fn ident(cursor: &mut Cursor) -> TokenKind {
444439
debug_assert!(is_id_start(cursor.prev()));
445-
// Start is already eaten, eat the rest of identifier.
446440
cursor.bump_while(is_id_continue);
447441
Ident
448442
}
449-
450-
/// Eats one identifier.
451-
pub(crate) fn eat_identifier(cursor: &mut Cursor) {
452-
if !is_id_start(cursor.peek()) {
453-
return;
454-
}
455-
cursor.bump();
456-
457-
cursor.bump_while(is_id_continue);
458-
}

compiler/rustc_lexer/src/literals.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::cursor::{Cursor, EOF_CHAR};
2-
use crate::{is_id_continue, is_id_start, TokenKind, eat_identifier};
2+
use crate::{ident, is_id_continue, is_id_start, TokenKind};
33
use std::convert::TryFrom;
44

55
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
@@ -279,7 +279,8 @@ pub(crate) fn raw_double_quoted_string(
279279
// Wrap the actual function to handle the error with too many hashes.
280280
// This way, it eats the whole raw string.
281281
let (n_hashes, err) = raw_string_unvalidated(cursor, prefix_len);
282-
// Only up to 65535 `#`s are allowed in raw strings
282+
283+
// Only up to 65535 `#`s are allowed in raw strings.
283284
match u16::try_from(n_hashes) {
284285
Ok(num) => (num, err),
285286
// We lie about the number of hashes here :P
@@ -354,5 +355,9 @@ fn raw_string_unvalidated(cursor: &mut Cursor, prefix_len: usize) -> (usize, Opt
354355

355356
/// Eats the suffix of a literal, e.g. "_u8".
356357
pub(crate) fn eat_literal_suffix(cursor: &mut Cursor) {
357-
eat_identifier(cursor);
358+
// Eats one identifier.
359+
if is_id_start(cursor.peek()) {
360+
cursor.bump();
361+
ident(cursor);
362+
}
358363
}

compiler/rustc_lexer/src/tests.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
use super::*;
2-
1+
use crate::cursor::Cursor;
2+
use crate::literals::{raw_double_quoted_string, RawStrError};
3+
use crate::{strip_shebang, tokenize};
34
use expect_test::{expect, Expect};
45

56
fn check_raw_str(s: &str, expected_hashes: u16, expected_err: Option<RawStrError>) {
67
let s = &format!("r{}", s);
7-
let mut cursor = Cursor::new(s);
8+
let cursor = &mut Cursor::new(s);
89
cursor.bump();
9-
let (n_hashes, err) = cursor.raw_double_quoted_string(0);
10+
let (n_hashes, err) = raw_double_quoted_string(cursor, 0);
1011
assert_eq!(n_hashes, expected_hashes);
1112
assert_eq!(err, expected_err);
1213
}

0 commit comments

Comments
 (0)