]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast_pretty/src/pprust/mod.rs
:arrow_up: rust-analyzer
[rust.git] / compiler / rustc_ast_pretty / src / pprust / mod.rs
1 #[cfg(test)]
2 mod tests;
3
4 pub mod state;
5 pub use state::{print_crate, AnnNode, Comments, PpAnn, PrintState, State};
6
7 use rustc_ast as ast;
8 use rustc_ast::token::{Nonterminal, Token, TokenKind};
9 use rustc_ast::tokenstream::{TokenStream, TokenTree};
10
11 use std::borrow::Cow;
12
13 pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
14     State::new().nonterminal_to_string(nt)
15 }
16
17 /// Print the token kind precisely, without converting `$crate` into its respective crate name.
18 pub fn token_kind_to_string(tok: &TokenKind) -> Cow<'static, str> {
19     State::new().token_kind_to_string(tok)
20 }
21
22 /// Print the token precisely, without converting `$crate` into its respective crate name.
23 pub fn token_to_string(token: &Token) -> Cow<'static, str> {
24     State::new().token_to_string(token)
25 }
26
27 pub fn ty_to_string(ty: &ast::Ty) -> String {
28     State::new().ty_to_string(ty)
29 }
30
31 pub fn bounds_to_string(bounds: &[ast::GenericBound]) -> String {
32     State::new().bounds_to_string(bounds)
33 }
34
35 pub fn pat_to_string(pat: &ast::Pat) -> String {
36     State::new().pat_to_string(pat)
37 }
38
39 pub fn expr_to_string(e: &ast::Expr) -> String {
40     State::new().expr_to_string(e)
41 }
42
43 pub fn tt_to_string(tt: &TokenTree) -> String {
44     State::new().tt_to_string(tt)
45 }
46
47 pub fn tts_to_string(tokens: &TokenStream) -> String {
48     State::new().tts_to_string(tokens)
49 }
50
51 pub fn item_to_string(i: &ast::Item) -> String {
52     State::new().item_to_string(i)
53 }
54
55 pub fn path_to_string(p: &ast::Path) -> String {
56     State::new().path_to_string(p)
57 }
58
59 pub fn path_segment_to_string(p: &ast::PathSegment) -> String {
60     State::new().path_segment_to_string(p)
61 }
62
63 pub fn vis_to_string(v: &ast::Visibility) -> String {
64     State::new().vis_to_string(v)
65 }
66
67 pub fn meta_list_item_to_string(li: &ast::NestedMetaItem) -> String {
68     State::new().meta_list_item_to_string(li)
69 }
70
71 pub fn attribute_to_string(attr: &ast::Attribute) -> String {
72     State::new().attribute_to_string(attr)
73 }
74
75 pub fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
76     State::to_string(f)
77 }
78
79 pub fn crate_to_string_for_macros(krate: &ast::Crate) -> String {
80     State::to_string(|s| {
81         s.print_inner_attributes(&krate.attrs);
82         for item in &krate.items {
83             s.print_item(item);
84         }
85     })
86 }