diff options
Diffstat (limited to 'crates/syntax/src/ast/make.rs')
-rw-r--r-- | crates/syntax/src/ast/make.rs | 53 |
1 files changed, 47 insertions, 6 deletions
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index c6a7b99b7..42da09606 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs | |||
@@ -29,9 +29,13 @@ pub fn ty(text: &str) -> ast::Type { | |||
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 { | 32 | pub fn ty_tuple(types: impl IntoIterator<Item = ast::Type>) -> ast::Type { |
34 | let contents = types.into_iter().join(", "); | 33 | let mut count: usize = 0; |
34 | let mut contents = types.into_iter().inspect(|_| count += 1).join(", "); | ||
35 | if count == 1 { | ||
36 | contents.push(','); | ||
37 | } | ||
38 | |||
35 | ty(&format!("({})", contents)) | 39 | ty(&format!("({})", contents)) |
36 | } | 40 | } |
37 | // FIXME: handle path to type | 41 | // FIXME: handle path to type |
@@ -133,6 +137,17 @@ pub fn use_(visibility: Option<ast::Visibility>, use_tree: ast::UseTree) -> ast: | |||
133 | ast_from_text(&format!("{}use {};", visibility, use_tree)) | 137 | ast_from_text(&format!("{}use {};", visibility, use_tree)) |
134 | } | 138 | } |
135 | 139 | ||
140 | pub fn record_expr(path: ast::Path, fields: ast::RecordExprFieldList) -> ast::RecordExpr { | ||
141 | ast_from_text(&format!("fn f() {{ {} {} }}", path, fields)) | ||
142 | } | ||
143 | |||
144 | pub fn record_expr_field_list( | ||
145 | fields: impl IntoIterator<Item = ast::RecordExprField>, | ||
146 | ) -> ast::RecordExprFieldList { | ||
147 | let fields = fields.into_iter().join(", "); | ||
148 | ast_from_text(&format!("fn f() {{ S {{ {} }} }}", fields)) | ||
149 | } | ||
150 | |||
136 | pub fn record_expr_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordExprField { | 151 | pub fn record_expr_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordExprField { |
137 | return match expr { | 152 | return match expr { |
138 | Some(expr) => from_text(&format!("{}: {}", name, expr)), | 153 | Some(expr) => from_text(&format!("{}: {}", name, expr)), |
@@ -290,13 +305,23 @@ pub fn wildcard_pat() -> ast::WildcardPat { | |||
290 | } | 305 | } |
291 | } | 306 | } |
292 | 307 | ||
308 | pub fn literal_pat(lit: &str) -> ast::LiteralPat { | ||
309 | return from_text(lit); | ||
310 | |||
311 | fn from_text(text: &str) -> ast::LiteralPat { | ||
312 | ast_from_text(&format!("fn f() {{ match x {{ {} => {{}} }} }}", text)) | ||
313 | } | ||
314 | } | ||
315 | |||
293 | /// Creates a tuple of patterns from an iterator of patterns. | 316 | /// Creates a tuple of patterns from an iterator of patterns. |
294 | /// | 317 | /// |
295 | /// Invariant: `pats` must be length > 1 | 318 | /// Invariant: `pats` must be length > 0 |
296 | /// | ||
297 | /// FIXME handle `pats` length == 1 | ||
298 | pub fn tuple_pat(pats: impl IntoIterator<Item = ast::Pat>) -> ast::TuplePat { | 319 | pub fn tuple_pat(pats: impl IntoIterator<Item = ast::Pat>) -> ast::TuplePat { |
299 | let pats_str = pats.into_iter().map(|p| p.to_string()).join(", "); | 320 | let mut count: usize = 0; |
321 | let mut pats_str = pats.into_iter().inspect(|_| count += 1).join(", "); | ||
322 | if count == 1 { | ||
323 | pats_str.push(','); | ||
324 | } | ||
300 | return from_text(&format!("({})", pats_str)); | 325 | return from_text(&format!("({})", pats_str)); |
301 | 326 | ||
302 | fn from_text(text: &str) -> ast::TuplePat { | 327 | fn from_text(text: &str) -> ast::TuplePat { |
@@ -325,6 +350,21 @@ pub fn record_pat(path: ast::Path, pats: impl IntoIterator<Item = ast::Pat>) -> | |||
325 | } | 350 | } |
326 | } | 351 | } |
327 | 352 | ||
353 | pub fn record_pat_with_fields(path: ast::Path, fields: ast::RecordPatFieldList) -> ast::RecordPat { | ||
354 | ast_from_text(&format!("fn f({} {}: ()))", path, fields)) | ||
355 | } | ||
356 | |||
357 | pub fn record_pat_field_list( | ||
358 | fields: impl IntoIterator<Item = ast::RecordPatField>, | ||
359 | ) -> ast::RecordPatFieldList { | ||
360 | let fields = fields.into_iter().join(", "); | ||
361 | ast_from_text(&format!("fn f(S {{ {} }}: ()))", fields)) | ||
362 | } | ||
363 | |||
364 | pub fn record_pat_field(name_ref: ast::NameRef, pat: ast::Pat) -> ast::RecordPatField { | ||
365 | ast_from_text(&format!("fn f(S {{ {}: {} }}: ()))", name_ref, pat)) | ||
366 | } | ||
367 | |||
328 | /// Returns a `BindPat` if the path has just one segment, a `PathPat` otherwise. | 368 | /// Returns a `BindPat` if the path has just one segment, a `PathPat` otherwise. |
329 | pub fn path_pat(path: ast::Path) -> ast::Pat { | 369 | pub fn path_pat(path: ast::Path) -> ast::Pat { |
330 | return from_text(&path.to_string()); | 370 | return from_text(&path.to_string()); |
@@ -592,6 +632,7 @@ pub mod tokens { | |||
592 | SOURCE_FILE | 632 | SOURCE_FILE |
593 | .tree() | 633 | .tree() |
594 | .syntax() | 634 | .syntax() |
635 | .clone_for_update() | ||
595 | .descendants_with_tokens() | 636 | .descendants_with_tokens() |
596 | .filter_map(|it| it.into_token()) | 637 | .filter_map(|it| it.into_token()) |
597 | .find(|it| it.kind() == WHITESPACE && it.text() == "\n\n") | 638 | .find(|it| it.kind() == WHITESPACE && it.text() == "\n\n") |