use std::panic::{catch_unwind, AssertUnwindSafe};
use rustc_ast::token::{BinOpToken, DelimToken, Token, TokenKind};
-use rustc_ast::tokenstream::{Cursor, TokenStream, TokenTree};
+use rustc_ast::tokenstream::{Cursor, LazyTokenStream, TokenStream, TokenTree};
use rustc_ast::{ast, ptr};
use rustc_ast_pretty::pprust;
-use rustc_parse::{new_parser_from_tts, parser::Parser};
-use rustc_span::{symbol::kw, BytePos, Span, Symbol, DUMMY_SP};
+use rustc_parse::parser::{ForceCollect, Parser};
+use rustc_parse::{stream_to_parser, MACRO_ARGUMENTS};
+use rustc_span::{
+ symbol::{self, kw},
+ BytePos, Span, Symbol, DUMMY_SP,
+};
use crate::comment::{
contains_comment, CharClasses, FindUncommented, FullCodeCharKind, LineClasses,
Ty(ptr::P<ast::Ty>),
Pat(ptr::P<ast::Pat>),
Item(ptr::P<ast::Item>),
- Keyword(ast::Ident, Span),
+ Keyword(symbol::Ident, Span),
}
impl 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) => {
);
parse_macro_arg!(
Pat,
- |parser: &mut rustc_parse::parser::Parser<'b>| parser.parse_pat(None),
+ |parser: &mut rustc_parse::parser::Parser<'b>| parser.parse_pat_no_top_alt(None),
|x: ptr::P<ast::Pat>| Some(x)
);
// `parse_item` returns `Option<ptr::P<ast::Item>>`.
parse_macro_arg!(
Item,
- |parser: &mut rustc_parse::parser::Parser<'b>| parser.parse_item(),
+ |parser: &mut rustc_parse::parser::Parser<'b>| parser.parse_item(ForceCollect::No),
|x: Option<ptr::P<ast::Item>>| x
);
fn rewrite_macro_name(
context: &RewriteContext<'_>,
path: &ast::Path,
- extra_ident: Option<ast::Ident>,
+ extra_ident: Option<symbol::Ident>,
) -> String {
let name = if path.segments.len() == 1 {
// Avoid using pretty-printer in the common case.
format!("{}!", pprust::path_to_string(path))
};
match extra_ident {
- Some(ident) if ident.name != kw::Invalid => format!("{} {}", name, ident),
+ Some(ident) if ident.name != kw::Empty => format!("{} {}", name, ident),
_ => name,
}
}
pub(crate) fn rewrite_macro(
mac: &ast::MacCall,
- extra_ident: Option<ast::Ident>,
+ extra_ident: Option<symbol::Ident>,
context: &RewriteContext<'_>,
shape: Shape,
position: MacroPosition,
})
{
parser.bump();
- let macro_arg =
- MacroArg::Keyword(ast::Ident::with_dummy_span(keyword), parser.prev_token.span);
- return Some(macro_arg);
+ return Some(MacroArg::Keyword(
+ symbol::Ident::with_dummy_span(keyword),
+ parser.prev_token.span,
+ ));
}
}
None
fn rewrite_macro_inner(
mac: &ast::MacCall,
- extra_ident: Option<ast::Ident>,
+ extra_ident: Option<symbol::Ident>,
context: &RewriteContext<'_>,
shape: Shape,
position: MacroPosition,
}
}
- 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;
shape: Shape,
indent: Indent,
def: &ast::MacroDef,
- ident: ast::Ident,
+ ident: symbol::Ident,
vis: &ast::Visibility,
span: Span,
) -> Option<String> {
#[derive(Debug, Clone)]
enum MacroArgKind {
/// e.g., `$x: expr`.
- MetaVariable(ast::Name, String),
+ MetaVariable(Symbol, String),
/// e.g., `$($foo: expr),*`
Repeat(
/// `()`, `[]` or `{}`.
| TokenKind::Pound
| TokenKind::Dollar
| TokenKind::OpenDelim(_)
- | TokenKind::CloseDelim(_)
- | TokenKind::Whitespace => SpaceState::Never,
+ | TokenKind::CloseDelim(_) => SpaceState::Never,
TokenKind::Literal(..) | TokenKind::Ident(..) | TokenKind::Lifetime(_) => SpaceState::Ident,
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());
Some(ast::Expr {
id: ast::NodeId::root(), // dummy value
kind: ast::ExprKind::Try(parser.parse_expr().ok()?),
span: mac.span(), // incorrect span, but shouldn't matter too much
attrs: ast::AttrVec::new(),
+ tokens: Some(LazyTokenStream::new(ts)),
})
} else {
None
span,
})) = self.toks.look_ahead(0)
{
- self.toks.next();
hi = span.hi();
+ self.toks.next();
}
Some(MacroBranch {
span: mk_sp(lo, hi),
config.set().max_width(new_width);
// First try to format as items, then as statements.
- let new_body_snippet = match crate::format_snippet(&body_str, &config) {
+ let new_body_snippet = match crate::format_snippet(&body_str, &config, true) {
Some(new_body) => new_body,
None => {
let new_width = new_width + config.tab_spaces();
config.set().max_width(new_width);
- match crate::format_code_block(&body_str, &config) {
+ match crate::format_code_block(&body_str, &config, true) {
Some(new_body) => new_body,
None => return None,
}
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);