Skip to content

Commit a7382ea

Browse files
committed
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.
1 parent 6b71399 commit a7382ea

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::base::{
3636
};
3737
use crate::expand::{AstFragment, AstFragmentKind, ensure_complete_parse, parse_ast_fragment};
3838
use crate::mbe::macro_parser::{Error, ErrorReported, Failure, MatcherLoc, Success, TtParser};
39-
use crate::mbe::quoted::RulePart;
39+
use crate::mbe::quoted::{RulePart, parse_one_tt};
4040
use crate::mbe::transcribe::transcribe;
4141
use crate::mbe::{self, KleeneOp, macro_check};
4242

@@ -395,16 +395,7 @@ pub fn compile_declarative_macro(
395395

396396
while p.token != token::Eof {
397397
let lhs_tt = p.parse_token_tree();
398-
let lhs_tt = mbe::quoted::parse(
399-
&TokenStream::new(vec![lhs_tt]),
400-
RulePart::Pattern,
401-
sess,
402-
node_id,
403-
features,
404-
edition,
405-
)
406-
.pop()
407-
.unwrap();
398+
let lhs_tt = parse_one_tt(lhs_tt, RulePart::Pattern, sess, node_id, features, edition);
408399
// We don't handle errors here, the driver will abort after parsing/expansion. We can
409400
// report every error in every macro this way.
410401
check_emission(check_lhs_nt_follows(sess, node_id, &lhs_tt));
@@ -422,16 +413,7 @@ pub fn compile_declarative_macro(
422413
return dummy_syn_ext(guar);
423414
}
424415
let rhs_tt = p.parse_token_tree();
425-
let rhs_tt = mbe::quoted::parse(
426-
&TokenStream::new(vec![rhs_tt]),
427-
RulePart::Body,
428-
sess,
429-
node_id,
430-
features,
431-
edition,
432-
)
433-
.pop()
434-
.unwrap();
416+
let rhs_tt = parse_one_tt(rhs_tt, RulePart::Body, sess, node_id, features, edition);
435417
check_emission(check_rhs(sess, &rhs_tt));
436418
check_emission(macro_check::check_meta_variables(&sess.psess, node_id, &lhs_tt, &rhs_tt));
437419
lhses.push(lhs_tt);

compiler/rustc_expand/src/mbe/quoted.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl RulePart {
5757
/// # Returns
5858
///
5959
/// A collection of `self::TokenTree`. There may also be some errors emitted to `sess`.
60-
pub(super) fn parse(
60+
fn parse(
6161
input: &tokenstream::TokenStream,
6262
part: RulePart,
6363
sess: &Session,
@@ -152,6 +152,22 @@ pub(super) fn parse(
152152
result
153153
}
154154

155+
/// Takes a `tokenstream::TokenTree` and returns a `self::TokenTree`. Like `parse`, but for a
156+
/// single token tree. Emits errors to `sess` if needed.
157+
#[inline]
158+
pub(super) fn parse_one_tt(
159+
input: tokenstream::TokenTree,
160+
part: RulePart,
161+
sess: &Session,
162+
node_id: NodeId,
163+
features: &Features,
164+
edition: Edition,
165+
) -> TokenTree {
166+
parse(&tokenstream::TokenStream::new(vec![input]), part, sess, node_id, features, edition)
167+
.pop()
168+
.unwrap()
169+
}
170+
155171
/// Asks for the `macro_metavar_expr` feature if it is not enabled
156172
fn maybe_emit_macro_metavar_expr_feature(features: &Features, sess: &Session, span: Span) {
157173
if !features.macro_metavar_expr() {

0 commit comments

Comments
 (0)