Replace MacroStyle with ast::DelimToken

This commit is contained in:
Seiichi Uchida 2018-03-26 07:32:48 +09:00
parent 903de92dae
commit cf6c67e1a6

View File

@ -45,14 +45,6 @@ use utils::{format_visibility, mk_sp, wrap_str};
const FORCED_BRACKET_MACROS: &[&str] = &["vec!"];
// FIXME: use the enum from libsyntax?
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum MacroStyle {
Parens,
Brackets,
Braces,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MacroPosition {
Item,
@ -61,16 +53,6 @@ pub enum MacroPosition {
Pat,
}
impl MacroStyle {
fn opener(&self) -> &'static str {
match *self {
MacroStyle::Parens => "(",
MacroStyle::Brackets => "[",
MacroStyle::Braces => "{",
}
}
}
#[derive(Debug)]
pub enum MacroArg {
Expr(ptr::P<ast::Expr>),
@ -177,7 +159,7 @@ pub fn rewrite_macro_inner(
let macro_name = rewrite_macro_name(&mac.node.path, extra_ident);
let style = if FORCED_BRACKET_MACROS.contains(&&macro_name[..]) {
MacroStyle::Brackets
DelimToken::Bracket
} else {
original_style
};
@ -186,12 +168,13 @@ pub fn rewrite_macro_inner(
let has_comment = contains_comment(context.snippet(mac.span));
if ts.is_empty() && !has_comment {
return match style {
MacroStyle::Parens if position == MacroPosition::Item => {
DelimToken::Paren if position == MacroPosition::Item => {
Some(format!("{}();", macro_name))
}
MacroStyle::Parens => Some(format!("{}()", macro_name)),
MacroStyle::Brackets => Some(format!("{}[]", macro_name)),
MacroStyle::Braces => Some(format!("{}{{}}", macro_name)),
DelimToken::Paren => Some(format!("{}()", macro_name)),
DelimToken::Bracket => Some(format!("{}[]", macro_name)),
DelimToken::Brace => Some(format!("{}{{}}", macro_name)),
_ => unreachable!(),
};
}
// Format well-known macros which cannot be parsed as a valid AST.
@ -207,7 +190,7 @@ pub fn rewrite_macro_inner(
let mut vec_with_semi = false;
let mut trailing_comma = false;
if MacroStyle::Braces != style {
if DelimToken::Brace != style {
loop {
match parse_macro_arg(&mut parser) {
Some(arg) => arg_vec.push(arg),
@ -250,7 +233,7 @@ pub fn rewrite_macro_inner(
}
match style {
MacroStyle::Parens => {
DelimToken::Paren => {
// Format macro invocation as function call, preserve the trailing
// comma because not all macros support them.
overflow::rewrite_with_parens(
@ -270,8 +253,7 @@ pub fn rewrite_macro_inner(
_ => rw,
})
}
MacroStyle::Brackets => {
let mac_shape = shape.offset_left(macro_name.len())?;
DelimToken::Bracket => {
// Handle special case: `vec![expr; expr]`
if vec_with_semi {
let (lbr, rbr) = if context.config.spaces_within_parens_and_brackets() {
@ -326,10 +308,11 @@ pub fn rewrite_macro_inner(
Some(format!("{}{}{}", macro_name, rewrite, comma))
}
}
MacroStyle::Braces => {
DelimToken::Brace => {
// Skip macro invocations with braces, for now.
indent_macro_snippet(context, context.snippet(mac.span), shape.indent)
}
_ => unreachable!(),
}
}
@ -1010,18 +993,18 @@ pub fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext) -> Option<ast::
}
}
fn macro_style(mac: &ast::Mac, context: &RewriteContext) -> MacroStyle {
fn macro_style(mac: &ast::Mac, context: &RewriteContext) -> DelimToken {
let snippet = context.snippet(mac.span);
let paren_pos = snippet.find_uncommented("(").unwrap_or(usize::max_value());
let bracket_pos = snippet.find_uncommented("[").unwrap_or(usize::max_value());
let brace_pos = snippet.find_uncommented("{").unwrap_or(usize::max_value());
if paren_pos < bracket_pos && paren_pos < brace_pos {
MacroStyle::Parens
DelimToken::Paren
} else if bracket_pos < brace_pos {
MacroStyle::Brackets
DelimToken::Bracket
} else {
MacroStyle::Braces
DelimToken::Brace
}
}