5 pub use state::{print_crate, AnnNode, Comments, PpAnn, PrintState, State};
7 use rustc_ast::token::{Nonterminal, Token, TokenKind};
8 use rustc_ast::tokenstream::{TokenStream, TokenTree};
9 use rustc_ast::{self as ast, AstDeref};
13 pub trait AstPrettyPrint {
14 fn pretty_print(&self) -> String;
17 impl<T: AstDeref<Target: AstPrettyPrint>> AstPrettyPrint for T {
18 fn pretty_print(&self) -> String {
19 self.ast_deref().pretty_print()
23 macro_rules! impl_ast_pretty_print {
24 ($($T:ty => $method:ident),+ $(,)?) => {
26 impl AstPrettyPrint for $T {
27 fn pretty_print(&self) -> String {
28 State::new().$method(self)
35 impl_ast_pretty_print! {
36 ast::Item => item_to_string,
37 ast::AssocItem => assoc_item_to_string,
38 ast::ForeignItem => foreign_item_to_string,
39 ast::Expr => expr_to_string,
40 ast::Stmt => stmt_to_string,
43 pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
44 State::new().nonterminal_to_string(nt)
47 /// Print the token kind precisely, without converting `$crate` into its respective crate name.
48 pub fn token_kind_to_string(tok: &TokenKind) -> Cow<'static, str> {
49 State::new().token_kind_to_string(tok)
52 /// Print the token precisely, without converting `$crate` into its respective crate name.
53 pub fn token_to_string(token: &Token) -> Cow<'static, str> {
54 State::new().token_to_string(token)
57 pub fn ty_to_string(ty: &ast::Ty) -> String {
58 State::new().ty_to_string(ty)
61 pub fn bounds_to_string(bounds: &[ast::GenericBound]) -> String {
62 State::new().bounds_to_string(bounds)
65 pub fn pat_to_string(pat: &ast::Pat) -> String {
66 State::new().pat_to_string(pat)
69 pub fn expr_to_string(e: &ast::Expr) -> String {
70 State::new().expr_to_string(e)
73 pub fn tt_to_string(tt: &TokenTree) -> String {
74 State::new().tt_to_string(tt)
77 pub fn tts_to_string(tokens: &TokenStream) -> String {
78 State::new().tts_to_string(tokens)
81 pub fn item_to_string(i: &ast::Item) -> String {
82 State::new().item_to_string(i)
85 pub fn path_to_string(p: &ast::Path) -> String {
86 State::new().path_to_string(p)
89 pub fn path_segment_to_string(p: &ast::PathSegment) -> String {
90 State::new().path_segment_to_string(p)
93 pub fn vis_to_string(v: &ast::Visibility) -> String {
94 State::new().vis_to_string(v)
97 pub fn meta_list_item_to_string(li: &ast::NestedMetaItem) -> String {
98 State::new().meta_list_item_to_string(li)
101 pub fn attribute_to_string(attr: &ast::Attribute) -> String {
102 State::new().attribute_to_string(attr)
105 pub fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
109 pub fn crate_to_string_for_macros(krate: &ast::Crate) -> String {
110 State::to_string(|s| {
111 s.print_inner_attributes(&krate.attrs);
112 for item in &krate.items {