1 use rustc_ast::token::{self, BinOpToken, Delimiter};
2 use rustc_ast::tokenstream::{TokenStream, TokenTree};
3 use rustc_ast_pretty::pprust::state::State as Printer;
4 use rustc_ast_pretty::pprust::PrintState;
5 use rustc_middle::ty::TyCtxt;
6 use rustc_session::parse::ParseSess;
7 use rustc_span::source_map::FilePathMapping;
8 use rustc_span::symbol::{kw, Ident, Symbol};
11 /// Render a macro matcher in a format suitable for displaying to the user
12 /// as part of an item declaration.
13 pub(super) fn render_macro_matcher(tcx: TyCtxt<'_>, matcher: &TokenTree) -> String {
14 if let Some(snippet) = snippet_equal_to_token(tcx, matcher) {
15 // If the original source code is known, we display the matcher exactly
16 // as present in the source code.
20 // If the matcher is macro-generated or some other reason the source code
21 // snippet is not available, we attempt to nicely render the token tree.
22 let mut printer = Printer::new();
24 // If the inner ibox fits on one line, we get:
26 // macro_rules! macroname {
27 // (the matcher) => {...};
30 // If the inner ibox gets wrapped, the cbox will break and get indented:
32 // macro_rules! macroname {
34 // the matcher ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!
43 TokenTree::Delimited(_span, _delim, tts) => print_tts(&mut printer, tts),
44 // Matcher which is not a Delimited is unexpected and should've failed
45 // to compile, but we render whatever it is wrapped in parens.
46 TokenTree::Token(..) => print_tt(&mut printer, matcher),
49 printer.break_offset_if_not_bol(0, -4);
55 /// Find the source snippet for this token's Span, reparse it, and return the
56 /// snippet if the reparsed TokenTree matches the argument TokenTree.
57 fn snippet_equal_to_token(tcx: TyCtxt<'_>, matcher: &TokenTree) -> Option<String> {
58 // Find what rustc thinks is the source snippet.
59 // This may not actually be anything meaningful if this matcher was itself
60 // generated by a macro.
61 let source_map = tcx.sess.source_map();
62 let span = matcher.span();
63 let snippet = source_map.span_to_snippet(span).ok()?;
66 let sess = ParseSess::new(FilePathMapping::empty());
67 let file_name = source_map.span_to_filename(span);
69 match rustc_parse::maybe_new_parser_from_source_str(&sess, file_name, snippet.clone()) {
77 // Reparse a single token tree.
78 let mut reparsed_trees = match parser.parse_all_token_trees() {
79 Ok(reparsed_trees) => reparsed_trees,
85 if reparsed_trees.len() != 1 {
88 let reparsed_tree = reparsed_trees.pop().unwrap();
90 // Compare against the original tree.
91 if reparsed_tree.eq_unspanned(matcher) { Some(snippet) } else { None }
94 fn print_tt(printer: &mut Printer<'_>, tt: &TokenTree) {
96 TokenTree::Token(token, _) => {
97 let token_str = printer.token_to_string(token);
98 printer.word(token_str);
99 if let token::DocComment(..) = token.kind {
103 TokenTree::Delimited(_span, delim, tts) => {
104 let open_delim = printer.token_kind_to_string(&token::OpenDelim(*delim));
105 printer.word(open_delim);
107 if *delim == Delimiter::Brace {
110 print_tts(printer, tts);
111 if *delim == Delimiter::Brace {
115 let close_delim = printer.token_kind_to_string(&token::CloseDelim(*delim));
116 printer.word(close_delim);
121 fn print_tts(printer: &mut Printer<'_>, tts: &TokenStream) {
122 #[derive(Copy, Clone, PartialEq)]
138 let mut state = Start;
139 for tt in tts.trees() {
140 let (needs_space, next_state) = match &tt {
141 TokenTree::Token(tt, _) => match (state, &tt.kind) {
142 (Dollar, token::Ident(..)) => (false, DollarIdent),
143 (DollarIdent, token::Colon) => (false, DollarIdentColon),
144 (DollarIdentColon, token::Ident(..)) => (false, Other),
147 token::BinOp(BinOpToken::Plus | BinOpToken::Star) | token::Question,
149 (DollarParen, _) => (false, DollarParenSep),
150 (DollarParenSep, token::BinOp(BinOpToken::Plus | BinOpToken::Star)) => {
153 (Pound, token::Not) => (false, PoundBang),
154 (_, token::Ident(symbol, /* is_raw */ false))
155 if !usually_needs_space_between_keyword_and_open_delim(*symbol, tt.span) =>
159 (_, token::Comma | token::Semi) => (false, Other),
160 (_, token::Dollar) => (true, Dollar),
161 (_, token::Pound) => (true, Pound),
162 (_, _) => (true, Other),
164 TokenTree::Delimited(_, delim, _) => match (state, delim) {
165 (Dollar, Delimiter::Parenthesis) => (false, DollarParen),
166 (Pound | PoundBang, Delimiter::Bracket) => (false, Other),
167 (Ident, Delimiter::Parenthesis | Delimiter::Bracket) => (false, Other),
168 (_, _) => (true, Other),
171 if state != Start && needs_space {
174 print_tt(printer, tt);
179 fn usually_needs_space_between_keyword_and_open_delim(symbol: Symbol, span: Span) -> bool {
180 let ident = Ident { name: symbol, span };
181 let is_keyword = ident.is_used_keyword() || ident.is_unused_keyword();
183 // An identifier that is not a keyword usually does not need a space
184 // before an open delim. For example: `f(0)` or `f[0]`.
189 // No space after keywords that are syntactically an expression. For
190 // example: a tuple struct created with `let _ = Self(0, 0)`, or if
191 // someone has `impl Index<MyStruct> for bool` then `true[MyStruct]`.
192 kw::False | kw::SelfLower | kw::SelfUpper | kw::True => false,
194 // No space, as in `let _: fn();`
197 // No space, as in `pub(crate) type T;`
200 // No space for keywords that can end an expression, as in `fut.await()`
201 // where fut's Output type is `fn()`.
204 // Otherwise space after keyword. Some examples:
212 // `type T = dyn (Fn() -> dyn Trait) + Send;`
214 // `for (tuple,) in iter {}`
216 // `if (tuple,) == v {}`
220 // `for x in [..] {}`
224 // `match [x, y] {...}`
230 // `fn f<T>() where (): Into<T>`
232 // `while (a + b).what() {}`