Parse bracket comment using external scanner

- Due to bracket comment has to have the pattern /[=*[/ follow '#'
  symbol immediately, it's easier to implement it using external scanner
This commit is contained in:
Uy Ha 2021-06-26 23:30:27 +02:00
parent 06d75be374
commit 9bc53cd0df
5 changed files with 9290 additions and 9295 deletions

View file

@ -79,7 +79,7 @@ message_args = [
module.exports = grammar({ module.exports = grammar({
name: "cmake", name: "cmake",
externals: ($) => [$.bracket_argument], externals: ($) => [$.bracket_argument, $.bracket_comment],
extras: ($) => [/[\s\n\r]/, $.comment], extras: ($) => [/[\s\n\r]/, $.comment],
rules: { rules: {
@ -141,7 +141,7 @@ module.exports = grammar({
$.message_command $.message_command
), ),
comment: ($) => seq("#", choice($.bracket_argument)), comment: ($) => choice($.bracket_comment),
...commandNames(...commands), ...commandNames(...commands),
identifier: (_) => /[A-Za-z_][A-Za-z0-9_]*/, identifier: (_) => /[A-Za-z_][A-Za-z0-9_]*/,

View file

@ -1911,20 +1911,11 @@
] ]
}, },
"comment": { "comment": {
"type": "SEQ", "type": "CHOICE",
"members": [ "members": [
{ {
"type": "STRING", "type": "SYMBOL",
"value": "#" "name": "bracket_comment"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "bracket_argument"
}
]
} }
] ]
}, },
@ -2005,6 +1996,10 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "bracket_argument" "name": "bracket_argument"
},
{
"type": "SYMBOL",
"name": "bracket_comment"
} }
], ],
"inline": [], "inline": [],

View file

@ -46,7 +46,7 @@
"required": true, "required": true,
"types": [ "types": [
{ {
"type": "bracket_argument", "type": "bracket_comment",
"named": true "named": true
} }
] ]
@ -730,10 +730,6 @@
"type": "\"", "type": "\"",
"named": false "named": false
}, },
{
"type": "#",
"named": false
},
{ {
"type": "$CACHE", "type": "$CACHE",
"named": false "named": false
@ -1018,6 +1014,10 @@
"type": "bracket_argument", "type": "bracket_argument",
"named": true "named": true
}, },
{
"type": "bracket_comment",
"named": true
},
{ {
"type": "else", "type": "else",
"named": true "named": true

18519
src/parser.c

File diff suppressed because it is too large Load diff

View file

@ -3,16 +3,19 @@
#include <tree_sitter/parser.h> #include <tree_sitter/parser.h>
namespace { namespace {
enum TokenType { BRACKET_ARGUMENT, LINE_COMMENT }; enum TokenType { BRACKET_ARGUMENT, BRACKET_COMMENT };
void skip(TSLexer *lexer) { lexer->advance(lexer, true); } void skip(TSLexer *lexer) { lexer->advance(lexer, true); }
void advance(TSLexer *lexer) { lexer->advance(lexer, false); } void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
bool scan_bracket_argument(TSLexer *lexer) { bool scan_bracket_argument(TSLexer *lexer, bool skip_wspace) {
while (std::iswspace(lexer->lookahead)) if (skip_wspace) {
skip(lexer); while (std::iswspace(lexer->lookahead)) {
skip(lexer);
}
}
if (lexer->lookahead != '[') if (lexer->lookahead != '[') {
return false; return false;
}
advance(lexer); advance(lexer);
int open_level = 0; int open_level = 0;
@ -21,8 +24,9 @@ bool scan_bracket_argument(TSLexer *lexer) {
advance(lexer); advance(lexer);
} }
if (lexer->lookahead != '[') if (lexer->lookahead != '[') {
return false; return false;
}
while (lexer->lookahead != '\0') { while (lexer->lookahead != '\0') {
advance(lexer); advance(lexer);
@ -44,9 +48,22 @@ bool scan_bracket_argument(TSLexer *lexer) {
} }
return false; return false;
} }
bool scan_bracket_comment(TSLexer *lexer) {
if (lexer->lookahead != '#') {
return false;
}
advance(lexer);
if (scan_bracket_argument(lexer, false)) {
lexer->result_symbol = BRACKET_COMMENT;
return true;
}
return false;
}
bool scan(void *payload, TSLexer *lexer, const bool *valid_symbols) { bool scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
if (valid_symbols[BRACKET_ARGUMENT]) if (valid_symbols[BRACKET_ARGUMENT])
return scan_bracket_argument(lexer); return scan_bracket_argument(lexer, true);
if (valid_symbols[BRACKET_ARGUMENT])
return scan_bracket_comment(lexer);
return false; return false;
} }