@@ -368,15 +368,7 @@ impl Cursor<'_> {
368368 fn line_comment ( & mut self ) -> TokenKind {
369369 debug_assert ! ( self . prev( ) == '/' && self . first( ) == '/' ) ;
370370 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' ) ;
380372 LineComment
381373 }
382374
@@ -409,9 +401,7 @@ impl Cursor<'_> {
409401
410402 fn whitespace ( & mut self ) -> TokenKind {
411403 debug_assert ! ( is_whitespace( self . prev( ) ) ) ;
412- while is_whitespace ( self . nth_char ( 0 ) ) {
413- self . bump ( ) ;
414- }
404+ self . eat_while ( is_whitespace) ;
415405 Whitespace
416406 }
417407
@@ -421,19 +411,17 @@ impl Cursor<'_> {
421411 && self . first( ) == '#'
422412 && is_id_start( self . second( ) )
423413 ) ;
414+ // Eat "#" symbol.
424415 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 ( ) ;
429418 RawIdent
430419 }
431420
432421 fn ident ( & mut self ) -> TokenKind {
433422 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) ;
437425 Ident
438426 }
439427
@@ -682,15 +670,33 @@ impl Cursor<'_> {
682670 if self . eat_decimal_digits ( ) { Ok ( ( ) ) } else { Err ( ( ) ) }
683671 }
684672
685- // Eats the suffix if it's an identifier .
673+ // Eats the suffix of the literal, e.g. "_u8" .
686674 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 ( ) ) {
688681 return ;
689682 }
690683 self . bump ( ) ;
691684
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 ;
693697 self . bump ( ) ;
694698 }
699+
700+ eaten
695701 }
696702}
0 commit comments