From c8f7c3d7f2d199f593104f91bfb125e0c854e8a4 Mon Sep 17 00:00:00 2001 From: Taus Date: Wed, 13 May 2026 13:49:30 +0000 Subject: [PATCH] unified: Group more paired items Same as in the preceding commit, these items do not make sense as separate fields on the parent node, so we materialise (or create new) intermediate nodes to group them together. --- .../extractor/tree-sitter-swift/grammar.js | 27 ++++++++++--------- .../tree-sitter-swift/node-types.yml | 19 ++++++++----- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/unified/extractor/tree-sitter-swift/grammar.js b/unified/extractor/tree-sitter-swift/grammar.js index 50c1a08c66c..385aeb4d867 100644 --- a/unified/extractor/tree-sitter-swift/grammar.js +++ b/unified/extractor/tree-sitter-swift/grammar.js @@ -105,8 +105,8 @@ module.exports = grammar({ // { (foo, bar) ... [$.expression, $.lambda_parameter], [$._primary_expression, $.lambda_parameter], - // (start: start, end: end) - [$._tuple_type_item_identifier, $.tuple_expression], + // (foo) where foo could be a binding pattern or a tuple expression item. + [$._binding_pattern_with_expr, $.tuple_expression_item], // After a `{` in a function or switch context, it's ambigous whether we're starting a set of local statements or // applying some modifiers to a capture or pattern. [$.modifiers], @@ -931,26 +931,25 @@ module.exports = grammar({ PRECS.tuple, seq( "(", - sep1Opt( - seq( - optional(seq(field("name", $.simple_identifier), ":")), - field("value", $.expression) - ), - "," - ), + sep1Opt(field("element", $.tuple_expression_item), ","), ")" ) ), + tuple_expression_item: ($) => + seq( + optional(seq(field("name", $.simple_identifier), ":")), + field("value", $.expression) + ), array_literal: ($) => seq("[", optional(sep1Opt(field("element", $.expression), ",")), "]"), dictionary_literal: ($) => seq( "[", - choice(":", sep1Opt($._dictionary_literal_item, ",")), + choice(":", sep1Opt(field("element", $.dictionary_literal_item), ",")), optional(","), "]" ), - _dictionary_literal_item: ($) => + dictionary_literal_item: ($) => seq(field("key", $.expression), ":", field("value", $.expression)), special_literal: ($) => seq( @@ -968,11 +967,13 @@ module.exports = grammar({ playground_literal: ($) => seq( $._hash_symbol, - choice("colorLiteral", "fileLiteral", "imageLiteral"), + field("kind", choice("colorLiteral", "fileLiteral", "imageLiteral")), "(", - sep1Opt(seq(field("name", $.simple_identifier), ":", field("value", $.expression)), ","), + sep1Opt(field("argument", $.playground_literal_argument), ","), ")" ), + playground_literal_argument: ($) => + seq(field("name", $.simple_identifier), ":", field("value", $.expression)), lambda_literal: ($) => prec.left( PRECS.lambda, diff --git a/unified/extractor/tree-sitter-swift/node-types.yml b/unified/extractor/tree-sitter-swift/node-types.yml index 1f2e27ea337..64e69142fb0 100644 --- a/unified/extractor/tree-sitter-swift/node-types.yml +++ b/unified/extractor/tree-sitter-swift/node-types.yml @@ -248,8 +248,10 @@ named: 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] diagnostic: dictionary_literal: - key*: expression - value*: expression + element*: dictionary_literal_item + dictionary_literal_item: + key: expression + value: expression dictionary_type: key: type value: type @@ -483,8 +485,11 @@ named: bound_identifier?: simple_identifier kind: [binding_pattern, case_pattern, expression, tuple_pattern, type_casting_pattern, wildcard_pattern] playground_literal: - name+: simple_identifier - value+: expression + argument+: playground_literal_argument + kind: ["colorLiteral", "fileLiteral", "imageLiteral"] + playground_literal_argument: + name: simple_identifier + value: expression postfix_expression: operation: ["++", "--", bang] target: expression @@ -613,8 +618,10 @@ named: operator: try_operator try_operator: tuple_expression: - name*: simple_identifier - value+: expression + element+: tuple_expression_item + tuple_expression_item: + name?: simple_identifier + value: expression tuple_pattern: item+: tuple_pattern_item tuple_pattern_item: