ast: Introduce some traits to get AST node properties generically
And use them to avoid constructing some artificial `Nonterminal` tokens during expansion
This commit is contained in:
parent
ee6eaabdd4
commit
f2b7fa4847
24 changed files with 593 additions and 500 deletions
|
@ -1,5 +1,7 @@
|
|||
#![feature(associated_type_bounds)]
|
||||
#![feature(crate_visibility_modifier)]
|
||||
#![feature(box_patterns)]
|
||||
#![feature(with_negative_coherence)]
|
||||
#![recursion_limit = "256"]
|
||||
|
||||
mod helpers;
|
||||
|
|
|
@ -4,12 +4,42 @@ mod tests;
|
|||
pub mod state;
|
||||
pub use state::{print_crate, AnnNode, Comments, PpAnn, PrintState, State};
|
||||
|
||||
use rustc_ast as ast;
|
||||
use rustc_ast::token::{Nonterminal, Token, TokenKind};
|
||||
use rustc_ast::tokenstream::{TokenStream, TokenTree};
|
||||
use rustc_ast::{self as ast, AstDeref};
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
pub trait AstPrettyPrint {
|
||||
fn pretty_print(&self) -> String;
|
||||
}
|
||||
|
||||
impl<T: AstDeref<Target: AstPrettyPrint>> AstPrettyPrint for T {
|
||||
fn pretty_print(&self) -> String {
|
||||
self.ast_deref().pretty_print()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_ast_pretty_print {
|
||||
($($T:ty => $method:ident),+ $(,)?) => {
|
||||
$(
|
||||
impl AstPrettyPrint for $T {
|
||||
fn pretty_print(&self) -> String {
|
||||
State::new().$method(self)
|
||||
}
|
||||
}
|
||||
)+
|
||||
};
|
||||
}
|
||||
|
||||
impl_ast_pretty_print! {
|
||||
ast::Item => item_to_string,
|
||||
ast::AssocItem => assoc_item_to_string,
|
||||
ast::ForeignItem => foreign_item_to_string,
|
||||
ast::Expr => expr_to_string,
|
||||
ast::Stmt => stmt_to_string,
|
||||
}
|
||||
|
||||
pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
|
||||
State::new().nonterminal_to_string(nt)
|
||||
}
|
||||
|
|
|
@ -858,6 +858,14 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
|
|||
Self::to_string(|s| s.print_item(i))
|
||||
}
|
||||
|
||||
fn assoc_item_to_string(&self, i: &ast::AssocItem) -> String {
|
||||
Self::to_string(|s| s.print_assoc_item(i))
|
||||
}
|
||||
|
||||
fn foreign_item_to_string(&self, i: &ast::ForeignItem) -> String {
|
||||
Self::to_string(|s| s.print_foreign_item(i))
|
||||
}
|
||||
|
||||
fn generic_params_to_string(&self, generic_params: &[ast::GenericParam]) -> String {
|
||||
Self::to_string(|s| s.print_generic_params(generic_params))
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ impl<'a> State<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn print_foreign_item(&mut self, item: &ast::ForeignItem) {
|
||||
crate fn print_foreign_item(&mut self, item: &ast::ForeignItem) {
|
||||
let ast::Item { id, span, ident, ref attrs, ref kind, ref vis, tokens: _ } = *item;
|
||||
self.ann.pre(self, AnnNode::SubItem(id));
|
||||
self.hardbreak_if_not_bol();
|
||||
|
@ -496,7 +496,7 @@ impl<'a> State<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn print_assoc_item(&mut self, item: &ast::AssocItem) {
|
||||
crate fn print_assoc_item(&mut self, item: &ast::AssocItem) {
|
||||
let ast::Item { id, span, ident, ref attrs, ref kind, ref vis, tokens: _ } = *item;
|
||||
self.ann.pre(self, AnnNode::SubItem(id));
|
||||
self.hardbreak_if_not_bol();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue