@@ -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,18 +133,27 @@ impl Lit {
134
133
}
135
134
}
136
135
137
- /// Keep this in sync with `Token::can_begin_literal_or_bool` 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
- Ident ( name, false ) if name. is_bool_lit ( ) => {
141
- Some ( Lit :: new ( Bool , name, None ) )
142
- }
140
+ Ident ( name, false ) if name. is_bool_lit ( ) => Some ( Lit :: new ( Bool , name, None ) ) ,
143
141
Literal ( token_lit) => Some ( token_lit) ,
144
- Interpolated ( ref nt)
145
- if let NtExpr ( expr) | NtLiteral ( expr) = & * * nt
146
- && let ast:: ExprKind :: Lit ( token_lit) = expr. kind =>
147
- {
148
- Some ( token_lit)
142
+ OpenDelim ( Delimiter :: Invisible ( InvisibleSource :: 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 ( InvisibleSource :: 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
+ // }
149
157
}
150
158
_ => None ,
151
159
}
@@ -417,6 +425,7 @@ impl Token {
417
425
Token :: new ( Ident ( ident. name , ident. is_raw_guess ( ) ) , ident. span )
418
426
}
419
427
428
+ /// njn: phase this out in favour of Parser::uninterpolated_span
420
429
/// For interpolated tokens, returns a span of the fragment to which the interpolated
421
430
/// token refers. For all other tokens this is just a regular span.
422
431
/// It is particularly important to use this for identifiers and lifetimes
@@ -469,9 +478,11 @@ impl Token {
469
478
ModSep | // global path
470
479
Lifetime ( ..) | // labeled loop
471
480
Pound => true , // expression attributes
472
- Interpolated ( ref nt) => matches ! ( * * nt, NtLiteral ( ..) | NtExpr ( ..) ) ,
473
481
OpenDelim ( Delimiter :: Invisible ( InvisibleSource :: MetaVar (
474
- NonterminalKind :: Block | NonterminalKind :: Path
482
+ NonterminalKind :: Block |
483
+ NonterminalKind :: Expr |
484
+ NonterminalKind :: Literal |
485
+ NonterminalKind :: Path
475
486
) ) )
476
487
=> true ,
477
488
_ => false ,
@@ -494,12 +505,12 @@ impl Token {
494
505
| DotDot | DotDotDot | DotDotEq // ranges
495
506
| Lt | BinOp ( Shl ) // associated path
496
507
| ModSep => true , // global path
497
- Interpolated ( ref nt) => matches ! ( * * nt, NtLiteral ( ..) ) ,
498
508
| OpenDelim ( Delimiter :: Invisible ( InvisibleSource :: MetaVar (
499
509
NonterminalKind :: Block |
500
510
NonterminalKind :: PatParam { .. } |
501
511
NonterminalKind :: PatWithOr |
502
- NonterminalKind :: Path
512
+ NonterminalKind :: Path |
513
+ NonterminalKind :: Literal
503
514
) ) ) => true ,
504
515
_ => false ,
505
516
}
@@ -532,10 +543,9 @@ impl Token {
532
543
pub fn can_begin_const_arg ( & self ) -> bool {
533
544
match self . kind {
534
545
OpenDelim ( Delimiter :: Brace ) => true ,
535
- Interpolated ( ref nt) => matches ! ( * * nt, NtExpr ( ..) | NtLiteral ( ..) ) ,
536
- OpenDelim ( Delimiter :: Invisible ( InvisibleSource :: MetaVar ( NonterminalKind :: Block ) ) ) => {
537
- true
538
- }
546
+ OpenDelim ( Delimiter :: Invisible ( InvisibleSource :: MetaVar (
547
+ NonterminalKind :: Block | NonterminalKind :: Expr | NonterminalKind :: Literal ,
548
+ ) ) ) => true ,
539
549
_ => self . can_begin_literal_maybe_minus ( ) ,
540
550
}
541
551
}
@@ -584,22 +594,15 @@ impl Token {
584
594
///
585
595
/// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
586
596
///
587
- /// Keep this in sync with and `Lit::from_token`, excluding unary negation.
597
+ /// Keep this in sync with `Lit::from_token` and
598
+ /// `Parser::maybe_parse_token_lit` (excluding unary negation).
588
599
pub fn can_begin_literal_maybe_minus ( & self ) -> bool {
589
600
match self . uninterpolate ( ) . kind {
590
601
Literal ( ..) | BinOp ( Minus ) => true ,
591
602
Ident ( name, false ) if name. is_bool_lit ( ) => true ,
592
- Interpolated ( ref nt) => match & * * nt {
593
- NtLiteral ( _) => true ,
594
- NtExpr ( e) => match & e. kind {
595
- ast:: ExprKind :: Lit ( _) => true ,
596
- ast:: ExprKind :: Unary ( ast:: UnOp :: Neg , e) => {
597
- matches ! ( & e. kind, ast:: ExprKind :: Lit ( _) )
598
- }
599
- _ => false ,
600
- } ,
601
- _ => false ,
602
- } ,
603
+ OpenDelim ( Delimiter :: Invisible ( InvisibleSource :: MetaVar (
604
+ NonterminalKind :: Literal | NonterminalKind :: Expr ,
605
+ ) ) ) => true ,
603
606
_ => false ,
604
607
}
605
608
}
@@ -615,7 +618,6 @@ impl Token {
615
618
Cow :: Owned ( Token :: new ( Ident ( ident. name , is_raw) , ident. span ) )
616
619
}
617
620
NtLifetime ( ident) => Cow :: Owned ( Token :: new ( Lifetime ( ident. name ) , ident. span ) ) ,
618
- _ => Cow :: Borrowed ( self ) ,
619
621
} ,
620
622
_ => Cow :: Borrowed ( self ) ,
621
623
}
@@ -665,22 +667,19 @@ impl Token {
665
667
self . ident ( ) . is_some_and ( |( ident, _) | ident. name == name)
666
668
}
667
669
668
- /// Would `maybe_whole_expr ` in `parser.rs` return `Ok(..)`?
670
+ /// Would `maybe_reparse_metavar_expr ` in `parser.rs` return `Ok(..)`?
669
671
/// That is, is this a pre-parsed expression dropped into the token stream
670
672
/// (which happens while parsing the result of macro expansion)?
671
- pub fn is_whole_expr ( & self ) -> bool {
672
- if let Interpolated ( nt) = & self . kind
673
- && let NtExpr ( _) | NtLiteral ( _) = * * nt
674
- {
675
- true
676
- } else if matches ! (
673
+ pub fn is_metavar_expr ( & self ) -> bool {
674
+ matches ! (
677
675
self . is_metavar_seq( ) ,
678
- Some ( NonterminalKind :: Block | NonterminalKind :: Path )
679
- ) {
680
- true
681
- } else {
682
- false
683
- }
676
+ Some (
677
+ NonterminalKind :: Expr
678
+ | NonterminalKind :: Literal
679
+ | NonterminalKind :: Block
680
+ | NonterminalKind :: Path
681
+ )
682
+ )
684
683
}
685
684
686
685
/// Are we at a block from a metavar (`$b:block`)?
@@ -843,10 +842,8 @@ impl PartialEq<TokenKind> for Token {
843
842
#[ derive( Clone , Encodable , Decodable ) ]
844
843
/// For interpolation during macro expansion.
845
844
pub enum Nonterminal {
846
- NtExpr ( P < ast:: Expr > ) ,
847
845
NtIdent ( Ident , /* is_raw */ bool ) ,
848
846
NtLifetime ( Ident ) ,
849
- NtLiteral ( P < ast:: Expr > ) ,
850
847
}
851
848
852
849
#[ derive( Debug , Copy , Clone , PartialEq , Eq , Encodable , Decodable , Hash , HashStable_Generic ) ]
@@ -930,7 +927,6 @@ impl fmt::Display for NonterminalKind {
930
927
impl Nonterminal {
931
928
pub fn span ( & self ) -> Span {
932
929
match self {
933
- NtExpr ( expr) | NtLiteral ( expr) => expr. span ,
934
930
NtIdent ( ident, _) | NtLifetime ( ident) => ident. span ,
935
931
}
936
932
}
@@ -955,9 +951,7 @@ impl PartialEq for Nonterminal {
955
951
impl fmt:: Debug for Nonterminal {
956
952
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
957
953
match * self {
958
- NtExpr ( ..) => f. pad ( "NtExpr(..)" ) ,
959
954
NtIdent ( ..) => f. pad ( "NtIdent(..)" ) ,
960
- NtLiteral ( ..) => f. pad ( "NtLiteral(..)" ) ,
961
955
NtLifetime ( ..) => f. pad ( "NtLifetime(..)" ) ,
962
956
}
963
957
}
0 commit comments