Skip to content

Commit 21699c4

Browse files
committed
Simplify exit of Delimited submatchers.
Currently, we detect an exit from a `Delimited` submatcher when `idx` exceeds the bounds of the current submatcher *and* there is a `stack` entry. This commit changes it to something simpler: just look for a `CloseDelim` token.
1 parent c5cf08d commit 21699c4

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

compiler/rustc_expand/src/mbe/macro_parser.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ crate use ParseResult::*;
7575

7676
use crate::mbe::{self, SequenceRepetition, TokenTree};
7777

78-
use rustc_ast::token::{self, DocComment, Nonterminal, Token};
78+
use rustc_ast::token::{self, DocComment, Nonterminal, Token, TokenKind};
7979
use rustc_parse::parser::{NtOrTt, Parser};
8080
use rustc_session::parse::ParseSess;
8181
use rustc_span::symbol::MacroRulesNormalizedIdent;
@@ -454,18 +454,6 @@ impl<'tt> TtParser<'tt> {
454454
let mut eof_mps = EofMatcherPositions::None;
455455

456456
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-
469457
// Get the current position of the "dot" (`idx`) in `mp` and the number of token
470458
// trees in the matcher (`len`).
471459
let idx = mp.idx;
@@ -516,11 +504,10 @@ impl<'tt> TtParser<'tt> {
516504

517505
TokenTree::Delimited(_, delimited) => {
518506
// 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.
524511
let tts = mem::replace(&mut mp.tts, &delimited.all_tts);
525512
let idx = mp.idx;
526513
mp.stack.push(MatcherPosFrame { tts, idx });
@@ -542,6 +529,13 @@ impl<'tt> TtParser<'tt> {
542529
mp.idx += 1;
543530
self.cur_mps.push(mp);
544531
} 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+
}
545539
mp.idx += 1;
546540
self.next_mps.push(mp);
547541
}

0 commit comments

Comments
 (0)