1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // Format list-like macro invocations. These are invocations whose token trees
12 // can be interpreted as expressions and separated by commas.
13 // Note that these token trees do not actually have to be interpreted as
14 // expressions by the compiler. An example of an invocation we would reformat is
15 // foo!( x, y, z ). The token x may represent an identifier in the code, but we
16 // interpreted as an expression.
17 // Macro uses which are not-list like, such as bar!(key => val), will not be
19 // List-like invocations with parentheses will be formatted as function calls,
20 // and those with brackets will be formatted as array literals.
23 use syntax::parse::token::{Eof, Comma, Token};
24 use syntax::parse::{ParseSess, tts_to_parser};
25 use syntax::codemap::{mk_sp, BytePos};
28 use rewrite::RewriteContext;
29 use expr::{rewrite_call, rewrite_array};
30 use comment::FindUncommented;
31 use utils::{wrap_str, span_after};
33 static FORCED_BRACKET_MACROS: &'static [&'static str] = &["vec!"];
35 // FIXME: use the enum from libsyntax?
36 #[derive(Clone, Copy)]
44 fn opener(&self) -> &'static str {
46 MacroStyle::Parens => "(",
47 MacroStyle::Brackets => "[",
48 MacroStyle::Braces => "{",
53 pub fn rewrite_macro(mac: &ast::Mac,
54 context: &RewriteContext,
58 let original_style = macro_style(mac, context);
59 let macro_name = format!("{}!", mac.node.path);
60 let style = if FORCED_BRACKET_MACROS.contains(&¯o_name[..]) {
66 if let MacroStyle::Braces = style {
68 } else if mac.node.tts.is_empty() {
69 return if let MacroStyle::Parens = style {
70 Some(format!("{}()", macro_name))
72 Some(format!("{}[]", macro_name))
76 let parse_session = ParseSess::new();
77 let mut parser = tts_to_parser(&parse_session, mac.node.tts.clone(), Vec::new());
78 let mut expr_vec = Vec::new();
81 expr_vec.push(match parser.parse_expr_nopanic() {
83 Err(..) => return None,
92 let _ = parser.bump();
94 if parser.token == Token::Eof {
100 MacroStyle::Parens => {
101 // Format macro invocation as function call.
102 rewrite_call(context, ¯o_name, &expr_vec, mac.span, width, offset)
104 MacroStyle::Brackets => {
105 // Format macro invocation as array literal.
106 let extra_offset = macro_name.len();
107 let rewrite = try_opt!(rewrite_array(expr_vec.iter().map(|x| &**x),
108 mk_sp(span_after(mac.span,
109 original_style.opener(),
111 mac.span.hi - BytePos(1)),
113 try_opt!(width.checked_sub(extra_offset)),
114 offset + extra_offset));
116 Some(format!("{}{}", macro_name, rewrite))
118 MacroStyle::Braces => {
119 // Skip macro invocations with braces, for now.
120 wrap_str(context.snippet(mac.span),
121 context.config.max_width,
128 fn macro_style(mac: &ast::Mac, context: &RewriteContext) -> MacroStyle {
129 let snippet = context.snippet(mac.span);
130 let paren_pos = snippet.find_uncommented("(").unwrap_or(usize::max_value());
131 let bracket_pos = snippet.find_uncommented("[").unwrap_or(usize::max_value());
132 let brace_pos = snippet.find_uncommented("{").unwrap_or(usize::max_value());
134 if paren_pos < bracket_pos && paren_pos < brace_pos {
136 } else if bracket_pos < brace_pos {