diff options
| author | alibektas <bektasali@protonmail.com> | 2023-04-17 00:41:08 +0300 |
|---|---|---|
| committer | alibektas <bektasali@protonmail.com> | 2023-04-21 21:19:36 +0300 |
| commit | 7c9e4e10bc406176ba60bd40487ac0fc7ad451a3 (patch) | |
| tree | 577eca4de687a85d9809cd379d4cbfd054244875 | |
| parent | a6464392c15fa8788215d669c4c0b1e46bcadeea (diff) | |
| download | rust-7c9e4e10bc406176ba60bd40487ac0fc7ad451a3.tar.gz rust-7c9e4e10bc406176ba60bd40487ac0fc7ad451a3.zip | |
Add syntax::make::ty_alias
The function is fully compliant with the specifications from the Rust Reference.
| -rw-r--r-- | crates/syntax/src/ast/make.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 5aebe4cd9f5..719d92b87d9 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<ast::GenericParamList>, + type_param_bounds: Option<ast::TypeParam>, + where_clause: Option<WhereClause>, + assignment: Option<(ast::Type, Option<ast::WhereClause>)>, +) -> 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 {}") } |
