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
nonterminal_to_string(nt: &Nonterminal) -> String13 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.
token_kind_to_string(tok: &TokenKind) -> Cow<'static, str>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.
token_to_string(token: &Token) -> Cow<'static, str>23 pub fn token_to_string(token: &Token) -> Cow<'static, str> {
24 State::new().token_to_string(token)
25 }
26
ty_to_string(ty: &ast::Ty) -> String27 pub fn ty_to_string(ty: &ast::Ty) -> String {
28 State::new().ty_to_string(ty)
29 }
30
bounds_to_string(bounds: &[ast::GenericBound]) -> String31 pub fn bounds_to_string(bounds: &[ast::GenericBound]) -> String {
32 State::new().bounds_to_string(bounds)
33 }
34
where_bound_predicate_to_string(where_bound_predicate: &ast::WhereBoundPredicate) -> String35 pub fn where_bound_predicate_to_string(where_bound_predicate: &ast::WhereBoundPredicate) -> String {
36 State::new().where_bound_predicate_to_string(where_bound_predicate)
37 }
38
pat_to_string(pat: &ast::Pat) -> String39 pub fn pat_to_string(pat: &ast::Pat) -> String {
40 State::new().pat_to_string(pat)
41 }
42
expr_to_string(e: &ast::Expr) -> String43 pub fn expr_to_string(e: &ast::Expr) -> String {
44 State::new().expr_to_string(e)
45 }
46
tt_to_string(tt: &TokenTree) -> String47 pub fn tt_to_string(tt: &TokenTree) -> String {
48 State::new().tt_to_string(tt)
49 }
50
tts_to_string(tokens: &TokenStream) -> String51 pub fn tts_to_string(tokens: &TokenStream) -> String {
52 State::new().tts_to_string(tokens)
53 }
54
item_to_string(i: &ast::Item) -> String55 pub fn item_to_string(i: &ast::Item) -> String {
56 State::new().item_to_string(i)
57 }
58
path_to_string(p: &ast::Path) -> String59 pub fn path_to_string(p: &ast::Path) -> String {
60 State::new().path_to_string(p)
61 }
62
path_segment_to_string(p: &ast::PathSegment) -> String63 pub fn path_segment_to_string(p: &ast::PathSegment) -> String {
64 State::new().path_segment_to_string(p)
65 }
66
vis_to_string(v: &ast::Visibility) -> String67 pub fn vis_to_string(v: &ast::Visibility) -> String {
68 State::new().vis_to_string(v)
69 }
70
meta_list_item_to_string(li: &ast::NestedMetaItem) -> String71 pub fn meta_list_item_to_string(li: &ast::NestedMetaItem) -> String {
72 State::new().meta_list_item_to_string(li)
73 }
74
attribute_to_string(attr: &ast::Attribute) -> String75 pub fn attribute_to_string(attr: &ast::Attribute) -> String {
76 State::new().attribute_to_string(attr)
77 }
78
to_string(f: impl FnOnce(&mut State<'_>)) -> String79 pub fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
80 State::to_string(f)
81 }
82
crate_to_string_for_macros(krate: &ast::Crate) -> String83 pub fn crate_to_string_for_macros(krate: &ast::Crate) -> String {
84 State::to_string(|s| {
85 s.print_inner_attributes(&krate.attrs);
86 for item in &krate.items {
87 s.print_item(item);
88 }
89 })
90 }
91