1
Fork 0

pprust: Support macro macros

This commit is contained in:
Vadim Petrochenkov 2019-07-14 16:09:39 +03:00
parent a2a1cd1864
commit 0cdd18d0a7
3 changed files with 31 additions and 9 deletions

View file

@ -20,6 +20,11 @@ use syntax_pos::{DUMMY_SP, FileName, Span};
use std::borrow::Cow; use std::borrow::Cow;
pub enum MacHeader<'a> {
Path(&'a ast::Path),
Keyword(&'static str),
}
pub enum AnnNode<'a> { pub enum AnnNode<'a> {
Ident(&'a ast::Ident), Ident(&'a ast::Ident),
Name(&'a ast::Name), Name(&'a ast::Name),
@ -620,7 +625,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
match attr.tokens.trees().next() { match attr.tokens.trees().next() {
Some(TokenTree::Delimited(_, delim, tts)) => { Some(TokenTree::Delimited(_, delim, tts)) => {
self.print_mac_common( 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 => { tree => {
@ -706,7 +711,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
fn print_mac_common( fn print_mac_common(
&mut self, &mut self,
path: Option<&ast::Path>, header: Option<MacHeader<'_>>,
has_bang: bool, has_bang: bool,
ident: Option<ast::Ident>, ident: Option<ast::Ident>,
delim: DelimToken, delim: DelimToken,
@ -717,8 +722,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
if delim == DelimToken::Brace { if delim == DelimToken::Brace {
self.cbox(INDENT_UNIT); self.cbox(INDENT_UNIT);
} }
if let Some(path) = path { match header {
self.print_path(path, false, 0); Some(MacHeader::Path(path)) => self.print_path(path, false, 0),
Some(MacHeader::Keyword(kw)) => self.word(kw),
None => {}
} }
if has_bang { if has_bang {
self.word("!"); self.word("!");
@ -729,7 +736,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
} }
match delim { match delim {
DelimToken::Brace => { DelimToken::Brace => {
if path.is_some() || has_bang || ident.is_some() { if header.is_some() || has_bang || ident.is_some() {
self.nbsp(); self.nbsp();
} }
self.word("{"); self.word("{");
@ -1357,9 +1364,11 @@ impl<'a> State<'a> {
} }
} }
ast::ItemKind::MacroDef(ref macro_def) => { ast::ItemKind::MacroDef(ref macro_def) => {
let (kw, has_bang) =
if macro_def.legacy { ("macro_rules", true) } else { ("macro", false) };
self.print_mac_common( self.print_mac_common(
Some(&ast::Path::from_ident(ast::Ident::with_empty_ctxt(sym::macro_rules))), Some(MacHeader::Keyword(kw)),
true, has_bang,
Some(item.ident), Some(item.ident),
DelimToken::Brace, DelimToken::Brace,
macro_def.stream(), macro_def.stream(),
@ -1754,7 +1763,13 @@ impl<'a> State<'a> {
crate fn print_mac(&mut self, m: &ast::Mac) { crate fn print_mac(&mut self, m: &ast::Mac) {
self.print_mac_common( 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,
); );
} }

7
src/test/pretty/macro.rs Normal file
View file

@ -0,0 +1,7 @@
// pp-exact
#![feature(decl_macro)]
macro mac { ($ arg : expr) => { $ arg + $ arg } }
fn main() { }

View file

@ -2,7 +2,7 @@
#![feature /* 0#0 */(no_core)] #![feature /* 0#0 */(no_core)]
#![no_core /* 0#0 */] #![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 */ } fn bar /* 0#0 */() { let x /* 0#0 */ = 1; y /* 0#1 */ + x /* 0#0 */ }