Add external parser for parsing bracket_argument
and also adjust the
test cases accordingly
This commit is contained in:
parent
364423376f
commit
0988d2ce86
|
@ -22,7 +22,7 @@ message([[an argument]])
|
|||
(source_file
|
||||
(message_command
|
||||
(message)
|
||||
(argument (bracket_argument (bracket_content)))
|
||||
(argument (bracket_argument))
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -36,8 +36,8 @@ message([[first argument]] [[second argument]])
|
|||
(source_file
|
||||
(message_command
|
||||
(message)
|
||||
(argument (bracket_argument (bracket_content)))
|
||||
(argument (bracket_argument (bracket_content)))
|
||||
(argument (bracket_argument))
|
||||
(argument (bracket_argument))
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -54,8 +54,8 @@ message(
|
|||
(source_file
|
||||
(message_command
|
||||
(message)
|
||||
(argument (bracket_argument (bracket_content)))
|
||||
(argument (bracket_argument (bracket_content)))
|
||||
(argument (bracket_argument))
|
||||
(argument (bracket_argument))
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -71,7 +71,23 @@ with line break
|
|||
(source_file
|
||||
(message_command
|
||||
(message)
|
||||
(argument (bracket_argument (bracket_content)))
|
||||
(argument (bracket_argument))
|
||||
)
|
||||
)
|
||||
|
||||
=====================================================================================
|
||||
Bracket argument with embedded brackets and equal signs line break [bracket_argument]
|
||||
=====================================================================================
|
||||
|
||||
message([===[an argument
|
||||
with line break ]==]
|
||||
]===])
|
||||
|
||||
---
|
||||
(source_file
|
||||
(message_command
|
||||
(message)
|
||||
(argument (bracket_argument))
|
||||
)
|
||||
)
|
||||
|
||||
|
|
|
@ -41,3 +41,18 @@ message(
|
|||
(message)
|
||||
)
|
||||
)
|
||||
|
||||
==================================================
|
||||
Message with STATUS and bracket argument [message]
|
||||
==================================================
|
||||
|
||||
message(STATUS [=[Some argument ]==] ]=] )
|
||||
|
||||
---
|
||||
|
||||
(source_file
|
||||
(message_command
|
||||
(message)
|
||||
(argument (bracket_argument))
|
||||
)
|
||||
)
|
||||
|
|
|
@ -79,6 +79,8 @@ message_args = [
|
|||
module.exports = grammar({
|
||||
name: "cmake",
|
||||
|
||||
externals: ($) => [$.bracket_argument],
|
||||
|
||||
rules: {
|
||||
source_file: ($) => repeat($._command_invocation),
|
||||
|
||||
|
@ -95,11 +97,6 @@ module.exports = grammar({
|
|||
|
||||
argument: ($) => choice($.bracket_argument, $.quoted_argument, $.unquoted_argument),
|
||||
|
||||
bracket_argument: ($) => seq($._bracket_open, optional($.bracket_content), $._bracket_close),
|
||||
_bracket_open: (_) => seq("[", repeat("="), "["),
|
||||
bracket_content: (_) => repeat1(/[^\]]/),
|
||||
_bracket_close: (_) => seq("]", repeat("="), "]"),
|
||||
|
||||
quoted_argument: ($) => seq('"', optional($.quoted_element), '"'),
|
||||
quoted_element: ($) => repeat1(choice($.variable_ref, /[^\\"]/, $.escape_sequence, seq("\\", newline()))),
|
||||
|
||||
|
|
|
@ -163,78 +163,6 @@
|
|||
}
|
||||
]
|
||||
},
|
||||
"bracket_argument": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "_bracket_open"
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "bracket_content"
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "_bracket_close"
|
||||
}
|
||||
]
|
||||
},
|
||||
"_bracket_open": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "["
|
||||
},
|
||||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
"type": "STRING",
|
||||
"value": "="
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "["
|
||||
}
|
||||
]
|
||||
},
|
||||
"bracket_content": {
|
||||
"type": "REPEAT1",
|
||||
"content": {
|
||||
"type": "PATTERN",
|
||||
"value": "[^\\]]"
|
||||
}
|
||||
},
|
||||
"_bracket_close": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "]"
|
||||
},
|
||||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
"type": "STRING",
|
||||
"value": "="
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "]"
|
||||
}
|
||||
]
|
||||
},
|
||||
"quoted_argument": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
|
@ -3337,7 +3265,12 @@
|
|||
],
|
||||
"conflicts": [],
|
||||
"precedences": [],
|
||||
"externals": [],
|
||||
"externals": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "bracket_argument"
|
||||
}
|
||||
],
|
||||
"inline": [],
|
||||
"supertypes": []
|
||||
}
|
||||
|
|
|
@ -22,26 +22,6 @@
|
|||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "bracket_argument",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": false,
|
||||
"required": false,
|
||||
"types": [
|
||||
{
|
||||
"type": "bracket_content",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "bracket_content",
|
||||
"named": true,
|
||||
"fields": {}
|
||||
},
|
||||
{
|
||||
"type": "cache_var",
|
||||
"named": true,
|
||||
|
@ -763,10 +743,6 @@
|
|||
"type": "1",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "=",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "AND",
|
||||
"named": false
|
||||
|
@ -1007,10 +983,6 @@
|
|||
"type": "ZIP_LISTS",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "[",
|
||||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "\\",
|
||||
"named": false
|
||||
|
@ -1028,8 +1000,8 @@
|
|||
"named": false
|
||||
},
|
||||
{
|
||||
"type": "]",
|
||||
"named": false
|
||||
"type": "bracket_argument",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "else",
|
||||
|
|
26203
src/parser.c
26203
src/parser.c
File diff suppressed because it is too large
Load diff
69
src/scanner.cc
Normal file
69
src/scanner.cc
Normal file
|
@ -0,0 +1,69 @@
|
|||
#include <cwctype>
|
||||
#include <iostream>
|
||||
#include <tree_sitter/parser.h>
|
||||
|
||||
namespace {
|
||||
enum TokenType { BRACKET_ARGUMENT };
|
||||
void skip(TSLexer *lexer) { lexer->advance(lexer, true); }
|
||||
void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
|
||||
bool scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
|
||||
using std::iswspace;
|
||||
|
||||
if (!valid_symbols[BRACKET_ARGUMENT])
|
||||
return false;
|
||||
|
||||
while (iswspace(lexer->lookahead))
|
||||
skip(lexer);
|
||||
|
||||
if (lexer->lookahead != '[')
|
||||
return false;
|
||||
|
||||
advance(lexer);
|
||||
|
||||
int open_level = 0;
|
||||
while (lexer->lookahead == '=') {
|
||||
++open_level;
|
||||
advance(lexer);
|
||||
}
|
||||
|
||||
if (lexer->lookahead != '[')
|
||||
return false;
|
||||
|
||||
while (lexer->lookahead != '\0') {
|
||||
advance(lexer);
|
||||
if (lexer->lookahead == ']') {
|
||||
advance(lexer);
|
||||
|
||||
int close_level = 0;
|
||||
while (lexer->lookahead == '=') {
|
||||
++close_level;
|
||||
advance(lexer);
|
||||
}
|
||||
|
||||
if (lexer->lookahead == ']' && close_level == open_level) {
|
||||
advance(lexer);
|
||||
lexer->result_symbol = BRACKET_ARGUMENT;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
extern "C" {
|
||||
void *tree_sitter_cmake_external_scanner_create() { return nullptr; }
|
||||
void tree_sitter_cmake_external_scanner_destroy(void *payload) {}
|
||||
unsigned tree_sitter_cmake_external_scanner_serialize(void *payload,
|
||||
char *buffer) {
|
||||
return 0;
|
||||
}
|
||||
void tree_sitter_cmake_external_scanner_deserialize(void *payload,
|
||||
const char *buffer,
|
||||
unsigned length) {}
|
||||
bool tree_sitter_cmake_external_scanner_scan(void *payload, TSLexer *lexer,
|
||||
const bool *valid_symbols) {
|
||||
return scan(payload, lexer, valid_symbols);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue