@@ -410,6 +410,8 @@ impl Cursor<'_> {
410
410
// Numeric literal.
411
411
c @ '0' ..='9' => {
412
412
let ( literal_kind, suffix_start) = self . number ( c) ;
413
+ let suffix_start = suffix_start. unwrap_or ( self . pos_within_token ( ) ) ;
414
+ self . eat_literal_suffix ( ) ;
413
415
TokenKind :: Literal { kind : literal_kind, suffix_start }
414
416
}
415
417
@@ -604,9 +606,9 @@ impl Cursor<'_> {
604
606
}
605
607
}
606
608
607
- /// Parses a number and in `.1` returns the offset of the literal suffix
608
- /// (this will be at the end of the token if there is no suffix)
609
- fn number ( & mut self , first_digit : char ) -> ( LiteralKind , u32 ) {
609
+ /// Parses a number and in `.1` returns the offset of the literal suffix if
610
+ /// different from the current position on return.
611
+ fn number ( & mut self , first_digit : char ) -> ( LiteralKind , Option < u32 > ) {
610
612
debug_assert ! ( '0' <= self . prev( ) && self . prev( ) <= '9' ) ;
611
613
let mut base = Base :: Decimal ;
612
614
if first_digit == '0' {
@@ -616,27 +618,21 @@ impl Cursor<'_> {
616
618
base = Base :: Binary ;
617
619
self . bump ( ) ;
618
620
if !self . eat_decimal_digits ( ) {
619
- let suffix_start = self . pos_within_token ( ) ;
620
- self . eat_literal_suffix ( ) ;
621
- return ( Int { base, empty_int : true } , suffix_start) ;
621
+ return ( Int { base, empty_int : true } , None ) ;
622
622
}
623
623
}
624
624
'o' => {
625
625
base = Base :: Octal ;
626
626
self . bump ( ) ;
627
627
if !self . eat_decimal_digits ( ) {
628
- let suffix_start = self . pos_within_token ( ) ;
629
- self . eat_literal_suffix ( ) ;
630
- return ( Int { base, empty_int : true } , suffix_start) ;
628
+ return ( Int { base, empty_int : true } , None ) ;
631
629
}
632
630
}
633
631
'x' => {
634
632
base = Base :: Hexadecimal ;
635
633
self . bump ( ) ;
636
634
if !self . eat_hexadecimal_digits ( ) {
637
- let suffix_start = self . pos_within_token ( ) ;
638
- self . eat_literal_suffix ( ) ;
639
- return ( Int { base, empty_int : true } , suffix_start) ;
635
+ return ( Int { base, empty_int : true } , None ) ;
640
636
}
641
637
}
642
638
// Not a base prefix; consume additional digits.
@@ -649,9 +645,7 @@ impl Cursor<'_> {
649
645
650
646
// Just a 0.
651
647
_ => {
652
- let suffix_start = self . pos_within_token ( ) ;
653
- self . eat_literal_suffix ( ) ;
654
- return ( Int { base, empty_int : false } , suffix_start) ;
648
+ return ( Int { base, empty_int : false } , None ) ;
655
649
}
656
650
}
657
651
} else {
@@ -668,11 +662,11 @@ impl Cursor<'_> {
668
662
// with a number
669
663
self . bump ( ) ;
670
664
let mut empty_exponent = false ;
671
- let mut suffix_start = self . pos_within_token ( ) ;
665
+ let mut suffix_start = None ;
672
666
if self . first ( ) . is_ascii_digit ( ) {
673
667
self . eat_decimal_digits ( ) ;
674
668
// This will be the start of the suffix if there is no exponent
675
- suffix_start = self . pos_within_token ( ) ;
669
+ suffix_start = Some ( self . pos_within_token ( ) ) ;
676
670
match ( self . first ( ) , self . second ( ) ) {
677
671
( 'e' | 'E' , '_' ) => {
678
672
// check if series of `_` is ended by a digit. If yes
@@ -682,21 +676,22 @@ impl Cursor<'_> {
682
676
while matches ! ( self . first( ) , '_' ) {
683
677
self . bump ( ) ;
684
678
}
679
+ // If we find a digit, then the exponential was valid
680
+ // so the suffix will start at the cursor as usual.
685
681
if self . first ( ) . is_ascii_digit ( ) {
686
682
self . eat_decimal_digits ( ) ;
687
- suffix_start = self . pos_within_token ( ) ;
683
+ suffix_start = None ;
688
684
}
689
685
}
690
686
( 'e' | 'E' , '0' ..='9' | '+' | '-' ) => {
691
687
// Definitely an exponent (which still can be empty).
692
688
self . bump ( ) ;
693
689
empty_exponent = !self . eat_float_exponent ( ) ;
694
- suffix_start = self . pos_within_token ( ) ;
690
+ suffix_start = None ;
695
691
}
696
692
_ => ( ) ,
697
693
}
698
694
}
699
- self . eat_literal_suffix ( ) ;
700
695
( Float { base, empty_exponent } , suffix_start)
701
696
}
702
697
( 'e' | 'E' , '_' ) => {
@@ -708,28 +703,20 @@ impl Cursor<'_> {
708
703
}
709
704
if self . first ( ) . is_ascii_digit ( ) {
710
705
self . eat_decimal_digits ( ) ;
711
- let suffix_start = self . pos_within_token ( ) ;
712
- self . eat_literal_suffix ( ) ;
713
- ( Float { base, empty_exponent : false } , suffix_start)
706
+ ( Float { base, empty_exponent : false } , None )
714
707
} else {
715
- // No digit means suffix, and therefore int
716
- self . eat_literal_suffix ( ) ;
717
- ( Int { base, empty_int : false } , non_exponent_suffix_start)
708
+ // No digit means the suffix begins at `e`, meaning the number
709
+ // is an integer.
710
+ ( Int { base, empty_int : false } , Some ( non_exponent_suffix_start) )
718
711
}
719
712
}
720
713
( 'e' | 'E' , '0' ..='9' | '+' | '-' ) => {
721
714
// // Definitely an exponent (which still can be empty).
722
715
self . bump ( ) ;
723
716
let empty_exponent = !self . eat_float_exponent ( ) ;
724
- let suffix_start = self . pos_within_token ( ) ;
725
- self . eat_literal_suffix ( ) ;
726
- ( Float { base, empty_exponent } , suffix_start)
727
- }
728
- _ => {
729
- let suffix_start = self . pos_within_token ( ) ;
730
- self . eat_literal_suffix ( ) ;
731
- ( Int { base, empty_int : false } , suffix_start)
717
+ ( Float { base, empty_exponent } , None )
732
718
}
719
+ _ => ( Int { base, empty_int : false } , None ) ,
733
720
}
734
721
}
735
722
@@ -1023,19 +1010,24 @@ impl Cursor<'_> {
1023
1010
self . eat_decimal_digits ( )
1024
1011
}
1025
1012
1026
- // Eats the suffix of the literal, e.g. "u8".
1027
- fn eat_literal_suffix ( & mut self ) {
1028
- self . eat_identifier ( ) ;
1013
+ /// Eats the suffix of the literal, e.g. "u8".
1014
+ ///
1015
+ /// Returns `true` if anything was eaten.
1016
+ fn eat_literal_suffix ( & mut self ) -> bool {
1017
+ self . eat_identifier ( )
1029
1018
}
1030
1019
1031
- // Eats the identifier. Note: succeeds on `_`, which isn't a valid
1032
- // identifier.
1033
- fn eat_identifier ( & mut self ) {
1020
+ /// Eats the identifier. Note: succeeds on `_`, which isn't a valid
1021
+ /// identifier.
1022
+ ///
1023
+ /// Returns `true` if anything was eaten.
1024
+ fn eat_identifier ( & mut self ) -> bool {
1034
1025
if !is_id_start ( self . first ( ) ) {
1035
- return ;
1026
+ return false ;
1036
1027
}
1037
1028
self . bump ( ) ;
1038
1029
1039
1030
self . eat_while ( is_id_continue) ;
1031
+ true
1040
1032
}
1041
1033
}
0 commit comments