@@ -4,7 +4,6 @@ pub use Nonterminal::*;
4
4
pub use TokenKind :: * ;
5
5
6
6
use crate :: ast;
7
- use crate :: ptr:: P ;
8
7
use crate :: util:: case:: Case ;
9
8
10
9
use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
@@ -134,16 +133,27 @@ impl Lit {
134
133
}
135
134
}
136
135
137
- /// Keep this in sync with `Token::can_begin_literal_maybe_minus` excluding unary negation.
136
+ /// Keep this in sync with `Token::can_begin_literal_maybe_minus` and
137
+ /// `Parser::maybe_parse_token_lit` (excluding unary negation).
138
138
pub fn from_token ( token : & Token ) -> Option < Lit > {
139
139
match token. uninterpolate ( ) . kind {
140
140
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => Some ( Lit :: new ( Bool , name, None ) ) ,
141
141
Literal ( token_lit) => Some ( token_lit) ,
142
- Interpolated ( ref nt)
143
- if let NtExpr ( expr) | NtLiteral ( expr) = & nt. 0
144
- && let ast:: ExprKind :: Lit ( token_lit) = expr. kind =>
145
- {
146
- Some ( token_lit)
142
+ OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar ( NonterminalKind :: Literal ) ) ) => {
143
+ panic ! ( "njn: FROM_TOKEN (1)" ) ;
144
+ // if let NtExpr(expr) | NtLiteral(expr) = &**nt
145
+ // && let ast::ExprKind::Lit(token_lit) = expr.kind =>
146
+ // {
147
+ // Some(token_lit)
148
+ // }
149
+ }
150
+ OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar ( NonterminalKind :: Expr ) ) ) => {
151
+ panic ! ( "njn: FROM_TOKEN (2)" ) ;
152
+ // if let NtExpr(expr) | NtLiteral(expr) = &**nt
153
+ // && let ast::ExprKind::Lit(token_lit) = expr.kind =>
154
+ // {
155
+ // Some(token_lit)
156
+ // }
147
157
}
148
158
_ => None ,
149
159
}
@@ -462,6 +472,7 @@ impl Token {
462
472
Token :: new ( Ident ( ident. name , ident. is_raw_guess ( ) . into ( ) ) , ident. span )
463
473
}
464
474
475
+ /// njn: phase this out in favour of Parser::uninterpolated_span
465
476
/// For interpolated tokens, returns a span of the fragment to which the interpolated
466
477
/// token refers. For all other tokens this is just a regular span.
467
478
/// It is particularly important to use this for identifiers and lifetimes
@@ -516,7 +527,6 @@ impl Token {
516
527
PathSep | // global path
517
528
Lifetime ( ..) | // labeled loop
518
529
Pound => true , // expression attributes
519
- Interpolated ( ref nt) => matches ! ( & nt. 0 , NtLiteral ( ..) | NtExpr ( ..) ) ,
520
530
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
521
531
NonterminalKind :: Block |
522
532
NonterminalKind :: Expr |
@@ -543,7 +553,6 @@ impl Token {
543
553
| DotDot | DotDotDot | DotDotEq // ranges
544
554
| Lt | BinOp ( Shl ) // associated path
545
555
| PathSep => true , // global path
546
- Interpolated ( ref nt) => matches ! ( & nt. 0 , NtLiteral ( ..) ) ,
547
556
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
548
557
NonterminalKind :: Block |
549
558
NonterminalKind :: PatParam { .. } |
@@ -584,7 +593,6 @@ impl Token {
584
593
pub fn can_begin_const_arg ( & self ) -> bool {
585
594
match self . kind {
586
595
OpenDelim ( Delimiter :: Brace ) => true ,
587
- Interpolated ( ref nt) => matches ! ( & nt. 0 , NtExpr ( ..) | NtLiteral ( ..) ) ,
588
596
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
589
597
NonterminalKind :: Expr | NonterminalKind :: Block | NonterminalKind :: Literal ,
590
598
) ) ) => true ,
@@ -627,22 +635,24 @@ impl Token {
627
635
///
628
636
/// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
629
637
///
630
- /// Keep this in sync with and `Lit::from_token`, excluding unary negation.
638
+ /// Keep this in sync with `Lit::from_token` and
639
+ /// `Parser::maybe_parse_token_lit` (excluding unary negation).
631
640
pub fn can_begin_literal_maybe_minus ( & self ) -> bool {
632
641
match self . uninterpolate ( ) . kind {
633
642
Literal ( ..) | BinOp ( Minus ) => true ,
634
643
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
635
- Interpolated ( ref nt) => match & nt. 0 {
636
- NtLiteral ( _) => true ,
637
- NtExpr ( e) => match & e. kind {
638
- ast:: ExprKind :: Lit ( _) => true ,
639
- ast:: ExprKind :: Unary ( ast:: UnOp :: Neg , e) => {
640
- matches ! ( & e. kind, ast:: ExprKind :: Lit ( _) )
641
- }
642
- _ => false ,
643
- } ,
644
- _ => false ,
645
- } ,
644
+ // njn: fix up
645
+ // Interpolated(ref nt) => match &nt.0 {
646
+ // NtLiteral(_) => true,
647
+ // NtExpr(e) => match &e.kind {
648
+ // ast::ExprKind::Lit(_) => true,
649
+ // ast::ExprKind::Unary(ast::UnOp::Neg, e) => {
650
+ // matches!(&e.kind, ast::ExprKind::Lit(_))
651
+ // }
652
+ // _ => false,
653
+ // },
654
+ // _ => false,
655
+ // },
646
656
// njn: too simple compared to what's above?
647
657
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
648
658
NonterminalKind :: Literal | NonterminalKind :: Expr ,
@@ -662,7 +672,6 @@ impl Token {
662
672
Cow :: Owned ( Token :: new ( Ident ( ident. name , * is_raw) , ident. span ) )
663
673
}
664
674
NtLifetime ( ident) => Cow :: Owned ( Token :: new ( Lifetime ( ident. name ) , ident. span ) ) ,
665
- _ => Cow :: Borrowed ( self ) ,
666
675
} ,
667
676
_ => Cow :: Borrowed ( self ) ,
668
677
}
@@ -712,22 +721,19 @@ impl Token {
712
721
self . ident ( ) . is_some_and ( |( ident, _) | ident. name == name)
713
722
}
714
723
715
- /// Would `maybe_whole_expr ` in `parser.rs` return `Ok(..)`?
724
+ /// Would `maybe_reparse_metavar_expr ` in `parser.rs` return `Ok(..)`?
716
725
/// That is, is this a pre-parsed expression dropped into the token stream
717
726
/// (which happens while parsing the result of macro expansion)?
718
- pub fn is_whole_expr ( & self ) -> bool {
719
- if let Interpolated ( nt) = & self . kind
720
- && let NtExpr ( _) | NtLiteral ( _) = & nt. 0
721
- {
722
- true
723
- } else if matches ! (
727
+ pub fn is_metavar_expr ( & self ) -> bool {
728
+ matches ! (
724
729
self . is_metavar_seq( ) ,
725
- Some ( NonterminalKind :: Block | NonterminalKind :: Path )
726
- ) {
727
- true
728
- } else {
729
- false
730
- }
730
+ Some (
731
+ NonterminalKind :: Expr
732
+ | NonterminalKind :: Literal
733
+ | NonterminalKind :: Block
734
+ | NonterminalKind :: Path
735
+ )
736
+ )
731
737
}
732
738
733
739
/// Are we at a block from a metavar (`$b:block`)?
@@ -895,10 +901,8 @@ impl PartialEq<TokenKind> for Token {
895
901
#[ derive( Clone , Encodable , Decodable ) ]
896
902
/// For interpolation during macro expansion.
897
903
pub enum Nonterminal {
898
- NtExpr ( P < ast:: Expr > ) ,
899
904
NtIdent ( Ident , IdentIsRaw ) ,
900
905
NtLifetime ( Ident ) ,
901
- NtLiteral ( P < ast:: Expr > ) ,
902
906
}
903
907
904
908
#[ derive( Debug , Copy , Clone , PartialEq , Eq , Encodable , Decodable , Hash , HashStable_Generic ) ]
@@ -982,15 +986,12 @@ impl fmt::Display for NonterminalKind {
982
986
impl Nonterminal {
983
987
pub fn use_span ( & self ) -> Span {
984
988
match self {
985
- NtExpr ( expr) | NtLiteral ( expr) => expr. span ,
986
989
NtIdent ( ident, _) | NtLifetime ( ident) => ident. span ,
987
990
}
988
991
}
989
992
990
993
pub fn descr ( & self ) -> & ' static str {
991
994
match self {
992
- NtExpr ( ..) => "expression" ,
993
- NtLiteral ( ..) => "literal" ,
994
995
NtIdent ( ..) => "identifier" ,
995
996
NtLifetime ( ..) => "lifetime" ,
996
997
}
@@ -1016,9 +1017,7 @@ impl PartialEq for Nonterminal {
1016
1017
impl fmt:: Debug for Nonterminal {
1017
1018
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1018
1019
match * self {
1019
- NtExpr ( ..) => f. pad ( "NtExpr(..)" ) ,
1020
1020
NtIdent ( ..) => f. pad ( "NtIdent(..)" ) ,
1021
- NtLiteral ( ..) => f. pad ( "NtLiteral(..)" ) ,
1022
1021
NtLifetime ( ..) => f. pad ( "NtLifetime(..)" ) ,
1023
1022
}
1024
1023
}
0 commit comments