Skip to content

Commit

Permalink
deps: update macro parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
calebcartwright committed Oct 20, 2020
1 parent 3a9704f commit 041e41b
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/formatting/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use rustc_ast::token::{BinOpToken, DelimToken, Token, TokenKind};
use rustc_ast::tokenstream::{Cursor, TokenStream, TokenTree};
use rustc_ast::{ast, ptr};
use rustc_ast_pretty::pprust;
use rustc_parse::{new_parser_from_tts, parser::Parser};
use rustc_parse::parser::Parser;
use rustc_parse::{stream_to_parser, MACRO_ARGUMENTS};
use rustc_span::{
symbol::{self, kw},
BytePos, Span, Symbol, DUMMY_SP,
Expand Down Expand Up @@ -88,6 +89,14 @@ impl Rewrite for MacroArg {
}
}

fn build_parser<'a>(context: &RewriteContext<'a>, cursor: Cursor) -> Parser<'a> {
stream_to_parser(
context.parse_sess.inner(),
cursor.collect(),
MACRO_ARGUMENTS,
)
}

fn parse_macro_arg<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {
macro_rules! parse_macro_arg {
($macro_arg:ident, $parser:expr, $f:expr) => {
Expand Down Expand Up @@ -297,7 +306,7 @@ fn rewrite_macro_inner(
}
}

let mut parser = new_parser_from_tts(context.parse_sess.inner(), ts.trees().collect());
let mut parser = build_parser(context, ts.trees());
let mut arg_vec = Vec::new();
let mut vec_with_semi = false;
let mut trailing_comma = false;
Expand Down Expand Up @@ -1204,7 +1213,7 @@ pub(crate) fn convert_try_mac(
let path = &pprust::path_to_string(&mac.path);
if path == "try" || path == "r#try" {
let ts = mac.args.inner_tokens();
let mut parser = new_parser_from_tts(context.parse_sess.inner(), ts.trees().collect());
let mut parser = build_parser(context, ts.trees());

let mut kind = parser.parse_expr().ok()?;
// take the end pos of mac so that the Try expression includes the closing parenthesis of
Expand Down Expand Up @@ -1445,7 +1454,7 @@ fn format_lazy_static(
ts: &TokenStream,
) -> Option<String> {
let mut result = String::with_capacity(1024);
let mut parser = new_parser_from_tts(context.parse_sess.inner(), ts.trees().collect());
let mut parser = build_parser(context, ts.trees());
let nested_shape = shape
.block_indent(context.config.tab_spaces())
.with_max_width(context.config);
Expand Down

0 comments on commit 041e41b

Please sign in to comment.