Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A new matcher representation for use in parse_tt #95555

Merged
merged 2 commits into from
Apr 4, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Reorder match arms in parse_tt_inner.
To match the order the variants are declared in.
nnethercote committed Apr 4, 2022
commit 0bd47e8a393d8a670d2770d35e9158adfa1f813e
62 changes: 31 additions & 31 deletions compiler/rustc_expand/src/mbe/macro_parser.rs
Original file line number Diff line number Diff line change
@@ -443,6 +443,29 @@ impl<'tt> TtParser<'tt> {

while let Some(mut mp) = self.cur_mps.pop() {
match &self.locs[mp.idx] {
MatcherLoc::Token { token: t } => {
// If it's a doc comment, we just ignore it and move on to the next tt in the
// matcher. This is a bug, but #95267 showed that existing programs rely on
// this behaviour, and changing it would require some care and a transition
// period.
//
// If the token matches, we can just advance the parser.
//
// Otherwise, this match has failed, there is nothing to do, and hopefully
// another mp in `cur_mps` will match.
if matches!(t, Token { kind: DocComment(..), .. }) {
mp.idx += 1;
self.cur_mps.push(mp);
} else if token_name_eq(&t, token) {
mp.idx += 1;
self.next_mps.push(mp);
}
}
MatcherLoc::Delimited => {
// Entering the delimeter is trivial.
mp.idx += 1;
self.cur_mps.push(mp);
}
&MatcherLoc::Sequence {
op,
num_metavar_decls,
@@ -471,37 +494,6 @@ impl<'tt> TtParser<'tt> {
mp.idx += 1;
self.cur_mps.push(mp);
}
MatcherLoc::MetaVarDecl { kind, .. } => {
// Built-in nonterminals never start with these tokens, so we can eliminate
// them from consideration. We use the span of the metavariable declaration
// to determine any edition-specific matching behavior for non-terminals.
if Parser::nonterminal_may_begin_with(*kind, token) {
self.bb_mps.push(mp);
}
}
MatcherLoc::Delimited => {
// Entering the delimeter is trivial.
mp.idx += 1;
self.cur_mps.push(mp);
}
MatcherLoc::Token { token: t } => {
// If it's a doc comment, we just ignore it and move on to the next tt in the
// matcher. This is a bug, but #95267 showed that existing programs rely on
// this behaviour, and changing it would require some care and a transition
// period.
//
// If the token matches, we can just advance the parser.
//
// Otherwise, this match has failed, there is nothing to do, and hopefully
// another mp in `cur_mps` will match.
if matches!(t, Token { kind: DocComment(..), .. }) {
mp.idx += 1;
self.cur_mps.push(mp);
} else if token_name_eq(&t, token) {
mp.idx += 1;
self.next_mps.push(mp);
}
}
&MatcherLoc::SequenceKleeneOpNoSep { op, idx_first } => {
// We are past the end of a sequence with no separator. Try ending the
// sequence. If that's not possible, `ending_mp` will fail quietly when it is
@@ -540,6 +532,14 @@ impl<'tt> TtParser<'tt> {
mp.idx = idx_first;
self.cur_mps.push(mp);
}
MatcherLoc::MetaVarDecl { kind, .. } => {
// Built-in nonterminals never start with these tokens, so we can eliminate
// them from consideration. We use the span of the metavariable declaration
// to determine any edition-specific matching behavior for non-terminals.
if Parser::nonterminal_may_begin_with(*kind, token) {
self.bb_mps.push(mp);
}
}
MatcherLoc::Eof => {
// We are past the matcher's end, and not in a sequence. Try to end things.
debug_assert_eq!(mp.idx, self.locs.len() - 1);