aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src
diff options
context:
space:
mode:
authorVladyslav Katasonov <[email protected]>2021-02-10 02:50:03 +0000
committerVladyslav Katasonov <[email protected]>2021-02-13 19:04:52 +0000
commitf345d1772ab3827fbc3e31428b0d9479cab0ea39 (patch)
tree6dcbe05090edffb1de7d5ac2c43785e9344633b4 /crates/syntax/src
parent0ad3d7938f18dd12df6b66a12656558b166c8d7f (diff)
handle return, break and continue when extracting function
Diffstat (limited to 'crates/syntax/src')
-rw-r--r--crates/syntax/src/ast/make.rs56
1 files changed, 47 insertions, 9 deletions
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs
index 1da5a125e..5f6b96c23 100644
--- a/crates/syntax/src/ast/make.rs
+++ b/crates/syntax/src/ast/make.rs
@@ -24,11 +24,24 @@ pub fn name_ref(text: &str) -> ast::NameRef {
24// FIXME: replace stringly-typed constructor with a family of typed ctors, a-la 24// FIXME: replace stringly-typed constructor with a family of typed ctors, a-la
25// `expr_xxx`. 25// `expr_xxx`.
26pub fn ty(text: &str) -> ast::Type { 26pub fn ty(text: &str) -> ast::Type {
27 ast_from_text(&format!("impl {} for D {{}};", text)) 27 ast_from_text(&format!("fn f() -> {} {{}}", text))
28} 28}
29pub fn ty_unit() -> ast::Type { 29pub fn ty_unit() -> ast::Type {
30 ty("()") 30 ty("()")
31} 31}
32// FIXME: handle types of length == 1
33pub fn ty_tuple(types: impl IntoIterator<Item = ast::Type>) -> ast::Type {
34 let contents = types.into_iter().join(", ");
35 ty(&format!("({})", contents))
36}
37// FIXME: handle path to type
38pub fn ty_generic(name: ast::NameRef, types: impl IntoIterator<Item = ast::Type>) -> ast::Type {
39 let contents = types.into_iter().join(", ");
40 ty(&format!("{}<{}>", name, contents))
41}
42pub fn ty_ref(target: ast::Type, exclusive: bool) -> ast::Type {
43 ty(&if exclusive { format!("&mut {}", target) } else { format!("&{}", target) })
44}
32 45
33pub fn assoc_item_list() -> ast::AssocItemList { 46pub fn assoc_item_list() -> ast::AssocItemList {
34 ast_from_text("impl C for D {};") 47 ast_from_text("impl C for D {};")
@@ -175,11 +188,17 @@ pub fn expr_path(path: ast::Path) -> ast::Expr {
175pub fn expr_continue() -> ast::Expr { 188pub fn expr_continue() -> ast::Expr {
176 expr_from_text("continue") 189 expr_from_text("continue")
177} 190}
178pub fn expr_break() -> ast::Expr { 191pub fn expr_break(expr: Option<ast::Expr>) -> ast::Expr {
179 expr_from_text("break") 192 match expr {
193 Some(expr) => expr_from_text(&format!("break {}", expr)),
194 None => expr_from_text("break"),
195 }
180} 196}
181pub fn expr_return() -> ast::Expr { 197pub fn expr_return(expr: Option<ast::Expr>) -> ast::Expr {
182 expr_from_text("return") 198 match expr {
199 Some(expr) => expr_from_text(&format!("return {}", expr)),
200 None => expr_from_text("return"),
201 }
183} 202}
184pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr { 203pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr {
185 expr_from_text(&format!("match {} {}", expr, match_arm_list)) 204 expr_from_text(&format!("match {} {}", expr, match_arm_list))
@@ -212,6 +231,10 @@ pub fn expr_ref(expr: ast::Expr, exclusive: bool) -> ast::Expr {
212pub fn expr_paren(expr: ast::Expr) -> ast::Expr { 231pub fn expr_paren(expr: ast::Expr) -> ast::Expr {
213 expr_from_text(&format!("({})", expr)) 232 expr_from_text(&format!("({})", expr))
214} 233}
234pub fn expr_tuple(elements: impl IntoIterator<Item = ast::Expr>) -> ast::Expr {
235 let expr = elements.into_iter().format(", ");
236 expr_from_text(&format!("({})", expr))
237}
215fn expr_from_text(text: &str) -> ast::Expr { 238fn expr_from_text(text: &str) -> ast::Expr {
216 ast_from_text(&format!("const C: () = {};", text)) 239 ast_from_text(&format!("const C: () = {};", text))
217} 240}
@@ -236,6 +259,13 @@ pub fn ident_pat(name: ast::Name) -> ast::IdentPat {
236 ast_from_text(&format!("fn f({}: ())", text)) 259 ast_from_text(&format!("fn f({}: ())", text))
237 } 260 }
238} 261}
262pub fn ident_mut_pat(name: ast::Name) -> ast::IdentPat {
263 return from_text(name.text());
264
265 fn from_text(text: &str) -> ast::IdentPat {
266 ast_from_text(&format!("fn f(mut {}: ())", text))
267 }
268}
239 269
240pub fn wildcard_pat() -> ast::WildcardPat { 270pub fn wildcard_pat() -> ast::WildcardPat {
241 return from_text("_"); 271 return from_text("_");
@@ -356,17 +386,25 @@ pub fn token(kind: SyntaxKind) -> SyntaxToken {
356 .unwrap_or_else(|| panic!("unhandled token: {:?}", kind)) 386 .unwrap_or_else(|| panic!("unhandled token: {:?}", kind))
357} 387}
358 388
359pub fn param(name: String, ty: String) -> ast::Param { 389pub fn param(pat: ast::Pat, ty: ast::Type) -> ast::Param {
360 ast_from_text(&format!("fn f({}: {}) {{ }}", name, ty)) 390 ast_from_text(&format!("fn f({}: {}) {{ }}", pat, ty))
361} 391}
362 392
363pub fn ret_type(ty: ast::Type) -> ast::RetType { 393pub fn ret_type(ty: ast::Type) -> ast::RetType {
364 ast_from_text(&format!("fn f() -> {} {{ }}", ty)) 394 ast_from_text(&format!("fn f() -> {} {{ }}", ty))
365} 395}
366 396
367pub fn param_list(pats: impl IntoIterator<Item = ast::Param>) -> ast::ParamList { 397pub fn param_list(
398 self_param: Option<ast::SelfParam>,
399 pats: impl IntoIterator<Item = ast::Param>,
400) -> ast::ParamList {
368 let args = pats.into_iter().join(", "); 401 let args = pats.into_iter().join(", ");
369 ast_from_text(&format!("fn f({}) {{ }}", args)) 402 let list = match self_param {
403 Some(self_param) if args.is_empty() => format!("fn f({}) {{ }}", self_param),
404 Some(self_param) => format!("fn f({}, {}) {{ }}", self_param, args),
405 None => format!("fn f({}) {{ }}", args),
406 };
407 ast_from_text(&list)
370} 408}
371 409
372pub fn generic_param(name: String, ty: Option<ast::TypeBoundList>) -> ast::GenericParam { 410pub fn generic_param(name: String, ty: Option<ast::TypeBoundList>) -> ast::GenericParam {