Skip to content

Commit cbe4c81

Browse files
committed
Unified: add tuple_pattern and sequence_condition; refine if-let/guard mapping
ast_types.yml additions: - tuple_pattern { element*: pattern } in the pattern supertype. - sequence_condition { stmt*: stmt, condition: condition } in the condition supertype. swift.rs: - Map Swift tuple destructuring (e.g. `let (a, b) = pair`) to the new tuple_pattern instead of synthesizing an apply_pattern. - if-let / guard-let: explicitly match the value_binding_pattern (the `let` keyword) and bind the source expression as the next condition child, so `let` no longer leaks into the output.
1 parent 3b7a53f commit cbe4c81

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

unified/extractor/ast_types.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ supertypes:
1818
condition:
1919
- expr_condition
2020
- let_pattern_condition
21+
- sequence_condition
2122
- unsupported_node
2223
pattern:
2324
- var_pattern
@@ -92,6 +93,13 @@ named:
9293
expr_condition:
9394
expr: expr
9495

96+
# A series of statements that are executed before evaluating the trailing condition.
97+
# Useful for languages where a conditional clause may be preceded by side-effecting
98+
# syntactic elements (e.g. binding clauses) that don't themselves form a condition.
99+
sequence_condition:
100+
stmt*: stmt
101+
condition: condition
102+
95103
# Evaluate 'expr' and match its result against 'pattern', and return true if it matches.
96104
# Variables bound by the pattern will be in scope within the 'true' branch controlled by this condition.
97105
let_pattern_condition:

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,50 +157,55 @@ fn translation_rules() -> Vec<yeast::Rule> {
157157
),
158158
// ---- Guard statement ----
159159
// `guard let x = e else { ... }` — currently only handles the
160-
// let-binding form, since plain `guard cond else { ... }` is not
161-
// exercised by any existing corpus test.
160+
// let-binding form. The Swift parser models the `let` keyword as a
161+
// `value_binding_pattern` child of `condition`, followed by an
162+
// unnamed `=` and the source expression.
162163
rule!(
163164
(guard_statement
164165
bound_identifier: (simple_identifier) @id
165-
condition: (_)* @cond_children
166+
condition: (value_binding_pattern)
167+
condition: (_) @value
166168
(else)
167169
(statements) @else_branch)
168170
=>
169171
(guard_if_stmt
170172
condition: (let_pattern_condition
171173
pattern: (var_pattern identifier: (identifier #{id}))
172-
value: {*cond_children.last().unwrap()})
174+
value: {value})
173175
else: {else_branch})
174176
),
175177
// ---- If statement ----
176178
// if-let binding (with optional else branch). The Swift parser puts
177-
// the bound name in `bound_identifier` and the source expression as
178-
// the last child of the multi-child `condition` field.
179+
// the bound name in `bound_identifier`, the `let` keyword as a
180+
// `value_binding_pattern` child of `condition`, and the source
181+
// expression as a separate child of `condition`.
179182
rule!(
180183
(if_statement
181184
bound_identifier: (simple_identifier) @id
182-
condition: (_)* @cond_children
185+
condition: (value_binding_pattern)
186+
condition: (_) @value
183187
(statements) @then
184188
(else)
185189
(_) @else_branch)
186190
=>
187191
(if_stmt
188192
condition: (let_pattern_condition
189193
pattern: (var_pattern identifier: (identifier #{id}))
190-
value: {*cond_children.last().unwrap()})
194+
value: {value})
191195
then: {then}
192196
else: {else_branch})
193197
),
194198
rule!(
195199
(if_statement
196200
bound_identifier: (simple_identifier) @id
197-
condition: (_)* @cond_children
201+
condition: (value_binding_pattern)
202+
condition: (_) @value
198203
(statements) @then)
199204
=>
200205
(if_stmt
201206
condition: (let_pattern_condition
202207
pattern: (var_pattern identifier: (identifier #{id}))
203-
value: {*cond_children.last().unwrap()})
208+
value: {value})
204209
then: {then})
205210
),
206211
// With explicit else branch (block or chained if).

0 commit comments

Comments
 (0)