Skip to content

Commit 18ee4f1

Browse files
ProfDoofamaanq
authored andcommitted
feat: support line and block doc comments
1 parent 836903c commit 18ee4f1

File tree

5 files changed

+416
-117
lines changed

5 files changed

+416
-117
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ package-lock.json
77
!examples/ast.rs
88
!examples/weird-exprs.rs
99
/target/
10+
/fuzzer/

grammar.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ module.exports = grammar({
7474
$._string_content,
7575
$.raw_string_literal,
7676
$.float_literal,
77-
$.block_comment,
77+
$._outer_block_doc_comment,
78+
$._inner_block_doc_comment,
79+
'*/',
80+
$._error_sentinel,
7881
],
7982

8083
supertypes: $ => [
@@ -1497,9 +1500,42 @@ module.exports = grammar({
14971500
$.block_comment,
14981501
),
14991502

1500-
line_comment: _ => token(seq(
1501-
'//', /.*/,
1502-
)),
1503+
line_comment: $ => seq(
1504+
// All line comments start with two //
1505+
'//',
1506+
// Then are followed by:
1507+
// - 2 or more slashes making it a regular comment
1508+
// - 1 slash or 1 or more bang operators making it a doc comment
1509+
// - or just content for the comment
1510+
choice(
1511+
// A tricky edge case where what looks like a doc comment is not
1512+
seq(token.immediate(prec(2, /\/\//)), /.*/),
1513+
// A regular doc comment
1514+
seq(field('doc', alias($._line_doc_comment, $.doc_comment)), /.*/),
1515+
token.immediate(prec(1, /.*/)),
1516+
),
1517+
),
1518+
1519+
_line_doc_comment: $ => choice(
1520+
// An outer line doc comment applies to the element that it is outside of
1521+
field('outer', alias($._outer_line_doc_comment, $.outer_doc_comment)),
1522+
// An inner line doc comment applies to the element it is inside of
1523+
field('inner', alias($._inner_line_doc_comment, $.inner_doc_comment)),
1524+
),
1525+
1526+
_inner_line_doc_comment: _ => token.immediate(prec(2, '!')),
1527+
_outer_line_doc_comment: _ => token.immediate(prec(2, /\/[^\/]/)),
1528+
1529+
block_comment: $ => seq(
1530+
'/*',
1531+
optional(field('doc', alias($._block_doc_comment, $.doc_comment))),
1532+
'*/',
1533+
),
1534+
1535+
_block_doc_comment: $ => choice(
1536+
field('inner', alias($._inner_block_doc_comment, $.inner_doc_comment)),
1537+
field('outer', alias($._outer_block_doc_comment, $.outer_doc_comment)),
1538+
),
15031539

15041540
_path: $ => choice(
15051541
$.self,

queries/highlights.scm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@
7070
(line_comment) @comment
7171
(block_comment) @comment
7272

73+
(line_comment (doc_comment)) @comment.documentation
74+
(block_comment (doc_comment)) @comment.documentation
75+
7376
"(" @punctuation.bracket
7477
")" @punctuation.bracket
7578
"[" @punctuation.bracket

0 commit comments

Comments
 (0)