From 52d72836f904996278dc745ae96984f3f299ad6b Mon Sep 17 00:00:00 2001 From: Taus Date: Tue, 12 May 2026 15:59:18 +0000 Subject: [PATCH] unified: Fix multiline_comment issue This named node (which is in fact emitted by the scanner as an `external`) was appearing as a child of `class_body` because of inlining via `_class_member_separator`. This, in itself, appears to be somewhat of a hack, to handle cases where a multiline comment signals the end of a class member. To fix this, we make the external node _unnamed_, but keep the `extras` node _named_ (so we can still extract it from the parse tree), and we add a new rule `multiline_comment` that mediates between the two. That way, the use inside `_class_member_separator` can use the unnamed variant, and no node is pushed into $children. --- unified/extractor/tree-sitter-swift/grammar.js | 9 +++++++-- unified/extractor/tree-sitter-swift/node-types.yml | 1 - 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/unified/extractor/tree-sitter-swift/grammar.js b/unified/extractor/tree-sitter-swift/grammar.js index 1bcdc4fbdf1..566b7d5567a 100644 --- a/unified/extractor/tree-sitter-swift/grammar.js +++ b/unified/extractor/tree-sitter-swift/grammar.js @@ -194,7 +194,7 @@ module.exports = grammar({ // `/*`, and decrement it whenever we see `*/`. A standard grammar would only be able to exit the comment at the // first `*/` (like C does). Similarly, when you start a string with `##"`, you're required to include the same // number of `#` symbols to end it. - $.multiline_comment, + $._multiline_comment, $.raw_str_part, $.raw_str_continuing_indicator, $.raw_str_end_part, @@ -270,6 +270,11 @@ module.exports = grammar({ // Lexical Structure - https://docs.swift.org/swift-book/ReferenceManual/LexicalStructure.html //////////////////////////////// comment: ($) => token(prec(PRECS.comment, seq("//", /.*/))), + // Named wrapper for the unnamed `_multiline_comment` external token, so + // that multi-line comments still appear in the AST (e.g. as extras between + // top-level statements) without bleeding into class_body's $children when + // used as a class member separator. + multiline_comment: ($) => $._multiline_comment, // Identifiers simple_identifier: ($) => choice( @@ -1603,7 +1608,7 @@ module.exports = grammar({ field("base", $.unannotated_type), optional(seq(".", sep1(field("member", $.simple_identifier), "."))) ), - _class_member_separator: ($) => choice($._semi, $.multiline_comment), + _class_member_separator: ($) => choice($._semi, $._multiline_comment), _class_member_declarations: ($) => seq( sep1(field("member", $.type_level_declaration), $._class_member_separator), diff --git a/unified/extractor/tree-sitter-swift/node-types.yml b/unified/extractor/tree-sitter-swift/node-types.yml index 3b3b5807e47..47f56435d03 100644 --- a/unified/extractor/tree-sitter-swift/node-types.yml +++ b/unified/extractor/tree-sitter-swift/node-types.yml @@ -184,7 +184,6 @@ named: target: expression type: type class_body: - $children*: multiline_comment member*: type_level_declaration class_declaration: attribute*: attribute