@@ -428,17 +428,26 @@ fn token_name_eq(t1: &Token, t2: &Token) -> bool {
428428 }
429429}
430430
431- pub struct TtParser {
431+ // Note: the item vectors could be created and dropped within `parse_tt`, but to avoid excess
432+ // allocations we have a single vector fo each kind that is cleared and reused repeatedly.
433+ pub struct TtParser < ' tt > {
432434 macro_name : Ident ,
433435
436+ /// The set of current items to be processed. This should be empty by the end of a successful
437+ /// execution of `parse_tt_inner`.
434438 cur_items : Vec < Box < MatcherPos < ' tt > > > ,
439+
440+ /// The set of newly generated items. These are used to replenish `cur_items` in the function
441+ /// `parse_tt`.
435442 next_items : Vec < Box < MatcherPos < ' tt > > > ,
443+
444+ /// The set of items that are waiting for the black-box parser.
436445 bb_items : Vec < Box < MatcherPos < ' tt > > > ,
437446}
438447
439- impl TtParser {
448+ impl < ' tt > TtParser < ' tt > {
440449 pub ( super ) fn new ( macro_name : Ident ) -> Self {
441- Self { macro_name }
450+ Self { macro_name, cur_items : vec ! [ ] , next_items : vec ! [ ] , bb_items : vec ! [ ] }
442451 }
443452
444453 /// Process the matcher positions of `cur_items` until it is empty. In the process, this will
@@ -447,33 +456,21 @@ impl TtParser {
447456 /// For more info about the how this happens, see the module-level doc comments and the inline
448457 /// comments of this function.
449458 ///
450- /// # Parameters
451- ///
452- /// - `cur_items`: the set of current items to be processed. This should be empty by the end of
453- /// a successful execution of this function.
454- /// - `next_items`: the set of newly generated items. These are used to replenish `cur_items` in
455- /// the function `parse`.
456- /// - `bb_items`: the set of items that are waiting for the black-box parser.
457- /// - `token`: the current token of the parser.
458- ///
459459 /// # Returns
460460 ///
461461 /// `Some(result)` if everything is finished, `None` otherwise. Note that matches are kept
462462 /// track of through the items generated.
463- fn parse_tt_inner < ' tt > (
464- & self ,
463+ fn parse_tt_inner (
464+ & mut self ,
465465 sess : & ParseSess ,
466466 ms : & [ TokenTree ] ,
467- cur_items : & mut SmallVec < [ Box < MatcherPos < ' tt > > ; 1 ] > ,
468- next_items : & mut SmallVec < [ Box < MatcherPos < ' tt > > ; 1 ] > ,
469- bb_items : & mut SmallVec < [ Box < MatcherPos < ' tt > > ; 1 ] > ,
470467 token : & Token ,
471468 ) -> Option < NamedParseResult > {
472469 // Matcher positions that would be valid if the macro invocation was over now. Only
473470 // modified if `token == Eof`.
474471 let mut eof_items = EofItems :: None ;
475472
476- while let Some ( mut item) = cur_items. pop ( ) {
473+ while let Some ( mut item) = self . cur_items . pop ( ) {
477474 // When unzipped trees end, remove them. This corresponds to backtracking out of a
478475 // delimited submatcher into which we already descended. When backtracking out again, we
479476 // need to advance the "dot" past the delimiters in the outer matcher.
@@ -506,11 +503,11 @@ impl TtParser {
506503 for idx in item. match_cur ..item. match_cur + seq. num_captures {
507504 new_item. push_match ( idx, MatchedSeq ( Lrc :: new ( smallvec ! [ ] ) ) ) ;
508505 }
509- cur_items. push ( new_item) ;
506+ self . cur_items . push ( new_item) ;
510507 }
511508
512509 // Allow for the possibility of one or more matches of this sequence.
513- cur_items. push ( box MatcherPos :: repetition ( item, sp, seq) ) ;
510+ self . cur_items . push ( box MatcherPos :: repetition ( item, sp, seq) ) ;
514511 }
515512
516513 TokenTree :: MetaVarDecl ( span, _, None ) => {
@@ -527,7 +524,7 @@ impl TtParser {
527524 // We use the span of the metavariable declaration to determine any
528525 // edition-specific matching behavior for non-terminals.
529526 if Parser :: nonterminal_may_begin_with ( kind, token) {
530- bb_items. push ( item) ;
527+ self . bb_items . push ( item) ;
531528 }
532529 }
533530
@@ -541,7 +538,7 @@ impl TtParser {
541538 let idx = item. idx ;
542539 item. stack . push ( MatcherTtFrame { elts : lower_elts, idx } ) ;
543540 item. idx = 0 ;
544- cur_items. push ( item) ;
541+ self . cur_items . push ( item) ;
545542 }
546543
547544 TokenTree :: Token ( t) => {
@@ -553,7 +550,7 @@ impl TtParser {
553550 // `cur_items` will match.
554551 if token_name_eq ( & t, token) {
555552 item. idx += 1 ;
556- next_items. push ( item) ;
553+ self . next_items . push ( item) ;
557554 }
558555 }
559556
@@ -576,23 +573,23 @@ impl TtParser {
576573 }
577574 new_pos. match_cur = item. match_hi ;
578575 new_pos. idx += 1 ;
579- cur_items. push ( new_pos) ;
576+ self . cur_items . push ( new_pos) ;
580577 }
581578
582579 if idx == len && repetition. sep . is_some ( ) {
583580 if repetition. sep . as_ref ( ) . map_or ( false , |sep| token_name_eq ( token, sep) ) {
584581 // The matcher has a separator, and it matches the current token. We can
585582 // advance past the separator token.
586583 item. idx += 1 ;
587- next_items. push ( item) ;
584+ self . next_items . push ( item) ;
588585 }
589586 } else if repetition. seq_op != mbe:: KleeneOp :: ZeroOrOne {
590587 // We don't need a separator. Move the "dot" back to the beginning of the
591588 // matcher and try to match again UNLESS we are only allowed to have _one_
592589 // repetition.
593590 item. match_cur = item. match_lo ;
594591 item. idx = 0 ;
595- cur_items. push ( item) ;
592+ self . cur_items . push ( item) ;
596593 }
597594 } else {
598595 // We are past the end of the matcher, and not in a repetition. Look for end of
@@ -635,41 +632,33 @@ impl TtParser {
635632 /// Use the given slice of token trees (`ms`) as a matcher. Match the token stream from the
636633 /// given `parser` against it and return the match.
637634 pub ( super ) fn parse_tt (
638- & self ,
635+ & mut self ,
639636 parser : & mut Cow < ' _ , Parser < ' _ > > ,
640- ms : & [ TokenTree ] ,
637+ ms : & ' tt [ TokenTree ] ,
641638 ) -> NamedParseResult {
642639 // A queue of possible matcher positions. We initialize it with the matcher position in
643640 // which the "dot" is before the first token of the first token tree in `ms`.
644641 // `parse_tt_inner` then processes all of these possible matcher positions and produces
645642 // possible next positions into `next_items`. After some post-processing, the contents of
646643 // `next_items` replenish `cur_items` and we start over again.
647- let mut cur_items = smallvec ! [ box MatcherPos :: new( ms) ] ;
644+ self . cur_items . clear ( ) ;
645+ self . cur_items . push ( box MatcherPos :: new ( ms) ) ;
648646
649647 loop {
650- let mut next_items = SmallVec :: new ( ) ;
651-
652- // Matcher positions black-box parsed by `Parser`.
653- let mut bb_items = SmallVec :: new ( ) ;
648+ self . next_items . clear ( ) ;
649+ self . bb_items . clear ( ) ;
654650
655651 // Process `cur_items` until either we have finished the input or we need to get some
656652 // parsing from the black-box parser done.
657- if let Some ( result) = self . parse_tt_inner (
658- parser. sess ,
659- ms,
660- & mut cur_items,
661- & mut next_items,
662- & mut bb_items,
663- & parser. token ,
664- ) {
653+ if let Some ( result) = self . parse_tt_inner ( parser. sess , ms, & parser. token ) {
665654 return result;
666655 }
667656
668657 // `parse_tt_inner` handled all cur_items, so it's empty.
669- assert ! ( cur_items. is_empty( ) ) ;
658+ assert ! ( self . cur_items. is_empty( ) ) ;
670659
671660 // Error messages here could be improved with links to original rules.
672- match ( next_items. len ( ) , bb_items. len ( ) ) {
661+ match ( self . next_items . len ( ) , self . bb_items . len ( ) ) {
673662 ( 0 , 0 ) => {
674663 // There are no possible next positions AND we aren't waiting for the black-box
675664 // parser: syntax error.
@@ -682,13 +671,13 @@ impl TtParser {
682671 ( _, 0 ) => {
683672 // Dump all possible `next_items` into `cur_items` for the next iteration. Then
684673 // process the next token.
685- cur_items. extend ( next_items. drain ( ..) ) ;
674+ self . cur_items . extend ( self . next_items . drain ( ..) ) ;
686675 parser. to_mut ( ) . bump ( ) ;
687676 }
688677
689678 ( 0 , 1 ) => {
690679 // We need to call the black-box parser to get some nonterminal.
691- let mut item = bb_items. pop ( ) . unwrap ( ) ;
680+ let mut item = self . bb_items . pop ( ) . unwrap ( ) ;
692681 if let TokenTree :: MetaVarDecl ( span, _, Some ( kind) ) =
693682 item. top_elts . get_tt ( item. idx )
694683 {
@@ -714,26 +703,22 @@ impl TtParser {
714703 } else {
715704 unreachable ! ( )
716705 }
717- cur_items. push ( item) ;
706+ self . cur_items . push ( item) ;
718707 }
719708
720709 ( _, _) => {
721710 // Too many possibilities!
722- return self . ambiguity_error ( next_items , bb_items , parser. token . span ) ;
711+ return self . ambiguity_error ( parser. token . span ) ;
723712 }
724713 }
725714
726- assert ! ( !cur_items. is_empty( ) ) ;
715+ assert ! ( !self . cur_items. is_empty( ) ) ;
727716 }
728717 }
729718
730- fn ambiguity_error < ' tt > (
731- & self ,
732- next_items : SmallVec < [ Box < MatcherPos < ' tt > > ; 1 ] > ,
733- bb_items : SmallVec < [ Box < MatcherPos < ' tt > > ; 1 ] > ,
734- token_span : rustc_span:: Span ,
735- ) -> NamedParseResult {
736- let nts = bb_items
719+ fn ambiguity_error ( & self , token_span : rustc_span:: Span ) -> NamedParseResult {
720+ let nts = self
721+ . bb_items
737722 . iter ( )
738723 . map ( |item| match item. top_elts . get_tt ( item. idx ) {
739724 TokenTree :: MetaVarDecl ( _, bind, Some ( kind) ) => {
@@ -749,7 +734,7 @@ impl TtParser {
749734 format ! (
750735 "local ambiguity when calling macro `{}`: multiple parsing options: {}" ,
751736 self . macro_name,
752- match next_items. len( ) {
737+ match self . next_items. len( ) {
753738 0 => format!( "built-in NTs {}." , nts) ,
754739 1 => format!( "built-in NTs {} or 1 other option." , nts) ,
755740 n => format!( "built-in NTs {} or {} other options." , nts, n) ,
0 commit comments