File tree Expand file tree Collapse file tree 3 files changed +46
-9
lines changed Expand file tree Collapse file tree 3 files changed +46
-9
lines changed Original file line number Diff line number Diff line change @@ -1020,20 +1020,30 @@ impl Literal {
1020
1020
impl FromStr for Literal {
1021
1021
type Err = LexError ;
1022
1022
1023
- fn from_str ( mut repr : & str ) -> Result < Self , Self :: Err > {
1024
- let negative = repr. starts_with ( '-' ) ;
1023
+ fn from_str ( repr : & str ) -> Result < Self , Self :: Err > {
1024
+ let mut cursor = get_cursor ( repr) ;
1025
+ #[ cfg( span_locations) ]
1026
+ let lo = cursor. off ;
1027
+
1028
+ let negative = cursor. starts_with_char ( '-' ) ;
1025
1029
if negative {
1026
- repr = & repr [ 1 .. ] ;
1027
- if !repr . starts_with ( |ch : char | ch. is_ascii_digit ( ) ) {
1030
+ cursor = cursor . advance ( 1 ) ;
1031
+ if !cursor . starts_with_fn ( |ch| ch. is_ascii_digit ( ) ) {
1028
1032
return Err ( LexError :: call_site ( ) ) ;
1029
1033
}
1030
1034
}
1031
- let cursor = get_cursor ( repr ) ;
1032
- if let Ok ( ( _rest , mut literal) ) = parse:: literal ( cursor) {
1033
- if literal . repr . len ( ) == repr . len ( ) {
1035
+
1036
+ if let Ok ( ( rest , mut literal) ) = parse:: literal ( cursor) {
1037
+ if rest . is_empty ( ) {
1034
1038
if negative {
1035
1039
literal. repr . insert ( 0 , '-' ) ;
1036
1040
}
1041
+ literal. span = Span {
1042
+ #[ cfg( span_locations) ]
1043
+ lo,
1044
+ #[ cfg( span_locations) ]
1045
+ hi : rest. off ,
1046
+ } ;
1037
1047
return Ok ( literal) ;
1038
1048
}
1039
1049
}
Original file line number Diff line number Diff line change @@ -27,11 +27,18 @@ impl<'a> Cursor<'a> {
27
27
self . rest . starts_with ( s)
28
28
}
29
29
30
- fn starts_with_char ( & self , ch : char ) -> bool {
30
+ pub fn starts_with_char ( & self , ch : char ) -> bool {
31
31
self . rest . starts_with ( ch)
32
32
}
33
33
34
- fn is_empty ( & self ) -> bool {
34
+ pub fn starts_with_fn < Pattern > ( & self , f : Pattern ) -> bool
35
+ where
36
+ Pattern : FnMut ( char ) -> bool ,
37
+ {
38
+ self . rest . starts_with ( f)
39
+ }
40
+
41
+ pub fn is_empty ( & self ) -> bool {
35
42
self . rest . is_empty ( )
36
43
}
37
44
Original file line number Diff line number Diff line change @@ -264,6 +264,26 @@ fn literal_parse() {
264
264
assert ! ( "-\" \" " . parse:: <Literal >( ) . is_err( ) ) ;
265
265
}
266
266
267
+ #[ test]
268
+ fn literal_span ( ) {
269
+ let positive = "0.1" . parse :: < Literal > ( ) . unwrap ( ) ;
270
+ let negative = "-0.1" . parse :: < Literal > ( ) . unwrap ( ) ;
271
+
272
+ #[ cfg( not( span_locations) ) ]
273
+ {
274
+ let _ = positive;
275
+ let _ = negative;
276
+ }
277
+
278
+ #[ cfg( span_locations) ]
279
+ {
280
+ assert_eq ! ( positive. span( ) . start( ) . column, 0 ) ;
281
+ assert_eq ! ( positive. span( ) . end( ) . column, 3 ) ;
282
+ assert_eq ! ( negative. span( ) . start( ) . column, 0 ) ;
283
+ assert_eq ! ( negative. span( ) . end( ) . column, 4 ) ;
284
+ }
285
+ }
286
+
267
287
#[ test]
268
288
fn roundtrip ( ) {
269
289
fn roundtrip ( p : & str ) {
You can’t perform that action at this time.
0 commit comments