Make comments a single node in the grammar

This looks to be a *strong* idiom in tree-sitter parsers.
This commit is contained in:
Bruno BELANYI 2024-04-11 18:33:52 +01:00
parent aa8472e73f
commit b62fc19da9
6 changed files with 588 additions and 723 deletions

View file

@ -11,8 +11,7 @@ module.exports = grammar({
extras: ($) => [
/\s+/,
$.line_comment,
$.block_comment,
$.comment,
],
rules: {
@ -23,9 +22,10 @@ module.exports = grammar({
$.module,
),
line_comment: (_) => seq("//", /[^\n]*/),
block_comment: (_) => seq("/*", /[^*]*\*+([^/*][^*]*\*+)*/, '/'),
comment: (_) => choice(
seq("//", /[^\n]*/),
seq("/*", /[^*]*\*+([^/*][^*]*\*+)*/, '/'),
),
// Definitions {{{

View file

@ -1,7 +1,4 @@
[
(line_comment)
(block_comment)
] @comment
(comment) @comment
; Operators {{{
(operator) @operator

57
src/grammar.json generated
View file

@ -21,33 +21,38 @@
}
]
},
"line_comment": {
"type": "SEQ",
"comment": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "//"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "//"
},
{
"type": "PATTERN",
"value": "[^\\n]*"
}
]
},
{
"type": "PATTERN",
"value": "[^\\n]*"
}
]
},
"block_comment": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "/*"
},
{
"type": "PATTERN",
"value": "[^*]*\\*+([^/*][^*]*\\*+)*"
},
{
"type": "STRING",
"value": "/"
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "/*"
},
{
"type": "PATTERN",
"value": "[^*]*\\*+([^/*][^*]*\\*+)*"
},
{
"type": "STRING",
"value": "/"
}
]
}
]
},
@ -956,11 +961,7 @@
},
{
"type": "SYMBOL",
"name": "line_comment"
},
{
"type": "SYMBOL",
"name": "block_comment"
"name": "comment"
}
],
"conflicts": [],

9
src/node-types.json generated
View file

@ -168,12 +168,12 @@
}
},
{
"type": "block_comment",
"type": "boolean_literal",
"named": true,
"fields": {}
},
{
"type": "boolean_literal",
"type": "comment",
"named": true,
"fields": {}
},
@ -259,11 +259,6 @@
]
}
},
{
"type": "line_comment",
"named": true,
"fields": {}
},
{
"type": "list_expression",
"named": true,

1208
src/parser.c generated

File diff suppressed because it is too large Load diff

View file

@ -7,7 +7,7 @@ Empty comment
--------------------------------------------------------------------------------
(source_file
(line_comment))
(comment))
================================================================================
Single comment
@ -18,7 +18,7 @@ Single comment
--------------------------------------------------------------------------------
(source_file
(line_comment))
(comment))
================================================================================
Multiple comments
@ -30,8 +30,8 @@ Multiple comments
--------------------------------------------------------------------------------
(source_file
(line_comment)
(line_comment))
(comment)
(comment))
================================================================================
Empty block comment
@ -42,7 +42,7 @@ Empty block comment
--------------------------------------------------------------------------------
(source_file
(block_comment))
(comment))
================================================================================
Whitespace block comment
@ -53,7 +53,7 @@ Whitespace block comment
--------------------------------------------------------------------------------
(source_file
(block_comment))
(comment))
================================================================================
Block comment
@ -64,7 +64,7 @@ Block comment
--------------------------------------------------------------------------------
(source_file
(block_comment))
(comment))
================================================================================
Block comment with slashes
@ -75,7 +75,7 @@ Block comment with slashes
--------------------------------------------------------------------------------
(source_file
(block_comment))
(comment))
================================================================================
Block comment with asterisks
@ -86,7 +86,7 @@ Block comment with asterisks
--------------------------------------------------------------------------------
(source_file
(block_comment))
(comment))
================================================================================
Block comment (multiline)
@ -103,7 +103,7 @@ Block comment (multiline)
--------------------------------------------------------------------------------
(source_file
(block_comment))
(comment))
================================================================================
Block comment is not recursive
@ -114,7 +114,7 @@ Block comment is not recursive
--------------------------------------------------------------------------------
(source_file
(block_comment))
(comment))
================================================================================
Unterminated comment