@@ -33,6 +33,21 @@ pub fn record_field(name: ast::NameRef, expr: Option<ast::Expr>) -> ast::RecordF
33
33
}
34
34
}
35
35
36
+ pub fn block_expr (
37
+ stmts : impl IntoIterator < Item = ast:: Stmt > ,
38
+ tail_expr : Option < ast:: Expr > ,
39
+ ) -> ast:: BlockExpr {
40
+ let mut text = "{\n " . to_string ( ) ;
41
+ for stmt in stmts. into_iter ( ) {
42
+ text += & format ! ( " {}\n " , stmt. syntax( ) ) ;
43
+ }
44
+ if let Some ( tail_expr) = tail_expr {
45
+ text += & format ! ( " {}\n " , tail_expr. syntax( ) )
46
+ }
47
+ text += "}" ;
48
+ ast_from_text ( & format ! ( "fn f() {}" , text) )
49
+ }
50
+
36
51
pub fn block_from_expr ( e : ast:: Expr ) -> ast:: Block {
37
52
return from_text ( & format ! ( "{{ {} }}" , e. syntax( ) ) ) ;
38
53
@@ -62,6 +77,9 @@ pub fn expr_return() -> ast::Expr {
62
77
pub fn expr_match ( expr : ast:: Expr , match_arm_list : ast:: MatchArmList ) -> ast:: Expr {
63
78
expr_from_text ( & format ! ( "match {} {}" , expr. syntax( ) , match_arm_list. syntax( ) ) )
64
79
}
80
+ pub fn expr_if ( condition : ast:: Expr , then_branch : ast:: BlockExpr ) -> ast:: Expr {
81
+ expr_from_text ( & format ! ( "if {} {}" , condition. syntax( ) , then_branch. syntax( ) ) )
82
+ }
65
83
pub fn expr_prefix ( op : SyntaxKind , expr : ast:: Expr ) -> ast:: Expr {
66
84
let token = token ( op) ;
67
85
expr_from_text ( & format ! ( "{}{}" , token, expr. syntax( ) ) )
@@ -162,21 +180,16 @@ pub fn where_clause(preds: impl IntoIterator<Item = ast::WherePred>) -> ast::Whe
162
180
}
163
181
}
164
182
165
- pub fn if_expression ( condition : ast:: Expr , statement : & str ) -> ast:: IfExpr {
166
- ast_from_text ( & format ! (
167
- "fn f() {{ if !{} {{\n {}\n }}\n }}" ,
168
- condition. syntax( ) . text( ) ,
169
- statement
170
- ) )
171
- }
172
-
173
183
pub fn let_stmt ( pattern : ast:: Pat , initializer : Option < ast:: Expr > ) -> ast:: LetStmt {
174
184
let text = match initializer {
175
185
Some ( it) => format ! ( "let {} = {};" , pattern. syntax( ) , it. syntax( ) ) ,
176
186
None => format ! ( "let {};" , pattern. syntax( ) ) ,
177
187
} ;
178
188
ast_from_text ( & format ! ( "fn f() {{ {} }}" , text) )
179
189
}
190
+ pub fn expr_stmt ( expr : ast:: Expr ) -> ast:: ExprStmt {
191
+ ast_from_text ( & format ! ( "fn f() {{ {}; }}" , expr. syntax( ) ) )
192
+ }
180
193
181
194
pub fn token ( kind : SyntaxKind ) -> SyntaxToken {
182
195
tokens:: SOURCE_FILE
0 commit comments