Skip to content

Commit 9283801

Browse files
committed
Unified: Add some more AST nodes and rules
1 parent 5772ee4 commit 9283801

10 files changed

Lines changed: 470 additions & 62 deletions

File tree

unified/extractor/ast_types.yml

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
supertypes:
22
expr:
3+
- name_expr
4+
- int_literal
5+
- string_literal
36
- binary_expr
47
- unary_expr
5-
- name_expr
68
- lambda_expr
79
- unsupported_node
810
stmt:
@@ -23,7 +25,17 @@ supertypes:
2325
named:
2426
# Top-level is the root node, currently containing a list of expressions
2527
top_level:
26-
body*: expr
28+
body*: [expr, stmt]
29+
30+
# An identifier used in the context of an expression
31+
name_expr:
32+
identifier: identifier
33+
34+
# An integer literal
35+
int_literal:
36+
37+
# A string literal
38+
string_literal:
2739

2840
# Application of a binary operator, such as `a + b`
2941
binary_expr:
@@ -36,10 +48,6 @@ named:
3648
operand: expr
3749
operator: operator
3850

39-
# An identifier used in the context of an expression
40-
name_expr:
41-
identifier: identifier
42-
4351
lambda_expr:
4452
parameter*: parameter
4553
body: [expr, stmt]
@@ -50,6 +58,12 @@ named:
5058

5159
empty_stmt:
5260

61+
block_stmt:
62+
body*: stmt
63+
64+
expr_stmt:
65+
expr: expr
66+
5367
if_stmt:
5468
condition: condition
5569
then?: stmt

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

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ fn translation_rules() -> Vec<yeast::Rule> {
1010
body: {..children}
1111
)
1212
),
13+
// ---- Binary expressions ----
14+
// Swift's parser produces a different node kind for each operator
15+
// family, but the field shape (`lhs` / `op` / `rhs`) is uniform, so
16+
// each maps onto `binary_expr`.
1317
rule!(
1418
(additive_expression
1519
lhs: (_) @left
@@ -33,10 +37,134 @@ fn translation_rules() -> Vec<yeast::Rule> {
3337
right: {right})
3438
),
3539
rule!(
36-
(simple_identifier)
40+
(comparison_expression
41+
lhs: (_) @left
42+
op: _ @operator
43+
rhs: (_) @right)
44+
=>
45+
(binary_expr
46+
left: {left}
47+
operator: (operator #{operator})
48+
right: {right})
49+
),
50+
rule!(
51+
(equality_expression
52+
lhs: (_) @left
53+
op: _ @operator
54+
rhs: (_) @right)
55+
=>
56+
(binary_expr
57+
left: {left}
58+
operator: (operator #{operator})
59+
right: {right})
60+
),
61+
rule!(
62+
(conjunction_expression
63+
lhs: (_) @left
64+
op: _ @operator
65+
rhs: (_) @right)
66+
=>
67+
(binary_expr
68+
left: {left}
69+
operator: (operator #{operator})
70+
right: {right})
71+
),
72+
rule!(
73+
(disjunction_expression
74+
lhs: (_) @left
75+
op: _ @operator
76+
rhs: (_) @right)
77+
=>
78+
(binary_expr
79+
left: {left}
80+
operator: (operator #{operator})
81+
right: {right})
82+
),
83+
rule!(
84+
(nil_coalescing_expression
85+
lhs: (_) @left
86+
op: _ @operator
87+
rhs: (_) @right)
88+
=>
89+
(binary_expr
90+
left: {left}
91+
operator: (operator #{operator})
92+
right: {right})
93+
),
94+
rule!(
95+
(range_expression
96+
start: (_) @left
97+
op: _ @operator
98+
end: (_) @right)
99+
=>
100+
(binary_expr
101+
left: {left}
102+
operator: (operator #{operator})
103+
right: {right})
104+
),
105+
// ---- Unary expressions ----
106+
rule!(
107+
(prefix_expression
108+
operation: _ @operator
109+
target: (_) @operand)
110+
=>
111+
(unary_expr
112+
operand: {operand}
113+
operator: (operator #{operator}))
114+
),
115+
// ---- Identifiers / name expressions ----
116+
rule!(
117+
(simple_identifier) @name
118+
=>
119+
(name_expr
120+
identifier: (identifier #{name}))
121+
),
122+
// ---- Literals ----
123+
rule!(
124+
(integer_literal) @lit
125+
=>
126+
(int_literal #{lit})
127+
),
128+
// String literals: render the *raw* source text, including the
129+
// surrounding quotes. Interpolations (e.g. `"hi \(x)"`) are not
130+
// yet broken out into structured pieces \u2014 they show up as part
131+
// of the literal's source text.
132+
rule!(
133+
(line_string_literal) @lit
134+
=>
135+
(string_literal #{lit})
136+
),
137+
// ---- Patterns ----
138+
// The Swift parser uses a `pattern` node with a `bound_identifier`
139+
// field for simple bindings such as `let x = ...`.
140+
rule!(
141+
(pattern bound_identifier: (simple_identifier) @id)
142+
=>
143+
(var_pattern
144+
identifier: (identifier #{id}))
145+
),
146+
// ---- Variable declarations ----
147+
// `let x = e` / `var x = e` (with initializer).
148+
rule!(
149+
(property_declaration
150+
name: (_) @pat
151+
value: (_) @value)
152+
=>
153+
(variable_declaration_stmt
154+
variable_declarator: (variable_declarator
155+
pattern: {pat}
156+
value: {value}))
157+
),
158+
// `var x: T` (no initializer).
159+
rule!(
160+
(property_declaration
161+
name: (_) @pat)
37162
=>
38-
name_expr
163+
(variable_declaration_stmt
164+
variable_declarator: (variable_declarator
165+
pattern: {pat}))
39166
),
167+
// ---- Fallbacks ----
40168
rule!(
41169
(_)
42170
=>

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
Closure with explicit parameters
33
===
44

5+
// TODO: map lambda_literal -> lambda_expr
56
let f = { (x: Int) -> Int in x * 2 }
67

78
---
89

910
source_file
11+
comment "// TODO: map lambda_literal -> lambda_expr"
1012
property_declaration
1113
name:
1214
pattern
@@ -35,7 +37,15 @@ source_file
3537
---
3638

3739
top_level
38-
body: unsupported_node "let f = { (x: Int) -> Int in x * 2 }"
40+
body:
41+
unsupported_node "// TODO: map lambda_literal -> lambda_expr"
42+
variable_declaration_stmt
43+
variable_declarator:
44+
variable_declarator
45+
value: unsupported_node "{ (x: Int) -> Int in x * 2 }"
46+
pattern:
47+
var_pattern
48+
identifier: identifier "f"
3949

4050
===
4151
Closure with shorthand parameters
@@ -63,7 +73,14 @@ source_file
6373
---
6474

6575
top_level
66-
body: unsupported_node "let f = { $0 + $1 }"
76+
body:
77+
variable_declaration_stmt
78+
variable_declarator:
79+
variable_declarator
80+
value: unsupported_node "{ $0 + $1 }"
81+
pattern:
82+
var_pattern
83+
identifier: identifier "f"
6784

6885
===
6986
Trailing closure
@@ -130,7 +147,14 @@ source_file
130147
---
131148

132149
top_level
133-
body: unsupported_node "let f = { [weak self] in self?.doThing() }"
150+
body:
151+
variable_declaration_stmt
152+
variable_declarator:
153+
variable_declarator
154+
value: unsupported_node "{ [weak self] in self?.doThing() }"
155+
pattern:
156+
var_pattern
157+
identifier: identifier "f"
134158

135159
===
136160
Multi-statement closure
@@ -185,4 +209,11 @@ source_file
185209
---
186210

187211
top_level
188-
body: unsupported_node "let f = { (x: Int) -> Int in\n let y = x + 1\n return y * 2\n}"
212+
body:
213+
variable_declaration_stmt
214+
variable_declarator:
215+
variable_declarator
216+
value: unsupported_node "{ (x: Int) -> Int in\n let y = x + 1\n return y * 2\n}"
217+
pattern:
218+
var_pattern
219+
identifier: identifier "f"

0 commit comments

Comments
 (0)