]>
Commit | Line | Data |
---|---|---|
29967ef6 XL |
1 | #[cfg(test)] |
2 | mod tests; | |
3 | ||
4 | pub mod state; | |
5 | pub use state::{print_crate, AnnNode, Comments, PpAnn, PrintState, State}; | |
6 | ||
29967ef6 XL |
7 | use rustc_ast::token::{Nonterminal, Token, TokenKind}; |
8 | use rustc_ast::tokenstream::{TokenStream, TokenTree}; | |
04454e1e | 9 | use rustc_ast::{self as ast, AstDeref}; |
29967ef6 | 10 | |
94222f64 XL |
11 | use std::borrow::Cow; |
12 | ||
04454e1e FG |
13 | pub trait AstPrettyPrint { |
14 | fn pretty_print(&self) -> String; | |
15 | } | |
16 | ||
17 | impl<T: AstDeref<Target: AstPrettyPrint>> AstPrettyPrint for T { | |
18 | fn pretty_print(&self) -> String { | |
19 | self.ast_deref().pretty_print() | |
20 | } | |
21 | } | |
22 | ||
23 | macro_rules! impl_ast_pretty_print { | |
24 | ($($T:ty => $method:ident),+ $(,)?) => { | |
25 | $( | |
26 | impl AstPrettyPrint for $T { | |
27 | fn pretty_print(&self) -> String { | |
28 | State::new().$method(self) | |
29 | } | |
30 | } | |
31 | )+ | |
32 | }; | |
33 | } | |
34 | ||
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, | |
41 | } | |
42 | ||
29967ef6 XL |
43 | pub fn nonterminal_to_string(nt: &Nonterminal) -> String { |
44 | State::new().nonterminal_to_string(nt) | |
45 | } | |
46 | ||
47 | /// Print the token kind precisely, without converting `$crate` into its respective crate name. | |
94222f64 | 48 | pub fn token_kind_to_string(tok: &TokenKind) -> Cow<'static, str> { |
29967ef6 XL |
49 | State::new().token_kind_to_string(tok) |
50 | } | |
51 | ||
52 | /// Print the token precisely, without converting `$crate` into its respective crate name. | |
94222f64 | 53 | pub fn token_to_string(token: &Token) -> Cow<'static, str> { |
29967ef6 XL |
54 | State::new().token_to_string(token) |
55 | } | |
56 | ||
29967ef6 XL |
57 | pub fn ty_to_string(ty: &ast::Ty) -> String { |
58 | State::new().ty_to_string(ty) | |
59 | } | |
60 | ||
61 | pub fn bounds_to_string(bounds: &[ast::GenericBound]) -> String { | |
62 | State::new().bounds_to_string(bounds) | |
63 | } | |
64 | ||
65 | pub fn pat_to_string(pat: &ast::Pat) -> String { | |
66 | State::new().pat_to_string(pat) | |
67 | } | |
68 | ||
69 | pub fn expr_to_string(e: &ast::Expr) -> String { | |
70 | State::new().expr_to_string(e) | |
71 | } | |
72 | ||
73 | pub fn tt_to_string(tt: &TokenTree) -> String { | |
74 | State::new().tt_to_string(tt) | |
75 | } | |
76 | ||
77 | pub fn tts_to_string(tokens: &TokenStream) -> String { | |
78 | State::new().tts_to_string(tokens) | |
79 | } | |
80 | ||
29967ef6 XL |
81 | pub fn item_to_string(i: &ast::Item) -> String { |
82 | State::new().item_to_string(i) | |
83 | } | |
84 | ||
29967ef6 XL |
85 | pub fn path_to_string(p: &ast::Path) -> String { |
86 | State::new().path_to_string(p) | |
87 | } | |
88 | ||
89 | pub fn path_segment_to_string(p: &ast::PathSegment) -> String { | |
90 | State::new().path_segment_to_string(p) | |
91 | } | |
92 | ||
93 | pub fn vis_to_string(v: &ast::Visibility) -> String { | |
94 | State::new().vis_to_string(v) | |
95 | } | |
96 | ||
29967ef6 XL |
97 | pub fn meta_list_item_to_string(li: &ast::NestedMetaItem) -> String { |
98 | State::new().meta_list_item_to_string(li) | |
99 | } | |
100 | ||
29967ef6 XL |
101 | pub fn attribute_to_string(attr: &ast::Attribute) -> String { |
102 | State::new().attribute_to_string(attr) | |
103 | } | |
104 | ||
29967ef6 | 105 | pub fn to_string(f: impl FnOnce(&mut State<'_>)) -> String { |
a2a8927a XL |
106 | State::to_string(f) |
107 | } | |
108 | ||
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 { | |
113 | s.print_item(item); | |
114 | } | |
115 | }) | |
29967ef6 | 116 | } |