11use rustc_ast:: ptr:: P ;
2- use rustc_ast:: token:: { self , Nonterminal , NonterminalKind , Token } ;
2+ use rustc_ast:: token:: { self , NonterminalKind , Token } ;
33use rustc_ast:: AstLike ;
44use rustc_ast_pretty:: pprust;
55use rustc_errors:: PResult ;
66use rustc_span:: symbol:: { kw, Ident } ;
77
88use crate :: parser:: pat:: { CommaRecoveryMode , RecoverColon , RecoverComma } ;
9- use crate :: parser:: { FollowedByType , ForceCollect , Parser , PathStyle } ;
9+ use crate :: parser:: { FollowedByType , ForceCollect , NtOrTt , Parser , PathStyle } ;
1010
1111impl < ' a > Parser < ' a > {
1212 /// Checks whether a non-terminal may begin with a particular token.
@@ -85,7 +85,7 @@ impl<'a> Parser<'a> {
8585 NonterminalKind :: Lifetime => match token. kind {
8686 token:: Lifetime ( _) => true ,
8787 token:: Interpolated ( ref nt) => {
88- matches ! ( * * nt, token:: NtLifetime ( _) | token :: NtTT ( _ ) )
88+ matches ! ( * * nt, token:: NtLifetime ( _) )
8989 }
9090 _ => false ,
9191 } ,
@@ -96,14 +96,16 @@ impl<'a> Parser<'a> {
9696 }
9797
9898 /// Parse a non-terminal (e.g. MBE `:pat` or `:ident`).
99- pub fn parse_nonterminal ( & mut self , kind : NonterminalKind ) -> PResult < ' a , Nonterminal > {
99+ pub fn parse_nonterminal ( & mut self , kind : NonterminalKind ) -> PResult < ' a , NtOrTt > {
100100 // Any `Nonterminal` which stores its tokens (currently `NtItem` and `NtExpr`)
101101 // needs to have them force-captured here.
102102 // A `macro_rules!` invocation may pass a captured item/expr to a proc-macro,
103103 // which requires having captured tokens available. Since we cannot determine
104104 // in advance whether or not a proc-macro will be (transitively) invoked,
105105 // we always capture tokens for any `Nonterminal` which needs them.
106106 let mut nt = match kind {
107+ // Note that TT is treated differently to all the others.
108+ NonterminalKind :: TT => return Ok ( NtOrTt :: Tt ( self . parse_token_tree ( ) ) ) ,
107109 NonterminalKind :: Item => match self . parse_item ( ForceCollect :: Yes ) ? {
108110 Some ( item) => token:: NtItem ( item) ,
109111 None => {
@@ -124,9 +126,12 @@ impl<'a> Parser<'a> {
124126 NonterminalKind :: PatParam { .. } | NonterminalKind :: PatWithOr { .. } => {
125127 token:: NtPat ( self . collect_tokens_no_attrs ( |this| match kind {
126128 NonterminalKind :: PatParam { .. } => this. parse_pat_no_top_alt ( None ) ,
127- NonterminalKind :: PatWithOr { .. } => {
128- this. parse_pat_allow_top_alt ( None , RecoverComma :: No , RecoverColon :: No , CommaRecoveryMode :: EitherTupleOrPipe )
129- }
129+ NonterminalKind :: PatWithOr { .. } => this. parse_pat_allow_top_alt (
130+ None ,
131+ RecoverComma :: No ,
132+ RecoverColon :: No ,
133+ CommaRecoveryMode :: EitherTupleOrPipe ,
134+ ) ,
130135 _ => unreachable ! ( ) ,
131136 } ) ?)
132137 }
@@ -139,9 +144,10 @@ impl<'a> Parser<'a> {
139144 )
140145 }
141146
142- NonterminalKind :: Ty => {
143- token:: NtTy ( self . collect_tokens_no_attrs ( |this| this. parse_no_question_mark_recover ( ) ) ?)
144- }
147+ NonterminalKind :: Ty => token:: NtTy (
148+ self . collect_tokens_no_attrs ( |this| this. parse_no_question_mark_recover ( ) ) ?,
149+ ) ,
150+
145151 // this could be handled like a token, since it is one
146152 NonterminalKind :: Ident
147153 if let Some ( ( ident, is_raw) ) = get_macro_ident ( & self . token ) =>
@@ -158,7 +164,6 @@ impl<'a> Parser<'a> {
158164 self . collect_tokens_no_attrs ( |this| this. parse_path ( PathStyle :: Type ) ) ?,
159165 ) ,
160166 NonterminalKind :: Meta => token:: NtMeta ( P ( self . parse_attr_item ( true ) ?) ) ,
161- NonterminalKind :: TT => token:: NtTT ( self . parse_token_tree ( ) ) ,
162167 NonterminalKind :: Vis => token:: NtVis (
163168 self . collect_tokens_no_attrs ( |this| this. parse_visibility ( FollowedByType :: Yes ) ) ?,
164169 ) ,
@@ -183,7 +188,7 @@ impl<'a> Parser<'a> {
183188 ) ;
184189 }
185190
186- Ok ( nt )
191+ Ok ( NtOrTt :: Nt ( nt ) )
187192 }
188193}
189194
0 commit comments