Add TtParser::macro_name.

Instead of passing it into `parse_tt`.
This commit is contained in:
Nicholas Nethercote 2022-03-19 08:03:48 +11:00
parent 354bd1071c
commit 39810a85da
2 changed files with 35 additions and 38 deletions

View file

@ -492,9 +492,15 @@ fn token_name_eq(t1: &Token, t2: &Token) -> bool {
}
}
pub struct TtParser;
pub struct TtParser {
macro_name: Ident,
}
impl TtParser {
pub(super) fn new(macro_name: Ident) -> Self {
Self { macro_name }
}
/// Process the matcher positions of `cur_items` until it is empty. In the process, this will
/// produce more items in `next_items` and `bb_items`.
///
@ -693,7 +699,6 @@ impl TtParser {
&self,
parser: &mut Cow<'_, Parser<'_>>,
ms: &[TokenTree],
macro_name: Ident,
) -> NamedParseResult {
// A queue of possible matcher positions. We initialize it with the matcher position in
// which the "dot" is before the first token of the first token tree in `ms`.
@ -779,12 +784,7 @@ impl TtParser {
(_, _) => {
// Too many possibilities!
return self.ambiguity_error(
macro_name,
next_items,
bb_items,
parser.token.span,
);
return self.ambiguity_error(next_items, bb_items, parser.token.span);
}
}
@ -794,7 +794,6 @@ impl TtParser {
fn ambiguity_error<'root, 'tt>(
&self,
macro_name: Ident,
next_items: SmallVec<[MatcherPosHandle<'root, 'tt>; 1]>,
bb_items: SmallVec<[MatcherPosHandle<'root, 'tt>; 1]>,
token_span: rustc_span::Span,
@ -813,7 +812,8 @@ impl TtParser {
Error(
token_span,
format!(
"local ambiguity when calling macro `{macro_name}`: multiple parsing options: {}",
"local ambiguity when calling macro `{}`: multiple parsing options: {}",
self.macro_name,
match next_items.len() {
0 => format!("built-in NTs {}.", nts),
1 => format!("built-in NTs {} or 1 other option.", nts),

View file

@ -245,7 +245,7 @@ fn generic_extension<'cx>(
// this situation.)
let parser = parser_from_cx(sess, arg.clone());
let tt_parser = TtParser;
let tt_parser = TtParser::new(name);
for (i, lhs) in lhses.iter().enumerate() {
// try each arm's matchers
let lhs_tt = match *lhs {
@ -259,7 +259,7 @@ fn generic_extension<'cx>(
// are not recorded. On the first `Success(..)`ful matcher, the spans are merged.
let mut gated_spans_snapshot = mem::take(&mut *sess.gated_spans.spans.borrow_mut());
match tt_parser.parse_tt(&mut Cow::Borrowed(&parser), lhs_tt, name) {
match tt_parser.parse_tt(&mut Cow::Borrowed(&parser), lhs_tt) {
Success(named_matches) => {
// The matcher was `Success(..)`ful.
// Merge the gated spans from parsing the matcher with the pre-existing ones.
@ -352,11 +352,9 @@ fn generic_extension<'cx>(
mbe::TokenTree::Delimited(_, ref delim) => &delim.tts,
_ => continue,
};
if let Success(_) = tt_parser.parse_tt(
&mut Cow::Borrowed(&parser_from_cx(sess, arg.clone())),
lhs_tt,
name,
) {
if let Success(_) =
tt_parser.parse_tt(&mut Cow::Borrowed(&parser_from_cx(sess, arg.clone())), lhs_tt)
{
if comma_span.is_dummy() {
err.note("you might be missing a comma");
} else {
@ -449,27 +447,26 @@ pub fn compile_declarative_macro(
];
let parser = Parser::new(&sess.parse_sess, body, true, rustc_parse::MACRO_ARGUMENTS);
let tt_parser = TtParser;
let argument_map =
match tt_parser.parse_tt(&mut Cow::Borrowed(&parser), &argument_gram, def.ident) {
Success(m) => m,
Failure(token, msg) => {
let s = parse_failure_msg(&token);
let sp = token.span.substitute_dummy(def.span);
sess.parse_sess.span_diagnostic.struct_span_err(sp, &s).span_label(sp, msg).emit();
return mk_syn_ext(Box::new(macro_rules_dummy_expander));
}
Error(sp, msg) => {
sess.parse_sess
.span_diagnostic
.struct_span_err(sp.substitute_dummy(def.span), &msg)
.emit();
return mk_syn_ext(Box::new(macro_rules_dummy_expander));
}
ErrorReported => {
return mk_syn_ext(Box::new(macro_rules_dummy_expander));
}
};
let tt_parser = TtParser::new(def.ident);
let argument_map = match tt_parser.parse_tt(&mut Cow::Borrowed(&parser), &argument_gram) {
Success(m) => m,
Failure(token, msg) => {
let s = parse_failure_msg(&token);
let sp = token.span.substitute_dummy(def.span);
sess.parse_sess.span_diagnostic.struct_span_err(sp, &s).span_label(sp, msg).emit();
return mk_syn_ext(Box::new(macro_rules_dummy_expander));
}
Error(sp, msg) => {
sess.parse_sess
.span_diagnostic
.struct_span_err(sp.substitute_dummy(def.span), &msg)
.emit();
return mk_syn_ext(Box::new(macro_rules_dummy_expander));
}
ErrorReported => {
return mk_syn_ext(Box::new(macro_rules_dummy_expander));
}
};
let mut valid = true;