Skip to content

Commit 97eb1b4

Browse files
committed
Change initial_matcher_pos() into MatcherPos::new().
1 parent e5f3fd6 commit 97eb1b4

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

compiler/rustc_expand/src/mbe/macro_parser.rs

+29-29
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ type NamedMatchVec = SmallVec<[NamedMatch; 4]>;
154154
/// lifetime. By separating `'tt` from `'root`, we can show that.
155155
#[derive(Clone)]
156156
struct MatcherPos<'root, 'tt> {
157-
/// The token or sequence of tokens that make up the matcher
157+
/// The token or sequence of tokens that make up the matcher. `elts` is short for "elements".
158158
top_elts: TokenTreeOrTokenTreeSlice<'tt>,
159159

160160
/// The position of the "dot" in this matcher
@@ -203,6 +203,33 @@ struct MatcherPos<'root, 'tt> {
203203
rustc_data_structures::static_assert_size!(MatcherPos<'_, '_>, 192);
204204

205205
impl<'root, 'tt> MatcherPos<'root, 'tt> {
206+
/// Generates the top-level matcher position in which the "dot" is before the first token of
207+
/// the matcher `ms`.
208+
fn new(ms: &'tt [TokenTree]) -> Self {
209+
let match_idx_hi = count_names(ms);
210+
MatcherPos {
211+
// Start with the top level matcher given to us.
212+
top_elts: TtSeq(ms),
213+
214+
// The "dot" is before the first token of the matcher.
215+
idx: 0,
216+
217+
// Initialize `matches` to a bunch of empty `Vec`s -- one for each metavar in
218+
// `top_elts`. `match_lo` for `top_elts` is 0 and `match_hi` is `match_idx_hi`.
219+
// `match_cur` is 0 since we haven't actually matched anything yet.
220+
matches: create_matches(match_idx_hi),
221+
match_lo: 0,
222+
match_cur: 0,
223+
match_hi: match_idx_hi,
224+
225+
// Haven't descended into any delimiters, so this is empty.
226+
stack: smallvec![],
227+
228+
// Haven't descended into any sequences, so this is `None`.
229+
repetition: None,
230+
}
231+
}
232+
206233
/// Adds `m` as a named match for the `idx`-th metavar.
207234
fn push_match(&mut self, idx: usize, m: NamedMatch) {
208235
let matches = Lrc::make_mut(&mut self.matches[idx]);
@@ -314,33 +341,6 @@ fn create_matches(len: usize) -> Box<[Lrc<NamedMatchVec>]> {
314341
.into_boxed_slice()
315342
}
316343

317-
/// Generates the top-level matcher position in which the "dot" is before the first token of the
318-
/// matcher `ms`.
319-
fn initial_matcher_pos<'root, 'tt>(ms: &'tt [TokenTree]) -> MatcherPos<'root, 'tt> {
320-
let match_idx_hi = count_names(ms);
321-
let matches = create_matches(match_idx_hi);
322-
MatcherPos {
323-
// Start with the top level matcher given to us
324-
top_elts: TtSeq(ms), // "elts" is an abbr. for "elements"
325-
// The "dot" is before the first token of the matcher
326-
idx: 0,
327-
328-
// Initialize `matches` to a bunch of empty `Vec`s -- one for each metavar in `top_elts`.
329-
// `match_lo` for `top_elts` is 0 and `match_hi` is `matches.len()`. `match_cur` is 0 since
330-
// we haven't actually matched anything yet.
331-
matches,
332-
match_lo: 0,
333-
match_cur: 0,
334-
match_hi: match_idx_hi,
335-
336-
// Haven't descended into any delimiters, so empty stack
337-
stack: smallvec![],
338-
339-
// Haven't descended into any sequences, so this is `None`.
340-
repetition: None,
341-
}
342-
}
343-
344344
/// `NamedMatch` is a pattern-match result for a single `token::MATCH_NONTERMINAL`:
345345
/// so it is associated with a single ident in a parse, and all
346346
/// `MatchedNonterminal`s in the `NamedMatch` have the same non-terminal type
@@ -642,7 +642,7 @@ pub(super) fn parse_tt(
642642
//
643643
// This MatcherPos instance is allocated on the stack. All others -- and
644644
// there are frequently *no* others! -- are allocated on the heap.
645-
let mut initial = initial_matcher_pos(ms);
645+
let mut initial = MatcherPos::new(ms);
646646
let mut cur_items = smallvec![MatcherPosHandle::Ref(&mut initial)];
647647
let mut next_items = Vec::new();
648648

0 commit comments

Comments
 (0)