Skip to content

Commit 8bd1bca

Browse files
committed
Factor out some code into MatcherPos::repetition.
Also move `create_matches` within `impl MatcherPos`, because it's only used within that impl block.
1 parent 5bbbee5 commit 8bd1bca

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

compiler/rustc_expand/src/mbe/macro_parser.rs

+37-28
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ crate use NamedMatch::*;
7474
crate use ParseResult::*;
7575
use TokenTreeOrTokenTreeSlice::*;
7676

77-
use crate::mbe::{self, TokenTree};
77+
use crate::mbe::{self, DelimSpan, SequenceRepetition, TokenTree};
7878

7979
use rustc_ast::token::{self, DocComment, Nonterminal, Token};
8080
use rustc_parse::parser::Parser;
@@ -203,6 +203,17 @@ struct MatcherPos<'root, 'tt> {
203203
rustc_data_structures::static_assert_size!(MatcherPos<'_, '_>, 240);
204204

205205
impl<'root, 'tt> MatcherPos<'root, 'tt> {
206+
/// `len` `Vec`s (initially shared and empty) that will store matches of metavars.
207+
fn create_matches(len: usize) -> Box<[Lrc<NamedMatchVec>]> {
208+
if len == 0 {
209+
vec![]
210+
} else {
211+
let empty_matches = Lrc::new(SmallVec::new());
212+
vec![empty_matches; len]
213+
}
214+
.into_boxed_slice()
215+
}
216+
206217
/// Generates the top-level matcher position in which the "dot" is before the first token of
207218
/// the matcher `ms`.
208219
fn new(ms: &'tt [TokenTree]) -> Self {
@@ -217,7 +228,7 @@ impl<'root, 'tt> MatcherPos<'root, 'tt> {
217228
// Initialize `matches` to a bunch of empty `Vec`s -- one for each metavar in
218229
// `top_elts`. `match_lo` for `top_elts` is 0 and `match_hi` is `match_idx_hi`.
219230
// `match_cur` is 0 since we haven't actually matched anything yet.
220-
matches: create_matches(match_idx_hi),
231+
matches: Self::create_matches(match_idx_hi),
221232
match_lo: 0,
222233
match_cur: 0,
223234
match_hi: match_idx_hi,
@@ -230,6 +241,27 @@ impl<'root, 'tt> MatcherPos<'root, 'tt> {
230241
}
231242
}
232243

244+
fn repetition(
245+
up: MatcherPosHandle<'root, 'tt>,
246+
sp: DelimSpan,
247+
seq: Lrc<SequenceRepetition>,
248+
) -> Self {
249+
MatcherPos {
250+
stack: smallvec![],
251+
idx: 0,
252+
matches: Self::create_matches(up.matches.len()),
253+
match_lo: up.match_cur,
254+
match_cur: up.match_cur,
255+
match_hi: up.match_cur + seq.num_captures,
256+
repetition: Some(MatcherPosRepetition {
257+
up,
258+
sep: seq.separator.clone(),
259+
seq_op: seq.kleene.op,
260+
}),
261+
top_elts: Tt(TokenTree::Sequence(sp, seq)),
262+
}
263+
}
264+
233265
/// Adds `m` as a named match for the `idx`-th metavar.
234266
fn push_match(&mut self, idx: usize, m: NamedMatch) {
235267
let matches = Lrc::make_mut(&mut self.matches[idx]);
@@ -333,17 +365,6 @@ pub(super) fn count_names(ms: &[TokenTree]) -> usize {
333365
})
334366
}
335367

336-
/// `len` `Vec`s (initially shared and empty) that will store matches of metavars.
337-
fn create_matches(len: usize) -> Box<[Lrc<NamedMatchVec>]> {
338-
if len == 0 {
339-
vec![]
340-
} else {
341-
let empty_matches = Lrc::new(SmallVec::new());
342-
vec![empty_matches; len]
343-
}
344-
.into_boxed_slice()
345-
}
346-
347368
/// `NamedMatch` is a pattern-match result for a single `token::MATCH_NONTERMINAL`:
348369
/// so it is associated with a single ident in a parse, and all
349370
/// `MatchedNonterminal`s in the `NamedMatch` have the same non-terminal type
@@ -599,21 +620,9 @@ fn parse_tt_inner<'root, 'tt>(
599620
cur_items.push(new_item);
600621
}
601622

602-
let matches = create_matches(item.matches.len());
603-
cur_items.push(MatcherPosHandle::Box(Box::new(MatcherPos {
604-
stack: smallvec![],
605-
idx: 0,
606-
matches,
607-
match_lo: item.match_cur,
608-
match_cur: item.match_cur,
609-
match_hi: item.match_cur + seq.num_captures,
610-
repetition: Some(MatcherPosRepetition {
611-
up: item,
612-
sep: seq.separator.clone(),
613-
seq_op: seq.kleene.op,
614-
}),
615-
top_elts: Tt(TokenTree::Sequence(sp, seq)),
616-
})));
623+
cur_items.push(MatcherPosHandle::Box(Box::new(MatcherPos::repetition(
624+
item, sp, seq,
625+
))));
617626
}
618627

619628
// We need to match a metavar (but the identifier is invalid)... this is an error

0 commit comments

Comments
 (0)