Skip to content

Commit 9e3cfdb

Browse files
committed
unified: add supertypes for various kinds of declarations
Hides a bunch of huge unions under (hopefully) sensible supertypes.
1 parent ebbedc0 commit 9e3cfdb

1 file changed

Lines changed: 23 additions & 17 deletions

File tree

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,14 @@ if (tree_sitter_version_supports_emoji()) {
8484

8585
module.exports = grammar({
8686
name: "swift",
87-
supertypes: ($) => [$.expression, $.unannotated_type],
87+
supertypes: ($) => [
88+
$.expression,
89+
$.unannotated_type,
90+
$.global_declaration,
91+
$.type_level_declaration,
92+
$.local_declaration,
93+
$.protocol_member_declaration,
94+
],
8895
conflicts: ($) => [
8996
// @Type(... could either be an annotation constructor invocation or an annotated expression
9097
[$.attribute],
@@ -1183,14 +1190,14 @@ module.exports = grammar({
11831190
_local_statement: ($) =>
11841191
choice(
11851192
$.expression,
1186-
$._local_declaration,
1193+
$.local_declaration,
11871194
$._labeled_statement,
11881195
$.control_transfer_statement
11891196
),
11901197
_top_level_statement: ($) =>
11911198
choice(
11921199
$.expression,
1193-
$._global_declaration,
1200+
$.global_declaration,
11941201
$._labeled_statement,
11951202
$._throw_statement
11961203
),
@@ -1298,7 +1305,7 @@ module.exports = grammar({
12981305
////////////////////////////////
12991306
// Declarations - https://docs.swift.org/swift-book/ReferenceManual/Declarations.html
13001307
////////////////////////////////
1301-
_global_declaration: ($) =>
1308+
global_declaration: ($) =>
13021309
choice(
13031310
$.import_declaration,
13041311
$.property_declaration,
@@ -1312,7 +1319,7 @@ module.exports = grammar({
13121319
$.associatedtype_declaration,
13131320
$.macro_declaration
13141321
),
1315-
_type_level_declaration: ($) =>
1322+
type_level_declaration: ($) =>
13161323
choice(
13171324
$.import_declaration,
13181325
$.property_declaration,
@@ -1327,7 +1334,7 @@ module.exports = grammar({
13271334
$.precedence_group_declaration,
13281335
$.associatedtype_declaration
13291336
),
1330-
_local_declaration: ($) =>
1337+
local_declaration: ($) =>
13311338
choice(
13321339
alias($._local_property_declaration, $.property_declaration),
13331340
alias($._local_typealias_declaration, $.typealias_declaration),
@@ -1598,7 +1605,7 @@ module.exports = grammar({
15981605
_class_member_separator: ($) => choice($._semi, $.multiline_comment),
15991606
_class_member_declarations: ($) =>
16001607
seq(
1601-
sep1($._type_level_declaration, $._class_member_separator),
1608+
sep1($.type_level_declaration, $._class_member_separator),
16021609
optional($._class_member_separator)
16031610
),
16041611
_function_value_parameters: ($) =>
@@ -1666,7 +1673,7 @@ module.exports = grammar({
16661673
throws_clause: ($) =>
16671674
seq($._throws_keyword, "(", field("type", $.unannotated_type), ")"),
16681675
enum_class_body: ($) =>
1669-
seq("{", repeat(choice($.enum_entry, $._type_level_declaration)), "}"),
1676+
seq("{", repeat(choice($.enum_entry, $.type_level_declaration)), "}"),
16701677
enum_entry: ($) =>
16711678
seq(
16721679
optional($.modifiers),
@@ -1718,23 +1725,22 @@ module.exports = grammar({
17181725
protocol_body: ($) =>
17191726
seq("{", optional($._protocol_member_declarations), "}"),
17201727
_protocol_member_declarations: ($) =>
1721-
seq(sep1($._protocol_member_declaration, $._semi), optional($._semi)),
1722-
_protocol_member_declaration: ($) =>
1728+
seq(sep1($.protocol_member_declaration, $._semi), optional($._semi)),
1729+
protocol_member_declaration: ($) =>
17231730
choice(
1724-
alias(
1725-
seq(
1726-
$._bodyless_function_declaration,
1727-
optional(field("body", $.function_body))
1728-
),
1729-
$.protocol_function_declaration
1730-
),
1731+
$.protocol_function_declaration,
17311732
$.init_declaration,
17321733
$.deinit_declaration,
17331734
$.protocol_property_declaration,
17341735
$.typealias_declaration,
17351736
$.associatedtype_declaration,
17361737
$.subscript_declaration
17371738
),
1739+
protocol_function_declaration: ($) =>
1740+
seq(
1741+
$._bodyless_function_declaration,
1742+
optional(field("body", $.function_body))
1743+
),
17381744
init_declaration: ($) =>
17391745
prec.right(
17401746
seq(

0 commit comments

Comments
 (0)