@@ -67,7 +67,6 @@ use std::{ascii, fmt, iter};
6767use std:: path:: PathBuf ;
6868use std:: str:: FromStr ;
6969
70- use syntax:: parse:: token;
7170use syntax:: symbol:: Symbol ;
7271
7372/// The main type provided by this crate, representing an abstract stream of
@@ -429,14 +428,39 @@ pub enum Spacing {
429428}
430429
431430/// 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+
433447#[ 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+ }
435456
436457#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
437458impl fmt:: Display for Literal {
438459 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)
440464 }
441465}
442466
@@ -454,13 +478,20 @@ impl Literal {
454478 /// Integer literal
455479 #[ unstable( feature = "proc_macro" , issue = "38356" ) ]
456480 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+ }
458486 }
459487
460488 int_literals ! ( u8 , i8 , u16 , i16 , u32 , i32 , u64 , i64 , usize , isize ) ;
461489 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+ }
464495 }
465496
466497 /// Floating point literal.
@@ -469,7 +500,11 @@ impl Literal {
469500 if !n. is_finite ( ) {
470501 panic ! ( "Invalid float literal {}" , n) ;
471502 }
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+ }
473508 }
474509
475510 /// Floating point literal.
@@ -478,8 +513,11 @@ impl Literal {
478513 if !n. is_finite ( ) {
479514 panic ! ( "Invalid f32 literal {}" , n) ;
480515 }
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+ }
483521 }
484522
485523 /// Floating point literal.
@@ -488,8 +526,11 @@ impl Literal {
488526 if !n. is_finite ( ) {
489527 panic ! ( "Invalid f64 literal {}" , n) ;
490528 }
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+ }
493534 }
494535
495536 /// String literal.
@@ -499,89 +540,54 @@ impl Literal {
499540 for ch in string. chars ( ) {
500541 escaped. extend ( ch. escape_debug ( ) ) ;
501542 }
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+ }
503548 }
504549
505550 /// Character literal.
506551 #[ unstable( feature = "proc_macro" , issue = "38356" ) ]
507552 pub fn character ( ch : char ) -> Literal {
508553 let mut escaped = String :: new ( ) ;
509554 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+ }
511560 }
512561
513562 /// Byte string literal.
514563 #[ unstable( feature = "proc_macro" , issue = "38356" ) ]
515564 pub fn byte_string ( bytes : & [ u8 ] ) -> Literal {
516565 let string = bytes. iter ( ) . cloned ( ) . flat_map ( ascii:: escape_default)
517566 . 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 ,
580571 }
581572 }
582573}
583574
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+ }
585591
586592/// An iterator over `TokenTree`s.
587593#[ derive( Clone ) ]
0 commit comments