@@ -246,7 +246,7 @@ pub mod token_stream {
246
246
247
247
/// `quote!(..)` accepts arbitrary tokens and expands into a `TokenStream` describing the input.
248
248
/// For example, `quote!(a + b)` will produce a expression, that, when evaluated, constructs
249
- /// the `TokenStream` `[Word("a"), Op ('+', Alone), Word("b")]`.
249
+ /// the `TokenStream` `[Word("a"), Punct ('+', Alone), Word("b")]`.
250
250
///
251
251
/// Unquoting is done with `$`, and works by taking the single next ident as the unquoted term.
252
252
/// To quote `$` itself, use `$$`.
@@ -499,17 +499,9 @@ pub enum TokenTree {
499
499
/// A token stream surrounded by bracket delimiters.
500
500
Group ( Group ) ,
501
501
/// An identifier or lifetime identifier.
502
- ///
503
- /// REVIEW Maybe let's name it `Ident` instead of inventing a new term, it's named "identifier"
504
- /// REVIEW everywhere in the compiler, including `ident` in `macro`/`macro_rules!` DSL.
505
- Term ( Term ) ,
502
+ Ident ( Ident ) ,
506
503
/// A single punctuation character (`+`, `,`, `$`, etc.).
507
- ///
508
- /// REVIEW This is not an operator, operators are more narrow set, they also can be
509
- /// REVIEW multicharacter, this is punctuation, even the comment says so!
510
- /// REVIEW @dtolnay suggested `Punct` in the original implementation PR too, and it was
511
- /// REVIEW received positively, but the renaming never actually happened.
512
- Op ( Op ) ,
504
+ Punct ( Punct ) ,
513
505
/// A literal character (`'a'`), string (`"hello"`), number (`2.3`), etc.
514
506
Literal ( Literal ) ,
515
507
}
@@ -526,8 +518,8 @@ impl TokenTree {
526
518
pub fn span ( & self ) -> Span {
527
519
match * self {
528
520
TokenTree :: Group ( ref t) => t. span ( ) ,
529
- TokenTree :: Term ( ref t) => t. span ( ) ,
530
- TokenTree :: Op ( ref t) => t. span ( ) ,
521
+ TokenTree :: Ident ( ref t) => t. span ( ) ,
522
+ TokenTree :: Punct ( ref t) => t. span ( ) ,
531
523
TokenTree :: Literal ( ref t) => t. span ( ) ,
532
524
}
533
525
}
@@ -541,8 +533,8 @@ impl TokenTree {
541
533
pub fn set_span ( & mut self , span : Span ) {
542
534
match * self {
543
535
TokenTree :: Group ( ref mut t) => t. set_span ( span) ,
544
- TokenTree :: Term ( ref mut t) => t. set_span ( span) ,
545
- TokenTree :: Op ( ref mut t) => t. set_span ( span) ,
536
+ TokenTree :: Ident ( ref mut t) => t. set_span ( span) ,
537
+ TokenTree :: Punct ( ref mut t) => t. set_span ( span) ,
546
538
TokenTree :: Literal ( ref mut t) => t. set_span ( span) ,
547
539
}
548
540
}
@@ -556,16 +548,16 @@ impl fmt::Debug for TokenTree {
556
548
// so don't bother with an extra layer of indirection
557
549
match * self {
558
550
TokenTree :: Group ( ref tt) => tt. fmt ( f) ,
559
- TokenTree :: Term ( ref tt) => tt. fmt ( f) ,
560
- TokenTree :: Op ( ref tt) => tt. fmt ( f) ,
551
+ TokenTree :: Ident ( ref tt) => tt. fmt ( f) ,
552
+ TokenTree :: Punct ( ref tt) => tt. fmt ( f) ,
561
553
TokenTree :: Literal ( ref tt) => tt. fmt ( f) ,
562
554
}
563
555
}
564
556
}
565
557
566
558
/// REVIEW the impls below are kind of `From<T> for Option<T>`, not strictly necessary,
567
559
/// REVIEW but convenient. No harm, I guess. I'd actually like to see impls
568
- /// REVIEW `From<Group/Term/Op /Literal> for TokenStream` to avoid stuttering like
560
+ /// REVIEW `From<Group/Ident/Punct /Literal> for TokenStream` to avoid stuttering like
569
561
/// REVIEW `TokenTree::Literal(Literal::string("lalala")).into()`.
570
562
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
571
563
impl From < Group > for TokenTree {
@@ -575,16 +567,16 @@ impl From<Group> for TokenTree {
575
567
}
576
568
577
569
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
578
- impl From < Term > for TokenTree {
579
- fn from ( g : Term ) -> TokenTree {
580
- TokenTree :: Term ( g)
570
+ impl From < Ident > for TokenTree {
571
+ fn from ( g : Ident ) -> TokenTree {
572
+ TokenTree :: Ident ( g)
581
573
}
582
574
}
583
575
584
576
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
585
- impl From < Op > for TokenTree {
586
- fn from ( g : Op ) -> TokenTree {
587
- TokenTree :: Op ( g)
577
+ impl From < Punct > for TokenTree {
578
+ fn from ( g : Punct ) -> TokenTree {
579
+ TokenTree :: Punct ( g)
588
580
}
589
581
}
590
582
@@ -603,8 +595,8 @@ impl fmt::Display for TokenTree {
603
595
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
604
596
match * self {
605
597
TokenTree :: Group ( ref t) => t. fmt ( f) ,
606
- TokenTree :: Term ( ref t) => t. fmt ( f) ,
607
- TokenTree :: Op ( ref t) => t. fmt ( f) ,
598
+ TokenTree :: Ident ( ref t) => t. fmt ( f) ,
599
+ TokenTree :: Punct ( ref t) => t. fmt ( f) ,
608
600
TokenTree :: Literal ( ref t) => t. fmt ( f) ,
609
601
}
610
602
}
@@ -703,37 +695,32 @@ impl fmt::Display for Group {
703
695
}
704
696
}
705
697
706
- /// An `Op ` is an single punctuation character like `+`, `-` or `#`.
698
+ /// An `Punct ` is an single punctuation character like `+`, `-` or `#`.
707
699
///
708
- /// Multicharacter operators like `+=` are represented as two instances of `Op ` with different
700
+ /// Multicharacter operators like `+=` are represented as two instances of `Punct ` with different
709
701
/// forms of `Spacing` returned.
710
702
///
711
- /// REVIEW This is not an operator, operators are more narrow set, they also can be
712
- /// REVIEW multicharacter, this is punctuation, even the comment says so!
713
- /// REVIEW @dtolnay suggested `Punct` in the original implementation PR too, and it was
714
- /// REVIEW received positively, but the renaming never actually happened.
715
- ///
716
- /// REVIEW We should guarantee that `Op` contains a valid punctuation character permitted by
703
+ /// REVIEW We should guarantee that `Punct` contains a valid punctuation character permitted by
717
704
/// REVIEW the language and not a random unicode code point. The check is already performed in
718
705
/// REVIEW `TokenTree::to_internal`, but we should do it on construction.
719
- /// REVIEW `Op ` can also avoid using `char` internally and keep an u8-like enum.
706
+ /// REVIEW `Punct ` can also avoid using `char` internally and keep an u8-like enum.
720
707
///
721
708
/// REVIEW ATTENTION: `Copy` impl on a struct with private fields.
722
- /// REVIEW Do we want to guarantee `Op ` to be `Copy`?
709
+ /// REVIEW Do we want to guarantee `Punct ` to be `Copy`?
723
710
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
724
711
#[ derive( Copy , Clone , Debug ) ]
725
- pub struct Op {
726
- op : char ,
712
+ pub struct Punct {
713
+ ch : char ,
727
714
spacing : Spacing ,
728
715
span : Span ,
729
716
}
730
717
731
718
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
732
- impl !Send for Op { }
719
+ impl !Send for Punct { }
733
720
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
734
- impl !Sync for Op { }
721
+ impl !Sync for Punct { }
735
722
736
- /// Whether an `Op ` is followed immediately by another `Op ` or
723
+ /// Whether an `Punct ` is followed immediately by another `Punct ` or
737
724
/// followed by another token or whitespace.
738
725
#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
739
726
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
@@ -744,10 +731,10 @@ pub enum Spacing {
744
731
Joint ,
745
732
}
746
733
747
- impl Op {
748
- /// Creates a new `Op ` from the given character and spacing.
734
+ impl Punct {
735
+ /// Creates a new `Punct ` from the given character and spacing.
749
736
///
750
- /// The returned `Op ` will have the default span of `Span::call_site()`
737
+ /// The returned `Punct ` will have the default span of `Span::call_site()`
751
738
/// which can be further configured with the `set_span` method below.
752
739
///
753
740
/// REVIEW Why we even use `char` here? There's no reason to use unicode here.
@@ -756,9 +743,9 @@ impl Op {
756
743
/// REVIEW TO_DO Do input validation on construction, the argument should be a valid punctuation
757
744
/// REVIEW character permitted by the language.
758
745
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
759
- pub fn new ( op : char , spacing : Spacing ) -> Op {
760
- Op {
761
- op : op ,
746
+ pub fn new ( ch : char , spacing : Spacing ) -> Punct {
747
+ Punct {
748
+ ch : ch ,
762
749
spacing : spacing,
763
750
span : Span :: call_site ( ) ,
764
751
}
@@ -770,12 +757,12 @@ impl Op {
770
757
/// REVIEW except for maybe future compatibility in case Rust turns into APL,
771
758
/// REVIEW but if it's more convenient to use `char` then that's okay.
772
759
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
773
- pub fn op ( & self ) -> char {
774
- self . op
760
+ pub fn as_char ( & self ) -> char {
761
+ self . ch
775
762
}
776
763
777
764
/// Returns the spacing of this punctuation character, indicating whether it's immediately
778
- /// followed by another `Op ` in the token stream, so they can potentially be combined into
765
+ /// followed by another `Punct ` in the token stream, so they can potentially be combined into
779
766
/// a multicharacter operator (`Joint`), or it's followed by some other token or whitespace
780
767
/// (`Alone`) so the operator has certainly ended.
781
768
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
@@ -799,43 +786,40 @@ impl Op {
799
786
/// Prints the punctuation character as a string that should be losslessly convertible
800
787
/// back into the same character.
801
788
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
802
- impl fmt:: Display for Op {
789
+ impl fmt:: Display for Punct {
803
790
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
804
791
TokenStream :: from ( TokenTree :: from ( self . clone ( ) ) ) . fmt ( f)
805
792
}
806
793
}
807
794
808
795
/// An identifier (`ident`) or lifetime identifier (`'ident`).
809
796
///
810
- /// REVIEW We should guarantee that `Term ` contains a valid identifier permitted by
797
+ /// REVIEW We should guarantee that `Ident ` contains a valid identifier permitted by
811
798
/// REVIEW the language and not a random unicode string, at least for a start.
812
799
///
813
- /// REVIEW Maybe let's name it `Ident` instead of inventing a new term, it's named "identifier"
814
- /// REVIEW everywhere in the compiler, including `ident` in `macro`/`macro_rules!` DSL.
815
- ///
816
800
/// REVIEW We need to support raw identifiers here (`r#ident`) or at least be future compatible
817
801
/// REVIEW with them. Currently they are supported using "string typing" - if string "r#ident" is
818
- /// REVIEW passed to `Term ::new` it will be interpreted as a raw identifier later on, we should add
819
- /// REVIEW a field `is_raw` and a separate constructor for it (`Term ::new_raw` or something) and
802
+ /// REVIEW passed to `Ident ::new` it will be interpreted as a raw identifier later on, we should add
803
+ /// REVIEW a field `is_raw` and a separate constructor for it (`Ident ::new_raw` or something) and
820
804
/// REVIEW keep it unstable until raw identifiers are stabilized.
821
805
///
822
806
/// REVIEW ATTENTION: `Copy` impl on a struct with private fields.
823
- /// REVIEW Do we want to guarantee `Term ` to be `Copy`?
807
+ /// REVIEW Do we want to guarantee `Ident ` to be `Copy`?
824
808
#[ derive( Copy , Clone , Debug ) ]
825
809
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
826
- pub struct Term {
810
+ pub struct Ident {
827
811
// REVIEW(INTERNAL) Symbol + Span is actually `ast::Ident`! We can use it here.
828
812
sym : Symbol ,
829
813
span : Span ,
830
814
}
831
815
832
816
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
833
- impl !Send for Term { }
817
+ impl !Send for Ident { }
834
818
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
835
- impl !Sync for Term { }
819
+ impl !Sync for Ident { }
836
820
837
- impl Term {
838
- /// Creates a new `Term ` with the given `string` as well as the specified
821
+ impl Ident {
822
+ /// Creates a new `Ident ` with the given `string` as well as the specified
839
823
/// `span`.
840
824
///
841
825
/// Note that `span`, currently in rustc, configures the hygiene information
@@ -856,8 +840,8 @@ impl Term {
856
840
/// REVIEW TO_DO Do input validation, the argument should be a valid identifier or
857
841
/// REVIEW lifetime identifier.
858
842
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
859
- pub fn new ( string : & str , span : Span ) -> Term {
860
- Term {
843
+ pub fn new ( string : & str , span : Span ) -> Ident {
844
+ Ident {
861
845
sym : Symbol :: intern ( string) ,
862
846
span,
863
847
}
@@ -870,14 +854,14 @@ impl Term {
870
854
unsafe { & * ( & * self . sym . as_str ( ) as * const str ) }
871
855
}
872
856
873
- /// Returns the span of this `Term `, encompassing the entire string returned
857
+ /// Returns the span of this `Ident `, encompassing the entire string returned
874
858
/// by `as_str`.
875
859
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
876
860
pub fn span ( & self ) -> Span {
877
861
self . span
878
862
}
879
863
880
- /// Configures the span of this `Term `, possibly changing its hygiene context.
864
+ /// Configures the span of this `Ident `, possibly changing its hygiene context.
881
865
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
882
866
pub fn set_span ( & mut self , span : Span ) {
883
867
self . span = span;
@@ -887,7 +871,7 @@ impl Term {
887
871
/// Prints the identifier as a string that should be losslessly convertible
888
872
/// back into the same identifier.
889
873
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
890
- impl fmt:: Display for Term {
874
+ impl fmt:: Display for Ident {
891
875
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
892
876
self . sym . as_str ( ) . fmt ( f)
893
877
}
@@ -896,7 +880,7 @@ impl fmt::Display for Term {
896
880
/// A literal string (`"hello"`), byte string (`b"hello"`),
897
881
/// character (`'a'`), byte character (`b'a'`), an integer or floating point number
898
882
/// with or without a suffix (`1`, `1u8`, `2.3`, `2.3f32`).
899
- /// Boolean literals like `true` and `false` do not belong here, they are `Term `s.
883
+ /// Boolean literals like `true` and `false` do not belong here, they are `Ident `s.
900
884
#[ derive( Clone , Debug ) ]
901
885
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
902
886
pub struct Literal {
@@ -1182,15 +1166,15 @@ impl TokenTree {
1182
1166
} )
1183
1167
}
1184
1168
macro_rules! op {
1185
- ( $a: expr) => ( tt!( Op :: new( $a, op_kind) ) ) ;
1169
+ ( $a: expr) => ( tt!( Punct :: new( $a, op_kind) ) ) ;
1186
1170
( $a: expr, $b: expr) => ( {
1187
- stack. push( tt!( Op :: new( $b, op_kind) ) ) ;
1188
- tt!( Op :: new( $a, Spacing :: Joint ) )
1171
+ stack. push( tt!( Punct :: new( $b, op_kind) ) ) ;
1172
+ tt!( Punct :: new( $a, Spacing :: Joint ) )
1189
1173
} ) ;
1190
1174
( $a: expr, $b: expr, $c: expr) => ( {
1191
- stack. push( tt!( Op :: new( $c, op_kind) ) ) ;
1192
- stack. push( tt!( Op :: new( $b, Spacing :: Joint ) ) ) ;
1193
- tt!( Op :: new( $a, Spacing :: Joint ) )
1175
+ stack. push( tt!( Punct :: new( $c, op_kind) ) ) ;
1176
+ stack. push( tt!( Punct :: new( $b, Spacing :: Joint ) ) ) ;
1177
+ tt!( Punct :: new( $a, Spacing :: Joint ) )
1194
1178
} )
1195
1179
}
1196
1180
@@ -1243,25 +1227,25 @@ impl TokenTree {
1243
1227
Question => op ! ( '?' ) ,
1244
1228
1245
1229
Ident ( ident, false ) | Lifetime ( ident) => {
1246
- tt ! ( Term :: new( & ident. name. as_str( ) , Span ( span) ) )
1230
+ tt ! ( self :: Ident :: new( & ident. name. as_str( ) , Span ( span) ) )
1247
1231
}
1248
1232
Ident ( ident, true ) => {
1249
- tt ! ( Term :: new( & format!( "r#{}" , ident) , Span ( span) ) )
1233
+ tt ! ( self :: Ident :: new( & format!( "r#{}" , ident) , Span ( span) ) )
1250
1234
}
1251
1235
Literal ( lit, suffix) => tt ! ( self :: Literal { lit, suffix, span: Span ( span) } ) ,
1252
1236
DocComment ( c) => {
1253
1237
let style = comments:: doc_comment_style ( & c. as_str ( ) ) ;
1254
1238
let stripped = comments:: strip_doc_comment_decoration ( & c. as_str ( ) ) ;
1255
1239
let stream = vec ! [
1256
- tt!( Term :: new( "doc" , Span ( span) ) ) ,
1257
- tt!( Op :: new( '=' , Spacing :: Alone ) ) ,
1240
+ tt!( self :: Ident :: new( "doc" , Span ( span) ) ) ,
1241
+ tt!( Punct :: new( '=' , Spacing :: Alone ) ) ,
1258
1242
tt!( self :: Literal :: string( & stripped) ) ,
1259
1243
] . into_iter ( ) . collect ( ) ;
1260
1244
stack. push ( tt ! ( Group :: new( Delimiter :: Bracket , stream) ) ) ;
1261
1245
if style == ast:: AttrStyle :: Inner {
1262
- stack. push ( tt ! ( Op :: new( '!' , Spacing :: Alone ) ) ) ;
1246
+ stack. push ( tt ! ( Punct :: new( '!' , Spacing :: Alone ) ) ) ;
1263
1247
}
1264
- tt ! ( Op :: new( '#' , Spacing :: Alone ) )
1248
+ tt ! ( Punct :: new( '#' , Spacing :: Alone ) )
1265
1249
}
1266
1250
1267
1251
Interpolated ( _) => {
@@ -1281,15 +1265,15 @@ impl TokenTree {
1281
1265
use syntax:: parse:: token:: * ;
1282
1266
use syntax:: tokenstream:: { TokenTree , Delimited } ;
1283
1267
1284
- let ( op , kind, span) = match self {
1285
- self :: TokenTree :: Op ( tt) => ( tt. op ( ) , tt. spacing ( ) , tt. span ( ) ) ,
1268
+ let ( ch , kind, span) = match self {
1269
+ self :: TokenTree :: Punct ( tt) => ( tt. as_char ( ) , tt. spacing ( ) , tt. span ( ) ) ,
1286
1270
self :: TokenTree :: Group ( tt) => {
1287
1271
return TokenTree :: Delimited ( tt. span . 0 , Delimited {
1288
1272
delim : tt. delimiter . to_internal ( ) ,
1289
1273
tts : tt. stream . 0 . into ( ) ,
1290
1274
} ) . into ( ) ;
1291
1275
} ,
1292
- self :: TokenTree :: Term ( tt) => {
1276
+ self :: TokenTree :: Ident ( tt) => {
1293
1277
let ident = ast:: Ident :: new ( tt. sym , tt. span . 0 ) ;
1294
1278
let sym_str = tt. sym . to_string ( ) ;
1295
1279
let token = if sym_str. starts_with ( "'" ) {
@@ -1337,7 +1321,7 @@ impl TokenTree {
1337
1321
}
1338
1322
} ;
1339
1323
1340
- let token = match op {
1324
+ let token = match ch {
1341
1325
'=' => Eq ,
1342
1326
'<' => Lt ,
1343
1327
'>' => Gt ,
@@ -1359,7 +1343,7 @@ impl TokenTree {
1359
1343
'#' => Pound ,
1360
1344
'$' => Dollar ,
1361
1345
'?' => Question ,
1362
- _ => panic ! ( "unsupported character {}" , op ) ,
1346
+ _ => panic ! ( "unsupported character {}" , ch ) ,
1363
1347
} ;
1364
1348
1365
1349
let tree = TokenTree :: Token ( span. 0 , token) ;
0 commit comments