mbe: Add a helper to parse a single TokenTree

The parser repeatedly invokes the `parse` function, constructing a
one-entry vector, and assuming that the return value will be a one-entry
vector. Add a helper for that case. This will simplify adding additional
callers, and put all the logic in one place to allow potential future
simplification of the one-TT case.
This commit is contained in:
Josh Triplett 2025-07-03 21:55:12 -07:00
parent 6b71399d38
commit a7382eae3f
2 changed files with 20 additions and 22 deletions

View file

@ -36,7 +36,7 @@ use crate::base::{
};
use crate::expand::{AstFragment, AstFragmentKind, ensure_complete_parse, parse_ast_fragment};
use crate::mbe::macro_parser::{Error, ErrorReported, Failure, MatcherLoc, Success, TtParser};
use crate::mbe::quoted::RulePart;
use crate::mbe::quoted::{RulePart, parse_one_tt};
use crate::mbe::transcribe::transcribe;
use crate::mbe::{self, KleeneOp, macro_check};
@ -395,16 +395,7 @@ pub fn compile_declarative_macro(
while p.token != token::Eof {
let lhs_tt = p.parse_token_tree();
let lhs_tt = mbe::quoted::parse(
&TokenStream::new(vec![lhs_tt]),
RulePart::Pattern,
sess,
node_id,
features,
edition,
)
.pop()
.unwrap();
let lhs_tt = parse_one_tt(lhs_tt, RulePart::Pattern, sess, node_id, features, edition);
// We don't handle errors here, the driver will abort after parsing/expansion. We can
// report every error in every macro this way.
check_emission(check_lhs_nt_follows(sess, node_id, &lhs_tt));
@ -422,16 +413,7 @@ pub fn compile_declarative_macro(
return dummy_syn_ext(guar);
}
let rhs_tt = p.parse_token_tree();
let rhs_tt = mbe::quoted::parse(
&TokenStream::new(vec![rhs_tt]),
RulePart::Body,
sess,
node_id,
features,
edition,
)
.pop()
.unwrap();
let rhs_tt = parse_one_tt(rhs_tt, RulePart::Body, sess, node_id, features, edition);
check_emission(check_rhs(sess, &rhs_tt));
check_emission(macro_check::check_meta_variables(&sess.psess, node_id, &lhs_tt, &rhs_tt));
lhses.push(lhs_tt);

View file

@ -57,7 +57,7 @@ impl RulePart {
/// # Returns
///
/// A collection of `self::TokenTree`. There may also be some errors emitted to `sess`.
pub(super) fn parse(
fn parse(
input: &tokenstream::TokenStream,
part: RulePart,
sess: &Session,
@ -152,6 +152,22 @@ pub(super) fn parse(
result
}
/// Takes a `tokenstream::TokenTree` and returns a `self::TokenTree`. Like `parse`, but for a
/// single token tree. Emits errors to `sess` if needed.
#[inline]
pub(super) fn parse_one_tt(
input: tokenstream::TokenTree,
part: RulePart,
sess: &Session,
node_id: NodeId,
features: &Features,
edition: Edition,
) -> TokenTree {
parse(&tokenstream::TokenStream::new(vec![input]), part, sess, node_id, features, edition)
.pop()
.unwrap()
}
/// Asks for the `macro_metavar_expr` feature if it is not enabled
fn maybe_emit_macro_metavar_expr_feature(features: &Features, sess: &Session, span: Span) {
if !features.macro_metavar_expr() {