Refactor rules

This commit is contained in:
Uy Ha 2021-06-16 21:37:14 +02:00
parent 9cfb644bc0
commit dabd265781
7 changed files with 2479 additions and 2425 deletions

View file

@ -8,9 +8,7 @@ message([[]])
(source_file
(normal_command
(identifier)
(arguments
(argument (bracket_argument))
)
(argument (bracket_argument))
)
)
@ -24,9 +22,7 @@ message([[an argument]])
(source_file
(normal_command
(identifier)
(arguments
(argument (bracket_argument (bracket_content)))
)
(argument (bracket_argument (bracket_content)))
)
)
@ -40,10 +36,8 @@ message([[first argument]] [[second argument]])
(source_file
(normal_command
(identifier)
(arguments
(argument (bracket_argument (bracket_content)))
(argument (bracket_argument (bracket_content)))
)
(argument (bracket_argument (bracket_content)))
(argument (bracket_argument (bracket_content)))
)
)
@ -60,10 +54,8 @@ message(
(source_file
(normal_command
(identifier)
(arguments
(argument (bracket_argument (bracket_content)))
(argument (bracket_argument (bracket_content)))
)
(argument (bracket_argument (bracket_content)))
(argument (bracket_argument (bracket_content)))
)
)
@ -79,9 +71,7 @@ with line break
(source_file
(normal_command
(identifier)
(arguments
(argument (bracket_argument (bracket_content)))
)
(argument (bracket_argument (bracket_content)))
)
)

View file

@ -8,9 +8,7 @@ message("")
(source_file
(normal_command
(identifier)
(arguments
(argument (quoted_argument))
)
(argument (quoted_argument))
)
)
@ -24,9 +22,7 @@ message("An argument")
(source_file
(normal_command
(identifier)
(arguments
(argument (quoted_argument (quoted_element)))
)
(argument (quoted_argument (quoted_element)))
)
)
@ -40,10 +36,8 @@ message("First argument" "Second argument")
(source_file
(normal_command
(identifier)
(arguments
(argument (quoted_argument (quoted_element)))
(argument (quoted_argument (quoted_element)))
)
(argument (quoted_argument (quoted_element)))
(argument (quoted_argument (quoted_element)))
)
)
@ -58,9 +52,7 @@ with line break")
(source_file
(normal_command
(identifier)
(arguments
(argument (quoted_argument (quoted_element)))
)
(argument (quoted_argument (quoted_element)))
)
)
@ -74,12 +66,10 @@ message("${var}")
(source_file
(normal_command
(identifier)
(arguments
(argument
(quoted_argument
(quoted_element
(variable_ref (normal_var (variable)))
)
(argument
(quoted_argument
(quoted_element
(variable_ref (normal_var (variable)))
)
)
)
@ -96,13 +86,11 @@ message("${var} ${var}")
(source_file
(normal_command
(identifier)
(arguments
(argument
(quoted_argument
(quoted_element
(variable_ref (normal_var (variable)))
(variable_ref (normal_var (variable)))
)
(argument
(quoted_argument
(quoted_element
(variable_ref (normal_var (variable)))
(variable_ref (normal_var (variable)))
)
)
)

View file

@ -9,9 +9,7 @@ message(STATUS)
(source_file
(normal_command
(identifier)
(arguments
(argument (unquoted_argument))
)
(argument (unquoted_argument))
)
)
@ -26,10 +24,8 @@ message(STATUS Hello)
(source_file
(normal_command
(identifier)
(arguments
(argument (unquoted_argument))
(argument (unquoted_argument))
)
(argument (unquoted_argument))
(argument (unquoted_argument))
)
)
@ -45,15 +41,11 @@ STATUS)
(source_file
(normal_command
(identifier)
(arguments
(argument (unquoted_argument))
)
(argument (unquoted_argument))
)
(normal_command
(identifier)
(arguments
(argument (unquoted_argument))
)
(argument (unquoted_argument))
)
)
============================================================
@ -68,15 +60,11 @@ STATUS)
(source_file
(normal_command
(identifier)
(arguments
(argument (unquoted_argument))
)
(argument (unquoted_argument))
)
(normal_command
(identifier)
(arguments
(argument (unquoted_argument))
)
(argument (unquoted_argument))
)
)
@ -89,11 +77,9 @@ message(${var_ref})
(source_file
(normal_command
(identifier)
(arguments
(argument
(unquoted_argument
(variable_ref (normal_var (variable)))
)
(argument
(unquoted_argument
(variable_ref (normal_var (variable)))
)
)
)

View file

@ -4,14 +4,6 @@ module.exports = grammar({
rules: {
source_file: ($) => repeat($._command_invocation),
_line_ending: ($) => $._newline,
_seperation: ($) => choice($._space, $._line_ending),
_space: ($) => /[ \t]+/,
_newline: ($) => /\n+/,
...commands("foreach", "endforeach"),
identifier: ($) => /[A-Za-z_][A-Za-z0-9_]*/,
integer: ($) => /[+-]*\d+/,
escape_sequence: ($) => choice($._escape_identity, $._escape_encoded, $._escape_semicolon),
_escape_identity: ($) => /\\[^A-Za-z0-9;]/,
_escape_encoded: ($) => choice("\\t", "\\r", "\\n"),
@ -20,54 +12,58 @@ module.exports = grammar({
variable: ($) => prec.left(repeat1(choice(/[a-zA-Z0-9/_.+-]/, $.escape_sequence))),
variable_ref: ($) => choice($.normal_var, $.env_var, $.cache_var),
normal_var: ($) => seq("${", $.variable, "}"),
env_var: ($) => seq("$ENV{", $.variable, "}"),
cache_var: ($) => seq("$CACHE{", $.variable, "}"),
env_var: ($) => seq("$ENV", "{", $.variable, "}"),
cache_var: ($) => seq("$CACHE", "{", $.variable, "}"),
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("="), "]"),
_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))),
quoted_element: ($) => repeat1(choice($.variable_ref, /[^\\"]/, $.escape_sequence, seq("\\", newline()))),
unquoted_argument: ($) => repeat1(choice($.variable_ref, /[^ ()#\"\\]/, $.escape_sequence)),
arguments: ($) => args($, $.argument),
foreach_command: ($) =>
seq($.foreach, "(", args($, $.argument, "IN", "ZIP_LIST", "RANGE", "LISTS", "ITEMS"), ")"),
endforeach_command: ($) =>
seq($.endforeach, "(", repeat($._seperation), optional($.argument), ")"),
foreach_loop: ($) =>
seq($.foreach_command, repeat($._command_invocation), $.endforeach_command),
normal_command: ($) =>
seq($.identifier, "(", repeat($._seperation), optional($.arguments), ")"),
foreach_command: ($) => command($.foreach, args(choice($.argument, "IN", "RANGE", "ZIP_LISTS", "LISTS", "ITEMS"))),
endforeach_command: ($) => command($.endforeach, optional($.argument)),
foreach_loop: ($) => seq($.foreach_command, repeat($._command_invocation), $.endforeach_command),
normal_command: ($) => command($.identifier, optional(args($.argument))),
_command_invocation: ($) => choice($.normal_command, $.foreach_loop),
...commandNames("foreach", "endforeach"),
identifier: ($) => /[A-Za-z_][A-Za-z0-9_]*/,
integer: ($) => /[+-]*\d+/,
},
});
function iregex(s) {
return new RegExp(
Array.from(s).reduce((acc, value) => acc + `[${value.toLowerCase()}${value.toUpperCase()}]`, "")
);
return new RegExp(Array.from(s).reduce((acc, value) => acc + `[${value.toLowerCase()}${value.toUpperCase()}]`, ""));
}
function commandName(name) {
return { [name]: ($) => iregex(name) };
return { [name]: (_) => iregex(name) };
}
function commands(...names) {
function commandNames(...names) {
return Object.assign({}, ...names.map(commandName));
}
function args($, ...rules) {
return seq(
choice(...rules),
repeat(prec.left(seq(repeat1($._seperation), optional(choice(...rules)))))
);
function args(rule) {
return seq(rule, repeat(prec.left(seq(repeat1(seperation()), optional(rule)))));
}
function command(name_rule, arg_rule) {
return seq(name_rule, "(", repeat(seperation()), arg_rule, ")");
}
function seperation() {
return choice(space(), newline());
}
function space() {
return /[ \t]+/;
}
function newline() {
return /\n+/;
}

View file

@ -8,47 +8,6 @@
"name": "_command_invocation"
}
},
"_line_ending": {
"type": "SYMBOL",
"name": "_newline"
},
"_seperation": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_space"
},
{
"type": "SYMBOL",
"name": "_line_ending"
}
]
},
"_space": {
"type": "PATTERN",
"value": "[ \\t]+"
},
"_newline": {
"type": "PATTERN",
"value": "\\n+"
},
"foreach": {
"type": "PATTERN",
"value": "[fF][oO][rR][eE][aA][cC][hH]"
},
"endforeach": {
"type": "PATTERN",
"value": "[eE][nN][dD][fF][oO][rR][eE][aA][cC][hH]"
},
"identifier": {
"type": "PATTERN",
"value": "[A-Za-z_][A-Za-z0-9_]*"
},
"integer": {
"type": "PATTERN",
"value": "[+-]*\\d+"
},
"escape_sequence": {
"type": "CHOICE",
"members": [
@ -150,7 +109,11 @@
"members": [
{
"type": "STRING",
"value": "$ENV{"
"value": "$ENV"
},
{
"type": "STRING",
"value": "{"
},
{
"type": "SYMBOL",
@ -167,7 +130,11 @@
"members": [
{
"type": "STRING",
"value": "$CACHE{"
"value": "$CACHE"
},
{
"type": "STRING",
"value": "{"
},
{
"type": "SYMBOL",
@ -318,8 +285,8 @@
"value": "\\"
},
{
"type": "SYMBOL",
"name": "_newline"
"type": "PATTERN",
"value": "\\n+"
}
]
}
@ -346,56 +313,6 @@
]
}
},
"arguments": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "argument"
}
]
},
{
"type": "REPEAT",
"content": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "_seperation"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "argument"
}
]
},
{
"type": "BLANK"
}
]
}
]
}
}
}
]
},
"foreach_command": {
"type": "SEQ",
"members": [
@ -407,6 +324,22 @@
"type": "STRING",
"value": "("
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[ \\t]+"
},
{
"type": "PATTERN",
"value": "\\n+"
}
]
}
},
{
"type": "SEQ",
"members": [
@ -423,11 +356,11 @@
},
{
"type": "STRING",
"value": "ZIP_LIST"
"value": "RANGE"
},
{
"type": "STRING",
"value": "RANGE"
"value": "ZIP_LISTS"
},
{
"type": "STRING",
@ -450,8 +383,17 @@
{
"type": "REPEAT1",
"content": {
"type": "SYMBOL",
"name": "_seperation"
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[ \\t]+"
},
{
"type": "PATTERN",
"value": "\\n+"
}
]
}
},
{
@ -470,11 +412,11 @@
},
{
"type": "STRING",
"value": "ZIP_LIST"
"value": "RANGE"
},
{
"type": "STRING",
"value": "RANGE"
"value": "ZIP_LISTS"
},
{
"type": "STRING",
@ -517,8 +459,17 @@
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_seperation"
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[ \\t]+"
},
{
"type": "PATTERN",
"value": "\\n+"
}
]
}
},
{
@ -573,16 +524,70 @@
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_seperation"
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[ \\t]+"
},
{
"type": "PATTERN",
"value": "\\n+"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "arguments"
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "argument"
},
{
"type": "REPEAT",
"content": {
"type": "PREC_LEFT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "REPEAT1",
"content": {
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[ \\t]+"
},
{
"type": "PATTERN",
"value": "\\n+"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "argument"
},
{
"type": "BLANK"
}
]
}
]
}
}
}
]
},
{
"type": "BLANK"
@ -607,6 +612,22 @@
"name": "foreach_loop"
}
]
},
"foreach": {
"type": "PATTERN",
"value": "[fF][oO][rR][eE][aA][cC][hH]"
},
"endforeach": {
"type": "PATTERN",
"value": "[eE][nN][dD][fF][oO][rR][eE][aA][cC][hH]"
},
"identifier": {
"type": "PATTERN",
"value": "[A-Za-z_][A-Za-z0-9_]*"
},
"integer": {
"type": "PATTERN",
"value": "[+-]*\\d+"
}
},
"extras": [

View file

@ -22,21 +22,6 @@
]
}
},
{
"type": "arguments",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "argument",
"named": true
}
]
}
},
{
"type": "bracket_argument",
"named": true,
@ -166,7 +151,7 @@
"required": true,
"types": [
{
"type": "arguments",
"type": "argument",
"named": true
},
{
@ -306,11 +291,11 @@
"named": false
},
{
"type": "$CACHE{",
"type": "$CACHE",
"named": false
},
{
"type": "$ENV{",
"type": "$ENV",
"named": false
},
{
@ -346,7 +331,7 @@
"named": false
},
{
"type": "ZIP_LIST",
"type": "ZIP_LISTS",
"named": false
},
{
@ -385,6 +370,10 @@
"type": "identifier",
"named": true
},
{
"type": "{",
"named": false
},
{
"type": "}",
"named": false

File diff suppressed because it is too large Load diff