Skip to content

Commit d95f6c6

Browse files
committed
unified: "Coerce" expr to pattern
This commits demonstrates the idea we're going to use in later commits, deliberately only done for one expr type so far.
1 parent 8358fd1 commit d95f6c6

2 files changed

Lines changed: 18 additions & 16 deletions

File tree

unified/extractor/src/languages/swift/swift.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,12 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
283283
// can't match on token text, so a small Rust block reads the spelling
284284
// and routes to `compound_assign_expr` or `binary_expr`. The operator
285285
// is captured raw (`@@op`) to read its spelling.
286+
rule!((infixOperatorExpr) @@expr where ctx.in_pattern =>
287+
pattern {
288+
ctx.in_pattern = false;
289+
tree!((expr_equality_pattern expr: {ctx.translate(expr)?}))
290+
}
291+
),
286292
rule!(
287293
(infixOperatorExpr leftOperand: @l operator: (binaryOperatorExpr) @@op rightOperand: @r)
288294
=>

unified/extractor/tests/corpus/swift/control-flow/switch-expression-pattern.output

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -291,33 +291,29 @@ top_level
291291
expr:
292292
name_expr
293293
identifier: identifier "y"
294-
binary_expr <-- ERROR: The field or_pattern.pattern should contain pattern, but got binary_expr
295-
left:
296-
expr_equality_pattern <-- ERROR: The field binary_expr.left should contain expr, but got expr_equality_pattern
297-
expr:
294+
expr_equality_pattern
295+
expr:
296+
binary_expr
297+
left:
298298
name_expr
299299
identifier: identifier "y"
300-
operator: infix_operator "+"
301-
right:
302-
expr_equality_pattern <-- ERROR: The field binary_expr.right should contain expr, but got expr_equality_pattern
303-
expr: int_literal "1"
300+
operator: infix_operator "+"
301+
right: int_literal "1"
304302
unary_expr <-- ERROR: The field or_pattern.pattern should contain pattern, but got unary_expr
305303
operand:
306304
expr_equality_pattern <-- ERROR: The field unary_expr.operand should contain expr, but got expr_equality_pattern
307305
expr:
308306
name_expr
309307
identifier: identifier "y"
310308
operator: prefix_operator "-"
311-
binary_expr <-- ERROR: The field or_pattern.pattern should contain pattern, but got binary_expr
312-
left:
313-
expr_equality_pattern <-- ERROR: The field binary_expr.left should contain expr, but got expr_equality_pattern
314-
expr:
309+
expr_equality_pattern
310+
expr:
311+
binary_expr
312+
left:
315313
name_expr
316314
identifier: identifier "y"
317-
operator: infix_operator "..."
318-
right:
319-
expr_equality_pattern <-- ERROR: The field binary_expr.right should contain expr, but got expr_equality_pattern
320-
expr:
315+
operator: infix_operator "..."
316+
right:
321317
name_expr
322318
identifier: identifier "z"
323319
string_interpolation_expr <-- ERROR: The field or_pattern.pattern should contain pattern, but got string_interpolation_expr

0 commit comments

Comments
 (0)