Skip to content

Commit 693c651

Browse files
committed
Nested block comments
Problem ------- Nested block comments are not parsed, e.g. ``` /* /* */ */ ``` Solution ------- Leave $.comment for single-line comments. Introduce $.block_comment for block-comments. Make $.block_comment out of several tokens to be able to detect nested block comments
1 parent 17a19b0 commit 693c651

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

corpus/comments.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
================================================================================
2+
Single line comments
3+
================================================================================
4+
5+
// comment 1
6+
// comment 2
7+
8+
--------------------------------------------------------------------------------
9+
10+
(compilation_unit
11+
(comment)
12+
(comment))
13+
14+
================================================================================
15+
Block comments
16+
================================================================================
17+
/**/
18+
/** comment 1
19+
* /* comment 2
20+
* /* / * * /comment 3 */
21+
// comment 4
22+
* @param
23+
* */
24+
*/
25+
26+
--------------------------------------------------------------------------------
27+
28+
(compilation_unit
29+
(block_comment)
30+
(block_comment
31+
(block_comment
32+
(block_comment)
33+
(comment))))

corpus/expressions.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class C {
7676
(identifier)
7777
(operator_identifier)
7878
(integer_literal)))))
79-
(comment)
79+
(block_comment)
8080
(call_expression
8181
(field_expression
8282
(identifier)
@@ -813,7 +813,7 @@ class C {
813813
(identifier)
814814
(identifier))
815815
(comment)
816-
(comment)
816+
(block_comment)
817817
(identifier)))))))
818818

819819
================================================================================

grammar.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ module.exports = grammar({
2727

2828
extras: $ => [
2929
/\s/,
30-
$.comment
30+
$.comment,
31+
$.block_comment,
3132
],
3233

3334
supertypes: $ => [
@@ -1294,7 +1295,7 @@ module.exports = grammar({
12941295
wildcard: $ => '_',
12951296

12961297
/**
1297-
* Regex patterns created to avoid matching // comments.
1298+
* Regex patterns created to avoid matching // comments and /* comment starts.
12981299
* This could technically match illeagal tokens such as val ?// = 1
12991300
*/
13001301
operator_identifier: $ => token(choice(
@@ -1309,8 +1310,8 @@ module.exports = grammar({
13091310
seq(
13101311
// opchar
13111312
/[\-!#%&*+\/\\:<=>?@\u005e\u007c~\p{Sm}\p{So}]/,
1312-
// opchar minus slash
1313-
/[\-!#%&*+\\:<=>?@\u005e\u007c~\p{Sm}\p{So}]/,
1313+
// opchar minus slash and asterisk
1314+
/[\-!#%&+\\:<=>?@\u005e\u007c~\p{Sm}\p{So}]/,
13141315
// opchar*
13151316
repeat(/[\-!#%&*+\/\\:<=>?@\u005e\u007c~\p{Sm}\p{So}]/),
13161317
),
@@ -1520,14 +1521,14 @@ module.exports = grammar({
15201521
repeat1($.guard),
15211522
),
15221523

1523-
comment: $ => token(choice(
1524-
seq('//', /.*/),
1525-
seq(
1526-
'/*',
1527-
/[^*]*\*+([^/*][^*]*\*+)*/,
1528-
'/'
1529-
)
1530-
))
1524+
comment: $ => token(seq('//', /.*/)),
1525+
1526+
block_comment: $ => seq(
1527+
token('/*'),
1528+
repeat(token(/./)),
1529+
token('*/')
1530+
),
1531+
15311532
}
15321533
})
15331534

0 commit comments

Comments
 (0)