From 680a0d54e4d2d474ae41f4f4a95c749495a02883 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 9 May 2021 19:11:42 +0300 Subject: internal: fix make API --- crates/syntax/src/ast/make.rs | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'crates/syntax/src/ast') diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 2289d8f3e..c39e248ce 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -10,7 +10,7 @@ //! `parse(format!())` we use internally is an implementation detail -- long //! term, it will be replaced with direct tree manipulation. use itertools::Itertools; -use stdx::format_to; +use stdx::{format_to, never}; use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken}; @@ -22,6 +22,16 @@ pub fn name_ref(text: &str) -> ast::NameRef { ast_from_text(&format!("fn f() {{ {}{}; }}", raw_ident_esc(text), text)) } +pub fn lifetime(text: &str) -> ast::Lifetime { + let mut text = text; + let tmp; + if never!(!text.starts_with('\'')) { + tmp = format!("'{}", text); + text = &tmp; + } + ast_from_text(&format!("fn f<{}>() {{ }}", text)) +} + fn raw_ident_esc(ident: &str) -> &'static str { let is_keyword = parser::SyntaxKind::from_keyword(ident).is_some(); if is_keyword && !matches!(ident, "self" | "crate" | "super" | "Self") { @@ -34,10 +44,13 @@ fn raw_ident_esc(ident: &str) -> &'static str { // FIXME: replace stringly-typed constructor with a family of typed ctors, a-la // `expr_xxx`. pub fn ty(text: &str) -> ast::Type { - ast_from_text(&format!("fn f() -> {} {{}}", text)) + ty_from_text(text) } pub fn ty_unit() -> ast::Type { - ty("()") + ty_from_text("()") +} +pub fn ty_bool() -> ast::Type { + ty_path(path_unqualified(path_segment(name_ref("bool")))) } pub fn ty_tuple(types: impl IntoIterator) -> ast::Type { let mut count: usize = 0; @@ -46,15 +59,21 @@ pub fn ty_tuple(types: impl IntoIterator) -> ast::Type { contents.push(','); } - ty(&format!("({})", contents)) + ty_from_text(&format!("({})", contents)) } // FIXME: handle path to type pub fn ty_generic(name: ast::NameRef, types: impl IntoIterator) -> ast::Type { let contents = types.into_iter().join(", "); - ty(&format!("{}<{}>", name, contents)) + ty_from_text(&format!("{}<{}>", name, contents)) } pub fn ty_ref(target: ast::Type, exclusive: bool) -> ast::Type { - ty(&if exclusive { format!("&mut {}", target) } else { format!("&{}", target) }) + ty_from_text(&if exclusive { format!("&mut {}", target) } else { format!("&{}", target) }) +} +pub fn ty_path(path: ast::Path) -> ast::Type { + ty_from_text(&path.to_string()) +} +fn ty_from_text(text: &str) -> ast::Type { + ast_from_text(&format!("type _T = {};", text)) } pub fn assoc_item_list() -> ast::AssocItemList { @@ -475,8 +494,8 @@ pub fn param_list( }; ast_from_text(&list) } -// FIXME: s/&str/ast:Name -pub fn generic_param(name: &str, ty: Option) -> ast::GenericParam { + +pub fn type_param(name: ast::Name, ty: Option) -> ast::TypeParam { let bound = match ty { Some(it) => format!(": {}", it), None => String::new(), @@ -484,6 +503,10 @@ pub fn generic_param(name: &str, ty: Option) -> ast::Generic ast_from_text(&format!("fn f<{}{}>() {{ }}", name, bound)) } +pub fn lifetime_param(lifetime: ast::Lifetime) -> ast::LifetimeParam { + ast_from_text(&format!("fn f<{}>() {{ }}", lifetime)) +} + pub fn generic_param_list( pats: impl IntoIterator, ) -> ast::GenericParamList { -- cgit v1.2.3