@@ -4,7 +4,6 @@ pub use Nonterminal::*;
44pub use TokenKind :: * ;
55
66use crate :: ast;
7- use crate :: ptr:: P ;
87use crate :: util:: case:: Case ;
98
109use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
@@ -134,18 +133,27 @@ impl Lit {
134133 }
135134 }
136135
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).
138138 pub fn from_token ( token : & Token ) -> Option < Lit > {
139139 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 ) ) ,
143141 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+ // }
149157 }
150158 _ => None ,
151159 }
@@ -417,6 +425,7 @@ impl Token {
417425 Token :: new ( Ident ( ident. name , ident. is_raw_guess ( ) ) , ident. span )
418426 }
419427
428+ /// njn: phase this out in favour of Parser::uninterpolated_span
420429 /// For interpolated tokens, returns a span of the fragment to which the interpolated
421430 /// token refers. For all other tokens this is just a regular span.
422431 /// It is particularly important to use this for identifiers and lifetimes
@@ -469,9 +478,11 @@ impl Token {
469478 ModSep | // global path
470479 Lifetime ( ..) | // labeled loop
471480 Pound => true , // expression attributes
472- Interpolated ( ref nt) => matches ! ( * * nt, NtLiteral ( ..) | NtExpr ( ..) ) ,
473481 OpenDelim ( Delimiter :: Invisible ( InvisibleSource :: MetaVar (
474- NonterminalKind :: Block | NonterminalKind :: Path
482+ NonterminalKind :: Block |
483+ NonterminalKind :: Expr |
484+ NonterminalKind :: Literal |
485+ NonterminalKind :: Path
475486 ) ) )
476487 => true ,
477488 _ => false ,
@@ -494,12 +505,12 @@ impl Token {
494505 | DotDot | DotDotDot | DotDotEq // ranges
495506 | Lt | BinOp ( Shl ) // associated path
496507 | ModSep => true , // global path
497- Interpolated ( ref nt) => matches ! ( * * nt, NtLiteral ( ..) ) ,
498508 | OpenDelim ( Delimiter :: Invisible ( InvisibleSource :: MetaVar (
499509 NonterminalKind :: Block |
500510 NonterminalKind :: PatParam { .. } |
501511 NonterminalKind :: PatWithOr |
502- NonterminalKind :: Path
512+ NonterminalKind :: Path |
513+ NonterminalKind :: Literal
503514 ) ) ) => true ,
504515 _ => false ,
505516 }
@@ -532,10 +543,9 @@ impl Token {
532543 pub fn can_begin_const_arg ( & self ) -> bool {
533544 match self . kind {
534545 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 ,
539549 _ => self . can_begin_literal_maybe_minus ( ) ,
540550 }
541551 }
@@ -584,22 +594,15 @@ impl Token {
584594 ///
585595 /// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
586596 ///
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).
588599 pub fn can_begin_literal_maybe_minus ( & self ) -> bool {
589600 match self . uninterpolate ( ) . kind {
590601 Literal ( ..) | BinOp ( Minus ) => true ,
591602 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 ,
603606 _ => false ,
604607 }
605608 }
@@ -615,7 +618,6 @@ impl Token {
615618 Cow :: Owned ( Token :: new ( Ident ( ident. name , is_raw) , ident. span ) )
616619 }
617620 NtLifetime ( ident) => Cow :: Owned ( Token :: new ( Lifetime ( ident. name ) , ident. span ) ) ,
618- _ => Cow :: Borrowed ( self ) ,
619621 } ,
620622 _ => Cow :: Borrowed ( self ) ,
621623 }
@@ -665,22 +667,19 @@ impl Token {
665667 self . ident ( ) . is_some_and ( |( ident, _) | ident. name == name)
666668 }
667669
668- /// Would `maybe_whole_expr ` in `parser.rs` return `Ok(..)`?
670+ /// Would `maybe_reparse_metavar_expr ` in `parser.rs` return `Ok(..)`?
669671 /// That is, is this a pre-parsed expression dropped into the token stream
670672 /// (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 ! (
677675 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+ )
684683 }
685684
686685 /// Are we at a block from a metavar (`$b:block`)?
@@ -843,10 +842,8 @@ impl PartialEq<TokenKind> for Token {
843842#[ derive( Clone , Encodable , Decodable ) ]
844843/// For interpolation during macro expansion.
845844pub enum Nonterminal {
846- NtExpr ( P < ast:: Expr > ) ,
847845 NtIdent ( Ident , /* is_raw */ bool ) ,
848846 NtLifetime ( Ident ) ,
849- NtLiteral ( P < ast:: Expr > ) ,
850847}
851848
852849#[ derive( Debug , Copy , Clone , PartialEq , Eq , Encodable , Decodable , Hash , HashStable_Generic ) ]
@@ -930,7 +927,6 @@ impl fmt::Display for NonterminalKind {
930927impl Nonterminal {
931928 pub fn span ( & self ) -> Span {
932929 match self {
933- NtExpr ( expr) | NtLiteral ( expr) => expr. span ,
934930 NtIdent ( ident, _) | NtLifetime ( ident) => ident. span ,
935931 }
936932 }
@@ -955,9 +951,7 @@ impl PartialEq for Nonterminal {
955951impl fmt:: Debug for Nonterminal {
956952 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
957953 match * self {
958- NtExpr ( ..) => f. pad ( "NtExpr(..)" ) ,
959954 NtIdent ( ..) => f. pad ( "NtIdent(..)" ) ,
960- NtLiteral ( ..) => f. pad ( "NtLiteral(..)" ) ,
961955 NtLifetime ( ..) => f. pad ( "NtLifetime(..)" ) ,
962956 }
963957 }
0 commit comments