1
Fork 0

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 { 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 /// 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`. /// produce more items in `next_items` and `bb_items`.
/// ///
@ -693,7 +699,6 @@ impl TtParser {
&self, &self,
parser: &mut Cow<'_, Parser<'_>>, parser: &mut Cow<'_, Parser<'_>>,
ms: &[TokenTree], ms: &[TokenTree],
macro_name: Ident,
) -> NamedParseResult { ) -> NamedParseResult {
// A queue of possible matcher positions. We initialize it with the matcher position in // 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`. // which the "dot" is before the first token of the first token tree in `ms`.
@ -779,12 +784,7 @@ impl TtParser {
(_, _) => { (_, _) => {
// Too many possibilities! // Too many possibilities!
return self.ambiguity_error( return self.ambiguity_error(next_items, bb_items, parser.token.span);
macro_name,
next_items,
bb_items,
parser.token.span,
);
} }
} }
@ -794,7 +794,6 @@ impl TtParser {
fn ambiguity_error<'root, 'tt>( fn ambiguity_error<'root, 'tt>(
&self, &self,
macro_name: Ident,
next_items: SmallVec<[MatcherPosHandle<'root, 'tt>; 1]>, next_items: SmallVec<[MatcherPosHandle<'root, 'tt>; 1]>,
bb_items: SmallVec<[MatcherPosHandle<'root, 'tt>; 1]>, bb_items: SmallVec<[MatcherPosHandle<'root, 'tt>; 1]>,
token_span: rustc_span::Span, token_span: rustc_span::Span,
@ -813,7 +812,8 @@ impl TtParser {
Error( Error(
token_span, token_span,
format!( 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() { match next_items.len() {
0 => format!("built-in NTs {}.", nts), 0 => format!("built-in NTs {}.", nts),
1 => format!("built-in NTs {} or 1 other option.", nts), 1 => format!("built-in NTs {} or 1 other option.", nts),

View file

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