7272
7373crate use NamedMatch :: * ;
7474crate use ParseResult :: * ;
75- use TokenTreeOrTokenTreeSlice :: * ;
7675
77- use crate :: mbe:: { self , DelimSpan , SequenceRepetition , TokenTree } ;
76+ use crate :: mbe:: { self , SequenceRepetition , TokenTree } ;
7877
7978use rustc_ast:: token:: { self , DocComment , Nonterminal , Token } ;
8079use rustc_parse:: parser:: Parser ;
@@ -90,43 +89,14 @@ use std::borrow::Cow;
9089use std:: collections:: hash_map:: Entry :: { Occupied , Vacant } ;
9190use std:: mem;
9291
93- // To avoid costly uniqueness checks, we require that `MatchSeq` always has a nonempty body.
94-
95- /// Either a slice of token trees or a single one. This is used as the representation of the
96- /// token trees that make up a matcher.
97- #[ derive( Clone ) ]
98- enum TokenTreeOrTokenTreeSlice < ' tt > {
99- Tt ( TokenTree ) ,
100- TtSlice ( & ' tt [ TokenTree ] ) ,
101- }
102-
103- impl < ' tt > TokenTreeOrTokenTreeSlice < ' tt > {
104- /// Returns the number of constituent top-level token trees of `self` (top-level in that it
105- /// will not recursively descend into subtrees).
106- fn len ( & self ) -> usize {
107- match * self {
108- TtSlice ( ref v) => v. len ( ) ,
109- Tt ( ref tt) => tt. len ( ) ,
110- }
111- }
112-
113- /// The `index`-th token tree of `self`.
114- fn get_tt ( & self , index : usize ) -> TokenTree {
115- match * self {
116- TtSlice ( ref v) => v[ index] . clone ( ) ,
117- Tt ( ref tt) => tt. get_tt ( index) ,
118- }
119- }
120- }
121-
12292/// An unzipping of `TokenTree`s... see the `stack` field of `MatcherPos`.
12393///
12494/// This is used by `parse_tt_inner` to keep track of delimited submatchers that we have
12595/// descended into.
12696#[ derive( Clone ) ]
12797struct MatcherTtFrame < ' tt > {
12898 /// The "parent" matcher that we are descending into.
129- elts : TokenTreeOrTokenTreeSlice < ' tt > ,
99+ elts : & ' tt [ TokenTree ] ,
130100 /// The position of the "dot" in `elts` at the time we descended.
131101 idx : usize ,
132102}
@@ -138,7 +108,7 @@ type NamedMatchVec = SmallVec<[NamedMatch; 4]>;
138108#[ derive( Clone ) ]
139109struct MatcherPos < ' tt > {
140110 /// The token or slice of tokens that make up the matcher. `elts` is short for "elements".
141- top_elts : TokenTreeOrTokenTreeSlice < ' tt > ,
111+ top_elts : & ' tt [ TokenTree ] ,
142112
143113 /// The position of the "dot" in this matcher
144114 idx : usize ,
@@ -183,7 +153,7 @@ struct MatcherPos<'tt> {
183153
184154// This type is used a lot. Make sure it doesn't unintentionally get bigger.
185155#[ cfg( all( target_arch = "x86_64" , target_pointer_width = "64" ) ) ]
186- rustc_data_structures:: static_assert_size!( MatcherPos <' _>, 232 ) ;
156+ rustc_data_structures:: static_assert_size!( MatcherPos <' _>, 136 ) ;
187157
188158impl < ' tt > MatcherPos < ' tt > {
189159 /// `len` `Vec`s (initially shared and empty) that will store matches of metavars.
@@ -203,7 +173,7 @@ impl<'tt> MatcherPos<'tt> {
203173 let match_idx_hi = count_names ( ms) ;
204174 MatcherPos {
205175 // Start with the top level matcher given to us.
206- top_elts : TtSlice ( ms ) ,
176+ top_elts : ms ,
207177
208178 // The "dot" is before the first token of the matcher.
209179 idx : 0 ,
@@ -224,9 +194,9 @@ impl<'tt> MatcherPos<'tt> {
224194 }
225195 }
226196
227- fn repetition ( up : Box < MatcherPos < ' tt > > , sp : DelimSpan , seq : Lrc < SequenceRepetition > ) -> Self {
197+ fn repetition ( up : Box < MatcherPos < ' tt > > , seq : & ' tt SequenceRepetition ) -> Self {
228198 MatcherPos {
229- stack : smallvec ! [ ] ,
199+ top_elts : & seq . tts ,
230200 idx : 0 ,
231201 matches : Self :: create_matches ( up. matches . len ( ) ) ,
232202 match_lo : up. match_cur ,
@@ -237,7 +207,7 @@ impl<'tt> MatcherPos<'tt> {
237207 sep : seq. separator . clone ( ) ,
238208 seq_op : seq. kleene . op ,
239209 } ) ,
240- top_elts : Tt ( TokenTree :: Sequence ( sp , seq ) ) ,
210+ stack : smallvec ! [ ] ,
241211 }
242212 }
243213
@@ -288,8 +258,8 @@ crate type NamedParseResult = ParseResult<FxHashMap<MacroRulesNormalizedIdent, N
288258pub ( super ) fn count_names ( ms : & [ TokenTree ] ) -> usize {
289259 ms. iter ( ) . fold ( 0 , |count, elt| {
290260 count
291- + match * elt {
292- TokenTree :: Delimited ( _, ref delim) => count_names ( & delim. tts ) ,
261+ + match elt {
262+ TokenTree :: Delimited ( _, delim) => count_names ( delim. inner_tts ( ) ) ,
293263 TokenTree :: MetaVar ( ..) => 0 ,
294264 TokenTree :: MetaVarDecl ( ..) => 1 ,
295265 // Panicking here would abort execution because `parse_tree` makes use of this
@@ -298,7 +268,7 @@ pub(super) fn count_names(ms: &[TokenTree]) -> usize {
298268 // `0` is still returned to inform that no meta-variable was found. `Meta-variables
299269 // != Meta-variable expressions`
300270 TokenTree :: MetaVarExpr ( ..) => 0 ,
301- TokenTree :: Sequence ( _, ref seq) => seq. num_captures ,
271+ TokenTree :: Sequence ( _, seq) => seq. num_captures ,
302272 TokenTree :: Token ( ..) => 0 ,
303273 }
304274 } )
@@ -382,7 +352,7 @@ fn nameize<I: Iterator<Item = NamedMatch>>(
382352 }
383353 }
384354 TokenTree :: Delimited ( _, ref delim) => {
385- for next_m in & delim. tts {
355+ for next_m in delim. inner_tts ( ) {
386356 n_rec ( sess, next_m, res. by_ref ( ) , ret_val) ?;
387357 }
388358 }
@@ -446,8 +416,8 @@ pub struct TtParser<'tt> {
446416}
447417
448418impl < ' tt > TtParser < ' tt > {
449- pub ( super ) fn new ( macro_name : Ident ) -> Self {
450- Self { macro_name, cur_items : vec ! [ ] , next_items : vec ! [ ] , bb_items : vec ! [ ] }
419+ pub ( super ) fn new ( macro_name : Ident ) -> TtParser < ' tt > {
420+ TtParser { macro_name, cur_items : vec ! [ ] , next_items : vec ! [ ] , bb_items : vec ! [ ] }
451421 }
452422
453423 /// Process the matcher positions of `cur_items` until it is empty. In the process, this will
@@ -492,8 +462,8 @@ impl<'tt> TtParser<'tt> {
492462 if idx < len {
493463 // We are in the middle of a matcher. Compare the matcher's current tt against
494464 // `token`.
495- match item. top_elts . get_tt ( idx) {
496- TokenTree :: Sequence ( sp , seq) => {
465+ match & item. top_elts [ idx] {
466+ TokenTree :: Sequence ( _sp , seq) => {
497467 let op = seq. kleene . op ;
498468 if op == mbe:: KleeneOp :: ZeroOrMore || op == mbe:: KleeneOp :: ZeroOrOne {
499469 // Allow for the possibility of zero matches of this sequence.
@@ -507,17 +477,17 @@ impl<'tt> TtParser<'tt> {
507477 }
508478
509479 // Allow for the possibility of one or more matches of this sequence.
510- self . cur_items . push ( box MatcherPos :: repetition ( item, sp , seq) ) ;
480+ self . cur_items . push ( box MatcherPos :: repetition ( item, & seq) ) ;
511481 }
512482
513- TokenTree :: MetaVarDecl ( span, _, None ) => {
483+ & TokenTree :: MetaVarDecl ( span, _, None ) => {
514484 // E.g. `$e` instead of `$e:expr`.
515485 if sess. missing_fragment_specifiers . borrow_mut ( ) . remove ( & span) . is_some ( ) {
516486 return Some ( Error ( span, "missing fragment specifier" . to_string ( ) ) ) ;
517487 }
518488 }
519489
520- TokenTree :: MetaVarDecl ( _, _, Some ( kind) ) => {
490+ & TokenTree :: MetaVarDecl ( _, _, Some ( kind) ) => {
521491 // Built-in nonterminals never start with these tokens, so we can eliminate
522492 // them from consideration.
523493 //
@@ -528,13 +498,14 @@ impl<'tt> TtParser<'tt> {
528498 }
529499 }
530500
531- seq @ TokenTree :: Delimited ( .. ) => {
501+ TokenTree :: Delimited ( _ , delimited ) => {
532502 // To descend into a delimited submatcher, we push the current matcher onto
533503 // a stack and push a new item containing the submatcher onto `cur_items`.
534504 //
535505 // At the beginning of the loop, if we reach the end of the delimited
536- // submatcher, we pop the stack to backtrack out of the descent.
537- let lower_elts = mem:: replace ( & mut item. top_elts , Tt ( seq) ) ;
506+ // submatcher, we pop the stack to backtrack out of the descent. Note that
507+ // we use `all_tts` to include the open and close delimiter tokens.
508+ let lower_elts = mem:: replace ( & mut item. top_elts , & delimited. all_tts ) ;
538509 let idx = item. idx ;
539510 item. stack . push ( MatcherTtFrame { elts : lower_elts, idx } ) ;
540511 item. idx = 0 ;
@@ -560,7 +531,6 @@ impl<'tt> TtParser<'tt> {
560531 } else if let Some ( repetition) = & item. repetition {
561532 // We are past the end of a repetition.
562533 debug_assert ! ( idx <= len + 1 ) ;
563- debug_assert ! ( matches!( item. top_elts, Tt ( TokenTree :: Sequence ( ..) ) ) ) ;
564534
565535 if idx == len {
566536 // Add all matches from the sequence to `up`, and move the "dot" past the
@@ -678,9 +648,7 @@ impl<'tt> TtParser<'tt> {
678648 ( 0 , 1 ) => {
679649 // We need to call the black-box parser to get some nonterminal.
680650 let mut item = self . bb_items . pop ( ) . unwrap ( ) ;
681- if let TokenTree :: MetaVarDecl ( span, _, Some ( kind) ) =
682- item. top_elts . get_tt ( item. idx )
683- {
651+ if let TokenTree :: MetaVarDecl ( span, _, Some ( kind) ) = item. top_elts [ item. idx ] {
684652 let match_cur = item. match_cur ;
685653 // We use the span of the metavariable declaration to determine any
686654 // edition-specific matching behavior for non-terminals.
@@ -720,7 +688,7 @@ impl<'tt> TtParser<'tt> {
720688 let nts = self
721689 . bb_items
722690 . iter ( )
723- . map ( |item| match item. top_elts . get_tt ( item. idx ) {
691+ . map ( |item| match item. top_elts [ item. idx ] {
724692 TokenTree :: MetaVarDecl ( _, bind, Some ( kind) ) => {
725693 format ! ( "{} ('{}')" , kind, bind)
726694 }
0 commit comments