Skip to content

Commit 2eee2e5

Browse files
committed
unified: clean up patterns
Mostly by materialising a bunch of (useful) intermediate nodes.
1 parent 2010844 commit 2eee2e5

2 files changed

Lines changed: 44 additions & 28 deletions

File tree

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

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ module.exports = grammar({
146146
[$._bodyless_function_declaration, $.property_modifier],
147147
[$.init_declaration, $.property_modifier],
148148
// Patterns, man
149-
[$._navigable_type_expression, $._case_pattern],
149+
[$._navigable_type_expression, $.case_pattern],
150150
[$._no_expr_pattern_already_bound, $._binding_pattern_no_expr],
151151

152152
// On encountering a closure starting with `{ @Foo ...`, we don't yet know if that attribute applies to the closure
@@ -1865,38 +1865,38 @@ module.exports = grammar({
18651865
_universally_allowed_pattern: ($) =>
18661866
choice(
18671867
$.wildcard_pattern,
1868-
$._tuple_pattern,
1869-
$._type_casting_pattern,
1870-
$._case_pattern
1868+
$.tuple_pattern,
1869+
$.type_casting_pattern,
1870+
$.case_pattern
18711871
),
18721872
_bound_identifier: ($) => field("bound_identifier", $.simple_identifier),
18731873

18741874
_binding_pattern_no_expr: ($) =>
18751875
seq(
1876-
choice(
1876+
field("kind", choice(
18771877
$._universally_allowed_pattern,
1878-
$._binding_pattern,
1878+
$.binding_pattern,
18791879
$._bound_identifier
1880-
),
1880+
)),
18811881
optional($._quest)
18821882
),
18831883
_no_expr_pattern_already_bound: ($) =>
18841884
seq(
1885-
choice($._universally_allowed_pattern, $._bound_identifier),
1885+
field("kind", choice($._universally_allowed_pattern, $._bound_identifier)),
18861886
optional($._quest)
18871887
),
18881888
_binding_pattern_with_expr: ($) =>
18891889
seq(
1890-
choice(
1890+
field("kind", choice(
18911891
$._universally_allowed_pattern,
1892-
$._binding_pattern,
1892+
$.binding_pattern,
18931893
$.expression
1894-
),
1894+
)),
18951895
optional($._quest)
18961896
),
18971897
_non_binding_pattern_with_expr: ($) =>
18981898
seq(
1899-
choice($._universally_allowed_pattern, $.expression),
1899+
field("kind", choice($._universally_allowed_pattern, $.expression)),
19001900
optional($._quest)
19011901
),
19021902
_direct_or_indirect_binding: ($) =>
@@ -1916,32 +1916,33 @@ module.exports = grammar({
19161916
$._no_expr_pattern_already_bound
19171917
),
19181918
wildcard_pattern: ($) => "_",
1919-
_tuple_pattern_item: ($) =>
1919+
tuple_pattern_item: ($) =>
19201920
choice(
19211921
seq(
1922-
$.simple_identifier,
1923-
seq(":", alias($._binding_pattern_with_expr, $.pattern))
1922+
field("name", $.simple_identifier),
1923+
":",
1924+
field("pattern", alias($._binding_pattern_with_expr, $.pattern))
19241925
),
1925-
alias($._binding_pattern_with_expr, $.pattern)
1926+
field("pattern", alias($._binding_pattern_with_expr, $.pattern))
19261927
),
1927-
_tuple_pattern: ($) => seq("(", sep1Opt($._tuple_pattern_item, ","), ")"),
1928-
_case_pattern: ($) =>
1928+
tuple_pattern: ($) => seq("(", sep1Opt(field("item", $.tuple_pattern_item), ","), ")"),
1929+
case_pattern: ($) =>
19291930
seq(
19301931
optional("case"),
1931-
optional($.user_type), // XXX this should just be _type but that creates ambiguity
1932+
optional(field("type", $.user_type)), // XXX this should just be _type but that creates ambiguity
19321933
$._dot,
1933-
$.simple_identifier,
1934-
optional($._tuple_pattern)
1934+
field("name", $.simple_identifier),
1935+
optional(field("arguments", $.tuple_pattern))
19351936
),
1936-
_type_casting_pattern: ($) =>
1937+
type_casting_pattern: ($) =>
19371938
choice(
1938-
seq("is", $.type),
1939-
seq(alias($._binding_pattern_no_expr, $.pattern), $._as, $.type)
1939+
seq("is", field("type", $.type)),
1940+
seq(field("pattern", alias($._binding_pattern_no_expr, $.pattern)), $._as, field("type", $.type))
19401941
),
1941-
_binding_pattern: ($) =>
1942+
binding_pattern: ($) =>
19421943
seq(
1943-
seq(optional("case"), $.value_binding_pattern),
1944-
$._no_expr_pattern_already_bound
1944+
seq(optional("case"), field("binding", $.value_binding_pattern)),
1945+
field("pattern", alias($._no_expr_pattern_already_bound, $.pattern))
19451946
),
19461947

19471948
// ==========

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ named:
148148
expr: expression
149149
bang:
150150
bin_literal:
151+
binding_pattern:
152+
binding: value_binding_pattern
153+
pattern: pattern
151154
bitwise_operation:
152155
lhs: expression
153156
op: ["&", "<<", ">>", "^", "|"]
@@ -166,6 +169,10 @@ named:
166169
name: [self_expression, simple_identifier]
167170
ownership?: ownership_modifier
168171
value?: expression
172+
case_pattern:
173+
arguments?: tuple_pattern
174+
name: simple_identifier
175+
type?: user_type
169176
catch_block:
170177
body?: statements
171178
error?: pattern
@@ -473,9 +480,9 @@ named:
473480
parameter_modifiers:
474481
modifier+: parameter_modifier
475482
pattern:
476-
$children*: [expression, pattern, type, user_type, value_binding_pattern, wildcard_pattern]
477483
binding?: value_binding_pattern
478484
bound_identifier?: simple_identifier
485+
kind: [binding_pattern, case_pattern, expression, tuple_pattern, type_casting_pattern, wildcard_pattern]
479486
playground_literal:
480487
name+: simple_identifier
481488
value+: expression
@@ -615,6 +622,11 @@ named:
615622
tuple_expression:
616623
name*: simple_identifier
617624
value+: expression
625+
tuple_pattern:
626+
item+: tuple_pattern_item
627+
tuple_pattern_item:
628+
name?: simple_identifier
629+
pattern: pattern
618630
tuple_type:
619631
element*: tuple_type_item
620632
tuple_type_item:
@@ -629,6 +641,9 @@ named:
629641
type: [implicitly_unwrapped_type, type]
630642
type_arguments:
631643
argument+: type
644+
type_casting_pattern:
645+
pattern?: pattern
646+
type: type
632647
type_constraint:
633648
constraint: [equality_constraint, inheritance_constraint]
634649
type_constraints:

0 commit comments

Comments
 (0)