1
Fork 0

Simplify and rename count_names.

This commit is contained in:
Nicholas Nethercote 2022-03-25 16:23:26 +11:00
parent df6ead557d
commit 2b60cc081b
2 changed files with 14 additions and 14 deletions

View file

@ -177,7 +177,7 @@ impl<'tt> MatcherPos<'tt> {
/// Generates the top-level matcher position in which the "dot" is before the first token of /// Generates the top-level matcher position in which the "dot" is before the first token of
/// the matcher `ms`. /// the matcher `ms`.
fn new(ms: &'tt [TokenTree]) -> Self { fn new(ms: &'tt [TokenTree]) -> Self {
let match_idx_hi = count_names(ms); let match_idx_hi = count_metavar_decls(ms);
MatcherPos { MatcherPos {
// Start with the top level matcher given to us. // Start with the top level matcher given to us.
top_elts: ms, top_elts: ms,
@ -254,24 +254,24 @@ crate enum ParseResult<T> {
/// of metavars to the token trees they bind to. /// of metavars to the token trees they bind to.
crate type NamedParseResult = ParseResult<FxHashMap<MacroRulesNormalizedIdent, NamedMatch>>; crate type NamedParseResult = ParseResult<FxHashMap<MacroRulesNormalizedIdent, NamedMatch>>;
/// Count how many metavars are named in the given matcher `ms`. /// Count how many metavars declarations are in `matcher`.
pub(super) fn count_names(ms: &[TokenTree]) -> usize { pub(super) fn count_metavar_decls(matcher: &[TokenTree]) -> usize {
ms.iter().fold(0, |count, elt| { matcher
count .iter()
+ match elt { .map(|tt| {
TokenTree::Delimited(_, delim) => count_names(delim.inner_tts()), match tt {
TokenTree::Delimited(_, delim) => count_metavar_decls(delim.inner_tts()),
TokenTree::MetaVar(..) => 0, TokenTree::MetaVar(..) => 0,
TokenTree::MetaVarDecl(..) => 1, TokenTree::MetaVarDecl(..) => 1,
// Panicking here would abort execution because `parse_tree` makes use of this // RHS meta-variable expressions eventually end-up here. `0` is returned to inform
// function. In other words, RHS meta-variable expressions eventually end-up here. // that no meta-variable was found, because "meta-variables" != "meta-variable
// // expressions".
// `0` is still returned to inform that no meta-variable was found. `Meta-variables
// != Meta-variable expressions`
TokenTree::MetaVarExpr(..) => 0, TokenTree::MetaVarExpr(..) => 0,
TokenTree::Sequence(_, seq) => seq.num_captures, TokenTree::Sequence(_, seq) => seq.num_captures,
TokenTree::Token(..) => 0, TokenTree::Token(..) => 0,
} }
}) })
.sum()
} }
/// `NamedMatch` is a pattern-match result for a single metavar. All /// `NamedMatch` is a pattern-match result for a single metavar. All

View file

@ -211,7 +211,7 @@ fn parse_tree(
let (separator, kleene) = let (separator, kleene) =
parse_sep_and_kleene_op(&mut trees, delim_span.entire(), sess); parse_sep_and_kleene_op(&mut trees, delim_span.entire(), sess);
// Count the number of captured "names" (i.e., named metavars) // Count the number of captured "names" (i.e., named metavars)
let name_captures = macro_parser::count_names(&sequence); let name_captures = macro_parser::count_metavar_decls(&sequence);
TokenTree::Sequence( TokenTree::Sequence(
delim_span, delim_span,
Lrc::new(SequenceRepetition { Lrc::new(SequenceRepetition {