@@ -428,17 +428,26 @@ fn token_name_eq(t1: &Token, t2: &Token) -> bool {
428
428
}
429
429
}
430
430
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 > {
432
434
macro_name : Ident ,
433
435
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`.
434
438
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`.
435
442
next_items : Vec < Box < MatcherPos < ' tt > > > ,
443
+
444
+ /// The set of items that are waiting for the black-box parser.
436
445
bb_items : Vec < Box < MatcherPos < ' tt > > > ,
437
446
}
438
447
439
- impl TtParser {
448
+ impl < ' tt > TtParser < ' tt > {
440
449
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 ! [ ] }
442
451
}
443
452
444
453
/// Process the matcher positions of `cur_items` until it is empty. In the process, this will
@@ -447,33 +456,21 @@ impl TtParser {
447
456
/// For more info about the how this happens, see the module-level doc comments and the inline
448
457
/// comments of this function.
449
458
///
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
- ///
459
459
/// # Returns
460
460
///
461
461
/// `Some(result)` if everything is finished, `None` otherwise. Note that matches are kept
462
462
/// track of through the items generated.
463
- fn parse_tt_inner < ' tt > (
464
- & self ,
463
+ fn parse_tt_inner (
464
+ & mut self ,
465
465
sess : & ParseSess ,
466
466
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 ] > ,
470
467
token : & Token ,
471
468
) -> Option < NamedParseResult > {
472
469
// Matcher positions that would be valid if the macro invocation was over now. Only
473
470
// modified if `token == Eof`.
474
471
let mut eof_items = EofItems :: None ;
475
472
476
- while let Some ( mut item) = cur_items. pop ( ) {
473
+ while let Some ( mut item) = self . cur_items . pop ( ) {
477
474
// When unzipped trees end, remove them. This corresponds to backtracking out of a
478
475
// delimited submatcher into which we already descended. When backtracking out again, we
479
476
// need to advance the "dot" past the delimiters in the outer matcher.
@@ -506,11 +503,11 @@ impl TtParser {
506
503
for idx in item. match_cur ..item. match_cur + seq. num_captures {
507
504
new_item. push_match ( idx, MatchedSeq ( Lrc :: new ( smallvec ! [ ] ) ) ) ;
508
505
}
509
- cur_items. push ( new_item) ;
506
+ self . cur_items . push ( new_item) ;
510
507
}
511
508
512
509
// 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) ) ;
514
511
}
515
512
516
513
TokenTree :: MetaVarDecl ( span, _, None ) => {
@@ -527,7 +524,7 @@ impl TtParser {
527
524
// We use the span of the metavariable declaration to determine any
528
525
// edition-specific matching behavior for non-terminals.
529
526
if Parser :: nonterminal_may_begin_with ( kind, token) {
530
- bb_items. push ( item) ;
527
+ self . bb_items . push ( item) ;
531
528
}
532
529
}
533
530
@@ -541,7 +538,7 @@ impl TtParser {
541
538
let idx = item. idx ;
542
539
item. stack . push ( MatcherTtFrame { elts : lower_elts, idx } ) ;
543
540
item. idx = 0 ;
544
- cur_items. push ( item) ;
541
+ self . cur_items . push ( item) ;
545
542
}
546
543
547
544
TokenTree :: Token ( t) => {
@@ -553,7 +550,7 @@ impl TtParser {
553
550
// `cur_items` will match.
554
551
if token_name_eq ( & t, token) {
555
552
item. idx += 1 ;
556
- next_items. push ( item) ;
553
+ self . next_items . push ( item) ;
557
554
}
558
555
}
559
556
@@ -576,23 +573,23 @@ impl TtParser {
576
573
}
577
574
new_pos. match_cur = item. match_hi ;
578
575
new_pos. idx += 1 ;
579
- cur_items. push ( new_pos) ;
576
+ self . cur_items . push ( new_pos) ;
580
577
}
581
578
582
579
if idx == len && repetition. sep . is_some ( ) {
583
580
if repetition. sep . as_ref ( ) . map_or ( false , |sep| token_name_eq ( token, sep) ) {
584
581
// The matcher has a separator, and it matches the current token. We can
585
582
// advance past the separator token.
586
583
item. idx += 1 ;
587
- next_items. push ( item) ;
584
+ self . next_items . push ( item) ;
588
585
}
589
586
} else if repetition. seq_op != mbe:: KleeneOp :: ZeroOrOne {
590
587
// We don't need a separator. Move the "dot" back to the beginning of the
591
588
// matcher and try to match again UNLESS we are only allowed to have _one_
592
589
// repetition.
593
590
item. match_cur = item. match_lo ;
594
591
item. idx = 0 ;
595
- cur_items. push ( item) ;
592
+ self . cur_items . push ( item) ;
596
593
}
597
594
} else {
598
595
// We are past the end of the matcher, and not in a repetition. Look for end of
@@ -635,41 +632,33 @@ impl TtParser {
635
632
/// Use the given slice of token trees (`ms`) as a matcher. Match the token stream from the
636
633
/// given `parser` against it and return the match.
637
634
pub ( super ) fn parse_tt (
638
- & self ,
635
+ & mut self ,
639
636
parser : & mut Cow < ' _ , Parser < ' _ > > ,
640
- ms : & [ TokenTree ] ,
637
+ ms : & ' tt [ TokenTree ] ,
641
638
) -> NamedParseResult {
642
639
// A queue of possible matcher positions. We initialize it with the matcher position in
643
640
// which the "dot" is before the first token of the first token tree in `ms`.
644
641
// `parse_tt_inner` then processes all of these possible matcher positions and produces
645
642
// possible next positions into `next_items`. After some post-processing, the contents of
646
643
// `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) ) ;
648
646
649
647
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 ( ) ;
654
650
655
651
// Process `cur_items` until either we have finished the input or we need to get some
656
652
// 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 ) {
665
654
return result;
666
655
}
667
656
668
657
// `parse_tt_inner` handled all cur_items, so it's empty.
669
- assert ! ( cur_items. is_empty( ) ) ;
658
+ assert ! ( self . cur_items. is_empty( ) ) ;
670
659
671
660
// 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 ( ) ) {
673
662
( 0 , 0 ) => {
674
663
// There are no possible next positions AND we aren't waiting for the black-box
675
664
// parser: syntax error.
@@ -682,13 +671,13 @@ impl TtParser {
682
671
( _, 0 ) => {
683
672
// Dump all possible `next_items` into `cur_items` for the next iteration. Then
684
673
// process the next token.
685
- cur_items. extend ( next_items. drain ( ..) ) ;
674
+ self . cur_items . extend ( self . next_items . drain ( ..) ) ;
686
675
parser. to_mut ( ) . bump ( ) ;
687
676
}
688
677
689
678
( 0 , 1 ) => {
690
679
// 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 ( ) ;
692
681
if let TokenTree :: MetaVarDecl ( span, _, Some ( kind) ) =
693
682
item. top_elts . get_tt ( item. idx )
694
683
{
@@ -714,26 +703,22 @@ impl TtParser {
714
703
} else {
715
704
unreachable ! ( )
716
705
}
717
- cur_items. push ( item) ;
706
+ self . cur_items . push ( item) ;
718
707
}
719
708
720
709
( _, _) => {
721
710
// Too many possibilities!
722
- return self . ambiguity_error ( next_items , bb_items , parser. token . span ) ;
711
+ return self . ambiguity_error ( parser. token . span ) ;
723
712
}
724
713
}
725
714
726
- assert ! ( !cur_items. is_empty( ) ) ;
715
+ assert ! ( !self . cur_items. is_empty( ) ) ;
727
716
}
728
717
}
729
718
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
737
722
. iter ( )
738
723
. map ( |item| match item. top_elts . get_tt ( item. idx ) {
739
724
TokenTree :: MetaVarDecl ( _, bind, Some ( kind) ) => {
@@ -749,7 +734,7 @@ impl TtParser {
749
734
format ! (
750
735
"local ambiguity when calling macro `{}`: multiple parsing options: {}" ,
751
736
self . macro_name,
752
- match next_items. len( ) {
737
+ match self . next_items. len( ) {
753
738
0 => format!( "built-in NTs {}." , nts) ,
754
739
1 => format!( "built-in NTs {} or 1 other option." , nts) ,
755
740
n => format!( "built-in NTs {} or {} other options." , nts, n) ,
0 commit comments