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:
parent
06d75be374
commit
9bc53cd0df
|
@ -79,7 +79,7 @@ message_args = [
|
|||
module.exports = grammar({
|
||||
name: "cmake",
|
||||
|
||||
externals: ($) => [$.bracket_argument],
|
||||
externals: ($) => [$.bracket_argument, $.bracket_comment],
|
||||
extras: ($) => [/[\s\n\r]/, $.comment],
|
||||
|
||||
rules: {
|
||||
|
@ -141,7 +141,7 @@ module.exports = grammar({
|
|||
$.message_command
|
||||
),
|
||||
|
||||
comment: ($) => seq("#", choice($.bracket_argument)),
|
||||
comment: ($) => choice($.bracket_comment),
|
||||
|
||||
...commandNames(...commands),
|
||||
identifier: (_) => /[A-Za-z_][A-Za-z0-9_]*/,
|
||||
|
|
|
@ -1911,20 +1911,11 @@
|
|||
]
|
||||
},
|
||||
"comment": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "#"
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "bracket_argument"
|
||||
}
|
||||
]
|
||||
"name": "bracket_comment"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
@ -2005,6 +1996,10 @@
|
|||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "bracket_argument"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "bracket_comment"
|
||||
}
|
||||
],
|
||||
"inline": [],
|
||||
|
|
|
@ -46,7 +46,7 @@
|
|||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "bracket_argument",
|
||||
"type": "bracket_comment",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
|
@ -730,10 +730,6 @@
|
|||
"type": "\"",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "#",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "$CACHE",
|
||||
"named": false
|
||||
|
@ -1018,6 +1014,10 @@
|
|||
"type": "bracket_argument",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "bracket_comment",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "else",
|
||||
"named": true
|
||||
|
|
18519
src/parser.c
18519
src/parser.c
File diff suppressed because it is too large
Load diff
|
@ -3,16 +3,19 @@
|
|||
#include <tree_sitter/parser.h>
|
||||
|
||||
namespace {
|
||||
enum TokenType { BRACKET_ARGUMENT, LINE_COMMENT };
|
||||
enum TokenType { BRACKET_ARGUMENT, BRACKET_COMMENT };
|
||||
void skip(TSLexer *lexer) { lexer->advance(lexer, true); }
|
||||
void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
|
||||
bool scan_bracket_argument(TSLexer *lexer) {
|
||||
while (std::iswspace(lexer->lookahead))
|
||||
bool scan_bracket_argument(TSLexer *lexer, bool skip_wspace) {
|
||||
if (skip_wspace) {
|
||||
while (std::iswspace(lexer->lookahead)) {
|
||||
skip(lexer);
|
||||
}
|
||||
}
|
||||
|
||||
if (lexer->lookahead != '[')
|
||||
if (lexer->lookahead != '[') {
|
||||
return false;
|
||||
|
||||
}
|
||||
advance(lexer);
|
||||
|
||||
int open_level = 0;
|
||||
|
@ -21,8 +24,9 @@ bool scan_bracket_argument(TSLexer *lexer) {
|
|||
advance(lexer);
|
||||
}
|
||||
|
||||
if (lexer->lookahead != '[')
|
||||
if (lexer->lookahead != '[') {
|
||||
return false;
|
||||
}
|
||||
|
||||
while (lexer->lookahead != '\0') {
|
||||
advance(lexer);
|
||||
|
@ -44,9 +48,22 @@ bool scan_bracket_argument(TSLexer *lexer) {
|
|||
}
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue