unified: clean up patterns

Mostly by materialising a bunch of (useful) intermediate nodes.
This commit is contained in:
Taus
2026-05-12 15:23:26 +00:00
parent 2010844b1e
commit 2eee2e50dc
2 changed files with 44 additions and 28 deletions

View File

@@ -146,7 +146,7 @@ module.exports = grammar({
[$._bodyless_function_declaration, $.property_modifier],
[$.init_declaration, $.property_modifier],
// Patterns, man
[$._navigable_type_expression, $._case_pattern],
[$._navigable_type_expression, $.case_pattern],
[$._no_expr_pattern_already_bound, $._binding_pattern_no_expr],
// 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({
_universally_allowed_pattern: ($) =>
choice(
$.wildcard_pattern,
$._tuple_pattern,
$._type_casting_pattern,
$._case_pattern
$.tuple_pattern,
$.type_casting_pattern,
$.case_pattern
),
_bound_identifier: ($) => field("bound_identifier", $.simple_identifier),
_binding_pattern_no_expr: ($) =>
seq(
choice(
field("kind", choice(
$._universally_allowed_pattern,
$._binding_pattern,
$.binding_pattern,
$._bound_identifier
),
)),
optional($._quest)
),
_no_expr_pattern_already_bound: ($) =>
seq(
choice($._universally_allowed_pattern, $._bound_identifier),
field("kind", choice($._universally_allowed_pattern, $._bound_identifier)),
optional($._quest)
),
_binding_pattern_with_expr: ($) =>
seq(
choice(
field("kind", choice(
$._universally_allowed_pattern,
$._binding_pattern,
$.binding_pattern,
$.expression
),
)),
optional($._quest)
),
_non_binding_pattern_with_expr: ($) =>
seq(
choice($._universally_allowed_pattern, $.expression),
field("kind", choice($._universally_allowed_pattern, $.expression)),
optional($._quest)
),
_direct_or_indirect_binding: ($) =>
@@ -1916,32 +1916,33 @@ module.exports = grammar({
$._no_expr_pattern_already_bound
),
wildcard_pattern: ($) => "_",
_tuple_pattern_item: ($) =>
tuple_pattern_item: ($) =>
choice(
seq(
$.simple_identifier,
seq(":", alias($._binding_pattern_with_expr, $.pattern))
field("name", $.simple_identifier),
":",
field("pattern", alias($._binding_pattern_with_expr, $.pattern))
),
alias($._binding_pattern_with_expr, $.pattern)
field("pattern", alias($._binding_pattern_with_expr, $.pattern))
),
_tuple_pattern: ($) => seq("(", sep1Opt($._tuple_pattern_item, ","), ")"),
_case_pattern: ($) =>
tuple_pattern: ($) => seq("(", sep1Opt(field("item", $.tuple_pattern_item), ","), ")"),
case_pattern: ($) =>
seq(
optional("case"),
optional($.user_type), // XXX this should just be _type but that creates ambiguity
optional(field("type", $.user_type)), // XXX this should just be _type but that creates ambiguity
$._dot,
$.simple_identifier,
optional($._tuple_pattern)
field("name", $.simple_identifier),
optional(field("arguments", $.tuple_pattern))
),
_type_casting_pattern: ($) =>
type_casting_pattern: ($) =>
choice(
seq("is", $.type),
seq(alias($._binding_pattern_no_expr, $.pattern), $._as, $.type)
seq("is", field("type", $.type)),
seq(field("pattern", alias($._binding_pattern_no_expr, $.pattern)), $._as, field("type", $.type))
),
_binding_pattern: ($) =>
binding_pattern: ($) =>
seq(
seq(optional("case"), $.value_binding_pattern),
$._no_expr_pattern_already_bound
seq(optional("case"), field("binding", $.value_binding_pattern)),
field("pattern", alias($._no_expr_pattern_already_bound, $.pattern))
),
// ==========

View File

@@ -148,6 +148,9 @@ named:
expr: expression
bang:
bin_literal:
binding_pattern:
binding: value_binding_pattern
pattern: pattern
bitwise_operation:
lhs: expression
op: ["&", "<<", ">>", "^", "|"]
@@ -166,6 +169,10 @@ named:
name: [self_expression, simple_identifier]
ownership?: ownership_modifier
value?: expression
case_pattern:
arguments?: tuple_pattern
name: simple_identifier
type?: user_type
catch_block:
body?: statements
error?: pattern
@@ -473,9 +480,9 @@ named:
parameter_modifiers:
modifier+: parameter_modifier
pattern:
$children*: [expression, pattern, type, user_type, value_binding_pattern, wildcard_pattern]
binding?: value_binding_pattern
bound_identifier?: simple_identifier
kind: [binding_pattern, case_pattern, expression, tuple_pattern, type_casting_pattern, wildcard_pattern]
playground_literal:
name+: simple_identifier
value+: expression
@@ -615,6 +622,11 @@ named:
tuple_expression:
name*: simple_identifier
value+: expression
tuple_pattern:
item+: tuple_pattern_item
tuple_pattern_item:
name?: simple_identifier
pattern: pattern
tuple_type:
element*: tuple_type_item
tuple_type_item:
@@ -629,6 +641,9 @@ named:
type: [implicitly_unwrapped_type, type]
type_arguments:
argument+: type
type_casting_pattern:
pattern?: pattern
type: type
type_constraint:
constraint: [equality_constraint, inheritance_constraint]
type_constraints: