@@ -134,16 +134,27 @@ impl Lit {
134
134
}
135
135
}
136
136
137
- /// Keep this in sync with `Token::can_begin_literal_maybe_minus` excluding unary negation.
137
+ /// Keep this in sync with `Token::can_begin_literal_maybe_minus` and
138
+ /// `Parser::maybe_parse_token_lit` (excluding unary negation).
138
139
pub fn from_token ( token : & Token ) -> Option < Lit > {
139
140
match token. uninterpolate ( ) . kind {
140
141
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => Some ( Lit :: new ( Bool , name, None ) ) ,
141
142
Literal ( token_lit) => Some ( token_lit) ,
142
- Interpolated ( ref nt)
143
- if let NtExpr ( expr) | NtLiteral ( expr) = & * * nt
144
- && let ast:: ExprKind :: Lit ( token_lit) = expr. kind =>
145
- {
146
- Some ( token_lit)
143
+ OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar ( NonterminalKind :: Literal ) ) ) => {
144
+ panic ! ( "njn: FROM_TOKEN (1)" ) ;
145
+ // if let NtExpr(expr) | NtLiteral(expr) = &**nt
146
+ // && let ast::ExprKind::Lit(token_lit) = expr.kind =>
147
+ // {
148
+ // Some(token_lit)
149
+ // }
150
+ }
151
+ OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar ( NonterminalKind :: Expr ) ) ) => {
152
+ panic ! ( "njn: FROM_TOKEN (2)" ) ;
153
+ // if let NtExpr(expr) | NtLiteral(expr) = &**nt
154
+ // && let ast::ExprKind::Lit(token_lit) = expr.kind =>
155
+ // {
156
+ // Some(token_lit)
157
+ // }
147
158
}
148
159
_ => None ,
149
160
}
@@ -476,6 +487,7 @@ impl Token {
476
487
Token :: new ( Ident ( ident. name , ident. is_raw_guess ( ) . into ( ) ) , ident. span )
477
488
}
478
489
490
+ /// njn: phase this out in favour of Parser::uninterpolated_token_span?
479
491
/// For interpolated tokens, returns a span of the fragment to which the interpolated
480
492
/// token refers. For all other tokens this is just a regular span.
481
493
/// It is particularly important to use this for identifiers and lifetimes
@@ -531,9 +543,7 @@ impl Token {
531
543
PathSep | // global path
532
544
Lifetime ( ..) | // labeled loop
533
545
Pound => true , // expression attributes
534
- Interpolated ( ref nt) => matches ! ( & * * nt, NtLiteral ( ..) |
535
- NtExpr ( ..) |
536
- NtBlock ( ..) ) ,
546
+ Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
537
547
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
538
548
NonterminalKind :: Block |
539
549
NonterminalKind :: Expr |
@@ -561,7 +571,7 @@ impl Token {
561
571
| DotDot | DotDotDot | DotDotEq // ranges
562
572
| Lt | BinOp ( Shl ) // associated path
563
573
| PathSep => true , // global path
564
- Interpolated ( ref nt) => matches ! ( & * * nt, NtLiteral ( .. ) | NtBlock ( ..) ) ,
574
+ Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
565
575
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
566
576
NonterminalKind :: Block |
567
577
NonterminalKind :: PatParam { .. } |
@@ -603,6 +613,7 @@ impl Token {
603
613
match self . kind {
604
614
OpenDelim ( Delimiter :: Brace ) | Literal ( ..) | BinOp ( Minus ) => true ,
605
615
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
616
+ Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
606
617
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
607
618
NonterminalKind :: Expr
608
619
| NonterminalKind :: Expr2021 { .. }
@@ -649,22 +660,24 @@ impl Token {
649
660
///
650
661
/// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
651
662
///
652
- /// Keep this in sync with and `Lit::from_token`, excluding unary negation.
663
+ /// Keep this in sync with `Lit::from_token` and
664
+ /// `Parser::maybe_parse_token_lit` (excluding unary negation).
653
665
pub fn can_begin_literal_maybe_minus ( & self ) -> bool {
654
666
match self . uninterpolate ( ) . kind {
655
667
Literal ( ..) | BinOp ( Minus ) => true ,
656
668
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
657
- Interpolated ( ref nt) => match & * * nt {
658
- NtLiteral ( _) => true ,
659
- NtExpr ( e) => match & e. kind {
660
- ast:: ExprKind :: Lit ( _) => true ,
661
- ast:: ExprKind :: Unary ( ast:: UnOp :: Neg , e) => {
662
- matches ! ( & e. kind, ast:: ExprKind :: Lit ( _) )
663
- }
664
- _ => false ,
665
- } ,
666
- _ => false ,
667
- } ,
669
+ // njn: fix up
670
+ // Interpolated(ref nt) => match &**nt {
671
+ // NtLiteral(_) => true,
672
+ // NtExpr(e) => match &e.kind {
673
+ // ast::ExprKind::Lit(_) => true,
674
+ // ast::ExprKind::Unary(ast::UnOp::Neg, e) => {
675
+ // matches!(&e.kind, ast::ExprKind::Lit(_))
676
+ // }
677
+ // _ => false,
678
+ // },
679
+ // _ => false,
680
+ // },
668
681
// njn: too simple compared to what's above?
669
682
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
670
683
NonterminalKind :: Literal | NonterminalKind :: Expr | NonterminalKind :: Expr2021 { .. } ,
@@ -676,14 +689,19 @@ impl Token {
676
689
pub fn can_begin_string_literal ( & self ) -> bool {
677
690
match self . uninterpolate ( ) . kind {
678
691
Literal ( ..) => true ,
679
- Interpolated ( ref nt) => match & * * nt {
680
- NtLiteral ( _) => true ,
681
- NtExpr ( e) => match & e. kind {
682
- ast:: ExprKind :: Lit ( _) => true ,
683
- _ => false ,
684
- } ,
685
- _ => false ,
686
- } ,
692
+ // njn: fix up
693
+ // Interpolated(ref nt) => match &**nt {
694
+ // NtLiteral(_) => true,
695
+ // NtExpr(e) => match &e.kind {
696
+ // ast::ExprKind::Lit(_) => true,
697
+ // _ => false,
698
+ // },
699
+ // _ => false,
700
+ // },
701
+ // njn: too simple compared to what's above?
702
+ OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
703
+ NonterminalKind :: Literal | NonterminalKind :: Expr | NonterminalKind :: Expr2021 ,
704
+ ) ) ) => true ,
687
705
_ => false ,
688
706
}
689
707
}
@@ -738,16 +756,19 @@ impl Token {
738
756
self . ident ( ) . is_some_and ( |( ident, _) | ident. name == name)
739
757
}
740
758
741
- /// Would `maybe_whole_expr ` in `parser.rs` return `Ok(..)`?
759
+ /// Would `maybe_reparse_metavar_expr ` in `parser.rs` return `Ok(..)`?
742
760
/// That is, is this a pre-parsed expression dropped into the token stream
743
761
/// (which happens while parsing the result of macro expansion)?
744
- pub fn is_whole_expr ( & self ) -> bool {
745
- #[ allow( irrefutable_let_patterns) ] // njn: temp
762
+ pub fn is_metavar_expr ( & self ) -> bool {
763
+ #[ allow( irrefutable_let_patterns) ] // FIXME: temporary
746
764
if let Interpolated ( nt) = & self . kind
747
- && let NtExpr ( _ ) | NtLiteral ( _) = & * * nt
765
+ && let NtBlock ( _) = & * * nt
748
766
{
749
767
true
750
- } else if matches ! ( self . is_metavar_seq( ) , Some ( NonterminalKind :: Path ) ) {
768
+ } else if matches ! (
769
+ self . is_metavar_seq( ) ,
770
+ Some ( NonterminalKind :: Expr | NonterminalKind :: Literal | NonterminalKind :: Path )
771
+ ) {
751
772
true
752
773
} else {
753
774
false
@@ -756,6 +777,7 @@ impl Token {
756
777
757
778
/// Is the token an interpolated block (`$b:block`)?
758
779
pub fn is_whole_block ( & self ) -> bool {
780
+ #[ allow( irrefutable_let_patterns) ] // FIXME: temporary
759
781
if let Interpolated ( nt) = & self . kind
760
782
&& let NtBlock ( ..) = & * * nt
761
783
{
@@ -928,8 +950,6 @@ impl PartialEq<TokenKind> for Token {
928
950
/// For interpolation during macro expansion.
929
951
pub enum Nonterminal {
930
952
NtBlock ( P < ast:: Block > ) ,
931
- NtExpr ( P < ast:: Expr > ) ,
932
- NtLiteral ( P < ast:: Expr > ) ,
933
953
}
934
954
935
955
#[ derive( Debug , Copy , Clone , PartialEq , Eq , Encodable , Decodable , Hash , HashStable_Generic ) ]
@@ -1027,15 +1047,12 @@ impl Nonterminal {
1027
1047
pub fn use_span ( & self ) -> Span {
1028
1048
match self {
1029
1049
NtBlock ( block) => block. span ,
1030
- NtExpr ( expr) | NtLiteral ( expr) => expr. span ,
1031
1050
}
1032
1051
}
1033
1052
1034
1053
pub fn descr ( & self ) -> & ' static str {
1035
1054
match self {
1036
1055
NtBlock ( ..) => "block" ,
1037
- NtExpr ( ..) => "expression" ,
1038
- NtLiteral ( ..) => "literal" ,
1039
1056
}
1040
1057
}
1041
1058
}
@@ -1054,8 +1071,6 @@ impl fmt::Debug for Nonterminal {
1054
1071
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1055
1072
match * self {
1056
1073
NtBlock ( ..) => f. pad ( "NtBlock(..)" ) ,
1057
- NtExpr ( ..) => f. pad ( "NtExpr(..)" ) ,
1058
- NtLiteral ( ..) => f. pad ( "NtLiteral(..)" ) ,
1059
1074
}
1060
1075
}
1061
1076
}
@@ -1077,7 +1092,7 @@ mod size_asserts {
1077
1092
// tidy-alphabetical-start
1078
1093
static_assert_size ! ( Lit , 12 ) ;
1079
1094
static_assert_size ! ( LitKind , 2 ) ;
1080
- static_assert_size ! ( Nonterminal , 16 ) ;
1095
+ static_assert_size ! ( Nonterminal , 8 ) ;
1081
1096
static_assert_size ! ( Token , 24 ) ;
1082
1097
static_assert_size ! ( TokenKind , 16 ) ;
1083
1098
// tidy-alphabetical-end
0 commit comments