]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_ast_pretty/src/pprust/mod.rs
New upstream version 1.62.1+dfsg1
[rustc.git] / compiler / rustc_ast_pretty / src / pprust / mod.rs
CommitLineData
29967ef6
XL
1#[cfg(test)]
2mod tests;
3
4pub mod state;
5pub use state::{print_crate, AnnNode, Comments, PpAnn, PrintState, State};
6
29967ef6
XL
7use rustc_ast::token::{Nonterminal, Token, TokenKind};
8use rustc_ast::tokenstream::{TokenStream, TokenTree};
04454e1e 9use rustc_ast::{self as ast, AstDeref};
29967ef6 10
94222f64
XL
11use std::borrow::Cow;
12
04454e1e
FG
13pub trait AstPrettyPrint {
14 fn pretty_print(&self) -> String;
15}
16
17impl<T: AstDeref<Target: AstPrettyPrint>> AstPrettyPrint for T {
18 fn pretty_print(&self) -> String {
19 self.ast_deref().pretty_print()
20 }
21}
22
23macro_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
35impl_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
43pub 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 48pub 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 53pub fn token_to_string(token: &Token) -> Cow<'static, str> {
29967ef6
XL
54 State::new().token_to_string(token)
55}
56
29967ef6
XL
57pub fn ty_to_string(ty: &ast::Ty) -> String {
58 State::new().ty_to_string(ty)
59}
60
61pub fn bounds_to_string(bounds: &[ast::GenericBound]) -> String {
62 State::new().bounds_to_string(bounds)
63}
64
65pub fn pat_to_string(pat: &ast::Pat) -> String {
66 State::new().pat_to_string(pat)
67}
68
69pub fn expr_to_string(e: &ast::Expr) -> String {
70 State::new().expr_to_string(e)
71}
72
73pub fn tt_to_string(tt: &TokenTree) -> String {
74 State::new().tt_to_string(tt)
75}
76
77pub fn tts_to_string(tokens: &TokenStream) -> String {
78 State::new().tts_to_string(tokens)
79}
80
29967ef6
XL
81pub fn item_to_string(i: &ast::Item) -> String {
82 State::new().item_to_string(i)
83}
84
29967ef6
XL
85pub fn path_to_string(p: &ast::Path) -> String {
86 State::new().path_to_string(p)
87}
88
89pub fn path_segment_to_string(p: &ast::PathSegment) -> String {
90 State::new().path_segment_to_string(p)
91}
92
93pub fn vis_to_string(v: &ast::Visibility) -> String {
94 State::new().vis_to_string(v)
95}
96
29967ef6
XL
97pub fn meta_list_item_to_string(li: &ast::NestedMetaItem) -> String {
98 State::new().meta_list_item_to_string(li)
99}
100
29967ef6
XL
101pub fn attribute_to_string(attr: &ast::Attribute) -> String {
102 State::new().attribute_to_string(attr)
103}
104
29967ef6 105pub fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
a2a8927a
XL
106 State::to_string(f)
107}
108
109pub 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}