@@ -314,15 +314,24 @@ pub enum TokenKind {
314
314
Literal ( Lit ) ,
315
315
316
316
/// Identifier token.
317
- /// Do not forget about `NtIdent ` when you want to match on identifiers.
317
+ /// Do not forget about `InterpolatedIdent ` when you want to match on identifiers.
318
318
/// It's recommended to use `Token::(ident,uninterpolate,uninterpolated_span)` to
319
319
/// treat regular and interpolated identifiers in the same way.
320
320
Ident ( Symbol , IdentIsRaw ) ,
321
+ /// This identifier (and its span) is the identifier passed to the
322
+ /// declarative macro. The span in the surrounding `Token` is the span of
323
+ /// the `ident` metavariable in the macro's RHS.
324
+ InterpolatedIdent ( Ident , IdentIsRaw ) ,
325
+
321
326
/// Lifetime identifier token.
322
- /// Do not forget about `NtLifetime ` when you want to match on lifetime identifiers.
327
+ /// Do not forget about `InterpolatedLIfetime ` when you want to match on lifetime identifiers.
323
328
/// It's recommended to use `Token::(lifetime,uninterpolate,uninterpolated_span)` to
324
329
/// treat regular and interpolated lifetime identifiers in the same way.
325
330
Lifetime ( Symbol ) ,
331
+ /// This identifier (and its span) is the lifetime passed to the
332
+ /// declarative macro. The span in the surrounding `Token` is the span of
333
+ /// the `lifetime` metavariable in the macro's RHS.
334
+ InterpolatedLifetime ( Ident ) ,
326
335
327
336
/// An embedded AST node, as produced by a macro. This only exists for
328
337
/// historical reasons. We'd like to get rid of it, for multiple reasons.
@@ -444,8 +453,9 @@ impl Token {
444
453
/// Note that keywords are also identifiers, so they should use this
445
454
/// if they keep spans or perform edition checks.
446
455
pub fn uninterpolated_span ( & self ) -> Span {
447
- match & self . kind {
448
- Interpolated ( nt) => nt. use_span ( ) ,
456
+ match self . kind {
457
+ InterpolatedIdent ( ident, _) | InterpolatedLifetime ( ident) => ident. span ,
458
+ Interpolated ( ref nt) => nt. use_span ( ) ,
449
459
_ => self . span ,
450
460
}
451
461
}
@@ -462,8 +472,16 @@ impl Token {
462
472
true
463
473
}
464
474
465
- OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | DocComment ( ..) | Ident ( ..)
466
- | Lifetime ( ..) | Interpolated ( ..) | Eof => false ,
475
+ OpenDelim ( ..)
476
+ | CloseDelim ( ..)
477
+ | Literal ( ..)
478
+ | DocComment ( ..)
479
+ | Ident ( ..)
480
+ | InterpolatedIdent ( ..)
481
+ | Lifetime ( ..)
482
+ | InterpolatedLifetime ( ..)
483
+ | Interpolated ( ..)
484
+ | Eof => false ,
467
485
}
468
486
}
469
487
@@ -613,14 +631,11 @@ impl Token {
613
631
/// into the regular identifier or lifetime token it refers to,
614
632
/// otherwise returns the original token.
615
633
pub fn uninterpolate ( & self ) -> Cow < ' _ , Token > {
616
- match & self . kind {
617
- Interpolated ( nt) => match & * * nt {
618
- NtIdent ( ident, is_raw) => {
619
- Cow :: Owned ( Token :: new ( Ident ( ident. name , * is_raw) , ident. span ) )
620
- }
621
- NtLifetime ( ident) => Cow :: Owned ( Token :: new ( Lifetime ( ident. name ) , ident. span ) ) ,
622
- _ => Cow :: Borrowed ( self ) ,
623
- } ,
634
+ match self . kind {
635
+ InterpolatedIdent ( ident, is_raw) => {
636
+ Cow :: Owned ( Token :: new ( Ident ( ident. name , is_raw) , ident. span ) )
637
+ }
638
+ InterpolatedLifetime ( ident) => Cow :: Owned ( Token :: new ( Lifetime ( ident. name ) , ident. span ) ) ,
624
639
_ => Cow :: Borrowed ( self ) ,
625
640
}
626
641
}
@@ -629,12 +644,9 @@ impl Token {
629
644
#[ inline]
630
645
pub fn ident ( & self ) -> Option < ( Ident , IdentIsRaw ) > {
631
646
// We avoid using `Token::uninterpolate` here because it's slow.
632
- match & self . kind {
633
- & Ident ( name, is_raw) => Some ( ( Ident :: new ( name, self . span ) , is_raw) ) ,
634
- Interpolated ( nt) => match & * * nt {
635
- NtIdent ( ident, is_raw) => Some ( ( * ident, * is_raw) ) ,
636
- _ => None ,
637
- } ,
647
+ match self . kind {
648
+ Ident ( name, is_raw) => Some ( ( Ident :: new ( name, self . span ) , is_raw) ) ,
649
+ InterpolatedIdent ( ident, is_raw) => Some ( ( ident, is_raw) ) ,
638
650
_ => None ,
639
651
}
640
652
}
@@ -643,12 +655,9 @@ impl Token {
643
655
#[ inline]
644
656
pub fn lifetime ( & self ) -> Option < Ident > {
645
657
// We avoid using `Token::uninterpolate` here because it's slow.
646
- match & self . kind {
647
- & Lifetime ( name) => Some ( Ident :: new ( name, self . span ) ) ,
648
- Interpolated ( nt) => match & * * nt {
649
- NtLifetime ( ident) => Some ( * ident) ,
650
- _ => None ,
651
- } ,
658
+ match self . kind {
659
+ Lifetime ( name) => Some ( Ident :: new ( name, self . span ) ) ,
660
+ InterpolatedLifetime ( ident) => Some ( ident) ,
652
661
_ => None ,
653
662
}
654
663
}
@@ -835,10 +844,36 @@ impl Token {
835
844
_ => return None ,
836
845
} ,
837
846
838
- Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq ( ..) | At | DotDotDot
839
- | DotDotEq | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar
840
- | Question | OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | Ident ( ..)
841
- | Lifetime ( ..) | Interpolated ( ..) | DocComment ( ..) | Eof => return None ,
847
+ Le
848
+ | EqEq
849
+ | Ne
850
+ | Ge
851
+ | AndAnd
852
+ | OrOr
853
+ | Tilde
854
+ | BinOpEq ( ..)
855
+ | At
856
+ | DotDotDot
857
+ | DotDotEq
858
+ | Comma
859
+ | Semi
860
+ | PathSep
861
+ | RArrow
862
+ | LArrow
863
+ | FatArrow
864
+ | Pound
865
+ | Dollar
866
+ | Question
867
+ | OpenDelim ( ..)
868
+ | CloseDelim ( ..)
869
+ | Literal ( ..)
870
+ | Ident ( ..)
871
+ | InterpolatedIdent ( ..)
872
+ | Lifetime ( ..)
873
+ | InterpolatedLifetime ( ..)
874
+ | Interpolated ( ..)
875
+ | DocComment ( ..)
876
+ | Eof => return None ,
842
877
} ;
843
878
844
879
Some ( Token :: new ( kind, self . span . to ( joint. span ) ) )
@@ -861,9 +896,6 @@ pub enum Nonterminal {
861
896
NtPat ( P < ast:: Pat > ) ,
862
897
NtExpr ( P < ast:: Expr > ) ,
863
898
NtTy ( P < ast:: Ty > ) ,
864
- /// The span is for the identifier argument passed to the macro.
865
- NtIdent ( Ident , IdentIsRaw ) ,
866
- NtLifetime ( Ident ) ,
867
899
NtLiteral ( P < ast:: Expr > ) ,
868
900
/// Stuff inside brackets for attributes
869
901
NtMeta ( P < ast:: AttrItem > ) ,
@@ -958,7 +990,6 @@ impl Nonterminal {
958
990
NtPat ( pat) => pat. span ,
959
991
NtExpr ( expr) | NtLiteral ( expr) => expr. span ,
960
992
NtTy ( ty) => ty. span ,
961
- NtIdent ( ident, _) | NtLifetime ( ident) => ident. span ,
962
993
NtMeta ( attr_item) => attr_item. span ( ) ,
963
994
NtPath ( path) => path. span ,
964
995
NtVis ( vis) => vis. span ,
@@ -974,8 +1005,6 @@ impl Nonterminal {
974
1005
NtExpr ( ..) => "expression" ,
975
1006
NtLiteral ( ..) => "literal" ,
976
1007
NtTy ( ..) => "type" ,
977
- NtIdent ( ..) => "identifier" ,
978
- NtLifetime ( ..) => "lifetime" ,
979
1008
NtMeta ( ..) => "attribute" ,
980
1009
NtPath ( ..) => "path" ,
981
1010
NtVis ( ..) => "visibility" ,
@@ -984,18 +1013,12 @@ impl Nonterminal {
984
1013
}
985
1014
986
1015
impl PartialEq for Nonterminal {
987
- fn eq ( & self , rhs : & Self ) -> bool {
988
- match ( self , rhs) {
989
- ( NtIdent ( ident_lhs, is_raw_lhs) , NtIdent ( ident_rhs, is_raw_rhs) ) => {
990
- ident_lhs == ident_rhs && is_raw_lhs == is_raw_rhs
991
- }
992
- ( NtLifetime ( ident_lhs) , NtLifetime ( ident_rhs) ) => ident_lhs == ident_rhs,
993
- // FIXME: Assume that all "complex" nonterminal are not equal, we can't compare them
994
- // correctly based on data from AST. This will prevent them from matching each other
995
- // in macros. The comparison will become possible only when each nonterminal has an
996
- // attached token stream from which it was parsed.
997
- _ => false ,
998
- }
1016
+ fn eq ( & self , _rhs : & Self ) -> bool {
1017
+ // FIXME: Assume that all nonterminals are not equal, we can't compare them
1018
+ // correctly based on data from AST. This will prevent them from matching each other
1019
+ // in macros. The comparison will become possible only when each nonterminal has an
1020
+ // attached token stream from which it was parsed.
1021
+ false
999
1022
}
1000
1023
}
1001
1024
@@ -1008,12 +1031,10 @@ impl fmt::Debug for Nonterminal {
1008
1031
NtPat ( ..) => f. pad ( "NtPat(..)" ) ,
1009
1032
NtExpr ( ..) => f. pad ( "NtExpr(..)" ) ,
1010
1033
NtTy ( ..) => f. pad ( "NtTy(..)" ) ,
1011
- NtIdent ( ..) => f. pad ( "NtIdent(..)" ) ,
1012
1034
NtLiteral ( ..) => f. pad ( "NtLiteral(..)" ) ,
1013
1035
NtMeta ( ..) => f. pad ( "NtMeta(..)" ) ,
1014
1036
NtPath ( ..) => f. pad ( "NtPath(..)" ) ,
1015
1037
NtVis ( ..) => f. pad ( "NtVis(..)" ) ,
1016
- NtLifetime ( ..) => f. pad ( "NtLifetime(..)" ) ,
1017
1038
}
1018
1039
}
1019
1040
}
0 commit comments