Apply consitant prec like treetop would

This commit is contained in:
Preston Baxter 2024-01-27 00:26:02 -06:00
parent 94c2f540ec
commit 7aadb6bb0f
4 changed files with 2486 additions and 2452 deletions

View File

@ -1,210 +1,205 @@
module.exports = grammar({
name: 'logstash',
name: 'logstash',
extras: $ => [
/\s/,
/\r?\n/,
$.comment
],
extras: $ => [
/\s/,
/\r?\n/,
$.comment
],
rules: {
// TODO: add the actual grammar rules
source_file: $ => repeat(seq($.plugin_section)),
rules: {
// TODO: add the actual grammar rules
source_file: $ => repeat(seq($.plugin_section)),
double_quoted_string: $ => seq(
"\"",
$.double_contents,
"\""
),
double_quoted_string: $ => seq(
"\"",
$.double_contents,
"\""
),
single_quoted_string: $ => seq(
"\'",
$.single_contents,
"\'"
),
single_quoted_string: $ => seq(
"\'",
$.single_contents,
"\'"
),
code_string: $ => seq(
"`",
$.code_contents,
"`"
),
code_string: $ => seq(
"`",
$.code_contents,
"`"
),
code_contents: $ => /[^`]+/,
code_contents: $ => /[^`]+/,
double_contents: $ => /[^"]+/,
double_contents: $ => /[^"]+/,
single_contents: $ => /[^']+/,
single_contents: $ => /[^']+/,
number: $ => seq(optional("-"), /[0-9]+/, optional(seq(".", repeat(/[0-9]/)))),
boolean: $ => choice($.true, $.false),
true: $ => "true",
false: $ => "false",
bareword: $ => /[A-Za-z_][A-Za-z0-9_]+/,
boolean_operator: $ => choice("and", "or", "xor", "nand"),
regexp_operator: $ => choice("=~", "!~"),
regexp: $ => seq("/", repeat(choice("\\/", /[^\/]/)) ,"/"),
string: $ => choice(
$.double_quoted_string,
$.single_quoted_string,
$.code_string
),
number: $ => seq(optional("-"), /[0-9]+/, optional(seq(".", repeat(/[0-9]/)))),
boolean: $ => choice($.true, $.false),
true: $ => "true",
false: $ => "false",
bareword: $ => /[A-Za-z_][A-Za-z0-9_]+/,
boolean_operator: $ => choice(prec(4, "and"), prec(4, "or"), prec(3, "xor"), prec(3, "nand")),
regexp_operator: $ => choice("=~", "!~"),
regexp: $ => seq("/", repeat(choice("\\/", /[^\/]/)), "/"),
string: $ => choice(
$.double_quoted_string,
$.single_quoted_string,
$.code_string
),
cs: $ => repeat1(seq(
$.comment,
)),
cs: $ => repeat1(seq(
$.comment,
)),
_comment_char: $ => "#",
_comment_char: $ => "#",
comment: $ => seq(
$._comment_char,
$._comment_contents,
),
comment: $ => seq(
$._comment_char,
$._comment_contents,
),
_comment_contents: $ => /[^\r\n]*/,
carriage_return: $ => /\r/,
whitespace: $ => /[ \t]+/,
_comment_contents: $ => /[^\r\n]*/,
carriage_return: $ => /\r/,
whitespace: $ => /[ \t]+/,
plugin_section: $ => seq(
$.plugin_type,
$._plugin_open,
repeat($.branch_or_plugin),
$._plugin_close,
),
plugin_section: $ => seq(
$.plugin_type,
$._plugin_open,
repeat($.branch_or_plugin),
$._plugin_close,
),
plugin_type: $ => choice('input', 'filter', 'output'),
plugin_name: $ => /[A-Za-z0-9_-]+/,
plugin_content: $ => /[A-Za-z0-9_-]+/,
plugin_type: $ => choice('input', 'filter', 'output'),
plugin_name: $ => /[A-Za-z0-9_-]+/,
plugin_content: $ => /[A-Za-z0-9_-]+/,
plugin: $ => seq(
alias($.bareword, $.plugin_name),
"{",
field("attributes", optional(repeat1($.attribute))),
"}",
),
plugin: $ => seq(
alias($.bareword, $.plugin_name),
"{",
field("attributes", optional(repeat1($.attribute))),
"}",
),
name: $ => choice(
/[A-Za-z0-9_-]+/,
$.string
),
name: $ => choice(
/[A-Za-z0-9_-]+/,
$.string
),
arrow: $ => "=>",
arrow: $ => "=>",
attribute: $ => seq(
$.name,
$.arrow,
$.value
),
attribute: $ => seq(
$.name,
$.arrow,
$.value
),
branch: $ => seq(
$.if,
repeat($.else_if),
optional($.else)
),
branch: $ => seq(
prec(10, $.if),
prec(9, repeat($.else_if)),
prec(8, optional($.else))
),
if: $ => seq("if", $.condition, "{", repeat($.branch_or_plugin), "}"),
else_if: $ => seq("elseif", $.condition, "{", repeat($.branch_or_plugin), "}"),
else: $ => seq("else", $.condition, "{", repeat($.branch_or_plugin), "}"),
if: $ => seq("if", $.condition, "{", repeat($.branch_or_plugin), "}"),
else_if: $ => seq("elseif", $.condition, "{", repeat($.branch_or_plugin), "}"),
else: $ => seq("else", $.condition, "{", repeat($.branch_or_plugin), "}"),
condition: $ => seq($.expression, repeat(seq($.boolean_operator, $.expression))),
condition: $ => seq($.expression, repeat(seq($.boolean_operator, $.expression))),
expression: $ => choice(
seq("(", $.condition, ")"),
$.negative_expression,
$.in_expression,
$.not_in_expression,
$.compare_expression,
$.regexp_expression,
$.rvalue
),
expression: $ => choice(
prec(10, seq("(", $.condition, ")")),
prec(9, $.negative_expression),
prec(8, $.in_expression),
prec(7, $.not_in_expression),
prec(6, $.compare_expression),
prec(5, $.regexp_expression),
prec(4, $.rvalue)
),
negative_expression: $ => choice(
seq("!", "(", $.condition, ")"),
seq("!", $.selector)
),
in_expression: $ => seq($.rvalue, $.in_operator, $.rvalue),
in_operator: $ => "in",
not_in_expression: $ => seq($.rvalue, $.not_in_operator, $.rvalue),
not_in_operator: $ => seq("not", "in"),
negative_expression: $ => choice(
prec(1, seq("!", "(", $.condition, ")")),
seq("!", $.selector)
),
in_expression: $ => seq($.rvalue, $.in_operator, $.rvalue),
in_operator: $ => "in",
not_in_expression: $ => seq($.rvalue, $.not_in_operator, $.rvalue),
not_in_operator: $ => seq("not", "in"),
regexp_expression: $ => seq($.rvalue, $.regexp_operator, choice($.string, $.regexp)),
regexp_expression: $ => seq($.rvalue, $.regexp_operator, choice(prec(1,$.string), $.regexp)),
rvalue: $ => choice(
$.string,
$.number,
$.boolean,
$.selector,
$.array,
$.method_call,
$.regexp,
),
rvalue: $ => choice(
prec(10, $.string),
prec(9, $.number),
prec(8, $.boolean),
prec(7, $.selector),
prec(6, $.array),
prec(5, $.method_call),
prec(4, $.regexp),
),
value: $ => choice(
prec(10, $.plugin),
prec(9, $.bareword),
prec(8, $.string),
prec(7, $.number),
prec(7, $.boolean),
prec(6, $.array),
prec(5, $.hash)
),
value: $ => choice(
prec(10, $.plugin),
prec(9, $.bareword),
prec(8, $.string),
prec(7, $.number),
prec(7, $.boolean),
prec(6, $.array),
prec(5, $.hash)
),
array_value: $ => choice(
$.bareword,
$.string,
$.number,
$.boolean,
$.array,
$.hash
),
array_value: $ => choice(
prec(10, $.bareword),
prec(9, $.string),
prec(8, $.number),
prec(7, $.boolean),
prec(6, $.array),
prec(5, $.hash)
),
selector: $ => repeat1($.selector_element),
selector_element: $ => /\[[^\]\[,]+\]/,
selector: $ => repeat1($.selector_element),
selector_element: $ => /\[[^\]\[,]+\]/,
array: $ => seq(
"[",
optional(seq($.value, repeat(seq(",", $.value)))),
"]"
),
array: $ => seq(
"[",
optional(seq($.value, repeat(seq(",", $.value)))),
"]"
),
hash: $ => seq(
"{",
optional($.hashentries),
"}",
),
hash: $ => seq(
"{",
optional($.hashentries),
"}",
),
hashentries: $ => seq(
$.hashentry, repeat($.hashentry)
),
hashentries: $ => seq(
$.hashentry, repeat($.hashentry)
),
hashentry: $ => seq(
field("name", choice($.number, $.bareword, $.string)),
$.arrow,
$.value
),
hashentry: $ => seq(
field("name", choice(prec(10, $.number), prec(9, $.bareword), prec(7, $.string))),
$.arrow,
$.value
),
method_call: $ => seq(
$.method, "(", optional(seq($.rvalue, repeat(seq(",", $.rvalue)))), ")"
),
method_call: $ => seq(
$.method, "(", optional(seq($.rvalue, repeat(seq(",", $.rvalue)))), ")"
),
method: $ => $.bareword,
method: $ => $.bareword,
compare_expression: $ => seq(
$.rvalue, $.compare_operator, $.rvalue,
),
compare_expression: $ => seq(
$.rvalue, $.compare_operator, $.rvalue,
),
compare_operator: $ => choice(
"==", "!=", "<=", ">=", "<", ">"
),
compare_operator: $ => choice(
"==", "!=", "<=", ">=", "<", ">"
),
branch_or_plugin: $ => prec(15, choice(
$.branch,
$.plugin
)),
_string_start: $ => "\"",
_string_end: $ => "\"",
_plugin_open: $ => "{",
_plugin_close: $ => "}",
}
branch_or_plugin: $ => prec(15, choice(
prec(10, $.branch),
prec(9, $.plugin)
)),
}
});

View File

@ -39,10 +39,7 @@
; Operators
[
"!"
"=="
"=~"
"!~"
(compare_operator)
] @operator
[

View File

@ -395,27 +395,39 @@
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "if"
},
{
"type": "REPEAT",
"type": "PREC",
"value": 10,
"content": {
"type": "SYMBOL",
"name": "else_if"
"name": "if"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "PREC",
"value": 9,
"content": {
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "else"
},
{
"type": "BLANK"
"name": "else_if"
}
]
}
},
{
"type": "PREC",
"value": 8,
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "else"
},
{
"type": "BLANK"
}
]
}
}
]
},
@ -1052,12 +1064,20 @@
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "branch"
"type": "PREC",
"value": 10,
"content": {
"type": "SYMBOL",
"name": "branch"
}
},
{
"type": "SYMBOL",
"name": "plugin"
"type": "PREC",
"value": 9,
"content": {
"type": "SYMBOL",
"name": "plugin"
}
}
]
}

File diff suppressed because it is too large Load Diff