Skip to content

Commit 6254b2a

Browse files
authored
Separate line and block comments into distinct node types (#271)
Replace the single `comment` external token (and the `_newline_and_comment` hack) with two distinct node types: `line_comment` (a grammar-level token matching `//...`) and `block_comment` (handled by the external scanner). This makes comment node types more precise and removes the workaround of aliasing `_newline_and_comment` as `comment` in statement delimiters. Update all query files and corpus tests to reference the new node names. Close #135 Co-authored-by: Claude Opus 4.7 noreply@anthropic.com
1 parent 3184459 commit 6254b2a

13 files changed

Lines changed: 142426 additions & 138927 deletions

File tree

grammar.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ module.exports = grammar({
55

66
externals: ($) => [
77
$._newline,
8-
$.comment,
9-
$._newline_and_comment,
8+
$.block_comment,
109
'"',
1110
"`",
1211
$._template_chars,
@@ -18,7 +17,12 @@ module.exports = grammar({
1817
$._decorator_inline,
1918
],
2019

21-
extras: ($) => [$.comment, $.decorator, /[\s\uFEFF\u2060\u200B\u00A0]/],
20+
extras: ($) => [
21+
$.block_comment,
22+
$.line_comment,
23+
$.decorator,
24+
/[\s\uFEFF\u2060\u200B\u00A0]/,
25+
],
2226

2327
supertypes: ($) => [
2428
$.statement,
@@ -121,8 +125,9 @@ module.exports = grammar({
121125

122126
_statement: ($) => seq($.statement, repeat1($._statement_delimeter)),
123127

124-
_statement_delimeter: ($) =>
125-
choice(";", $._newline, alias($._newline_and_comment, $.comment)),
128+
_statement_delimeter: ($) => choice(";", $._newline),
129+
130+
line_comment: ($) => token(seq("//", /[^\n]*/)),
126131

127132
_one_or_more_statements: ($) =>
128133
seq(repeat($._statement), $.statement, optional($._statement_delimeter)),

queries/highlights.scm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
(comment) @comment
1+
[
2+
(line_comment)
3+
(block_comment)
4+
] @comment
25

36
; Identifiers
47
;------------

queries/injections.scm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
((comment) @injection.content (#set! injection.language "comment"))
1+
([
2+
(line_comment)
3+
(block_comment)
4+
] @injection.content (#set! injection.language "comment"))
25

36
; regex literal
47
(regex

queries/textobjects.scm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
; Comments
2525
;---------
2626

27-
(comment) @comment.outer
27+
[
28+
(line_comment)
29+
(block_comment)
30+
] @comment.outer
2831

2932
; Parameters
3033
;-----------

src/grammar.json

Lines changed: 22 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/node-types.json

Lines changed: 10 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)