Skip to content

Commit 5d6dc5c

Browse files
committed
unified: Clean up statements/block mess
Introduces (by making it named) a `block` node, and conversely makes `statements` anonymous. This enables us to sensibly distinguish between the "then" and "else" branch of an `if_statement`, which we were not able to previously.
1 parent bfe5aa8 commit 5d6dc5c

2 files changed

Lines changed: 45 additions & 52 deletions

File tree

unified/extractor/tree-sitter-swift/grammar.js

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ module.exports = grammar({
979979
seq(
980980
choice("{", "^{"),
981981
optional($._lambda_type_declaration),
982-
field("body", optional($.statements)),
982+
optional($._statements),
983983
"}"
984984
)
985985
),
@@ -1041,14 +1041,14 @@ module.exports = grammar({
10411041
),
10421042
self_expression: ($) => "self",
10431043
super_expression: ($) => seq("super"),
1044-
_else_options: ($) => choice($._block, field("else_branch", $.if_statement)),
1044+
_else_options: ($) => choice(field("else_branch", $.block), field("else_branch", $.if_statement)),
10451045
if_statement: ($) =>
10461046
prec.right(
10471047
PRECS["if"],
10481048
seq(
10491049
"if",
10501050
sep1(field("condition", $.if_condition), ","),
1051-
$._block,
1051+
field("body", $.block),
10521052
optional(seq(field("else_keyword", $["else"]), $._else_options))
10531053
)
10541054
),
@@ -1067,7 +1067,7 @@ module.exports = grammar({
10671067
"guard",
10681068
sep1(field("condition", $.if_condition), ","),
10691069
field("else_keyword", $["else"]),
1070-
$._block
1070+
field("body", $.block)
10711071
)
10721072
),
10731073
switch_statement: ($) =>
@@ -1094,18 +1094,18 @@ module.exports = grammar({
10941094
field("default", $.default_keyword)
10951095
),
10961096
":",
1097-
field("body", $.statements),
1097+
$._statements,
10981098
optional("fallthrough")
10991099
),
11001100
switch_pattern: ($) => field("pattern", alias($._binding_pattern_with_expr, $.pattern)),
11011101
do_statement: ($) =>
1102-
prec.right(PRECS["do"], seq("do", $._block, repeat(field("catch", $.catch_block)))),
1102+
prec.right(PRECS["do"], seq("do", field("body", $.block), repeat(field("catch", $.catch_block)))),
11031103
catch_block: ($) =>
11041104
seq(
11051105
field("keyword", $.catch_keyword),
11061106
field("error", optional(alias($._binding_pattern_no_expr, $.pattern))),
11071107
field("where", optional($.where_clause)),
1108-
$._block
1108+
field("body", $.block)
11091109
),
11101110
where_clause: ($) => prec.left(seq(field("keyword", $.where_keyword), field("expr", $.expression))),
11111111
key_path_expression: ($) =>
@@ -1181,7 +1181,7 @@ module.exports = grammar({
11811181
////////////////////////////////
11821182
// Statements - https://docs.swift.org/swift-book/ReferenceManual/Statements.html
11831183
////////////////////////////////
1184-
statements: ($) =>
1184+
_statements: ($) =>
11851185
prec.left(
11861186
// Left precedence is required in switch statements
11871187
seq(
@@ -1204,7 +1204,7 @@ module.exports = grammar({
12041204
$._labeled_statement,
12051205
$._throw_statement
12061206
),
1207-
_block: ($) => prec(PRECS.block, seq("{", field("body", optional($.statements)), "}")),
1207+
block: ($) => prec(PRECS.block, seq("{", optional($._statements), "}")),
12081208
_labeled_statement: ($) =>
12091209
seq(
12101210
optional($.statement_label),
@@ -1231,7 +1231,7 @@ module.exports = grammar({
12311231
"in",
12321232
field("collection", $._for_statement_collection),
12331233
field("where", optional($.where_clause)),
1234-
$._block
1234+
field("body", $.block)
12351235
)
12361236
),
12371237
_for_statement_collection: ($) =>
@@ -1248,19 +1248,15 @@ module.exports = grammar({
12481248
seq(
12491249
"while",
12501250
sep1(field("condition", $.if_condition), ","),
1251-
"{",
1252-
field("body", optional($.statements)),
1253-
"}"
1251+
field("body", $.block)
12541252
)
12551253
),
12561254
repeat_while_statement: ($) =>
12571255
prec(
12581256
PRECS.loop,
12591257
seq(
12601258
"repeat",
1261-
"{",
1262-
field("body", optional($.statements)),
1263-
"}",
1259+
field("body", $.block),
12641260
// Make sure we make it to the `while` before assuming this is a parameter pack.
12651261
repeat($._implicit_semi),
12661262
"while",
@@ -1443,14 +1439,14 @@ module.exports = grammar({
14431439
field("modifiers", optional($.modifiers)),
14441440
"willSet",
14451441
optional(seq("(", field("parameter", $.simple_identifier), ")")),
1446-
$._block
1442+
field("body", $.block)
14471443
),
14481444
didset_clause: ($) =>
14491445
seq(
14501446
field("modifiers", optional($.modifiers)),
14511447
"didSet",
14521448
optional(seq("(", field("parameter", $.simple_identifier), ")")),
1453-
$._block
1449+
field("body", $.block)
14541450
),
14551451
typealias_declaration: ($) =>
14561452
seq(field("modifiers", optional($.modifiers)), $._modifierless_typealias_declaration),
@@ -1464,13 +1460,13 @@ module.exports = grammar({
14641460
),
14651461
function_declaration: ($) =>
14661462
prec.right(
1467-
seq($._bodyless_function_declaration, field("body", $.function_body))
1463+
seq($._bodyless_function_declaration, field("body", $.block))
14681464
),
14691465
_modifierless_function_declaration: ($) =>
14701466
prec.right(
14711467
seq(
14721468
$._modifierless_function_declaration_no_body,
1473-
field("body", $.function_body)
1469+
field("body", $.block)
14741470
)
14751471
),
14761472
_bodyless_function_declaration: ($) =>
@@ -1496,7 +1492,6 @@ module.exports = grammar({
14961492
field("type_constraints", optional($.type_constraints))
14971493
)
14981494
),
1499-
function_body: ($) => $._block,
15001495
macro_declaration: ($) =>
15011496
seq(
15021497
$._macro_head,
@@ -1742,7 +1737,7 @@ module.exports = grammar({
17421737
protocol_function_declaration: ($) =>
17431738
seq(
17441739
$._bodyless_function_declaration,
1745-
optional(field("body", $.function_body))
1740+
optional(field("body", $.block))
17461741
),
17471742
init_declaration: ($) =>
17481743
prec.right(
@@ -1756,12 +1751,12 @@ module.exports = grammar({
17561751
field("async", optional($._async_keyword)),
17571752
field("throws", optional(choice($.throws_clause, $.throws))),
17581753
field("type_constraints", optional($.type_constraints)),
1759-
optional(field("body", $.function_body))
1754+
optional(field("body", $.block))
17601755
)
17611756
),
17621757
deinit_declaration: ($) =>
17631758
prec.right(
1764-
seq(field("modifiers", optional($.modifiers)), "deinit", field("body", $.function_body))
1759+
seq(field("modifiers", optional($.modifiers)), "deinit", field("body", $.block))
17651760
),
17661761
subscript_declaration: ($) =>
17671762
prec.right(
@@ -1784,23 +1779,23 @@ module.exports = grammar({
17841779
seq(
17851780
"{",
17861781
choice(
1787-
field("body", optional($.statements)),
1782+
optional($._statements),
17881783
repeat(
17891784
field("accessor", choice($.computed_getter, $.computed_setter, $.computed_modify))
17901785
)
17911786
),
17921787
"}"
17931788
),
17941789
computed_getter: ($) =>
1795-
seq(repeat(field("attribute", $.attribute)), field("specifier", $.getter_specifier), optional($._block)),
1790+
seq(repeat(field("attribute", $.attribute)), field("specifier", $.getter_specifier), optional(field("body", $.block))),
17961791
computed_modify: ($) =>
1797-
seq(repeat(field("attribute", $.attribute)), field("specifier", $.modify_specifier), optional($._block)),
1792+
seq(repeat(field("attribute", $.attribute)), field("specifier", $.modify_specifier), optional(field("body", $.block))),
17981793
computed_setter: ($) =>
17991794
seq(
18001795
repeat(field("attribute", $.attribute)),
18011796
field("specifier", $.setter_specifier),
18021797
optional(seq("(", field("parameter", $.simple_identifier), ")")),
1803-
optional($._block)
1798+
optional(field("body", $.block))
18041799
),
18051800
getter_specifier: ($) =>
18061801
seq(field("mutation", optional($.mutation_modifier)), "get", optional($._getter_effects)),

unified/extractor/tree-sitter-swift/node-types.yml

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ named:
155155
lhs: expression
156156
op: ["&", "<<", ">>", "^", "|"]
157157
rhs: expression
158+
block:
159+
statement*: [control_transfer_statement, do_statement, expression, for_statement, guard_statement, local_declaration, repeat_while_statement, statement_label, while_statement]
158160
boolean_literal:
159161
call_expression:
160162
function: expression
@@ -174,7 +176,7 @@ named:
174176
name: simple_identifier
175177
type?: user_type
176178
catch_block:
177-
body?: statements
179+
body: block
178180
error?: pattern
179181
keyword: catch_keyword
180182
where?: where_clause
@@ -209,18 +211,18 @@ named:
209211
version*: integer_literal
210212
computed_getter:
211213
attribute*: attribute
212-
body?: statements
214+
body?: block
213215
specifier: getter_specifier
214216
computed_modify:
215217
attribute*: attribute
216-
body?: statements
218+
body?: block
217219
specifier: modify_specifier
218220
computed_property:
219221
accessor*: [computed_getter, computed_modify, computed_setter]
220-
body?: statements
222+
statement*: [control_transfer_statement, do_statement, expression, for_statement, guard_statement, local_declaration, repeat_while_statement, statement_label, while_statement]
221223
computed_setter:
222224
attribute*: attribute
223-
body?: statements
225+
body?: block
224226
parameter?: simple_identifier
225227
specifier: setter_specifier
226228
conjunction_expression:
@@ -240,7 +242,7 @@ named:
240242
custom_operator:
241243
default_keyword:
242244
deinit_declaration:
243-
body: function_body
245+
body: block
244246
modifiers?: modifiers
245247
deprecated_operator_declaration_body:
246248
entry*: [bin_literal, boolean_literal, hex_literal, integer_literal, line_string_literal, multi_line_string_literal, "nil", oct_literal, raw_string_literal, real_literal, regex_literal, simple_identifier]
@@ -252,7 +254,7 @@ named:
252254
key: type
253255
value: type
254256
didset_clause:
255-
body?: statements
257+
body: block
256258
modifiers?: modifiers
257259
parameter?: simple_identifier
258260
directive:
@@ -264,7 +266,7 @@ named:
264266
op: "||"
265267
rhs: expression
266268
do_statement:
267-
body?: statements
269+
body: block
268270
catch*: catch_block
269271
else:
270272
enum_class_body:
@@ -294,19 +296,17 @@ named:
294296
external_macro_definition:
295297
arguments: value_arguments
296298
for_statement:
297-
body?: statements
299+
body: block
298300
collection: expression
299301
item: pattern
300302
try?: try_operator
301303
type?: type_annotation
302304
where?: where_clause
303305
fully_open_range:
304-
function_body:
305-
body?: statements
306306
function_declaration:
307307
async?: "async"
308308
attribute*: attribute
309-
body: function_body
309+
body: block
310310
default_value*: expression
311311
modifiers*: [attribute, inheritance_modifier, modifiers, ownership_modifier, property_behavior_modifier]
312312
name: [referenceable_operator, simple_identifier]
@@ -325,7 +325,7 @@ named:
325325
effect*: [async_keyword, throws, throws_clause]
326326
mutation?: mutation_modifier
327327
guard_statement:
328-
body?: statements
328+
body: block
329329
condition+: if_condition
330330
else_keyword: else
331331
hex_literal:
@@ -339,9 +339,9 @@ named:
339339
value?: expression
340340
where?: where_clause
341341
if_statement:
342-
body*: statements
342+
body: block
343343
condition+: if_condition
344-
else_branch?: if_statement
344+
else_branch?: [block, if_statement]
345345
else_keyword?: else
346346
implicitly_unwrapped_type:
347347
name: type
@@ -363,7 +363,7 @@ named:
363363
async?: "async"
364364
attribute*: attribute
365365
bang?: bang
366-
body?: function_body
366+
body?: block
367367
default_value*: expression
368368
modifiers?: modifiers
369369
name: "init"
@@ -397,8 +397,8 @@ named:
397397
parameter+: lambda_parameter
398398
lambda_literal:
399399
attribute*: attribute
400-
body?: statements
401400
captures?: capture_list
401+
statement*: [control_transfer_statement, do_statement, expression, for_statement, guard_statement, local_declaration, repeat_while_statement, statement_label, while_statement]
402402
type?: lambda_function_type
403403
lambda_parameter:
404404
external_name?: simple_identifier
@@ -528,7 +528,7 @@ named:
528528
protocol_function_declaration:
529529
async?: "async"
530530
attribute*: attribute
531-
body?: function_body
531+
body?: block
532532
default_value*: expression
533533
modifiers?: modifiers
534534
name: [referenceable_operator, simple_identifier]
@@ -565,7 +565,7 @@ named:
565565
operator: ["!=", "!==", "%", "%=", "&", "*", "*=", "+", "++", "+=", "-", "--", "-=", "/", "/=", "<", "<<", "<=", "=", "==", "===", ">", ">=", ">>", "^", bang, custom_operator, "|", "~"]
566566
regex_literal:
567567
repeat_while_statement:
568-
body?: statements
568+
body: block
569569
condition+: if_condition
570570
selector_expression:
571571
expr: expression
@@ -582,8 +582,6 @@ named:
582582
statement*: [do_statement, expression, for_statement, global_declaration, guard_statement, repeat_while_statement, statement_label, throw_keyword, while_statement]
583583
special_literal:
584584
statement_label:
585-
statements:
586-
statement+: [control_transfer_statement, do_statement, expression, for_statement, guard_statement, local_declaration, repeat_while_statement, statement_label, while_statement]
587585
str_escaped_char:
588586
subscript_declaration:
589587
attribute*: attribute
@@ -598,10 +596,10 @@ named:
598596
suppressed_constraint:
599597
suppressed: type_identifier
600598
switch_entry:
601-
body: statements
602599
default?: default_keyword
603600
modifiers?: modifiers
604601
pattern*: switch_pattern
602+
statement+: [control_transfer_statement, do_statement, expression, for_statement, guard_statement, local_declaration, repeat_while_statement, statement_label, while_statement]
605603
where?: where_clause
606604
switch_pattern:
607605
pattern: pattern
@@ -694,11 +692,11 @@ named:
694692
keyword: where_keyword
695693
where_keyword:
696694
while_statement:
697-
body?: statements
695+
body: block
698696
condition+: if_condition
699697
wildcard_pattern:
700698
willset_clause:
701-
body?: statements
699+
body: block
702700
modifiers?: modifiers
703701
parameter?: simple_identifier
704702
willset_didset_block:

0 commit comments

Comments
 (0)