Skip to content

Commit

Permalink
pprust: Support macro macros
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Jul 15, 2019
1 parent a2a1cd1 commit 0cdd18d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
31 changes: 23 additions & 8 deletions src/libsyntax/print/pprust.rs
Expand Up @@ -20,6 +20,11 @@ use syntax_pos::{DUMMY_SP, FileName, Span};

use std::borrow::Cow;

pub enum MacHeader<'a> {
Path(&'a ast::Path),
Keyword(&'static str),
}

pub enum AnnNode<'a> {
Ident(&'a ast::Ident),
Name(&'a ast::Name),
Expand Down Expand Up @@ -620,7 +625,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
match attr.tokens.trees().next() {
Some(TokenTree::Delimited(_, delim, tts)) => {
self.print_mac_common(
Some(&attr.path), false, None, delim, tts, true, attr.span
Some(MacHeader::Path(&attr.path)), false, None, delim, tts, true, attr.span
);
}
tree => {
Expand Down Expand Up @@ -706,7 +711,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM

fn print_mac_common(
&mut self,
path: Option<&ast::Path>,
header: Option<MacHeader<'_>>,
has_bang: bool,
ident: Option<ast::Ident>,
delim: DelimToken,
Expand All @@ -717,8 +722,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
if delim == DelimToken::Brace {
self.cbox(INDENT_UNIT);
}
if let Some(path) = path {
self.print_path(path, false, 0);
match header {
Some(MacHeader::Path(path)) => self.print_path(path, false, 0),
Some(MacHeader::Keyword(kw)) => self.word(kw),
None => {}
}
if has_bang {
self.word("!");
Expand All @@ -729,7 +736,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
}
match delim {
DelimToken::Brace => {
if path.is_some() || has_bang || ident.is_some() {
if header.is_some() || has_bang || ident.is_some() {
self.nbsp();
}
self.word("{");
Expand Down Expand Up @@ -1357,9 +1364,11 @@ impl<'a> State<'a> {
}
}
ast::ItemKind::MacroDef(ref macro_def) => {
let (kw, has_bang) =
if macro_def.legacy { ("macro_rules", true) } else { ("macro", false) };
self.print_mac_common(
Some(&ast::Path::from_ident(ast::Ident::with_empty_ctxt(sym::macro_rules))),
true,
Some(MacHeader::Keyword(kw)),
has_bang,
Some(item.ident),
DelimToken::Brace,
macro_def.stream(),
Expand Down Expand Up @@ -1754,7 +1763,13 @@ impl<'a> State<'a> {

crate fn print_mac(&mut self, m: &ast::Mac) {
self.print_mac_common(
Some(&m.node.path), true, None, m.node.delim.to_token(), m.node.stream(), true, m.span
Some(MacHeader::Path(&m.node.path)),
true,
None,
m.node.delim.to_token(),
m.node.stream(),
true,
m.span,
);
}

Expand Down
7 changes: 7 additions & 0 deletions src/test/pretty/macro.rs
@@ -0,0 +1,7 @@
// pp-exact

#![feature(decl_macro)]

macro mac { ($ arg : expr) => { $ arg + $ arg } }

fn main() { }
Expand Up @@ -2,7 +2,7 @@
#![feature /* 0#0 */(no_core)]
#![no_core /* 0#0 */]

macro_rules /* 0#0 */! foo /* 0#0 */ { ($ x : ident) => { y + $ x } }
macro_rules! foo /* 0#0 */ { ($ x : ident) => { y + $ x } }

fn bar /* 0#0 */() { let x /* 0#0 */ = 1; y /* 0#1 */ + x /* 0#0 */ }

Expand Down

0 comments on commit 0cdd18d

Please sign in to comment.