@@ -67,7 +67,6 @@ use std::{ascii, fmt, iter};
67
67
use std:: path:: PathBuf ;
68
68
use std:: str:: FromStr ;
69
69
70
- use syntax:: parse:: token;
71
70
use syntax:: symbol:: Symbol ;
72
71
73
72
/// The main type provided by this crate, representing an abstract stream of
@@ -429,14 +428,39 @@ pub enum Spacing {
429
428
}
430
429
431
430
/// A literal character (`'a'`), string (`"hello"`), or number (`2.3`).
432
- #[ derive( Clone , Debug ) ]
431
+ #[ derive( Clone ) ]
432
+ #[ unstable( feature = "proc_macro" , issue = "38356" ) ]
433
+ pub struct Literal {
434
+ #[ unstable( feature = "proc_macro_internals" , issue = "27812" ) ]
435
+ #[ doc( hidden) ]
436
+ pub kind : LiteralKind ,
437
+
438
+ #[ unstable( feature = "proc_macro_internals" , issue = "27812" ) ]
439
+ #[ doc( hidden) ]
440
+ pub contents : Term ,
441
+
442
+ #[ unstable( feature = "proc_macro_internals" , issue = "27812" ) ]
443
+ #[ doc( hidden) ]
444
+ pub suffix : Option < Term > ,
445
+ }
446
+
433
447
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
434
- pub struct Literal ( token:: Token ) ;
448
+ impl fmt:: Debug for Literal {
449
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
450
+ fmt:: Debug :: fmt ( & TokenTree {
451
+ kind : TokenNode :: Literal ( self . clone ( ) ) ,
452
+ span : Span :: def_site ( )
453
+ } , f)
454
+ }
455
+ }
435
456
436
457
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
437
458
impl fmt:: Display for Literal {
438
459
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
439
- TokenTree { kind : TokenNode :: Literal ( self . clone ( ) ) , span : Span :: def_site ( ) } . fmt ( f)
460
+ fmt:: Display :: fmt ( & TokenTree {
461
+ kind : TokenNode :: Literal ( self . clone ( ) ) ,
462
+ span : Span :: def_site ( )
463
+ } , f)
440
464
}
441
465
}
442
466
@@ -454,13 +478,20 @@ impl Literal {
454
478
/// Integer literal
455
479
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
456
480
pub fn integer ( n : i128 ) -> Literal {
457
- Literal ( token:: Literal ( token:: Lit :: Integer ( Symbol :: intern ( & n. to_string ( ) ) ) , None ) )
481
+ Literal {
482
+ kind : LiteralKind :: Integer ,
483
+ contents : Term :: intern ( & n. to_string ( ) ) ,
484
+ suffix : None ,
485
+ }
458
486
}
459
487
460
488
int_literals ! ( u8 , i8 , u16 , i16 , u32 , i32 , u64 , i64 , usize , isize ) ;
461
489
fn typed_integer ( n : i128 , kind : & ' static str ) -> Literal {
462
- Literal ( token:: Literal ( token:: Lit :: Integer ( Symbol :: intern ( & n. to_string ( ) ) ) ,
463
- Some ( Symbol :: intern ( kind) ) ) )
490
+ Literal {
491
+ kind : LiteralKind :: Integer ,
492
+ contents : Term :: intern ( & n. to_string ( ) ) ,
493
+ suffix : Some ( Term :: intern ( kind) ) ,
494
+ }
464
495
}
465
496
466
497
/// Floating point literal.
@@ -469,7 +500,11 @@ impl Literal {
469
500
if !n. is_finite ( ) {
470
501
panic ! ( "Invalid float literal {}" , n) ;
471
502
}
472
- Literal ( token:: Literal ( token:: Lit :: Float ( Symbol :: intern ( & n. to_string ( ) ) ) , None ) )
503
+ Literal {
504
+ kind : LiteralKind :: Float ,
505
+ contents : Term :: intern ( & n. to_string ( ) ) ,
506
+ suffix : None ,
507
+ }
473
508
}
474
509
475
510
/// Floating point literal.
@@ -478,8 +513,11 @@ impl Literal {
478
513
if !n. is_finite ( ) {
479
514
panic ! ( "Invalid f32 literal {}" , n) ;
480
515
}
481
- Literal ( token:: Literal ( token:: Lit :: Float ( Symbol :: intern ( & n. to_string ( ) ) ) ,
482
- Some ( Symbol :: intern ( "f32" ) ) ) )
516
+ Literal {
517
+ kind : LiteralKind :: Float ,
518
+ contents : Term :: intern ( & n. to_string ( ) ) ,
519
+ suffix : Some ( Term :: intern ( "f32" ) ) ,
520
+ }
483
521
}
484
522
485
523
/// Floating point literal.
@@ -488,8 +526,11 @@ impl Literal {
488
526
if !n. is_finite ( ) {
489
527
panic ! ( "Invalid f64 literal {}" , n) ;
490
528
}
491
- Literal ( token:: Literal ( token:: Lit :: Float ( Symbol :: intern ( & n. to_string ( ) ) ) ,
492
- Some ( Symbol :: intern ( "f64" ) ) ) )
529
+ Literal {
530
+ kind : LiteralKind :: Float ,
531
+ contents : Term :: intern ( & n. to_string ( ) ) ,
532
+ suffix : Some ( Term :: intern ( "f64" ) ) ,
533
+ }
493
534
}
494
535
495
536
/// String literal.
@@ -499,89 +540,54 @@ impl Literal {
499
540
for ch in string. chars ( ) {
500
541
escaped. extend ( ch. escape_debug ( ) ) ;
501
542
}
502
- Literal ( token:: Literal ( token:: Lit :: Str_ ( Symbol :: intern ( & escaped) ) , None ) )
543
+ Literal {
544
+ kind : LiteralKind :: Str_ ,
545
+ contents : Term :: intern ( & escaped) ,
546
+ suffix : None ,
547
+ }
503
548
}
504
549
505
550
/// Character literal.
506
551
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
507
552
pub fn character ( ch : char ) -> Literal {
508
553
let mut escaped = String :: new ( ) ;
509
554
escaped. extend ( ch. escape_unicode ( ) ) ;
510
- Literal ( token:: Literal ( token:: Lit :: Char ( Symbol :: intern ( & escaped) ) , None ) )
555
+ Literal {
556
+ kind : LiteralKind :: Char ,
557
+ contents : Term :: intern ( & escaped) ,
558
+ suffix : None ,
559
+ }
511
560
}
512
561
513
562
/// Byte string literal.
514
563
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
515
564
pub fn byte_string ( bytes : & [ u8 ] ) -> Literal {
516
565
let string = bytes. iter ( ) . cloned ( ) . flat_map ( ascii:: escape_default)
517
566
. map ( Into :: < char > :: into) . collect :: < String > ( ) ;
518
- Literal ( token:: Literal ( token:: Lit :: ByteStr ( Symbol :: intern ( & string) ) , None ) )
519
- }
520
- }
521
-
522
- macro_rules! literals {
523
- ( $( $i: ident) ,* ; $( $raw: ident) ,* ) => {
524
- #[ unstable( feature = "proc_macro_internals" , issue = "27812" ) ]
525
- #[ doc( hidden) ]
526
- pub enum LiteralKind {
527
- $( $i, ) *
528
- $( $raw( usize ) , ) *
529
- }
530
-
531
- impl LiteralKind {
532
- #[ unstable( feature = "proc_macro_internals" , issue = "27812" ) ]
533
- #[ doc( hidden) ]
534
- pub fn with_contents_and_suffix( self , contents: Term , suffix: Option <Term >)
535
- -> Literal {
536
- let contents = contents. 0 ;
537
- let suffix = suffix. map( |t| t. 0 ) ;
538
- match self {
539
- $( LiteralKind :: $i => {
540
- Literal ( token:: Literal ( token:: Lit :: $i( contents) , suffix) )
541
- } ) *
542
- $( LiteralKind :: $raw( n) => {
543
- Literal ( token:: Literal ( token:: Lit :: $raw( contents, n) , suffix) )
544
- } ) *
545
- }
546
- }
547
- }
548
-
549
- impl Literal {
550
- fn kind( & self ) -> LiteralKind {
551
- let lit = match self . 0 {
552
- token:: Literal ( lit, _) => lit,
553
- _ => panic!( "unsupported literal {:?}" , self . 0 ) ,
554
- } ;
555
-
556
- match lit {
557
- $( token:: Lit :: $i( _) => LiteralKind :: $i, ) *
558
- $( token:: Lit :: $raw( _, n) => LiteralKind :: $raw( n) , ) *
559
- }
560
- }
561
-
562
- fn contents( & self ) -> Term {
563
- let lit = match self . 0 {
564
- token:: Literal ( lit, _) => lit,
565
- _ => panic!( "unsupported literal {:?}" , self . 0 ) ,
566
- } ;
567
-
568
- match lit {
569
- $( token:: Lit :: $i( contents) ) |* |
570
- $( token:: Lit :: $raw( contents, _) ) |* => Term ( contents)
571
- }
572
- }
573
-
574
- fn suffix( & self ) -> Option <Term > {
575
- match self . 0 {
576
- token:: Literal ( _, suffix) => suffix. map( Term ) ,
577
- _ => panic!( "unsupported literal {:?}" , self . 0 ) ,
578
- }
579
- }
567
+ Literal {
568
+ kind : LiteralKind :: ByteStr ,
569
+ contents : Term :: intern ( & string) ,
570
+ suffix : None ,
580
571
}
581
572
}
582
573
}
583
574
584
- literals ! ( Byte , Char , Float , Str_ , Integer , ByteStr ; StrRaw , ByteStrRaw ) ;
575
+ #[ derive( Copy , Clone ) ]
576
+ #[ unstable( feature = "proc_macro_internals" , issue = "27812" ) ]
577
+ #[ doc( hidden) ]
578
+ pub enum LiteralKind {
579
+ DocComment ,
580
+
581
+ Byte ,
582
+ Char ,
583
+ Float ,
584
+ Str_ ,
585
+ Integer ,
586
+ ByteStr ,
587
+
588
+ StrRaw ( usize ) ,
589
+ ByteStrRaw ( usize ) ,
590
+ }
585
591
586
592
/// An iterator over `TokenTree`s.
587
593
#[ derive( Clone ) ]
0 commit comments