@@ -28,11 +28,13 @@ pub mod unescape;
28
28
#[ cfg( test) ]
29
29
mod tests;
30
30
31
+ pub use crate :: literals:: { Base , LiteralKind , RawStrError } ;
32
+
31
33
use self :: TokenKind :: * ;
32
34
use crate :: cursor:: Cursor ;
33
35
use crate :: literals:: {
34
36
double_quoted_string, eat_literal_suffix, lifetime_or_char, number, raw_double_quoted_string,
35
- single_quoted_string, LiteralKind ,
37
+ single_quoted_string,
36
38
} ;
37
39
38
40
/// Parsed token.
@@ -165,12 +167,6 @@ pub fn strip_shebang(input: &str) -> Option<usize> {
165
167
None
166
168
}
167
169
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
-
174
170
/// Creates an iterator that produces tokens from the input string.
175
171
pub fn tokenize ( mut input : & str ) -> impl Iterator < Item = Token > + ' _ {
176
172
std:: iter:: from_fn ( move || {
@@ -250,8 +246,11 @@ pub fn is_ident(string: &str) -> bool {
250
246
}
251
247
}
252
248
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
+
255
254
let first_char = cursor. bump ( ) . unwrap ( ) ;
256
255
let token_kind = match first_char {
257
256
// Slash, comment or block comment.
@@ -262,11 +261,21 @@ fn advance_token(cursor: &mut Cursor) -> Token {
262
261
} ,
263
262
264
263
// 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
+ }
266
268
267
269
// Raw identifier, raw string literal or identifier.
268
270
'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
+ }
270
279
( '#' , _) | ( '"' , _) => {
271
280
let ( n_hashes, err) = raw_double_quoted_string ( cursor, 1 ) ;
272
281
let suffix_start = cursor. len_consumed ( ) ;
@@ -425,34 +434,9 @@ fn block_comment(cursor: &mut Cursor) -> TokenKind {
425
434
BlockComment { doc_style, terminated : depth == 0 }
426
435
}
427
436
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 {
444
439
debug_assert ! ( is_id_start( cursor. prev( ) ) ) ;
445
- // Start is already eaten, eat the rest of identifier.
446
440
cursor. bump_while ( is_id_continue) ;
447
441
Ident
448
442
}
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
- }
0 commit comments