Apply consitant prec like treetop would

This commit is contained in:
Preston Baxter 2024-01-27 00:26:02 -06:00
parent 94c2f540ec
commit 47af6cec04
4 changed files with 3737 additions and 3295 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,
"{",
repeat($.branch_or_plugin),
"}",
),
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

@ -150,20 +150,36 @@
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "and"
"type": "PREC",
"value": 4,
"content": {
"type": "STRING",
"value": "and"
}
},
{
"type": "STRING",
"value": "or"
"type": "PREC",
"value": 4,
"content": {
"type": "STRING",
"value": "or"
}
},
{
"type": "STRING",
"value": "xor"
"type": "PREC",
"value": 3,
"content": {
"type": "STRING",
"value": "xor"
}
},
{
"type": "STRING",
"value": "nand"
"type": "PREC",
"value": 3,
"content": {
"type": "STRING",
"value": "nand"
}
}
]
},
@ -275,8 +291,8 @@
"name": "plugin_type"
},
{
"type": "SYMBOL",
"name": "_plugin_open"
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT",
@ -286,8 +302,8 @@
}
},
{
"type": "SYMBOL",
"name": "_plugin_close"
"type": "STRING",
"value": "}"
}
]
},
@ -395,27 +411,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"
}
]
}
}
]
},
@ -532,45 +560,73 @@
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "SYMBOL",
"name": "condition"
},
{
"type": "STRING",
"value": ")"
}
]
"type": "PREC",
"value": 10,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "("
},
{
"type": "SYMBOL",
"name": "condition"
},
{
"type": "STRING",
"value": ")"
}
]
}
},
{
"type": "SYMBOL",
"name": "negative_expression"
"type": "PREC",
"value": 9,
"content": {
"type": "SYMBOL",
"name": "negative_expression"
}
},
{
"type": "SYMBOL",
"name": "in_expression"
"type": "PREC",
"value": 8,
"content": {
"type": "SYMBOL",
"name": "in_expression"
}
},
{
"type": "SYMBOL",
"name": "not_in_expression"
"type": "PREC",
"value": 7,
"content": {
"type": "SYMBOL",
"name": "not_in_expression"
}
},
{
"type": "SYMBOL",
"name": "compare_expression"
"type": "PREC",
"value": 6,
"content": {
"type": "SYMBOL",
"name": "compare_expression"
}
},
{
"type": "SYMBOL",
"name": "regexp_expression"
"type": "PREC",
"value": 5,
"content": {
"type": "SYMBOL",
"name": "regexp_expression"
}
},
{
"type": "SYMBOL",
"name": "rvalue"
"type": "PREC",
"value": 4,
"content": {
"type": "SYMBOL",
"name": "rvalue"
}
}
]
},
@ -578,25 +634,29 @@
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "!"
},
{
"type": "STRING",
"value": "("
},
{
"type": "SYMBOL",
"name": "condition"
},
{
"type": "STRING",
"value": ")"
}
]
"type": "PREC",
"value": 1,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "!"
},
{
"type": "STRING",
"value": "("
},
{
"type": "SYMBOL",
"name": "condition"
},
{
"type": "STRING",
"value": ")"
}
]
}
},
{
"type": "SEQ",
@ -679,8 +739,12 @@
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "string"
"type": "PREC",
"value": 1,
"content": {
"type": "SYMBOL",
"name": "string"
}
},
{
"type": "SYMBOL",
@ -694,32 +758,60 @@
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "string"
"type": "PREC",
"value": 10,
"content": {
"type": "SYMBOL",
"name": "string"
}
},
{
"type": "SYMBOL",
"name": "number"
"type": "PREC",
"value": 9,
"content": {
"type": "SYMBOL",
"name": "number"
}
},
{
"type": "SYMBOL",
"name": "boolean"
"type": "PREC",
"value": 8,
"content": {
"type": "SYMBOL",
"name": "boolean"
}
},
{
"type": "SYMBOL",
"name": "selector"
"type": "PREC",
"value": 7,
"content": {
"type": "SYMBOL",
"name": "selector"
}
},
{
"type": "SYMBOL",
"name": "array"
"type": "PREC",
"value": 6,
"content": {
"type": "SYMBOL",
"name": "array"
}
},
{
"type": "SYMBOL",
"name": "method_call"
"type": "PREC",
"value": 5,
"content": {
"type": "SYMBOL",
"name": "method_call"
}
},
{
"type": "SYMBOL",
"name": "regexp"
"type": "PREC",
"value": 4,
"content": {
"type": "SYMBOL",
"name": "regexp"
}
}
]
},
@ -788,28 +880,52 @@
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "bareword"
"type": "PREC",
"value": 10,
"content": {
"type": "SYMBOL",
"name": "bareword"
}
},
{
"type": "SYMBOL",
"name": "string"
"type": "PREC",
"value": 9,
"content": {
"type": "SYMBOL",
"name": "string"
}
},
{
"type": "SYMBOL",
"name": "number"
"type": "PREC",
"value": 8,
"content": {
"type": "SYMBOL",
"name": "number"
}
},
{
"type": "SYMBOL",
"name": "boolean"
"type": "PREC",
"value": 7,
"content": {
"type": "SYMBOL",
"name": "boolean"
}
},
{
"type": "SYMBOL",
"name": "array"
"type": "PREC",
"value": 6,
"content": {
"type": "SYMBOL",
"name": "array"
}
},
{
"type": "SYMBOL",
"name": "hash"
"type": "PREC",
"value": 5,
"content": {
"type": "SYMBOL",
"name": "hash"
}
}
]
},
@ -921,16 +1037,28 @@
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "number"
"type": "PREC",
"value": 10,
"content": {
"type": "SYMBOL",
"name": "number"
}
},
{
"type": "SYMBOL",
"name": "bareword"
"type": "PREC",
"value": 9,
"content": {
"type": "SYMBOL",
"name": "bareword"
}
},
{
"type": "SYMBOL",
"name": "string"
"type": "PREC",
"value": 7,
"content": {
"type": "SYMBOL",
"name": "string"
}
}
]
}
@ -1052,31 +1180,23 @@
"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"
}
}
]
}
},
"_string_start": {
"type": "STRING",
"value": "\""
},
"_string_end": {
"type": "STRING",
"value": "\""
},
"_plugin_open": {
"type": "STRING",
"value": "{"
},
"_plugin_close": {
"type": "STRING",
"value": "}"
}
},
"extras": [

File diff suppressed because it is too large Load Diff