Skip to content

Commit 52d7283

Browse files
committed
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.
1 parent eb480d1 commit 52d7283

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ module.exports = grammar({
194194
// `/*`, and decrement it whenever we see `*/`. A standard grammar would only be able to exit the comment at the
195195
// first `*/` (like C does). Similarly, when you start a string with `##"`, you're required to include the same
196196
// number of `#` symbols to end it.
197-
$.multiline_comment,
197+
$._multiline_comment,
198198
$.raw_str_part,
199199
$.raw_str_continuing_indicator,
200200
$.raw_str_end_part,
@@ -270,6 +270,11 @@ module.exports = grammar({
270270
// Lexical Structure - https://docs.swift.org/swift-book/ReferenceManual/LexicalStructure.html
271271
////////////////////////////////
272272
comment: ($) => token(prec(PRECS.comment, seq("//", /.*/))),
273+
// Named wrapper for the unnamed `_multiline_comment` external token, so
274+
// that multi-line comments still appear in the AST (e.g. as extras between
275+
// top-level statements) without bleeding into class_body's $children when
276+
// used as a class member separator.
277+
multiline_comment: ($) => $._multiline_comment,
273278
// Identifiers
274279
simple_identifier: ($) =>
275280
choice(
@@ -1603,7 +1608,7 @@ module.exports = grammar({
16031608
field("base", $.unannotated_type),
16041609
optional(seq(".", sep1(field("member", $.simple_identifier), ".")))
16051610
),
1606-
_class_member_separator: ($) => choice($._semi, $.multiline_comment),
1611+
_class_member_separator: ($) => choice($._semi, $._multiline_comment),
16071612
_class_member_declarations: ($) =>
16081613
seq(
16091614
sep1(field("member", $.type_level_declaration), $._class_member_separator),

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ named:
184184
target: expression
185185
type: type
186186
class_body:
187-
$children*: multiline_comment
188187
member*: type_level_declaration
189188
class_declaration:
190189
attribute*: attribute

0 commit comments

Comments
 (0)