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 rewrite::{Rewrite, RewriteContext};
37 use shape::{Indent, Shape};
38 use utils::{format_visibility, mk_sp};
40 const FORCED_BRACKET_MACROS: &[&str] = &["vec!"];
42 // FIXME: use the enum from libsyntax?
43 #[derive(Clone, Copy, PartialEq, Eq, Debug)]
50 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
51 pub enum MacroPosition {
59 fn opener(&self) -> &'static str {
61 MacroStyle::Parens => "(",
62 MacroStyle::Brackets => "[",
63 MacroStyle::Braces => "{",
75 impl Rewrite for MacroArg {
76 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
78 MacroArg::Expr(ref expr) => expr.rewrite(context, shape),
79 MacroArg::Ty(ref ty) => ty.rewrite(context, shape),
80 MacroArg::Pat(ref pat) => pat.rewrite(context, shape),
85 fn parse_macro_arg(parser: &mut Parser) -> Option<MacroArg> {
86 macro_rules! parse_macro_arg {
87 ($macro_arg: ident, $parser: ident) => {
88 let mut cloned_parser = (*parser).clone();
89 match cloned_parser.$parser() {
91 if parser.sess.span_diagnostic.has_errors() {
92 parser.sess.span_diagnostic.reset_err_count();
95 *parser = cloned_parser;
96 return Some(MacroArg::$macro_arg((*x).clone()));
101 parser.sess.span_diagnostic.reset_err_count();
107 parse_macro_arg!(Expr, parse_expr);
108 parse_macro_arg!(Ty, parse_ty);
109 parse_macro_arg!(Pat, parse_pat);
114 pub fn rewrite_macro(
116 extra_ident: Option<ast::Ident>,
117 context: &RewriteContext,
119 position: MacroPosition,
120 ) -> Option<String> {
121 let context = &mut context.clone();
122 context.inside_macro = true;
123 if context.config.use_try_shorthand() {
124 if let Some(expr) = convert_try_mac(mac, context) {
125 context.inside_macro = false;
126 return expr.rewrite(context, shape);
130 let original_style = macro_style(mac, context);
132 let macro_name = match extra_ident {
133 None => format!("{}!", mac.node.path),
135 if ident == symbol::keywords::Invalid.ident() {
136 format!("{}!", mac.node.path)
138 format!("{}! {}", mac.node.path, ident)
143 let style = if FORCED_BRACKET_MACROS.contains(&¯o_name[..]) {
149 let ts: TokenStream = mac.node.stream();
150 if ts.is_empty() && !contains_comment(context.snippet(mac.span)) {
152 MacroStyle::Parens if position == MacroPosition::Item => {
153 Some(format!("{}();", macro_name))
155 MacroStyle::Parens => Some(format!("{}()", macro_name)),
156 MacroStyle::Brackets => Some(format!("{}[]", macro_name)),
157 MacroStyle::Braces => Some(format!("{}{{}}", macro_name)),
161 let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
162 let mut arg_vec = Vec::new();
163 let mut vec_with_semi = false;
164 let mut trailing_comma = false;
166 if MacroStyle::Braces != style {
168 match parse_macro_arg(&mut parser) {
169 Some(arg) => arg_vec.push(arg),
170 None => return Some(context.snippet(mac.span).to_owned()),
177 // Try to parse `vec![expr; expr]`
178 if FORCED_BRACKET_MACROS.contains(&¯o_name[..]) {
180 if parser.token != Token::Eof {
181 match parse_macro_arg(&mut parser) {
185 if parser.token == Token::Eof && arg_vec.len() == 2 {
186 vec_with_semi = true;
190 None => return Some(context.snippet(mac.span).to_owned()),
194 return Some(context.snippet(mac.span).to_owned());
196 _ => return Some(context.snippet(mac.span).to_owned()),
201 if parser.token == Token::Eof {
202 trailing_comma = true;
209 MacroStyle::Parens => {
210 // Format macro invocation as function call, forcing no trailing
211 // comma because not all macros support them.
215 &arg_vec.iter().map(|e| &*e).collect::<Vec<_>>()[..],
218 context.config.width_heuristics().fn_call_width,
220 ).map(|rw| match position {
221 MacroPosition::Item => format!("{};", rw),
225 MacroStyle::Brackets => {
226 let mac_shape = shape.offset_left(macro_name.len())?;
227 // Handle special case: `vec![expr; expr]`
229 let (lbr, rbr) = if context.config.spaces_within_parens_and_brackets() {
235 let total_overhead = lbr.len() + rbr.len() + 6;
236 let nested_shape = mac_shape.block_indent(context.config.tab_spaces());
237 let lhs = arg_vec[0].rewrite(context, nested_shape)?;
238 let rhs = arg_vec[1].rewrite(context, nested_shape)?;
239 if !lhs.contains('\n') && !rhs.contains('\n')
240 && lhs.len() + rhs.len() + total_overhead <= shape.width
242 Some(format!("{}{}{}; {}{}", macro_name, lbr, lhs, rhs, rbr))
245 "{}{}\n{}{};\n{}{}\n{}{}",
248 nested_shape.indent.to_string(context.config),
250 nested_shape.indent.to_string(context.config),
252 shape.indent.to_string(context.config),
257 // If we are rewriting `vec!` macro or other special macros,
258 // then we can rewrite this as an usual array literal.
259 // Otherwise, we must preserve the original existence of trailing comma.
260 if FORCED_BRACKET_MACROS.contains(¯o_name.as_str()) {
261 context.inside_macro = false;
262 trailing_comma = false;
264 // Convert `MacroArg` into `ast::Expr`, as `rewrite_array` only accepts the latter.
268 .span_after(mac.span, original_style.opener()),
269 mac.span.hi() - BytePos(1),
271 let arg_vec = &arg_vec.iter().map(|e| &*e).collect::<Vec<_>>()[..];
272 let rewrite = rewrite_array(arg_vec, sp, context, mac_shape, trailing_comma)?;
274 Some(format!("{}{}", macro_name, rewrite))
277 MacroStyle::Braces => {
278 // Skip macro invocations with braces, for now.
279 indent_macro_snippet(context, context.snippet(mac.span), shape.indent)
284 pub fn rewrite_macro_def(
285 context: &RewriteContext,
289 vis: &ast::Visibility,
291 ) -> Option<String> {
292 let snippet = Some(remove_trailing_white_spaces(context.snippet(span)));
294 let mut parser = MacroParser::new(def.stream().into_trees());
295 let parsed_def = match parser.parse() {
297 None => return snippet,
300 let mut result = if def.legacy {
301 String::from("macro_rules!")
303 format!("{}macro", format_visibility(vis))
307 result += &ident.name.as_str();
309 let multi_branch_style = def.legacy || parsed_def.branches.len() != 1;
311 let mac_indent = if multi_branch_style {
313 indent.block_indent(context.config)
318 let mac_indent_str = mac_indent.to_string(context.config);
320 for branch in parsed_def.branches {
321 // Only attempt to format function-like macros.
322 if branch.args_paren_kind != DelimToken::Paren {
323 // FIXME(#1539): implement for non-sugared macros.
327 let args = format!("({})", format_macro_args(branch.args)?);
329 if multi_branch_style {
331 result += &mac_indent_str;
340 // The macro body is the most interesting part. It might end up as various
341 // AST nodes, but also has special variables (e.g, `$foo`) which can't be
342 // parsed as regular Rust code (and note that these can be escaped using
343 // `$$`). We'll try and format like an AST node, but we'll substitute
344 // variables for new names with the same length first.
346 let old_body = context.snippet(branch.body).trim();
347 let (body_str, substs) = match replace_names(old_body) {
348 Some(result) => result,
349 None => return snippet,
352 // We'll hack the indent below, take this into account when formatting,
353 let mut config = context.config.clone();
354 let body_indent = mac_indent.block_indent(&config);
355 let new_width = config.max_width() - body_indent.width();
356 config.set().max_width(new_width);
357 config.set().hide_parse_errors(true);
359 // First try to format as items, then as statements.
360 let new_body = match ::format_snippet(&body_str, &config) {
361 Some(new_body) => new_body,
362 None => match ::format_code_block(&body_str, &config) {
363 Some(new_body) => new_body,
364 None => return snippet,
368 // Indent the body since it is in a block.
369 let indent_str = body_indent.to_string(&config);
370 let mut new_body = new_body
373 .fold(String::new(), |mut s, l| {
380 // Undo our replacement of macro variables.
381 // FIXME: this could be *much* more efficient.
382 for (old, new) in &substs {
383 if old_body.find(new).is_some() {
385 "rewrite_macro_def: bailing matching variable: `{}` in `{}`",
390 new_body = new_body.replace(new, old);
395 result += &mac_indent_str;
403 if multi_branch_style {
404 result += &indent.to_string(context.config);
411 // Replaces `$foo` with `zfoo`. We must check for name overlap to ensure we
412 // aren't causing problems.
413 // This should also work for escaped `$` variables, where we leave earlier `$`s.
414 fn replace_names(input: &str) -> Option<(String, HashMap<String, String>)> {
415 // Each substitution will require five or six extra bytes.
416 let mut result = String::with_capacity(input.len() + 64);
417 let mut substs = HashMap::new();
418 let mut dollar_count = 0;
419 let mut cur_name = String::new();
421 for c in input.chars() {
424 } else if dollar_count == 0 {
426 } else if !c.is_alphanumeric() && !cur_name.is_empty() {
427 // Terminates a name following one or more dollars.
428 let mut new_name = String::new();
429 let mut old_name = String::new();
431 for _ in 0..(dollar_count - 1) {
436 new_name.push_str(&cur_name);
437 old_name.push_str(&cur_name);
439 result.push_str(&new_name);
440 substs.insert(old_name, new_name);
445 cur_name = String::new();
446 } else if c == '(' && cur_name.is_empty() {
447 // FIXME: Support macro def with repeat.
449 } else if c.is_alphanumeric() {
454 // FIXME: duplicate code
455 if !cur_name.is_empty() {
456 let mut new_name = String::new();
457 let mut old_name = String::new();
459 for _ in 0..(dollar_count - 1) {
464 new_name.push_str(&cur_name);
465 old_name.push_str(&cur_name);
467 result.push_str(&new_name);
468 substs.insert(old_name, new_name);
471 debug!("replace_names `{}` {:?}", result, substs);
473 Some((result, substs))
476 // This is a bit sketchy. The token rules probably need tweaking, but it works
477 // for some common cases. I hope the basic logic is sufficient. Note that the
478 // meaning of some tokens is a bit different here from usual Rust, e.g., `*`
479 // and `(`/`)` have special meaning.
481 // We always try and format on one line.
482 fn format_macro_args(toks: ThinTokenStream) -> Option<String> {
483 let mut result = String::with_capacity(128);
484 let mut insert_space = SpaceState::Never;
486 for tok in (toks.into(): TokenStream).trees() {
488 TokenTree::Token(_, t) => {
489 if !result.is_empty() && force_space_before(&t) {
490 insert_space = SpaceState::Always;
492 if force_no_space_before(&t) {
493 insert_space = SpaceState::Never;
495 match (insert_space, ident_like(&t)) {
496 (SpaceState::Always, _)
497 | (SpaceState::Punctuation, false)
498 | (SpaceState::Ident, true) => {
503 result.push_str(&pprust::token_to_string(&t));
504 insert_space = next_space(&t);
506 TokenTree::Delimited(_, d) => {
507 if let SpaceState::Always = insert_space {
510 let formatted = format_macro_args(d.tts)?;
512 DelimToken::Paren => {
513 result.push_str(&format!("({})", formatted));
514 insert_space = SpaceState::Always;
516 DelimToken::Bracket => {
517 result.push_str(&format!("[{}]", formatted));
518 insert_space = SpaceState::Always;
520 DelimToken::Brace => {
521 result.push_str(&format!(" {{ {} }}", formatted));
522 insert_space = SpaceState::Always;
524 DelimToken::NoDelim => {
525 result.push_str(&format!("{}", formatted));
526 insert_space = SpaceState::Always;
536 // We should insert a space if the next token is a:
537 #[derive(Copy, Clone)]
541 Ident, // Or ident/literal-like thing.
545 fn force_space_before(tok: &Token) -> bool {
564 | Token::Dollar => true,
565 Token::BinOp(bot) => bot != BinOpToken::Star,
570 fn force_no_space_before(tok: &Token) -> bool {
572 Token::Semi | Token::Comma | Token::Dot => true,
573 Token::BinOp(bot) => bot == BinOpToken::Star,
577 fn ident_like(tok: &Token) -> bool {
579 Token::Ident(_) | Token::Literal(..) | Token::Lifetime(_) => true,
584 fn next_space(tok: &Token) -> SpaceState {
597 | Token::BinOp(_) => SpaceState::Punctuation,
602 | Token::OpenDelim(_)
603 | Token::CloseDelim(_)
604 | Token::Whitespace => SpaceState::Never,
606 Token::Literal(..) | Token::Ident(_) | Token::Lifetime(_) => SpaceState::Ident,
608 _ => SpaceState::Always,
612 /// Tries to convert a macro use into a short hand try expression. Returns None
613 /// when the macro is not an instance of try! (or parsing the inner expression
615 pub fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext) -> Option<ast::Expr> {
616 if &format!("{}", mac.node.path)[..] == "try" {
617 let ts: TokenStream = mac.node.tts.clone().into();
618 let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
621 id: ast::NodeId::new(0), // dummy value
622 node: ast::ExprKind::Try(parser.parse_expr().ok()?),
623 span: mac.span, // incorrect span, but shouldn't matter too much
624 attrs: ThinVec::new(),
631 fn macro_style(mac: &ast::Mac, context: &RewriteContext) -> MacroStyle {
632 let snippet = context.snippet(mac.span);
633 let paren_pos = snippet.find_uncommented("(").unwrap_or(usize::max_value());
634 let bracket_pos = snippet.find_uncommented("[").unwrap_or(usize::max_value());
635 let brace_pos = snippet.find_uncommented("{").unwrap_or(usize::max_value());
637 if paren_pos < bracket_pos && paren_pos < brace_pos {
639 } else if bracket_pos < brace_pos {
646 /// Indent each line according to the specified `indent`.
671 fn indent_macro_snippet(
672 context: &RewriteContext,
675 ) -> Option<String> {
676 let mut lines = macro_str.lines();
677 let first_line = lines.next().map(|s| s.trim_right())?;
678 let mut trimmed_lines = Vec::with_capacity(16);
680 let min_prefix_space_width = lines
682 let prefix_space_width = if is_empty_line(line) {
685 Some(get_prefix_space_width(context, line))
687 trimmed_lines.push((line.trim(), prefix_space_width));
693 String::from(first_line) + "\n"
696 .map(|&(line, prefix_space_width)| match prefix_space_width {
697 Some(original_indent_width) => {
698 let new_indent_width = indent.width()
699 + original_indent_width
700 .checked_sub(min_prefix_space_width)
702 let new_indent = Indent::from_width(context.config, new_indent_width);
703 format!("{}{}", new_indent.to_string(context.config), line.trim())
705 None => String::new(),
712 fn get_prefix_space_width(context: &RewriteContext, s: &str) -> usize {
717 '\t' => width += context.config.tab_spaces(),
724 fn is_empty_line(s: &str) -> bool {
725 s.is_empty() || s.chars().all(char::is_whitespace)
728 // A very simple parser that just parses a macros 2.0 definition into its branches.
729 // Currently we do not attempt to parse any further than that.
736 // (`(` ... `)` `=>` `{` ... `}`)*
737 fn parse(&mut self) -> Option<Macro> {
738 let mut branches = vec![];
739 while self.toks.look_ahead(1).is_some() {
740 branches.push(self.parse_branch()?);
743 Some(Macro { branches })
746 // `(` ... `)` `=>` `{` ... `}`
747 fn parse_branch(&mut self) -> Option<MacroBranch> {
748 let (args_paren_kind, args) = match self.toks.next()? {
749 TokenTree::Token(..) => return None,
750 TokenTree::Delimited(_, ref d) => (d.delim, d.tts.clone()),
752 match self.toks.next()? {
753 TokenTree::Token(_, Token::FatArrow) => {}
756 let body = match self.toks.next()? {
757 TokenTree::Token(..) => return None,
758 TokenTree::Delimited(sp, _) => {
759 let data = sp.data();
760 Span::new(data.lo + BytePos(1), data.hi - BytePos(1), data.ctxt)
763 if let Some(TokenTree::Token(_, Token::Semi)) = self.toks.look_ahead(0) {
774 // A parsed macros 2.0 macro definition.
776 branches: Vec<MacroBranch>,
779 // FIXME: it would be more efficient to use references to the token streams
780 // rather than clone them, if we can make the borrowing work out.
782 args: ThinTokenStream,
783 args_paren_kind: DelimToken,
790 use syntax::parse::{parse_stream_from_source_str, ParseSess};
791 use syntax::codemap::{FileName, FilePathMapping};
793 fn format_macro_args_str(s: &str) -> String {
794 let input = parse_stream_from_source_str(
795 FileName::Custom("stdin".to_owned()),
797 &ParseSess::new(FilePathMapping::empty()),
800 format_macro_args(input.into()).unwrap()
804 fn test_format_macro_args() {
805 assert_eq!(format_macro_args_str(""), "".to_owned());
806 assert_eq!(format_macro_args_str("$ x : ident"), "$x: ident".to_owned());
808 format_macro_args_str("$ m1 : ident , $ m2 : ident , $ x : ident"),
809 "$m1: ident, $m2: ident, $x: ident".to_owned()
812 format_macro_args_str("$($beginning:ident),*;$middle:ident;$($end:ident),*"),
813 "$($beginning: ident),*; $middle: ident; $($end: ident),*".to_owned()
816 format_macro_args_str(
817 "$ name : ident ( $ ( $ dol : tt $ var : ident ) * ) $ ( $ body : tt ) *"
819 "$name: ident($($dol: tt $var: ident)*) $($body: tt)*".to_owned()