Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/syntax/src/ast/make.rs')
| -rw-r--r-- | crates/syntax/src/ast/make.rs | 52 |
1 files changed, 29 insertions, 23 deletions
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 231c21c38f..d608a35eff 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -19,9 +19,9 @@ use rowan::NodeOrToken; use stdx::{format_to, format_to_acc, never}; use crate::{ - ast::{self, make::quote::quote, Param}, - utils::is_raw_identifier, AstNode, SourceFile, SyntaxKind, SyntaxToken, + ast::{self, Param, make::quote::quote}, + utils::is_raw_identifier, }; /// While the parent module defines basic atomic "constructors", the `ext` @@ -32,12 +32,9 @@ pub mod ext { use super::*; pub fn simple_ident_pat(name: ast::Name) -> ast::IdentPat { - return from_text(&name.text()); - - fn from_text(text: &str) -> ast::IdentPat { - ast_from_text(&format!("fn f({text}: ())")) - } + ast_from_text(&format!("fn f({}: ())", name.text())) } + pub fn ident_path(ident: &str) -> ast::Path { path_unqualified(path_segment(name_ref(ident))) } @@ -81,7 +78,6 @@ pub mod ext { pub fn expr_self() -> ast::Expr { expr_from_text("self") } - pub fn zero_number() -> ast::Expr { expr_from_text("0") } @@ -116,6 +112,10 @@ pub mod ext { pub fn ty_result(t: ast::Type, e: ast::Type) -> ast::Type { ty_from_text(&format!("Result<{t}, {e}>")) } + + pub fn token_tree_from_node(node: &ast::SyntaxNode) -> ast::TokenTree { + ast_from_text(&format!("todo!{node}")) + } } pub fn name(name: &str) -> ast::Name { @@ -131,11 +131,7 @@ pub fn name_ref(name_ref: &str) -> ast::NameRef { } } fn raw_ident_esc(ident: &str) -> &'static str { - if is_raw_identifier(ident, Edition::CURRENT) { - "r#" - } else { - "" - } + if is_raw_identifier(ident, Edition::CURRENT) { "r#" } else { "" } } pub fn lifetime(text: &str) -> ast::Lifetime { @@ -328,7 +324,9 @@ pub fn impl_trait( None => String::new(), }; - ast_from_text(&format!("{is_unsafe}impl{gen_params} {is_negative}{path_type}{trait_gen_args} for {ty}{type_gen_args}{where_clause}{{{body_newline}{body}}}")) + ast_from_text(&format!( + "{is_unsafe}impl{gen_params} {is_negative}{path_type}{trait_gen_args} for {ty}{type_gen_args}{where_clause}{{{body_newline}{body}}}" + )) } pub fn impl_trait_type(bounds: ast::TypeBoundList) -> ast::ImplTraitType { @@ -623,6 +621,10 @@ pub fn expr_for_loop(pat: ast::Pat, expr: ast::Expr, block: ast::BlockExpr) -> a expr_from_text(&format!("for {pat} in {expr} {block}")) } +pub fn expr_while_loop(condition: ast::Expr, block: ast::BlockExpr) -> ast::WhileExpr { + expr_from_text(&format!("while {condition} {block}")) +} + pub fn expr_loop(block: ast::BlockExpr) -> ast::Expr { expr_from_text(&format!("loop {block}")) } @@ -631,18 +633,18 @@ pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::PrefixExpr { let token = token(op); expr_from_text(&format!("{token}{expr}")) } -pub fn expr_call(f: ast::Expr, arg_list: ast::ArgList) -> ast::Expr { +pub fn expr_call(f: ast::Expr, arg_list: ast::ArgList) -> ast::CallExpr { expr_from_text(&format!("{f}{arg_list}")) } pub fn expr_method_call( receiver: ast::Expr, method: ast::NameRef, arg_list: ast::ArgList, -) -> ast::Expr { +) -> ast::MethodCallExpr { expr_from_text(&format!("{receiver}.{method}{arg_list}")) } -pub fn expr_macro_call(f: ast::Expr, arg_list: ast::ArgList) -> ast::Expr { - expr_from_text(&format!("{f}!{arg_list}")) +pub fn expr_macro(path: ast::Path, tt: ast::TokenTree) -> ast::MacroExpr { + expr_from_text(&format!("{path}!{tt}")) } pub fn expr_ref(expr: ast::Expr, exclusive: bool) -> ast::Expr { expr_from_text(&if exclusive { format!("&mut {expr}") } else { format!("&{expr}") }) @@ -650,14 +652,17 @@ pub fn expr_ref(expr: ast::Expr, exclusive: bool) -> ast::Expr { pub fn expr_reborrow(expr: ast::Expr) -> ast::Expr { expr_from_text(&format!("&mut *{expr}")) } -pub fn expr_closure(pats: impl IntoIterator<Item = ast::Param>, expr: ast::Expr) -> ast::Expr { +pub fn expr_closure( + pats: impl IntoIterator<Item = ast::Param>, + expr: ast::Expr, +) -> ast::ClosureExpr { let params = pats.into_iter().join(", "); expr_from_text(&format!("|{params}| {expr}")) } pub fn expr_field(receiver: ast::Expr, field: &str) -> ast::Expr { expr_from_text(&format!("{receiver}.{field}")) } -pub fn expr_paren(expr: ast::Expr) -> ast::Expr { +pub fn expr_paren(expr: ast::Expr) -> ast::ParenExpr { expr_from_text(&format!("({expr})")) } pub fn expr_tuple(elements: impl IntoIterator<Item = ast::Expr>) -> ast::TupleExpr { @@ -1221,7 +1226,7 @@ pub fn meta_path(path: ast::Path) -> ast::Meta { pub fn token_tree( delimiter: SyntaxKind, - tt: Vec<NodeOrToken<ast::TokenTree, SyntaxToken>>, + tt: impl IntoIterator<Item = NodeOrToken<ast::TokenTree, SyntaxToken>>, ) -> ast::TokenTree { let (l_delimiter, r_delimiter) = match delimiter { T!['('] => ('(', ')'), @@ -1270,11 +1275,12 @@ pub mod tokens { use parser::Edition; - use crate::{ast, AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken}; + use crate::{AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken, ast}; pub(super) static SOURCE_FILE: LazyLock<Parse<SourceFile>> = LazyLock::new(|| { SourceFile::parse( - "use crate::foo; const C: <()>::Item = ( true && true , true || true , 1 != 1, 2 == 2, 3 < 3, 4 <= 4, 5 > 5, 6 >= 6, !true, *p, &p , &mut p, async { let _ @ [] })\n;\n\nimpl A for B where: {}", Edition::CURRENT, + "use crate::foo; const C: <()>::Item = ( true && true , true || true , 1 != 1, 2 == 2, 3 < 3, 4 <= 4, 5 > 5, 6 >= 6, !true, *p, &p , &mut p, async { let _ @ [] })\n;\n\nunsafe impl A for B where: {}", + Edition::CURRENT, ) }); |