diff options
Diffstat (limited to 'crates/syntax')
-rw-r--r-- | crates/syntax/src/ast/make.rs | 56 |
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`. |
26 | pub fn ty(text: &str) -> ast::Type { | 26 | pub 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 | } |
29 | pub fn ty_unit() -> ast::Type { | 29 | pub fn ty_unit() -> ast::Type { |
30 | ty("()") | 30 | ty("()") |
31 | } | 31 | } |
32 | // FIXME: handle types of length == 1 | ||
33 | pub 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 | ||
38 | pub 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 | } | ||
42 | pub fn ty_ref(target: ast::Type, exclusive: bool) -> ast::Type { | ||
43 | ty(&if exclusive { format!("&mut {}", target) } else { format!("&{}", target) }) | ||
44 | } | ||
32 | 45 | ||
33 | pub fn assoc_item_list() -> ast::AssocItemList { | 46 | pub 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 { | |||
175 | pub fn expr_continue() -> ast::Expr { | 188 | pub fn expr_continue() -> ast::Expr { |
176 | expr_from_text("continue") | 189 | expr_from_text("continue") |
177 | } | 190 | } |
178 | pub fn expr_break() -> ast::Expr { | 191 | pub 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 | } |
181 | pub fn expr_return() -> ast::Expr { | 197 | pub 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 | } |
184 | pub fn expr_match(expr: ast::Expr, match_arm_list: ast::MatchArmList) -> ast::Expr { | 203 | pub 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 { | |||
212 | pub fn expr_paren(expr: ast::Expr) -> ast::Expr { | 231 | pub fn expr_paren(expr: ast::Expr) -> ast::Expr { |
213 | expr_from_text(&format!("({})", expr)) | 232 | expr_from_text(&format!("({})", expr)) |
214 | } | 233 | } |
234 | pub 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 | } | ||
215 | fn expr_from_text(text: &str) -> ast::Expr { | 238 | fn 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 | } |
262 | pub 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 | ||
240 | pub fn wildcard_pat() -> ast::WildcardPat { | 270 | pub 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 | ||
359 | pub fn param(name: String, ty: String) -> ast::Param { | 389 | pub 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 | ||
363 | pub fn ret_type(ty: ast::Type) -> ast::RetType { | 393 | pub 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 | ||
367 | pub fn param_list(pats: impl IntoIterator<Item = ast::Param>) -> ast::ParamList { | 397 | pub 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 | ||
372 | pub fn generic_param(name: String, ty: Option<ast::TypeBoundList>) -> ast::GenericParam { | 410 | pub fn generic_param(name: String, ty: Option<ast::TypeBoundList>) -> ast::GenericParam { |