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

View file

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

57
src/grammar.json generated
View file

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

9
src/node-types.json generated
View file

@ -168,12 +168,12 @@
} }
}, },
{ {
"type": "block_comment", "type": "boolean_literal",
"named": true, "named": true,
"fields": {} "fields": {}
}, },
{ {
"type": "boolean_literal", "type": "comment",
"named": true, "named": true,
"fields": {} "fields": {}
}, },
@ -259,11 +259,6 @@
] ]
} }
}, },
{
"type": "line_comment",
"named": true,
"fields": {}
},
{ {
"type": "list_expression", "type": "list_expression",
"named": true, "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 (source_file
(line_comment)) (comment))
================================================================================ ================================================================================
Single comment Single comment
@ -18,7 +18,7 @@ Single comment
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(line_comment)) (comment))
================================================================================ ================================================================================
Multiple comments Multiple comments
@ -30,8 +30,8 @@ Multiple comments
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(line_comment) (comment)
(line_comment)) (comment))
================================================================================ ================================================================================
Empty block comment Empty block comment
@ -42,7 +42,7 @@ Empty block comment
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(block_comment)) (comment))
================================================================================ ================================================================================
Whitespace block comment Whitespace block comment
@ -53,7 +53,7 @@ Whitespace block comment
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(block_comment)) (comment))
================================================================================ ================================================================================
Block comment Block comment
@ -64,7 +64,7 @@ Block comment
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(block_comment)) (comment))
================================================================================ ================================================================================
Block comment with slashes Block comment with slashes
@ -75,7 +75,7 @@ Block comment with slashes
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(block_comment)) (comment))
================================================================================ ================================================================================
Block comment with asterisks Block comment with asterisks
@ -86,7 +86,7 @@ Block comment with asterisks
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(block_comment)) (comment))
================================================================================ ================================================================================
Block comment (multiline) Block comment (multiline)
@ -103,7 +103,7 @@ Block comment (multiline)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(block_comment)) (comment))
================================================================================ ================================================================================
Block comment is not recursive Block comment is not recursive
@ -114,7 +114,7 @@ Block comment is not recursive
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(source_file (source_file
(block_comment)) (comment))
================================================================================ ================================================================================
Unterminated comment Unterminated comment