@@ -74,7 +74,7 @@ crate use NamedMatch::*;
74
74
crate use ParseResult :: * ;
75
75
use TokenTreeOrTokenTreeSlice :: * ;
76
76
77
- use crate :: mbe:: { self , TokenTree } ;
77
+ use crate :: mbe:: { self , DelimSpan , SequenceRepetition , TokenTree } ;
78
78
79
79
use rustc_ast:: token:: { self , DocComment , Nonterminal , Token } ;
80
80
use rustc_parse:: parser:: Parser ;
@@ -203,6 +203,17 @@ struct MatcherPos<'root, 'tt> {
203
203
rustc_data_structures:: static_assert_size!( MatcherPos <' _, ' _>, 240 ) ;
204
204
205
205
impl < ' root , ' tt > MatcherPos < ' root , ' tt > {
206
+ /// `len` `Vec`s (initially shared and empty) that will store matches of metavars.
207
+ fn create_matches ( len : usize ) -> Box < [ Lrc < NamedMatchVec > ] > {
208
+ if len == 0 {
209
+ vec ! [ ]
210
+ } else {
211
+ let empty_matches = Lrc :: new ( SmallVec :: new ( ) ) ;
212
+ vec ! [ empty_matches; len]
213
+ }
214
+ . into_boxed_slice ( )
215
+ }
216
+
206
217
/// Generates the top-level matcher position in which the "dot" is before the first token of
207
218
/// the matcher `ms`.
208
219
fn new ( ms : & ' tt [ TokenTree ] ) -> Self {
@@ -217,7 +228,7 @@ impl<'root, 'tt> MatcherPos<'root, 'tt> {
217
228
// Initialize `matches` to a bunch of empty `Vec`s -- one for each metavar in
218
229
// `top_elts`. `match_lo` for `top_elts` is 0 and `match_hi` is `match_idx_hi`.
219
230
// `match_cur` is 0 since we haven't actually matched anything yet.
220
- matches : create_matches ( match_idx_hi) ,
231
+ matches : Self :: create_matches ( match_idx_hi) ,
221
232
match_lo : 0 ,
222
233
match_cur : 0 ,
223
234
match_hi : match_idx_hi,
@@ -230,6 +241,27 @@ impl<'root, 'tt> MatcherPos<'root, 'tt> {
230
241
}
231
242
}
232
243
244
+ fn repetition (
245
+ up : MatcherPosHandle < ' root , ' tt > ,
246
+ sp : DelimSpan ,
247
+ seq : Lrc < SequenceRepetition > ,
248
+ ) -> Self {
249
+ MatcherPos {
250
+ stack : smallvec ! [ ] ,
251
+ idx : 0 ,
252
+ matches : Self :: create_matches ( up. matches . len ( ) ) ,
253
+ match_lo : up. match_cur ,
254
+ match_cur : up. match_cur ,
255
+ match_hi : up. match_cur + seq. num_captures ,
256
+ repetition : Some ( MatcherPosRepetition {
257
+ up,
258
+ sep : seq. separator . clone ( ) ,
259
+ seq_op : seq. kleene . op ,
260
+ } ) ,
261
+ top_elts : Tt ( TokenTree :: Sequence ( sp, seq) ) ,
262
+ }
263
+ }
264
+
233
265
/// Adds `m` as a named match for the `idx`-th metavar.
234
266
fn push_match ( & mut self , idx : usize , m : NamedMatch ) {
235
267
let matches = Lrc :: make_mut ( & mut self . matches [ idx] ) ;
@@ -333,17 +365,6 @@ pub(super) fn count_names(ms: &[TokenTree]) -> usize {
333
365
} )
334
366
}
335
367
336
- /// `len` `Vec`s (initially shared and empty) that will store matches of metavars.
337
- fn create_matches ( len : usize ) -> Box < [ Lrc < NamedMatchVec > ] > {
338
- if len == 0 {
339
- vec ! [ ]
340
- } else {
341
- let empty_matches = Lrc :: new ( SmallVec :: new ( ) ) ;
342
- vec ! [ empty_matches; len]
343
- }
344
- . into_boxed_slice ( )
345
- }
346
-
347
368
/// `NamedMatch` is a pattern-match result for a single `token::MATCH_NONTERMINAL`:
348
369
/// so it is associated with a single ident in a parse, and all
349
370
/// `MatchedNonterminal`s in the `NamedMatch` have the same non-terminal type
@@ -599,21 +620,9 @@ fn parse_tt_inner<'root, 'tt>(
599
620
cur_items. push ( new_item) ;
600
621
}
601
622
602
- let matches = create_matches ( item. matches . len ( ) ) ;
603
- cur_items. push ( MatcherPosHandle :: Box ( Box :: new ( MatcherPos {
604
- stack : smallvec ! [ ] ,
605
- idx : 0 ,
606
- matches,
607
- match_lo : item. match_cur ,
608
- match_cur : item. match_cur ,
609
- match_hi : item. match_cur + seq. num_captures ,
610
- repetition : Some ( MatcherPosRepetition {
611
- up : item,
612
- sep : seq. separator . clone ( ) ,
613
- seq_op : seq. kleene . op ,
614
- } ) ,
615
- top_elts : Tt ( TokenTree :: Sequence ( sp, seq) ) ,
616
- } ) ) ) ;
623
+ cur_items. push ( MatcherPosHandle :: Box ( Box :: new ( MatcherPos :: repetition (
624
+ item, sp, seq,
625
+ ) ) ) ) ;
617
626
}
618
627
619
628
// We need to match a metavar (but the identifier is invalid)... this is an error
0 commit comments