Skip to content

Commit 3522f35

Browse files
committed
unified/swift: add collections, optionals/errors
1 parent 938396a commit 3522f35

5 files changed

Lines changed: 144 additions & 10 deletions

File tree

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,81 @@ fn translation_rules() -> Vec<yeast::Rule> {
558558
let name = __yeast_ctx.literal("identifier", &text[..text.len() - 1]);
559559
vec![__yeast_ctx.node("labeled_stmt", vec![("label", vec![name]), ("stmt", vec![stmt.into()])])]
560560
}}),
561+
// ---- Collections ----
562+
// Array literal
563+
rule!((array_literal element: _* @elems) => (array_literal element: {..elems})),
564+
// Empty array literal
565+
rule!((array_literal) => (array_literal)),
566+
// Dictionary literal — zip keys and values into key_value_pairs
567+
rule!(
568+
(dictionary_literal key: _* @keys value: _* @vals)
569+
=>
570+
(map_literal element: {..{
571+
keys.iter().zip(vals.iter()).map(|(&k, &v)| {
572+
let k_id: usize = k.into();
573+
let v_id: usize = v.into();
574+
__yeast_ctx.node("key_value_pair", vec![
575+
("key", vec![k_id]),
576+
("value", vec![v_id]),
577+
])
578+
}).collect::<Vec<_>>()
579+
}})
580+
),
581+
rule!((dictionary_literal element: _* @elems) => (map_literal element: {..elems})),
582+
rule!((dictionary_literal_item key: @k value: @v) => (key_value_pair key: {k} value: {v})),
583+
// ---- Optionals and errors ----
584+
// Optional chaining — unwrap the marker
585+
rule!((optional_chain_marker expr: @inner) => {inner}),
586+
// try/try?/try! expr → unary_expr with operator "try", "try?" or "try!"
587+
rule!((try_expression (try_operator) @op expr: @inner) => (unary_expr operator: (prefix_operator #{op}) operand: {inner})),
588+
rule!((try_expression operator: (try_operator) @op expr: @inner) => (unary_expr operator: (prefix_operator #{op}) operand: {inner})),
589+
// Do-catch → try_expr
590+
rule!(
591+
(do_statement body: (block statement: _* @body) catch: (catch_block)* @catches)
592+
=>
593+
(try_expr
594+
body: (block stmt: {..body})
595+
catch_clause: {..catches})
596+
),
597+
// Catch block with bound identifier; optional where-clause guard.
598+
rule!(
599+
(catch_block
600+
keyword: (catch_keyword)
601+
error: @pattern
602+
where: (where_clause expr: @guard)?
603+
body: (block statement: _* @body))
604+
=>
605+
(catch_clause
606+
pattern: {pattern}
607+
guard: {..guard}
608+
body: (block stmt: {..body}))
609+
),
610+
// Catch block without error binding
611+
rule!(
612+
(catch_block keyword: (catch_keyword) body: (block statement: _* @body))
613+
=>
614+
(catch_clause body: (block stmt: {..body}))
615+
),
616+
// Empty catch block: catch {}
617+
rule!(
618+
(catch_block (catch_keyword))
619+
=>
620+
(catch_clause body: (block))
621+
),
622+
// Catch block with unhandled pattern — preserve pattern; optional body.
623+
rule!(
624+
(catch_block keyword: (catch_keyword) error: @pat body: (block statement: _* @body))
625+
=>
626+
(catch_clause
627+
pattern: {pat}
628+
body: (block stmt: {..body}))
629+
),
630+
// As expression (type cast) — as?, as!
631+
rule!((as_expression (as_operator) @op expr: @val type: @ty) => (type_cast_expr expr: {val} operator: (infix_operator #{op}) type: {ty})),
632+
// Check expression (`x is T`) → type_test_expr
633+
rule!((check_expression op: @op target: @val type: @ty) => (type_test_expr expr: {val} operator: (infix_operator #{op}) type: {ty})),
634+
// Await expression → unary_expr with operator "await"
635+
rule!((await_expression expr: @val) => (unary_expr operator: (prefix_operator "await") operand: {val})),
561636
// ---- Fallbacks ----
562637
rule!(
563638
(_)

unified/extractor/tests/corpus/swift/closures.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ top_level
245245
call_expr
246246
callee:
247247
member_access_expr
248-
base: unsupported_node "self?"
248+
base: unsupported_node "self"
249249
member: identifier "doThing"
250250
capture_declaration:
251251
variable_declaration

unified/extractor/tests/corpus/swift/collections.txt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ top_level
3535
pattern:
3636
name_pattern
3737
identifier: identifier "xs"
38-
value: unsupported_node "[1, 2, 3]"
38+
value:
39+
array_literal
40+
element:
41+
int_literal "1"
42+
int_literal "2"
43+
int_literal "3"
3944

4045
===
4146
Empty array literal with type
@@ -84,7 +89,7 @@ top_level
8489
name_pattern
8590
identifier: identifier "xs"
8691
type: unsupported_node ": [Int]"
87-
value: unsupported_node "[]"
92+
value: array_literal "[]"
8893

8994
===
9095
Dictionary literal
@@ -130,7 +135,7 @@ top_level
130135
pattern:
131136
name_pattern
132137
identifier: identifier "d"
133-
value: unsupported_node "[\"a\": 1, \"b\": 2]"
138+
value: map_literal "[\"a\": 1, \"b\": 2]"
134139

135140
===
136141
Set literal
@@ -188,7 +193,12 @@ top_level
188193
name_pattern
189194
identifier: identifier "s"
190195
type: unsupported_node ": Set<Int>"
191-
value: unsupported_node "[1, 2, 3]"
196+
value:
197+
array_literal
198+
element:
199+
int_literal "1"
200+
int_literal "2"
201+
int_literal "3"
192202

193203
===
194204
Tuple literal

unified/extractor/tests/corpus/swift/loops.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ top_level
5555
pattern:
5656
name_pattern
5757
identifier: identifier "x"
58-
iterable: unsupported_node "[1, 2, 3]"
58+
iterable:
59+
array_literal
60+
element:
61+
int_literal "1"
62+
int_literal "2"
63+
int_literal "3"
5964

6065
===
6166
For-in over range

unified/extractor/tests/corpus/swift/optionals-and-errors.txt

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,12 @@ top_level
9292
identifier: identifier "n"
9393
value:
9494
member_access_expr
95-
base: unsupported_node "obj?.foo?"
95+
base:
96+
member_access_expr
97+
base:
98+
name_expr
99+
identifier: identifier "obj"
100+
member: identifier "foo"
96101
member: identifier "bar"
97102

98103
===
@@ -274,7 +279,32 @@ source_file
274279
top_level
275280
body:
276281
block
277-
stmt: unsupported_node "do {\n try foo()\n} catch {\n print(error)\n}"
282+
stmt:
283+
try_expr
284+
body:
285+
block
286+
stmt:
287+
unary_expr
288+
operand:
289+
call_expr
290+
callee:
291+
name_expr
292+
identifier: identifier "foo"
293+
operator: prefix_operator "try"
294+
catch_clause:
295+
catch_clause
296+
body:
297+
block
298+
stmt:
299+
call_expr
300+
argument:
301+
argument
302+
value:
303+
name_expr
304+
identifier: identifier "error"
305+
callee:
306+
name_expr
307+
identifier: identifier "print"
278308

279309
===
280310
Try? expression
@@ -318,7 +348,14 @@ top_level
318348
pattern:
319349
name_pattern
320350
identifier: identifier "result"
321-
value: unsupported_node "try? foo()"
351+
value:
352+
unary_expr
353+
operand:
354+
call_expr
355+
callee:
356+
name_expr
357+
identifier: identifier "foo"
358+
operator: prefix_operator "try?"
322359

323360
===
324361
Try! expression
@@ -362,4 +399,11 @@ top_level
362399
pattern:
363400
name_pattern
364401
identifier: identifier "result"
365-
value: unsupported_node "try! foo()"
402+
value:
403+
unary_expr
404+
operand:
405+
call_expr
406+
callee:
407+
name_expr
408+
identifier: identifier "foo"
409+
operator: prefix_operator "try!"

0 commit comments

Comments
 (0)