@@ -75,7 +75,7 @@ crate use ParseResult::*;
75
75
76
76
use crate :: mbe:: { self , SequenceRepetition , TokenTree } ;
77
77
78
- use rustc_ast:: token:: { self , DocComment , Nonterminal , Token } ;
78
+ use rustc_ast:: token:: { self , DocComment , Nonterminal , Token , TokenKind } ;
79
79
use rustc_parse:: parser:: { NtOrTt , Parser } ;
80
80
use rustc_session:: parse:: ParseSess ;
81
81
use rustc_span:: symbol:: MacroRulesNormalizedIdent ;
@@ -454,18 +454,6 @@ impl<'tt> TtParser<'tt> {
454
454
let mut eof_mps = EofMatcherPositions :: None ;
455
455
456
456
while let Some ( mut mp) = self . cur_mps . pop ( ) {
457
- // Backtrack out of delimited submatcher when necessary. When backtracking out again,
458
- // we need to advance the "dot" past the delimiters in the parent matcher(s).
459
- while mp. idx >= mp. tts . len ( ) {
460
- match mp. stack . pop ( ) {
461
- Some ( MatcherPosFrame { tts, idx } ) => {
462
- mp. tts = tts;
463
- mp. idx = idx + 1 ;
464
- }
465
- None => break ,
466
- }
467
- }
468
-
469
457
// Get the current position of the "dot" (`idx`) in `mp` and the number of token
470
458
// trees in the matcher (`len`).
471
459
let idx = mp. idx ;
@@ -516,11 +504,10 @@ impl<'tt> TtParser<'tt> {
516
504
517
505
TokenTree :: Delimited ( _, delimited) => {
518
506
// To descend into a delimited submatcher, we push the current matcher onto
519
- // a stack and push a new mp containing the submatcher onto `cur_mps`.
520
- //
521
- // At the beginning of the loop, if we reach the end of the delimited
522
- // submatcher, we pop the stack to backtrack out of the descent. Note that
523
- // we use `all_tts` to include the open and close delimiter tokens.
507
+ // a stack and push a new mp containing the submatcher onto `cur_mps`. When
508
+ // we reach the closing delimiter, we will pop the stack to backtrack out
509
+ // of the descent. Note that we use `all_tts` to include the open and close
510
+ // delimiter tokens.
524
511
let tts = mem:: replace ( & mut mp. tts , & delimited. all_tts ) ;
525
512
let idx = mp. idx ;
526
513
mp. stack . push ( MatcherPosFrame { tts, idx } ) ;
@@ -542,6 +529,13 @@ impl<'tt> TtParser<'tt> {
542
529
mp. idx += 1 ;
543
530
self . cur_mps . push ( mp) ;
544
531
} else if token_name_eq ( & t, token) {
532
+ if let TokenKind :: CloseDelim ( _) = token. kind {
533
+ // Ascend out of the delimited submatcher.
534
+ debug_assert_eq ! ( idx, len - 1 ) ;
535
+ let frame = mp. stack . pop ( ) . unwrap ( ) ;
536
+ mp. tts = frame. tts ;
537
+ mp. idx = frame. idx ;
538
+ }
545
539
mp. idx += 1 ;
546
540
self . next_mps . push ( mp) ;
547
541
}
0 commit comments