|
10 | 10 | //! `parse(format!())` we use internally is an implementation detail -- long
|
11 | 11 | //! term, it will be replaced with direct tree manipulation.
|
12 | 12 | use itertools::Itertools;
|
| 13 | +use parser::T; |
| 14 | +use rowan::NodeOrToken; |
13 | 15 | use stdx::{format_to, never};
|
14 | 16 |
|
15 | 17 | use crate::{ast, utils::is_raw_identifier, AstNode, SourceFile, SyntaxKind, SyntaxToken};
|
@@ -1030,6 +1032,41 @@ pub fn struct_(
|
1030 | 1032 | ast_from_text(&format!("{visibility}struct {strukt_name}{type_params}{field_list}{semicolon}",))
|
1031 | 1033 | }
|
1032 | 1034 |
|
| 1035 | +pub fn attr_outer(meta: ast::Meta) -> ast::Attr { |
| 1036 | + ast_from_text(&format!("#[{meta}]")) |
| 1037 | +} |
| 1038 | + |
| 1039 | +pub fn attr_inner(meta: ast::Meta) -> ast::Attr { |
| 1040 | + ast_from_text(&format!("#![{meta}]")) |
| 1041 | +} |
| 1042 | + |
| 1043 | +pub fn meta_expr(path: ast::Path, expr: ast::Expr) -> ast::Meta { |
| 1044 | + ast_from_text(&format!("#[{path} = {expr}]")) |
| 1045 | +} |
| 1046 | + |
| 1047 | +pub fn meta_token_tree(path: ast::Path, tt: ast::TokenTree) -> ast::Meta { |
| 1048 | + ast_from_text(&format!("#[{path}{tt}]")) |
| 1049 | +} |
| 1050 | + |
| 1051 | +pub fn meta_path(path: ast::Path) -> ast::Meta { |
| 1052 | + ast_from_text(&format!("#[{path}]")) |
| 1053 | +} |
| 1054 | + |
| 1055 | +pub fn token_tree( |
| 1056 | + delimiter: SyntaxKind, |
| 1057 | + tt: Vec<NodeOrToken<ast::TokenTree, SyntaxToken>>, |
| 1058 | +) -> ast::TokenTree { |
| 1059 | + let (l_delimiter, r_delimiter) = match delimiter { |
| 1060 | + T!['('] => ('(', ')'), |
| 1061 | + T!['['] => ('[', ']'), |
| 1062 | + T!['{'] => ('{', '}'), |
| 1063 | + _ => panic!("invalid delimiter `{delimiter:?}`"), |
| 1064 | + }; |
| 1065 | + let tt = tt.into_iter().join(""); |
| 1066 | + |
| 1067 | + ast_from_text(&format!("tt!{l_delimiter}{tt}{r_delimiter}")) |
| 1068 | +} |
| 1069 | + |
1033 | 1070 | #[track_caller]
|
1034 | 1071 | fn ast_from_text<N: AstNode>(text: &str) -> N {
|
1035 | 1072 | let parse = SourceFile::parse(text);
|
|
0 commit comments