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.
22 use std::collections::HashMap;
24 use syntax::codemap::{BytePos, Span};
25 use syntax::parse::new_parser_from_tts;
26 use syntax::parse::parser::Parser;
27 use syntax::parse::token::{BinOpToken, DelimToken, Token};
28 use syntax::print::pprust;
30 use syntax::tokenstream::{Cursor, ThinTokenStream, TokenStream, TokenTree};
31 use syntax::util::ThinVec;
33 use codemap::SpanUtils;
34 use comment::{contains_comment, remove_trailing_white_spaces, FindUncommented};
35 use expr::{rewrite_array, rewrite_call_inner};
36 use lists::{itemize_list, write_list, DefinitiveListTactic, ListFormatting, SeparatorPlace, SeparatorTactic};
37 use rewrite::{Rewrite, RewriteContext};
38 use shape::{Indent, Shape};
39 use utils::{format_visibility, mk_sp};
41 const FORCED_BRACKET_MACROS: &[&str] = &["vec!"];
43 // FIXME: use the enum from libsyntax?
44 #[derive(Clone, Copy, PartialEq, Eq, Debug)]
51 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
52 pub enum MacroPosition {
60 fn opener(&self) -> &'static str {
62 MacroStyle::Parens => "(",
63 MacroStyle::Brackets => "[",
64 MacroStyle::Braces => "{",
76 impl Rewrite for MacroArg {
77 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
79 MacroArg::Expr(ref expr) => expr.rewrite(context, shape),
80 MacroArg::Ty(ref ty) => ty.rewrite(context, shape),
81 MacroArg::Pat(ref pat) => pat.rewrite(context, shape),
86 fn parse_macro_arg(parser: &mut Parser) -> Option<MacroArg> {
87 macro_rules! parse_macro_arg {
88 ($macro_arg: ident, $parser: ident) => {
89 let mut cloned_parser = (*parser).clone();
90 match cloned_parser.$parser() {
92 if parser.sess.span_diagnostic.has_errors() {
93 parser.sess.span_diagnostic.reset_err_count();
96 *parser = cloned_parser;
97 return Some(MacroArg::$macro_arg((*x).clone()));
102 parser.sess.span_diagnostic.reset_err_count();
108 parse_macro_arg!(Expr, parse_expr);
109 parse_macro_arg!(Ty, parse_ty);
110 parse_macro_arg!(Pat, parse_pat);
115 pub fn rewrite_macro(
117 extra_ident: Option<ast::Ident>,
118 context: &RewriteContext,
120 position: MacroPosition,
121 ) -> Option<String> {
122 let context = &mut context.clone();
123 context.inside_macro = true;
124 if context.config.use_try_shorthand() {
125 if let Some(expr) = convert_try_mac(mac, context) {
126 context.inside_macro = false;
127 return expr.rewrite(context, shape);
131 let original_style = macro_style(mac, context);
133 let macro_name = match extra_ident {
134 None => format!("{}!", mac.node.path),
136 if ident == symbol::keywords::Invalid.ident() {
137 format!("{}!", mac.node.path)
139 format!("{}! {}", mac.node.path, ident)
144 let style = if FORCED_BRACKET_MACROS.contains(&¯o_name[..]) {
150 let ts: TokenStream = mac.node.stream();
151 if ts.is_empty() && !contains_comment(context.snippet(mac.span)) {
153 MacroStyle::Parens if position == MacroPosition::Item => {
154 Some(format!("{}();", macro_name))
156 MacroStyle::Parens => Some(format!("{}()", macro_name)),
157 MacroStyle::Brackets => Some(format!("{}[]", macro_name)),
158 MacroStyle::Braces => Some(format!("{}{{}}", macro_name)),
162 let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
163 let mut arg_vec = Vec::new();
164 let mut vec_with_semi = false;
165 let mut trailing_comma = false;
167 if MacroStyle::Braces != style {
169 match parse_macro_arg(&mut parser) {
170 Some(arg) => arg_vec.push(arg),
171 None => return Some(context.snippet(mac.span).to_owned()),
178 // Try to parse `vec![expr; expr]`
179 if FORCED_BRACKET_MACROS.contains(&¯o_name[..]) {
181 if parser.token != Token::Eof {
182 match parse_macro_arg(&mut parser) {
186 if parser.token == Token::Eof && arg_vec.len() == 2 {
187 vec_with_semi = true;
191 None => return Some(context.snippet(mac.span).to_owned()),
195 return Some(context.snippet(mac.span).to_owned());
197 _ => return Some(context.snippet(mac.span).to_owned()),
202 if parser.token == Token::Eof {
203 trailing_comma = true;
210 MacroStyle::Parens => {
211 // Format macro invocation as function call, forcing no trailing
212 // comma because not all macros support them.
216 &arg_vec.iter().map(|e| &*e).collect::<Vec<_>>()[..],
219 context.config.width_heuristics().fn_call_width,
221 ).map(|rw| match position {
222 MacroPosition::Item => format!("{};", rw),
226 MacroStyle::Brackets => {
227 let mac_shape = shape.offset_left(macro_name.len())?;
228 // Handle special case: `vec![expr; expr]`
230 let (lbr, rbr) = if context.config.spaces_within_parens_and_brackets() {
236 let total_overhead = lbr.len() + rbr.len() + 6;
237 let nested_shape = mac_shape.block_indent(context.config.tab_spaces());
238 let lhs = arg_vec[0].rewrite(context, nested_shape)?;
239 let rhs = arg_vec[1].rewrite(context, nested_shape)?;
240 if !lhs.contains('\n') && !rhs.contains('\n')
241 && lhs.len() + rhs.len() + total_overhead <= shape.width
243 Some(format!("{}{}{}; {}{}", macro_name, lbr, lhs, rhs, rbr))
246 "{}{}\n{}{};\n{}{}\n{}{}",
249 nested_shape.indent.to_string(context.config),
251 nested_shape.indent.to_string(context.config),
253 shape.indent.to_string(context.config),
258 // If we are rewriting `vec!` macro or other special macros,
259 // then we can rewrite this as an usual array literal.
260 // Otherwise, we must preserve the original existence of trailing comma.
261 if FORCED_BRACKET_MACROS.contains(¯o_name.as_str()) {
262 context.inside_macro = false;
263 trailing_comma = false;
265 // Convert `MacroArg` into `ast::Expr`, as `rewrite_array` only accepts the latter.
269 .span_after(mac.span, original_style.opener()),
270 mac.span.hi() - BytePos(1),
272 let arg_vec = &arg_vec.iter().map(|e| &*e).collect::<Vec<_>>()[..];
273 let rewrite = rewrite_array(arg_vec, sp, context, mac_shape, trailing_comma)?;
275 Some(format!("{}{}", macro_name, rewrite))
278 MacroStyle::Braces => {
279 // Skip macro invocations with braces, for now.
280 indent_macro_snippet(context, context.snippet(mac.span), shape.indent)
285 pub fn rewrite_macro_def(
286 context: &RewriteContext,
291 vis: &ast::Visibility,
293 ) -> Option<String> {
294 let snippet = Some(remove_trailing_white_spaces(context.snippet(span)));
296 let mut parser = MacroParser::new(def.stream().into_trees());
297 let parsed_def = match parser.parse() {
299 None => return snippet,
302 let mut result = if def.legacy {
303 String::from("macro_rules!")
305 format!("{}macro", format_visibility(vis))
309 result += &ident.name.as_str();
311 let multi_branch_style = def.legacy || parsed_def.branches.len() != 1;
313 let mac_indent = if multi_branch_style {
314 indent.block_indent(context.config)
319 let mac_indent_str = mac_indent.to_string(context.config);
321 let branch_items = itemize_list(
323 parsed_def.branches.iter(),
326 |branch| branch.span.lo(),
327 |branch| branch.span.hi(),
329 // Only attempt to format function-like macros.
330 if branch.args_paren_kind != DelimToken::Paren {
331 // FIXME(#1539): implement for non-sugared macros.
335 let mut result = format_macro_args(branch.args.clone())?;
337 if multi_branch_style {
341 // The macro body is the most interesting part. It might end up as various
342 // AST nodes, but also has special variables (e.g, `$foo`) which can't be
343 // parsed as regular Rust code (and note that these can be escaped using
344 // `$$`). We'll try and format like an AST node, but we'll substitute
345 // variables for new names with the same length first.
347 let old_body = context.snippet(branch.body).trim();
348 let (body_str, substs) = match replace_names(old_body) {
349 Some(result) => result,
350 None => return snippet,
353 let mut config = context.config.clone();
354 config.set().hide_parse_errors(true);
358 let has_block_body = old_body.starts_with('{');
360 let body_indent = if has_block_body {
363 // We'll hack the indent below, take this into account when formatting,
364 let body_indent = mac_indent.block_indent(&config);
365 let new_width = config.max_width() - body_indent.width();
366 config.set().max_width(new_width);
370 // First try to format as items, then as statements.
371 let new_body = match ::format_snippet(&body_str, &config) {
372 Some(new_body) => new_body,
373 None => match ::format_code_block(&body_str, &config) {
374 Some(new_body) => new_body,
379 // Indent the body since it is in a block.
380 let indent_str = body_indent.to_string(&config);
381 let mut new_body = new_body
384 .fold(String::new(), |mut s, l| {
391 // Undo our replacement of macro variables.
392 // FIXME: this could be *much* more efficient.
393 for (old, new) in &substs {
394 if old_body.find(new).is_some() {
396 "rewrite_macro_def: bailing matching variable: `{}` in `{}`",
401 new_body = new_body.replace(new, old);
405 result += new_body.trim();
406 } else if !new_body.is_empty() {
409 result += &mac_indent_str;
416 context.codemap.span_after(span, "{"),
419 ).collect::<Vec<_>>();
421 let arm_shape = shape
422 .block_indent(context.config.tab_spaces())
423 .with_max_width(context.config);
425 let fmt = ListFormatting {
426 tactic: DefinitiveListTactic::Vertical,
427 separator: if def.legacy { ";" } else { "" },
428 trailing_separator: SeparatorTactic::Always,
429 separator_place: SeparatorPlace::Back,
431 ends_with_newline: true,
432 preserve_newline: true,
433 config: context.config,
436 if multi_branch_style {
438 result += &mac_indent_str;
441 result += write_list(&branch_items, &fmt)?.as_str();
443 if multi_branch_style {
445 result += &indent.to_string(context.config);
452 // Replaces `$foo` with `zfoo`. We must check for name overlap to ensure we
453 // aren't causing problems.
454 // This should also work for escaped `$` variables, where we leave earlier `$`s.
455 fn replace_names(input: &str) -> Option<(String, HashMap<String, String>)> {
456 // Each substitution will require five or six extra bytes.
457 let mut result = String::with_capacity(input.len() + 64);
458 let mut substs = HashMap::new();
459 let mut dollar_count = 0;
460 let mut cur_name = String::new();
462 for c in input.chars() {
465 } else if dollar_count == 0 {
467 } else if !c.is_alphanumeric() && !cur_name.is_empty() {
468 // Terminates a name following one or more dollars.
469 let mut new_name = String::new();
470 let mut old_name = String::new();
472 for _ in 0..(dollar_count - 1) {
477 new_name.push_str(&cur_name);
478 old_name.push_str(&cur_name);
480 result.push_str(&new_name);
481 substs.insert(old_name, new_name);
486 cur_name = String::new();
487 } else if c == '(' && cur_name.is_empty() {
488 // FIXME: Support macro def with repeat.
490 } else if c.is_alphanumeric() {
495 // FIXME: duplicate code
496 if !cur_name.is_empty() {
497 let mut new_name = String::new();
498 let mut old_name = String::new();
500 for _ in 0..(dollar_count - 1) {
505 new_name.push_str(&cur_name);
506 old_name.push_str(&cur_name);
508 result.push_str(&new_name);
509 substs.insert(old_name, new_name);
512 debug!("replace_names `{}` {:?}", result, substs);
514 Some((result, substs))
517 // This is a bit sketchy. The token rules probably need tweaking, but it works
518 // for some common cases. I hope the basic logic is sufficient. Note that the
519 // meaning of some tokens is a bit different here from usual Rust, e.g., `*`
520 // and `(`/`)` have special meaning.
522 // We always try and format on one line.
523 fn format_macro_args(toks: ThinTokenStream) -> Option<String> {
524 let mut result = String::with_capacity(128);
525 let mut insert_space = SpaceState::Never;
527 for tok in (toks.into(): TokenStream).trees() {
529 TokenTree::Token(_, t) => {
530 if !result.is_empty() && force_space_before(&t) {
531 insert_space = SpaceState::Always;
533 if force_no_space_before(&t) {
534 insert_space = SpaceState::Never;
536 match (insert_space, ident_like(&t)) {
537 (SpaceState::Always, _)
538 | (SpaceState::Punctuation, false)
539 | (SpaceState::Ident, true) => {
544 result.push_str(&pprust::token_to_string(&t));
545 insert_space = next_space(&t);
547 TokenTree::Delimited(_, d) => {
548 if let SpaceState::Always = insert_space {
551 let formatted = format_macro_args(d.tts)?;
553 DelimToken::Paren => {
554 result.push_str(&format!("({})", formatted));
555 insert_space = SpaceState::Always;
557 DelimToken::Bracket => {
558 result.push_str(&format!("[{}]", formatted));
559 insert_space = SpaceState::Always;
561 DelimToken::Brace => {
562 result.push_str(&format!(" {{ {} }}", formatted));
563 insert_space = SpaceState::Always;
565 DelimToken::NoDelim => {
566 result.push_str(&format!("{}", formatted));
567 insert_space = SpaceState::Always;
577 // We should insert a space if the next token is a:
578 #[derive(Copy, Clone)]
582 Ident, // Or ident/literal-like thing.
586 fn force_space_before(tok: &Token) -> bool {
605 | Token::Dollar => true,
606 Token::BinOp(bot) => bot != BinOpToken::Star,
611 fn force_no_space_before(tok: &Token) -> bool {
613 Token::Semi | Token::Comma | Token::Dot => true,
614 Token::BinOp(bot) => bot == BinOpToken::Star,
618 fn ident_like(tok: &Token) -> bool {
620 Token::Ident(_) | Token::Literal(..) | Token::Lifetime(_) => true,
625 fn next_space(tok: &Token) -> SpaceState {
638 | Token::BinOp(_) => SpaceState::Punctuation,
643 | Token::OpenDelim(_)
644 | Token::CloseDelim(_)
645 | Token::Whitespace => SpaceState::Never,
647 Token::Literal(..) | Token::Ident(_) | Token::Lifetime(_) => SpaceState::Ident,
649 _ => SpaceState::Always,
653 /// Tries to convert a macro use into a short hand try expression. Returns None
654 /// when the macro is not an instance of try! (or parsing the inner expression
656 pub fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext) -> Option<ast::Expr> {
657 if &format!("{}", mac.node.path)[..] == "try" {
658 let ts: TokenStream = mac.node.tts.clone().into();
659 let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
662 id: ast::NodeId::new(0), // dummy value
663 node: ast::ExprKind::Try(parser.parse_expr().ok()?),
664 span: mac.span, // incorrect span, but shouldn't matter too much
665 attrs: ThinVec::new(),
672 fn macro_style(mac: &ast::Mac, context: &RewriteContext) -> MacroStyle {
673 let snippet = context.snippet(mac.span);
674 let paren_pos = snippet.find_uncommented("(").unwrap_or(usize::max_value());
675 let bracket_pos = snippet.find_uncommented("[").unwrap_or(usize::max_value());
676 let brace_pos = snippet.find_uncommented("{").unwrap_or(usize::max_value());
678 if paren_pos < bracket_pos && paren_pos < brace_pos {
680 } else if bracket_pos < brace_pos {
687 /// Indent each line according to the specified `indent`.
712 fn indent_macro_snippet(
713 context: &RewriteContext,
716 ) -> Option<String> {
717 let mut lines = macro_str.lines();
718 let first_line = lines.next().map(|s| s.trim_right())?;
719 let mut trimmed_lines = Vec::with_capacity(16);
721 let min_prefix_space_width = lines
723 let prefix_space_width = if is_empty_line(line) {
726 Some(get_prefix_space_width(context, line))
728 trimmed_lines.push((line.trim(), prefix_space_width));
734 String::from(first_line) + "\n"
737 .map(|&(line, prefix_space_width)| match prefix_space_width {
738 Some(original_indent_width) => {
739 let new_indent_width = indent.width()
740 + original_indent_width
741 .checked_sub(min_prefix_space_width)
743 let new_indent = Indent::from_width(context.config, new_indent_width);
744 format!("{}{}", new_indent.to_string(context.config), line.trim())
746 None => String::new(),
753 fn get_prefix_space_width(context: &RewriteContext, s: &str) -> usize {
758 '\t' => width += context.config.tab_spaces(),
765 fn is_empty_line(s: &str) -> bool {
766 s.is_empty() || s.chars().all(char::is_whitespace)
769 // A very simple parser that just parses a macros 2.0 definition into its branches.
770 // Currently we do not attempt to parse any further than that.
777 // (`(` ... `)` `=>` `{` ... `}`)*
778 fn parse(&mut self) -> Option<Macro> {
779 let mut branches = vec![];
780 while self.toks.look_ahead(1).is_some() {
781 branches.push(self.parse_branch()?);
784 Some(Macro { branches })
787 // `(` ... `)` `=>` `{` ... `}`
788 fn parse_branch(&mut self) -> Option<MacroBranch> {
789 let tok = self.toks.next()?;
790 let (lo, args_paren_kind) = match tok {
791 TokenTree::Token(..) => return None,
792 TokenTree::Delimited(sp, ref d) => (sp.lo(), d.delim),
794 let args = tok.joint().into();
795 match self.toks.next()? {
796 TokenTree::Token(_, Token::FatArrow) => {}
799 let (mut hi, body) = match self.toks.next()? {
800 TokenTree::Token(..) => return None,
801 TokenTree::Delimited(sp, _) => {
802 let data = sp.data();
805 Span::new(data.lo + BytePos(1), data.hi - BytePos(1), data.ctxt),
809 if let Some(TokenTree::Token(sp, Token::Semi)) = self.toks.look_ahead(0) {
822 // A parsed macros 2.0 macro definition.
824 branches: Vec<MacroBranch>,
827 // FIXME: it would be more efficient to use references to the token streams
828 // rather than clone them, if we can make the borrowing work out.
831 args_paren_kind: DelimToken,
832 args: ThinTokenStream,
839 use syntax::parse::{parse_stream_from_source_str, ParseSess};
840 use syntax::codemap::{FileName, FilePathMapping};
842 fn format_macro_args_str(s: &str) -> String {
843 let input = parse_stream_from_source_str(
844 FileName::Custom("stdin".to_owned()),
846 &ParseSess::new(FilePathMapping::empty()),
849 format_macro_args(input.into()).unwrap()
853 fn test_format_macro_args() {
854 assert_eq!(format_macro_args_str(""), "".to_owned());
855 assert_eq!(format_macro_args_str("$ x : ident"), "$x: ident".to_owned());
857 format_macro_args_str("$ m1 : ident , $ m2 : ident , $ x : ident"),
858 "$m1: ident, $m2: ident, $x: ident".to_owned()
861 format_macro_args_str("$($beginning:ident),*;$middle:ident;$($end:ident),*"),
862 "$($beginning: ident),*; $middle: ident; $($end: ident),*".to_owned()
865 format_macro_args_str(
866 "$ name : ident ( $ ( $ dol : tt $ var : ident ) * ) $ ( $ body : tt ) *"
868 "$name: ident($($dol: tt $var: ident)*) $($body: tt)*".to_owned()