1
1
use rustc_ast:: ptr:: P ;
2
- use rustc_ast:: token:: { self , Nonterminal , NonterminalKind , Token } ;
2
+ use rustc_ast:: token:: { self , NonterminalKind , Token } ;
3
3
use rustc_ast:: AstLike ;
4
4
use rustc_ast_pretty:: pprust;
5
5
use rustc_errors:: PResult ;
6
6
use rustc_span:: symbol:: { kw, Ident } ;
7
7
8
8
use crate :: parser:: pat:: { CommaRecoveryMode , RecoverColon , RecoverComma } ;
9
- use crate :: parser:: { FollowedByType , ForceCollect , Parser , PathStyle } ;
9
+ use crate :: parser:: { FollowedByType , ForceCollect , NtOrTt , Parser , PathStyle } ;
10
10
11
11
impl < ' a > Parser < ' a > {
12
12
/// Checks whether a non-terminal may begin with a particular token.
@@ -85,7 +85,7 @@ impl<'a> Parser<'a> {
85
85
NonterminalKind :: Lifetime => match token. kind {
86
86
token:: Lifetime ( _) => true ,
87
87
token:: Interpolated ( ref nt) => {
88
- matches ! ( * * nt, token:: NtLifetime ( _) | token :: NtTT ( _ ) )
88
+ matches ! ( * * nt, token:: NtLifetime ( _) )
89
89
}
90
90
_ => false ,
91
91
} ,
@@ -96,14 +96,16 @@ impl<'a> Parser<'a> {
96
96
}
97
97
98
98
/// 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 > {
100
100
// Any `Nonterminal` which stores its tokens (currently `NtItem` and `NtExpr`)
101
101
// needs to have them force-captured here.
102
102
// A `macro_rules!` invocation may pass a captured item/expr to a proc-macro,
103
103
// which requires having captured tokens available. Since we cannot determine
104
104
// in advance whether or not a proc-macro will be (transitively) invoked,
105
105
// we always capture tokens for any `Nonterminal` which needs them.
106
106
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 ( ) ) ) ,
107
109
NonterminalKind :: Item => match self . parse_item ( ForceCollect :: Yes ) ? {
108
110
Some ( item) => token:: NtItem ( item) ,
109
111
None => {
@@ -124,9 +126,12 @@ impl<'a> Parser<'a> {
124
126
NonterminalKind :: PatParam { .. } | NonterminalKind :: PatWithOr { .. } => {
125
127
token:: NtPat ( self . collect_tokens_no_attrs ( |this| match kind {
126
128
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
+ ) ,
130
135
_ => unreachable ! ( ) ,
131
136
} ) ?)
132
137
}
@@ -139,9 +144,10 @@ impl<'a> Parser<'a> {
139
144
)
140
145
}
141
146
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
+
145
151
// this could be handled like a token, since it is one
146
152
NonterminalKind :: Ident
147
153
if let Some ( ( ident, is_raw) ) = get_macro_ident ( & self . token ) =>
@@ -158,7 +164,6 @@ impl<'a> Parser<'a> {
158
164
self . collect_tokens_no_attrs ( |this| this. parse_path ( PathStyle :: Type ) ) ?,
159
165
) ,
160
166
NonterminalKind :: Meta => token:: NtMeta ( P ( self . parse_attr_item ( true ) ?) ) ,
161
- NonterminalKind :: TT => token:: NtTT ( self . parse_token_tree ( ) ) ,
162
167
NonterminalKind :: Vis => token:: NtVis (
163
168
self . collect_tokens_no_attrs ( |this| this. parse_visibility ( FollowedByType :: Yes ) ) ?,
164
169
) ,
@@ -183,7 +188,7 @@ impl<'a> Parser<'a> {
183
188
) ;
184
189
}
185
190
186
- Ok ( nt )
191
+ Ok ( NtOrTt :: Nt ( nt ) )
187
192
}
188
193
}
189
194
0 commit comments