@@ -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,17 +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
- // njn: deal with later, with NtExpr/NtLiteral
143
- Interpolated ( ref nt)
144
- if let NtExpr ( expr) | NtLiteral ( expr) = & nt. 0
145
- && let ast:: ExprKind :: Lit ( token_lit) = expr. kind =>
146
- {
147
- 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
+ // }
148
157
}
149
158
_ => None ,
150
159
}
@@ -464,6 +473,7 @@ impl Token {
464
473
Token :: new ( Ident ( ident. name , ident. is_raw_guess ( ) . into ( ) ) , ident. span )
465
474
}
466
475
476
+ /// njn: phase this out in favour of Parser::uninterpolated_span
467
477
/// For interpolated tokens, returns a span of the fragment to which the interpolated
468
478
/// token refers. For all other tokens this is just a regular span.
469
479
/// It is particularly important to use this for identifiers and lifetimes
@@ -520,7 +530,6 @@ impl Token {
520
530
PathSep | // global path
521
531
Lifetime ( ..) | // labeled loop
522
532
Pound => true , // expression attributes
523
- Interpolated ( ref nt) => matches ! ( & nt. 0 , NtLiteral ( ..) | NtExpr ( ..) ) ,
524
533
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
525
534
NonterminalKind :: Block |
526
535
NonterminalKind :: Expr |
@@ -547,7 +556,6 @@ impl Token {
547
556
| DotDot | DotDotDot | DotDotEq // ranges
548
557
| Lt | BinOp ( Shl ) // associated path
549
558
| PathSep => true , // global path
550
- Interpolated ( ref nt) => matches ! ( & nt. 0 , NtLiteral ( ..) ) ,
551
559
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
552
560
NonterminalKind :: Block |
553
561
NonterminalKind :: PatParam { .. } |
@@ -588,7 +596,6 @@ impl Token {
588
596
pub fn can_begin_const_arg ( & self ) -> bool {
589
597
match self . kind {
590
598
OpenDelim ( Delimiter :: Brace ) => true ,
591
- Interpolated ( ref nt) => matches ! ( & nt. 0 , NtExpr ( ..) | NtLiteral ( ..) ) ,
592
599
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
593
600
NonterminalKind :: Expr | NonterminalKind :: Block | NonterminalKind :: Literal ,
594
601
) ) ) => true ,
@@ -631,22 +638,24 @@ impl Token {
631
638
///
632
639
/// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
633
640
///
634
- /// Keep this in sync with and `Lit::from_token`, excluding unary negation.
641
+ /// Keep this in sync with `Lit::from_token` and
642
+ /// `Parser::maybe_parse_token_lit` (excluding unary negation).
635
643
pub fn can_begin_literal_maybe_minus ( & self ) -> bool {
636
644
match self . uninterpolate ( ) . kind {
637
645
Literal ( ..) | BinOp ( Minus ) => true ,
638
646
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
639
- Interpolated ( ref nt) => match & nt. 0 {
640
- NtLiteral ( _) => true ,
641
- NtExpr ( e) => match & e. kind {
642
- ast:: ExprKind :: Lit ( _) => true ,
643
- ast:: ExprKind :: Unary ( ast:: UnOp :: Neg , e) => {
644
- matches ! ( & e. kind, ast:: ExprKind :: Lit ( _) )
645
- }
646
- _ => false ,
647
- } ,
648
- _ => false ,
649
- } ,
647
+ // njn: fix up
648
+ // Interpolated(ref nt) => match &nt.0 {
649
+ // NtLiteral(_) => true,
650
+ // NtExpr(e) => match &e.kind {
651
+ // ast::ExprKind::Lit(_) => true,
652
+ // ast::ExprKind::Unary(ast::UnOp::Neg, e) => {
653
+ // matches!(&e.kind, ast::ExprKind::Lit(_))
654
+ // }
655
+ // _ => false,
656
+ // },
657
+ // _ => false,
658
+ // },
650
659
// njn: too simple compared to what's above?
651
660
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
652
661
NonterminalKind :: Literal | NonterminalKind :: Expr ,
@@ -667,7 +676,6 @@ impl Token {
667
676
Cow :: Owned ( Token :: new ( Ident ( ident. name , * is_raw) , ident. span ) )
668
677
}
669
678
NtLifetime ( ident) => Cow :: Owned ( Token :: new ( Lifetime ( ident. name ) , ident. span ) ) ,
670
- _ => Cow :: Borrowed ( self ) ,
671
679
} ,
672
680
_ => Cow :: Borrowed ( self ) ,
673
681
}
@@ -719,23 +727,19 @@ impl Token {
719
727
self . ident ( ) . is_some_and ( |( ident, _) | ident. name == name)
720
728
}
721
729
722
- /// Would `maybe_whole_expr ` in `parser.rs` return `Ok(..)`?
730
+ /// Would `maybe_reparse_metavar_expr ` in `parser.rs` return `Ok(..)`?
723
731
/// That is, is this a pre-parsed expression dropped into the token stream
724
732
/// (which happens while parsing the result of macro expansion)?
725
- pub fn is_whole_expr ( & self ) -> bool {
726
- // njn: nothing needed, just deal with NtExpr/NtLiteral/NtPath/NtBlock later
727
- if let Interpolated ( nt) = & self . kind
728
- && let NtExpr ( _) | NtLiteral ( _) = & nt. 0
729
- {
730
- true
731
- } else if matches ! (
733
+ pub fn is_metavar_expr ( & self ) -> bool {
734
+ matches ! (
732
735
self . is_metavar_seq( ) ,
733
- Some ( NonterminalKind :: Block | NonterminalKind :: Path )
734
- ) {
735
- true
736
- } else {
737
- false
738
- }
736
+ Some (
737
+ NonterminalKind :: Expr
738
+ | NonterminalKind :: Literal
739
+ | NonterminalKind :: Block
740
+ | NonterminalKind :: Path
741
+ )
742
+ )
739
743
}
740
744
741
745
/// Are we at a block from a metavar (`$b:block`)?
@@ -904,10 +908,8 @@ impl PartialEq<TokenKind> for Token {
904
908
#[ derive( Clone , Encodable , Decodable ) ]
905
909
/// For interpolation during macro expansion.
906
910
pub enum Nonterminal {
907
- NtExpr ( P < ast:: Expr > ) ,
908
911
NtIdent ( Ident , IdentIsRaw ) ,
909
912
NtLifetime ( Ident ) ,
910
- NtLiteral ( P < ast:: Expr > ) ,
911
913
}
912
914
913
915
#[ derive( Debug , Copy , Clone , PartialEq , Eq , Encodable , Decodable , Hash , HashStable_Generic ) ]
@@ -991,15 +993,12 @@ impl fmt::Display for NonterminalKind {
991
993
impl Nonterminal {
992
994
pub fn use_span ( & self ) -> Span {
993
995
match self {
994
- NtExpr ( expr) | NtLiteral ( expr) => expr. span ,
995
996
NtIdent ( ident, _) | NtLifetime ( ident) => ident. span ,
996
997
}
997
998
}
998
999
999
1000
pub fn descr ( & self ) -> & ' static str {
1000
1001
match self {
1001
- NtExpr ( ..) => "expression" ,
1002
- NtLiteral ( ..) => "literal" ,
1003
1002
NtIdent ( ..) => "identifier" ,
1004
1003
NtLifetime ( ..) => "lifetime" ,
1005
1004
}
@@ -1025,9 +1024,7 @@ impl PartialEq for Nonterminal {
1025
1024
impl fmt:: Debug for Nonterminal {
1026
1025
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1027
1026
match * self {
1028
- NtExpr ( ..) => f. pad ( "NtExpr(..)" ) ,
1029
1027
NtIdent ( ..) => f. pad ( "NtIdent(..)" ) ,
1030
- NtLiteral ( ..) => f. pad ( "NtLiteral(..)" ) ,
1031
1028
NtLifetime ( ..) => f. pad ( "NtLifetime(..)" ) ,
1032
1029
}
1033
1030
}
0 commit comments