@@ -410,7 +410,10 @@ impl Cursor<'_> {
410
410
// Numeric literal.
411
411
c @ '0' ..='9' => {
412
412
let ( literal_kind, suffix_start) = self . number ( c) ;
413
- TokenKind :: Literal { kind : literal_kind, suffix_start }
413
+ TokenKind :: Literal {
414
+ kind : literal_kind,
415
+ suffix_start : suffix_start. unwrap_or ( self . pos_within_token ( ) ) ,
416
+ }
414
417
}
415
418
416
419
// Guarded string literal prefix: `#"` or `##`
@@ -604,9 +607,9 @@ impl Cursor<'_> {
604
607
}
605
608
}
606
609
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 ) {
610
+ /// Parses a number and in `.1` returns the offset of the literal suffix if
611
+ /// different from the output of `.pos_within_token()`.
612
+ fn number ( & mut self , first_digit : char ) -> ( LiteralKind , Option < u32 > ) {
610
613
debug_assert ! ( '0' <= self . prev( ) && self . prev( ) <= '9' ) ;
611
614
let mut base = Base :: Decimal ;
612
615
if first_digit == '0' {
@@ -617,26 +620,26 @@ impl Cursor<'_> {
617
620
self . bump ( ) ;
618
621
if !self . eat_decimal_digits ( ) {
619
622
let suffix_start = self . pos_within_token ( ) ;
620
- self . eat_literal_suffix ( ) ;
621
- return ( Int { base, empty_int : true } , suffix_start ) ;
623
+ let offset = self . eat_literal_suffix ( ) . then_some ( suffix_start ) ;
624
+ return ( Int { base, empty_int : true } , offset ) ;
622
625
}
623
626
}
624
627
'o' => {
625
628
base = Base :: Octal ;
626
629
self . bump ( ) ;
627
630
if !self . eat_decimal_digits ( ) {
628
631
let suffix_start = self . pos_within_token ( ) ;
629
- self . eat_literal_suffix ( ) ;
630
- return ( Int { base, empty_int : true } , suffix_start ) ;
632
+ let offset = self . eat_literal_suffix ( ) . then_some ( suffix_start ) ;
633
+ return ( Int { base, empty_int : true } , offset ) ;
631
634
}
632
635
}
633
636
'x' => {
634
637
base = Base :: Hexadecimal ;
635
638
self . bump ( ) ;
636
639
if !self . eat_hexadecimal_digits ( ) {
637
640
let suffix_start = self . pos_within_token ( ) ;
638
- self . eat_literal_suffix ( ) ;
639
- return ( Int { base, empty_int : true } , suffix_start ) ;
641
+ let offset = self . eat_literal_suffix ( ) . then_some ( suffix_start ) ;
642
+ return ( Int { base, empty_int : true } , offset ) ;
640
643
}
641
644
}
642
645
// Not a base prefix; consume additional digits.
@@ -650,8 +653,8 @@ impl Cursor<'_> {
650
653
// Just a 0.
651
654
_ => {
652
655
let suffix_start = self . pos_within_token ( ) ;
653
- self . eat_literal_suffix ( ) ;
654
- return ( Int { base, empty_int : false } , suffix_start ) ;
656
+ let offset = self . eat_literal_suffix ( ) . then_some ( suffix_start ) ;
657
+ return ( Int { base, empty_int : false } , offset ) ;
655
658
}
656
659
}
657
660
} else {
@@ -696,8 +699,8 @@ impl Cursor<'_> {
696
699
_ => ( ) ,
697
700
}
698
701
}
699
- self . eat_literal_suffix ( ) ;
700
- ( Float { base, empty_exponent } , suffix_start )
702
+ let offset = self . eat_literal_suffix ( ) . then_some ( suffix_start ) ;
703
+ ( Float { base, empty_exponent } , offset )
701
704
}
702
705
( 'e' | 'E' , '_' ) => {
703
706
// See above block for similar approach.
@@ -710,25 +713,27 @@ impl Cursor<'_> {
710
713
self . eat_decimal_digits ( ) ;
711
714
let suffix_start = self . pos_within_token ( ) ;
712
715
self . eat_literal_suffix ( ) ;
713
- ( Float { base, empty_exponent : false } , suffix_start)
716
+ let offset = self . eat_literal_suffix ( ) . then_some ( suffix_start) ;
717
+ ( Float { base, empty_exponent : false } , offset)
714
718
} else {
715
719
// No digit means suffix, and therefore int
716
720
self . eat_literal_suffix ( ) ;
717
- ( Int { base, empty_int : false } , non_exponent_suffix_start)
721
+ let offset = self . eat_literal_suffix ( ) . then_some ( non_exponent_suffix_start) ;
722
+ ( Int { base, empty_int : false } , offset)
718
723
}
719
724
}
720
725
( 'e' | 'E' , '0' ..='9' | '+' | '-' ) => {
721
726
// // Definitely an exponent (which still can be empty).
722
727
self . bump ( ) ;
723
728
let empty_exponent = !self . eat_float_exponent ( ) ;
724
729
let suffix_start = self . pos_within_token ( ) ;
725
- self . eat_literal_suffix ( ) ;
726
- ( Float { base, empty_exponent } , suffix_start )
730
+ let offset = self . eat_literal_suffix ( ) . then_some ( suffix_start ) ;
731
+ ( Float { base, empty_exponent } , offset )
727
732
}
728
733
_ => {
729
734
let suffix_start = self . pos_within_token ( ) ;
730
- self . eat_literal_suffix ( ) ;
731
- ( Int { base, empty_int : false } , suffix_start )
735
+ let offset = self . eat_literal_suffix ( ) . then_some ( suffix_start ) ;
736
+ ( Int { base, empty_int : false } , offset )
732
737
}
733
738
}
734
739
}
@@ -1023,19 +1028,24 @@ impl Cursor<'_> {
1023
1028
self . eat_decimal_digits ( )
1024
1029
}
1025
1030
1026
- // Eats the suffix of the literal, e.g. "u8".
1027
- fn eat_literal_suffix ( & mut self ) {
1028
- self . eat_identifier ( ) ;
1031
+ /// Eats the suffix of the literal, e.g. "u8".
1032
+ ///
1033
+ /// Returns `true` if anything was eaten.
1034
+ fn eat_literal_suffix ( & mut self ) -> bool {
1035
+ self . eat_identifier ( )
1029
1036
}
1030
1037
1031
- // Eats the identifier. Note: succeeds on `_`, which isn't a valid
1032
- // identifier.
1033
- fn eat_identifier ( & mut self ) {
1038
+ /// Eats the identifier. Note: succeeds on `_`, which isn't a valid
1039
+ /// identifier.
1040
+ ///
1041
+ /// Returns `true` if anything was eaten.
1042
+ fn eat_identifier ( & mut self ) -> bool {
1034
1043
if !is_id_start ( self . first ( ) ) {
1035
- return ;
1044
+ return false ;
1036
1045
}
1037
1046
self . bump ( ) ;
1038
1047
1039
1048
self . eat_while ( is_id_continue) ;
1049
+ true
1040
1050
}
1041
1051
}
0 commit comments