1
- #![ allow( dead_code) ]
1
+ #![ cfg_attr ( not ( procmacro2_semver_exempt ) , allow( dead_code) ) ]
2
2
3
- use std:: ascii;
4
3
use std:: borrow:: Borrow ;
5
4
use std:: cell:: RefCell ;
6
5
#[ cfg( procmacro2_semver_exempt) ]
@@ -260,7 +259,7 @@ impl FileInfo {
260
259
}
261
260
}
262
261
263
- /// Computes the offsets of each line in the given source string.
262
+ /// Computesthe offsets of each line in the given source string.
264
263
#[ cfg( procmacro2_semver_exempt) ]
265
264
fn lines_offsets ( s : & str ) -> Vec < usize > {
266
265
let mut lines = vec ! [ 0 ] ;
@@ -406,7 +405,7 @@ impl Term {
406
405
pub fn new ( string : & str , span : Span ) -> Term {
407
406
Term {
408
407
intern : SYMBOLS . with ( |s| s. borrow_mut ( ) . intern ( string) ) ,
409
- span,
408
+ span : span ,
410
409
}
411
410
}
412
411
@@ -477,25 +476,86 @@ pub struct Literal {
477
476
span : Span ,
478
477
}
479
478
479
+ macro_rules! suffixed_numbers {
480
+ ( $( $name: ident => $kind: ident, ) * ) => ( $(
481
+ pub fn $name( n: $kind) -> Literal {
482
+ Literal :: _new( format!( concat!( "{}" , stringify!( $kind) ) , n) )
483
+ }
484
+ ) * )
485
+ }
486
+
487
+ macro_rules! unsuffixed_numbers {
488
+ ( $( $name: ident => $kind: ident, ) * ) => ( $(
489
+ pub fn $name( n: $kind) -> Literal {
490
+ Literal :: _new( n. to_string( ) )
491
+ }
492
+ ) * )
493
+ }
494
+
480
495
impl Literal {
481
496
fn _new ( text : String ) -> Literal {
482
497
Literal {
483
- text,
498
+ text : text ,
484
499
span : Span :: call_site ( ) ,
485
500
}
486
501
}
487
502
488
- pub fn byte_char ( byte : u8 ) -> Literal {
489
- match byte {
490
- 0 => Literal :: _new ( format ! ( "b'\\ 0'" ) ) ,
491
- b'\"' => Literal :: _new ( format ! ( "b'\" '" ) ) ,
492
- n => {
493
- let mut escaped = "b'" . to_string ( ) ;
494
- escaped. extend ( ascii:: escape_default ( n) . map ( |c| c as char ) ) ;
495
- escaped. push ( '\'' ) ;
496
- Literal :: _new ( escaped)
497
- }
503
+ suffixed_numbers ! {
504
+ u8_suffixed => u8 ,
505
+ u16_suffixed => u16 ,
506
+ u32_suffixed => u32 ,
507
+ u64_suffixed => u64 ,
508
+ usize_suffixed => usize ,
509
+ i8_suffixed => i8 ,
510
+ i16_suffixed => i16 ,
511
+ i32_suffixed => i32 ,
512
+ i64_suffixed => i64 ,
513
+ isize_suffixed => isize ,
514
+
515
+ f32_suffixed => f32 ,
516
+ f64_suffixed => f64 ,
517
+ }
518
+
519
+ unsuffixed_numbers ! {
520
+ u8_unsuffixed => u8 ,
521
+ u16_unsuffixed => u16 ,
522
+ u32_unsuffixed => u32 ,
523
+ u64_unsuffixed => u64 ,
524
+ usize_unsuffixed => usize ,
525
+ i8_unsuffixed => i8 ,
526
+ i16_unsuffixed => i16 ,
527
+ i32_unsuffixed => i32 ,
528
+ i64_unsuffixed => i64 ,
529
+ isize_unsuffixed => isize ,
530
+ }
531
+
532
+ pub fn f32_unsuffixed ( f : f32 ) -> Literal {
533
+ let mut s = f. to_string ( ) ;
534
+ if !s. contains ( "." ) {
535
+ s. push_str ( ".0" ) ;
498
536
}
537
+ Literal :: _new ( s)
538
+ }
539
+
540
+ pub fn f64_unsuffixed ( f : f64 ) -> Literal {
541
+ let mut s = f. to_string ( ) ;
542
+ if !s. contains ( "." ) {
543
+ s. push_str ( ".0" ) ;
544
+ }
545
+ Literal :: _new ( s)
546
+ }
547
+
548
+ pub fn string ( t : & str ) -> Literal {
549
+ let mut s = t. chars ( )
550
+ . flat_map ( |c| c. escape_default ( ) )
551
+ . collect :: < String > ( ) ;
552
+ s. push ( '"' ) ;
553
+ s. insert ( 0 , '"' ) ;
554
+ Literal :: _new ( s)
555
+ }
556
+
557
+ pub fn character ( t : char ) -> Literal {
558
+ Literal :: _new ( format ! ( "'{}'" , t. escape_default( ) . collect:: <String >( ) ) )
499
559
}
500
560
501
561
pub fn byte_string ( bytes : & [ u8 ] ) -> Literal {
@@ -516,21 +576,6 @@ impl Literal {
516
576
Literal :: _new ( escaped)
517
577
}
518
578
519
- pub fn float ( n : f64 ) -> Literal {
520
- if !n. is_finite ( ) {
521
- panic ! ( "Invalid float literal {}" , n) ;
522
- }
523
- let mut s = n. to_string ( ) ;
524
- if !s. contains ( '.' ) {
525
- s += ".0" ;
526
- }
527
- Literal :: _new ( s)
528
- }
529
-
530
- pub fn integer ( s : i64 ) -> Literal {
531
- Literal :: _new ( s. to_string ( ) )
532
- }
533
-
534
579
pub fn span ( & self ) -> Span {
535
580
self . span
536
581
}
@@ -546,54 +591,6 @@ impl fmt::Display for Literal {
546
591
}
547
592
}
548
593
549
- macro_rules! ints {
550
- ( $( $t: ty, ) * ) => { $(
551
- impl From <$t> for Literal {
552
- fn from( t: $t) -> Literal {
553
- Literal :: _new( format!( concat!( "{}" , stringify!( $t) ) , t) )
554
- }
555
- }
556
- ) * }
557
- }
558
-
559
- ints ! {
560
- u8 , u16 , u32 , u64 , usize ,
561
- i8 , i16 , i32 , i64 , isize ,
562
- }
563
-
564
- macro_rules! floats {
565
- ( $( $t: ty, ) * ) => { $(
566
- impl From <$t> for Literal {
567
- fn from( t: $t) -> Literal {
568
- assert!( !t. is_nan( ) ) ;
569
- assert!( !t. is_infinite( ) ) ;
570
- Literal :: _new( format!( concat!( "{}" , stringify!( $t) ) , t) )
571
- }
572
- }
573
- ) * }
574
- }
575
-
576
- floats ! {
577
- f32 , f64 ,
578
- }
579
-
580
- impl < ' a > From < & ' a str > for Literal {
581
- fn from ( t : & ' a str ) -> Literal {
582
- let mut s = t. chars ( )
583
- . flat_map ( |c| c. escape_default ( ) )
584
- . collect :: < String > ( ) ;
585
- s. push ( '"' ) ;
586
- s. insert ( 0 , '"' ) ;
587
- Literal :: _new ( s)
588
- }
589
- }
590
-
591
- impl From < char > for Literal {
592
- fn from ( t : char ) -> Literal {
593
- Literal :: _new ( format ! ( "'{}'" , t. escape_default( ) . collect:: <String >( ) ) )
594
- }
595
- }
596
-
597
594
named ! ( token_stream -> :: TokenStream , map!(
598
595
many0!( token_tree) ,
599
596
|trees| :: TokenStream :: _new( TokenStream { inner: trees } )
0 commit comments