@@ -368,15 +368,7 @@ impl Cursor<'_> {
368
368
fn line_comment ( & mut self ) -> TokenKind {
369
369
debug_assert ! ( self . prev( ) == '/' && self . first( ) == '/' ) ;
370
370
self . bump ( ) ;
371
- loop {
372
- match self . nth_char ( 0 ) {
373
- '\n' => break ,
374
- EOF_CHAR if self . is_eof ( ) => break ,
375
- _ => {
376
- self . bump ( ) ;
377
- }
378
- }
379
- }
371
+ self . eat_while ( |c| c != '\n' ) ;
380
372
LineComment
381
373
}
382
374
@@ -409,9 +401,7 @@ impl Cursor<'_> {
409
401
410
402
fn whitespace ( & mut self ) -> TokenKind {
411
403
debug_assert ! ( is_whitespace( self . prev( ) ) ) ;
412
- while is_whitespace ( self . nth_char ( 0 ) ) {
413
- self . bump ( ) ;
414
- }
404
+ self . eat_while ( is_whitespace) ;
415
405
Whitespace
416
406
}
417
407
@@ -421,19 +411,17 @@ impl Cursor<'_> {
421
411
&& self . first( ) == '#'
422
412
&& is_id_start( self . second( ) )
423
413
) ;
414
+ // Eat "#" symbol.
424
415
self . bump ( ) ;
425
- self . bump ( ) ;
426
- while is_id_continue ( self . nth_char ( 0 ) ) {
427
- self . bump ( ) ;
428
- }
416
+ // Eat the identifier part of RawIdent.
417
+ self . eat_identifier ( ) ;
429
418
RawIdent
430
419
}
431
420
432
421
fn ident ( & mut self ) -> TokenKind {
433
422
debug_assert ! ( is_id_start( self . prev( ) ) ) ;
434
- while is_id_continue ( self . nth_char ( 0 ) ) {
435
- self . bump ( ) ;
436
- }
423
+ // Start is already eaten, eat the rest of identifier.
424
+ self . eat_while ( is_id_continue) ;
437
425
Ident
438
426
}
439
427
@@ -682,15 +670,33 @@ impl Cursor<'_> {
682
670
if self . eat_decimal_digits ( ) { Ok ( ( ) ) } else { Err ( ( ) ) }
683
671
}
684
672
685
- // Eats the suffix if it's an identifier .
673
+ // Eats the suffix of the literal, e.g. "_u8" .
686
674
fn eat_literal_suffix ( & mut self ) {
687
- if !is_id_start ( self . nth_char ( 0 ) ) {
675
+ self . eat_identifier ( ) ;
676
+ }
677
+
678
+ // Eats the identifier.
679
+ fn eat_identifier ( & mut self ) {
680
+ if !is_id_start ( self . first ( ) ) {
688
681
return ;
689
682
}
690
683
self . bump ( ) ;
691
684
692
- while is_id_continue ( self . nth_char ( 0 ) ) {
685
+ self . eat_while ( is_id_continue) ;
686
+ }
687
+
688
+ /// Eats symbols while predicate returns true or until the end of file is reached.
689
+ /// Returns amount of eaten symbols.
690
+ fn eat_while < F > ( & mut self , mut predicate : F ) -> usize
691
+ where
692
+ F : FnMut ( char ) -> bool
693
+ {
694
+ let mut eaten: usize = 0 ;
695
+ while predicate ( self . first ( ) ) && !self . is_eof ( ) {
696
+ eaten += 1 ;
693
697
self . bump ( ) ;
694
698
}
699
+
700
+ eaten
695
701
}
696
702
}
0 commit comments