Skip to content

Commit 0e9d17b

Browse files
committed
unified/swift: add top-level normalization and fallback scaffold
1 parent 6c74cd3 commit 0e9d17b

12 files changed

Lines changed: 185 additions & 6 deletions

File tree

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
use codeql_extractor::extractor::simple;
2-
use yeast::{build::BuildCtx, rule, DesugaringConfig, PhaseKind};
2+
use yeast::{rule, DesugaringConfig, PhaseKind};
33

44
fn translation_rules() -> Vec<yeast::Rule> {
55
vec![
6+
// ---- Top-level ----
7+
// Capture all top-level statements, including unnamed tokens like `nil`.
8+
rule!(
9+
(source_file statement: _* @children)
10+
=>
11+
(top_level
12+
body: (block stmt: {..children})
13+
)
14+
),
15+
// Declarations may be wrapped in local/global wrapper nodes.
16+
rule!((global_declaration _ @inner) => {inner}),
17+
rule!((local_declaration _ @inner) => {inner}),
618
// ---- Fallbacks ----
719
rule!(
820
(_)

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ source_file
5050

5151
top_level
5252
body:
53+
block
54+
stmt: unsupported_node "let f = { (x: Int) -> Int in x * 2 }"
5355

5456
===
5557
Closure with shorthand parameters
@@ -82,6 +84,8 @@ source_file
8284

8385
top_level
8486
body:
87+
block
88+
stmt: unsupported_node "let f = { $0 + $1 }"
8589

8690
===
8791
Trailing closure
@@ -114,6 +118,8 @@ source_file
114118

115119
top_level
116120
body:
121+
block
122+
stmt: unsupported_node "xs.map { $0 * 2 }"
117123

118124
===
119125
Closure with capture list
@@ -163,6 +169,8 @@ source_file
163169

164170
top_level
165171
body:
172+
block
173+
stmt: unsupported_node "let f = { [weak self] in self?.doThing() }"
166174

167175
===
168176
Multi-statement closure
@@ -236,3 +244,5 @@ source_file
236244

237245
top_level
238246
body:
247+
block
248+
stmt: unsupported_node "let f = { (x: Int) -> Int in\n let y = x + 1\n return y * 2\n}"

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ source_file
2828

2929
top_level
3030
body:
31+
block
32+
stmt: unsupported_node "let xs = [1, 2, 3]"
3133

3234
===
3335
Empty array literal with type
@@ -68,6 +70,8 @@ source_file
6870

6971
top_level
7072
body:
73+
block
74+
stmt: unsupported_node "let xs: [Int] = []"
7175

7276
===
7377
Dictionary literal
@@ -106,6 +110,8 @@ source_file
106110

107111
top_level
108112
body:
113+
block
114+
stmt: unsupported_node "let d = [\"a\": 1, \"b\": 2]"
109115

110116
===
111117
Set literal
@@ -155,6 +161,8 @@ source_file
155161

156162
top_level
157163
body:
164+
block
165+
stmt: unsupported_node "let s: Set<Int> = [1, 2, 3]"
158166

159167
===
160168
Tuple literal
@@ -191,6 +199,8 @@ source_file
191199

192200
top_level
193201
body:
202+
block
203+
stmt: unsupported_node "let t = (1, \"two\", 3.0)"
194204

195205
===
196206
Subscript access
@@ -232,9 +242,8 @@ source_file
232242

233243
top_level
234244
body:
235-
unsupported_node "// TODO: tree-sitter-swift parses `xs[0]` as a call_expression (same shape"
236-
unsupported_node "// as `xs(0)`), so the mapping currently produces a call_expr. Update the"
237-
unsupported_node "// parser / add a separate subscript_expr node and remap when fixed."
245+
block
246+
stmt: unsupported_node "let first = xs[0]"
238247

239248
===
240249
Dictionary subscript
@@ -276,8 +285,8 @@ source_file
276285

277286
top_level
278287
body:
279-
unsupported_node "// TODO: same parser issue as the array subscript case above —"
280-
unsupported_node "// `d[\"key\"]` is parsed as `call_expression(d, (\"key\"))`."
288+
block
289+
stmt: unsupported_node "let v = d[\"key\"]"
281290

282291
===
283292
Tuple member access
@@ -309,3 +318,5 @@ source_file
309318

310319
top_level
311320
body:
321+
block
322+
stmt: unsupported_node "let n = t.0"

unified/extractor/tests/corpus/swift/control-flow.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ source_file
3535

3636
top_level
3737
body:
38+
block
39+
stmt: unsupported_node "if x > 0 {\n print(x)\n}"
3840

3941
===
4042
If-else
@@ -90,6 +92,8 @@ source_file
9092

9193
top_level
9294
body:
95+
block
96+
stmt: unsupported_node "if x > 0 {\n print(x)\n} else {\n print(-x)\n}"
9397

9498
===
9599
If-else-if chain
@@ -165,6 +169,8 @@ source_file
165169

166170
top_level
167171
body:
172+
block
173+
stmt: unsupported_node "if x > 0 {\n print(1)\n} else if x < 0 {\n print(2)\n} else {\n print(3)\n}"
168174

169175
===
170176
If-let optional binding
@@ -207,6 +213,8 @@ source_file
207213

208214
top_level
209215
body:
216+
block
217+
stmt: unsupported_node "if let value = optional {\n print(value)\n}"
210218

211219
===
212220
Guard let
@@ -240,6 +248,8 @@ source_file
240248

241249
top_level
242250
body:
251+
block
252+
stmt: unsupported_node "guard let value = optional else { return }"
243253

244254
===
245255
Ternary expression
@@ -277,6 +287,8 @@ source_file
277287

278288
top_level
279289
body:
290+
block
291+
stmt: unsupported_node "let y = x > 0 ? 1 : -1"
280292

281293
===
282294
Switch statement
@@ -357,6 +369,8 @@ source_file
357369

358370
top_level
359371
body:
372+
block
373+
stmt: unsupported_node "switch x {\ncase 1:\n print(\"one\")\ncase 2, 3:\n print(\"two or three\")\ndefault:\n print(\"other\")\n}"
360374

361375
===
362376
Switch with binding pattern
@@ -445,3 +459,5 @@ source_file
445459

446460
top_level
447461
body:
462+
block
463+
stmt: unsupported_node "switch shape {\ncase .circle(let r):\n print(r)\ncase .square(let s):\n print(s)\n}"

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ source_file
1717

1818
top_level
1919
body:
20+
block
21+
stmt: unsupported_node "1 + 2"
2022

2123
===
2224
Another additive expression is desugared
@@ -37,3 +39,5 @@ source_file
3739

3840
top_level
3941
body:
42+
block
43+
stmt: unsupported_node "foo + bar"

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ source_file
3131

3232
top_level
3333
body:
34+
block
35+
stmt: unsupported_node "func greet() {\n print(\"hello\")\n}"
3436

3537
===
3638
Function with parameters and return type
@@ -93,6 +95,8 @@ source_file
9395

9496
top_level
9597
body:
98+
block
99+
stmt: unsupported_node "func add(_ a: Int, _ b: Int) -> Int {\n return a + b\n}"
96100

97101
===
98102
Function with named parameters
@@ -138,6 +142,8 @@ source_file
138142

139143
top_level
140144
body:
145+
block
146+
stmt: unsupported_node "func greet(person name: String) {\n print(name)\n}"
141147

142148
===
143149
Function with default parameter value
@@ -185,6 +191,8 @@ source_file
185191

186192
top_level
187193
body:
194+
block
195+
stmt: unsupported_node "func greet(name: String = \"world\") {\n print(name)\n}"
188196

189197
===
190198
Variadic function
@@ -249,6 +257,8 @@ source_file
249257

250258
top_level
251259
body:
260+
block
261+
stmt: unsupported_node "func sum(_ values: Int...) -> Int {\n return values.reduce(0, +)\n}"
252262

253263
===
254264
Function call
@@ -276,6 +286,8 @@ source_file
276286

277287
top_level
278288
body:
289+
block
290+
stmt: unsupported_node "foo(1, 2)"
279291

280292
===
281293
Function call with labelled arguments
@@ -306,6 +318,8 @@ source_file
306318

307319
top_level
308320
body:
321+
block
322+
stmt: unsupported_node "greet(person: \"Bob\")"
309323

310324
===
311325
Method call
@@ -336,6 +350,8 @@ source_file
336350

337351
top_level
338352
body:
353+
block
354+
stmt: unsupported_node "list.append(1)"
339355

340356
===
341357
Generic function
@@ -387,3 +403,5 @@ source_file
387403

388404
top_level
389405
body:
406+
block
407+
stmt: unsupported_node "func identity<T>(_ x: T) -> T {\n return x\n}"

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ source_file
1313

1414
top_level
1515
body:
16+
block
17+
stmt: unsupported_node "42"
1618

1719
===
1820
Negative integer literal
@@ -32,6 +34,8 @@ source_file
3234

3335
top_level
3436
body:
37+
block
38+
stmt: unsupported_node "-7"
3539

3640
===
3741
Floating-point literal
@@ -48,6 +52,8 @@ source_file
4852

4953
top_level
5054
body:
55+
block
56+
stmt: unsupported_node "3.14"
5157

5258
===
5359
Boolean literals
@@ -67,6 +73,10 @@ source_file
6773

6874
top_level
6975
body:
76+
block
77+
stmt:
78+
unsupported_node "true"
79+
unsupported_node "false"
7080

7181
===
7282
Nil literal
@@ -83,6 +93,8 @@ source_file
8393

8494
top_level
8595
body:
96+
block
97+
stmt: unsupported_node "nil"
8698

8799
===
88100
String literal
@@ -101,6 +113,8 @@ source_file
101113

102114
top_level
103115
body:
116+
block
117+
stmt: unsupported_node "\"hello\""
104118

105119
===
106120
String with interpolation
@@ -122,3 +136,5 @@ source_file
122136

123137
top_level
124138
body:
139+
block
140+
stmt: unsupported_node "\"hello \\(name)\""

0 commit comments

Comments
 (0)