From 7c9e4e10bc406176ba60bd40487ac0fc7ad451a3 Mon Sep 17 00:00:00 2001 From: alibektas Date: Mon, 17 Apr 2023 00:41:08 +0300 Subject: [PATCH 1/3] Add syntax::make::ty_alias The function is fully compliant with the specifications from the Rust Reference. --- crates/syntax/src/ast/make.rs | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 5aebe4cd9f53..719d92b87d9a 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -14,6 +14,8 @@ use stdx::{format_to, never}; use crate::{ast, utils::is_raw_identifier, AstNode, SourceFile, SyntaxKind, SyntaxToken}; +use super::WhereClause; + /// While the parent module defines basic atomic "constructors", the `ext` /// module defines shortcuts for common things. /// @@ -158,6 +160,52 @@ fn ty_from_text(text: &str) -> ast::Type { ast_from_text(&format!("type _T = {text};")) } +/** Related goto [link](https://doc.rust-lang.org/reference/items/type-aliases.html) + Type Alias syntax is + + ``` + TypeAlias : + type IDENTIFIER GenericParams? ( : TypeParamBounds )? WhereClause? ( = Type WhereClause?)? ; + ``` + + FIXME : ident should be of type ast::Ident +*/ +pub fn ty_alias( + ident: String, + generic_param_list: Option, + type_param_bounds: Option, + where_clause: Option, + assignment: Option<(ast::Type, Option)>, +) -> ast::TypeAlias { + let mut s = String::new(); + s.push_str(format!("type {}", ident.as_str()).as_str()); + + if let Some(list) = generic_param_list { + s.push_str(list.to_string().as_str()); + } + + if let Some(list) = type_param_bounds { + s.push_str(format!(" : {}", list.to_string().as_str()).as_str()); + } + + if let Some(cl) = where_clause { + s.push_str(format!(" {}", cl.to_string().as_str()).as_str()); + } + + if let Some(exp) = assignment { + if let Some(cl) = exp.1 { + s.push_str( + format!("= {} {}", exp.0.to_string().as_str(), cl.to_string().as_str()).as_str(), + ); + } else { + s.push_str(format!("= {}", exp.0.to_string().as_str()).as_str()); + } + } + + s.push_str(";"); + ast_from_text(s.as_str()) +} + pub fn assoc_item_list() -> ast::AssocItemList { ast_from_text("impl C for D {}") } From caa13b3f95c26db7450f6efa5371298ea650792b Mon Sep 17 00:00:00 2001 From: alibektas Date: Fri, 21 Apr 2023 22:09:11 +0300 Subject: [PATCH 2/3] Small changes in str formatting --- crates/syntax/src/ast/make.rs | 41 +++++++++++++++-------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 719d92b87d9a..74cf9888e9b8 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -14,8 +14,6 @@ use stdx::{format_to, never}; use crate::{ast, utils::is_raw_identifier, AstNode, SourceFile, SyntaxKind, SyntaxToken}; -use super::WhereClause; - /// While the parent module defines basic atomic "constructors", the `ext` /// module defines shortcuts for common things. /// @@ -160,50 +158,47 @@ fn ty_from_text(text: &str) -> ast::Type { ast_from_text(&format!("type _T = {text};")) } -/** Related goto [link](https://doc.rust-lang.org/reference/items/type-aliases.html) - Type Alias syntax is - - ``` - TypeAlias : - type IDENTIFIER GenericParams? ( : TypeParamBounds )? WhereClause? ( = Type WhereClause?)? ; - ``` - - FIXME : ident should be of type ast::Ident -*/ +/// Related goto [link](https://doc.rust-lang.org/reference/items/type-aliases.html) +/// Type Alias syntax is +/// +/// ``` +/// TypeAlias : +/// type IDENTIFIER GenericParams? ( : TypeParamBounds )? WhereClause? ( = Type WhereClause?)? ; +/// ``` +/// +/// FIXME : ident should be of type ast::Ident pub fn ty_alias( - ident: String, + ident: &str, generic_param_list: Option, type_param_bounds: Option, - where_clause: Option, + where_clause: Option, assignment: Option<(ast::Type, Option)>, ) -> ast::TypeAlias { let mut s = String::new(); - s.push_str(format!("type {}", ident.as_str()).as_str()); + s.push_str(&format!("type {}", ident)); if let Some(list) = generic_param_list { - s.push_str(list.to_string().as_str()); + s.push_str(&list.to_string()); } if let Some(list) = type_param_bounds { - s.push_str(format!(" : {}", list.to_string().as_str()).as_str()); + s.push_str(&format!(" : {}", &list.to_string())); } if let Some(cl) = where_clause { - s.push_str(format!(" {}", cl.to_string().as_str()).as_str()); + s.push_str(&format!(" {}", &cl.to_string())); } if let Some(exp) = assignment { if let Some(cl) = exp.1 { - s.push_str( - format!("= {} {}", exp.0.to_string().as_str(), cl.to_string().as_str()).as_str(), - ); + s.push_str(&format!("= {} {}", &exp.0.to_string(), &cl.to_string())); } else { - s.push_str(format!("= {}", exp.0.to_string().as_str()).as_str()); + s.push_str(&format!("= {}", &exp.0.to_string())); } } s.push_str(";"); - ast_from_text(s.as_str()) + ast_from_text(&s) } pub fn assoc_item_list() -> ast::AssocItemList { From e275f77b2cb070e84f76e9e493560d2d9a531bff Mon Sep 17 00:00:00 2001 From: alibektas Date: Sat, 22 Apr 2023 13:23:52 +0200 Subject: [PATCH 3/3] Comment clean-up. Use display where possible --- crates/syntax/src/ast/make.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 74cf9888e9b8..48f52c904cfb 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -159,14 +159,12 @@ fn ty_from_text(text: &str) -> ast::Type { } /// Related goto [link](https://doc.rust-lang.org/reference/items/type-aliases.html) -/// Type Alias syntax is -/// -/// ``` -/// TypeAlias : -/// type IDENTIFIER GenericParams? ( : TypeParamBounds )? WhereClause? ( = Type WhereClause?)? ; -/// ``` -/// -/// FIXME : ident should be of type ast::Ident +/// Type Alias syntax is +/// ``` +/// TypeAlias : +/// type IDENTIFIER GenericParams? ( : TypeParamBounds )? WhereClause? ( = Type WhereClause?)? ; +/// ``` +/// FIXME : ident should be of type ast::Ident pub fn ty_alias( ident: &str, generic_param_list: Option, @@ -182,7 +180,7 @@ pub fn ty_alias( } if let Some(list) = type_param_bounds { - s.push_str(&format!(" : {}", &list.to_string())); + s.push_str(&format!(" : {}", &list)); } if let Some(cl) = where_clause {