Skip to content

Commit 6298756

Browse files
refactor: extract final rustc_parse touchpoint from macros.rs
1 parent c8cf454 commit 6298756

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/macros.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::expr::{rewrite_array, rewrite_assign_rhs, RhsAssignKind};
2929
use crate::lists::{itemize_list, write_list, ListFormatting};
3030
use crate::overflow;
3131
use crate::parse::macros::lazy_static::parse_lazy_static;
32-
use crate::parse::macros::{build_parser, parse_macro_args, ParsedMacroArgs};
32+
use crate::parse::macros::{parse_expr, parse_macro_args, ParsedMacroArgs};
3333
use crate::rewrite::{Rewrite, RewriteContext};
3434
use crate::shape::{Indent, Shape};
3535
use crate::source_map::SpanUtils;
@@ -1060,11 +1060,10 @@ pub(crate) fn convert_try_mac(
10601060
let path = &pprust::path_to_string(&mac.path);
10611061
if path == "try" || path == "r#try" {
10621062
let ts = mac.args.inner_tokens();
1063-
let mut parser = build_parser(context, ts);
10641063

10651064
Some(ast::Expr {
10661065
id: ast::NodeId::root(), // dummy value
1067-
kind: ast::ExprKind::Try(parser.parse_expr().ok()?),
1066+
kind: ast::ExprKind::Try(parse_expr(context, ts)?),
10681067
span: mac.span(), // incorrect span, but shouldn't matter too much
10691068
attrs: ast::AttrVec::new(),
10701069
tokens: None,

src/parse/macros/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::rewrite::{Rewrite, RewriteContext};
1313

1414
pub(crate) mod lazy_static;
1515

16-
pub(crate) fn build_parser<'a>(context: &RewriteContext<'a>, tokens: TokenStream) -> Parser<'a> {
16+
fn build_parser<'a>(context: &RewriteContext<'a>, tokens: TokenStream) -> Parser<'a> {
1717
stream_to_parser(context.parse_sess.inner(), tokens, MACRO_ARGUMENTS)
1818
}
1919

@@ -155,6 +155,14 @@ pub(crate) fn parse_macro_args(
155155
})
156156
}
157157

158+
pub(crate) fn parse_expr(
159+
context: &RewriteContext<'_>,
160+
tokens: TokenStream,
161+
) -> Option<ptr::P<ast::Expr>> {
162+
let mut parser = build_parser(context, tokens);
163+
parser.parse_expr().ok()
164+
}
165+
158166
const RUST_KW: [Symbol; 59] = [
159167
kw::PathRoot,
160168
kw::DollarCrate,

0 commit comments

Comments
 (0)