Refactor rules
This commit is contained in:
parent
9cfb644bc0
commit
dabd265781
|
@ -8,11 +8,9 @@ message([[]])
|
|||
(source_file
|
||||
(normal_command
|
||||
(identifier)
|
||||
(arguments
|
||||
(argument (bracket_argument))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
=======================================
|
||||
One bracket argument [bracket_argument]
|
||||
|
@ -24,11 +22,9 @@ message([[an argument]])
|
|||
(source_file
|
||||
(normal_command
|
||||
(identifier)
|
||||
(arguments
|
||||
(argument (bracket_argument (bracket_content)))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
========================================
|
||||
Two bracket arguments [bracket_argument]
|
||||
|
@ -40,12 +36,10 @@ message([[first argument]] [[second argument]])
|
|||
(source_file
|
||||
(normal_command
|
||||
(identifier)
|
||||
(arguments
|
||||
(argument (bracket_argument (bracket_content)))
|
||||
(argument (bracket_argument (bracket_content)))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
========================================================
|
||||
Two bracket with two equals arguments [bracket_argument]
|
||||
|
@ -60,12 +54,10 @@ message(
|
|||
(source_file
|
||||
(normal_command
|
||||
(identifier)
|
||||
(arguments
|
||||
(argument (bracket_argument (bracket_content)))
|
||||
(argument (bracket_argument (bracket_content)))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
===================================================
|
||||
Bracket argument with line break [bracket_argument]
|
||||
|
@ -79,9 +71,7 @@ with line break
|
|||
(source_file
|
||||
(normal_command
|
||||
(identifier)
|
||||
(arguments
|
||||
(argument (bracket_argument (bracket_content)))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
|
|
@ -8,11 +8,9 @@ message("")
|
|||
(source_file
|
||||
(normal_command
|
||||
(identifier)
|
||||
(arguments
|
||||
(argument (quoted_argument))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
=====================================
|
||||
One quoted argument [quoted_argument]
|
||||
|
@ -24,11 +22,9 @@ message("An argument")
|
|||
(source_file
|
||||
(normal_command
|
||||
(identifier)
|
||||
(arguments
|
||||
(argument (quoted_argument (quoted_element)))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
======================================
|
||||
Two quoted arguments [quoted_argument]
|
||||
|
@ -40,12 +36,10 @@ message("First argument" "Second argument")
|
|||
(source_file
|
||||
(normal_command
|
||||
(identifier)
|
||||
(arguments
|
||||
(argument (quoted_argument (quoted_element)))
|
||||
(argument (quoted_argument (quoted_element)))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
===================================================
|
||||
A quoted argument with line break [quoted_argument]
|
||||
|
@ -58,11 +52,9 @@ with line break")
|
|||
(source_file
|
||||
(normal_command
|
||||
(identifier)
|
||||
(arguments
|
||||
(argument (quoted_argument (quoted_element)))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
========================================
|
||||
One variable reference [quoted_argument]
|
||||
|
@ -74,7 +66,6 @@ message("${var}")
|
|||
(source_file
|
||||
(normal_command
|
||||
(identifier)
|
||||
(arguments
|
||||
(argument
|
||||
(quoted_argument
|
||||
(quoted_element
|
||||
|
@ -84,7 +75,6 @@ message("${var}")
|
|||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
=========================================
|
||||
Two Variable references [quoted_argument]
|
||||
|
@ -96,7 +86,6 @@ message("${var} ${var}")
|
|||
(source_file
|
||||
(normal_command
|
||||
(identifier)
|
||||
(arguments
|
||||
(argument
|
||||
(quoted_argument
|
||||
(quoted_element
|
||||
|
@ -107,4 +96,3 @@ message("${var} ${var}")
|
|||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
|
|
@ -9,11 +9,9 @@ message(STATUS)
|
|||
(source_file
|
||||
(normal_command
|
||||
(identifier)
|
||||
(arguments
|
||||
(argument (unquoted_argument))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
=========================================================
|
||||
Command invocation with two arguments [unquoted_argument]
|
||||
|
@ -26,12 +24,10 @@ message(STATUS Hello)
|
|||
(source_file
|
||||
(normal_command
|
||||
(identifier)
|
||||
(arguments
|
||||
(argument (unquoted_argument))
|
||||
(argument (unquoted_argument))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
===============================================================
|
||||
Command invocations with leading seperation [unquoted_argument]
|
||||
|
@ -45,17 +41,13 @@ STATUS)
|
|||
(source_file
|
||||
(normal_command
|
||||
(identifier)
|
||||
(arguments
|
||||
(argument (unquoted_argument))
|
||||
)
|
||||
)
|
||||
(normal_command
|
||||
(identifier)
|
||||
(arguments
|
||||
(argument (unquoted_argument))
|
||||
)
|
||||
)
|
||||
)
|
||||
============================================================
|
||||
Command invocations with escape sequence [unquoted_argument]
|
||||
============================================================
|
||||
|
@ -68,17 +60,13 @@ STATUS)
|
|||
(source_file
|
||||
(normal_command
|
||||
(identifier)
|
||||
(arguments
|
||||
(argument (unquoted_argument))
|
||||
)
|
||||
)
|
||||
(normal_command
|
||||
(identifier)
|
||||
(arguments
|
||||
(argument (unquoted_argument))
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
========================================
|
||||
Variable referencing [unquoted_argument]
|
||||
|
@ -89,7 +77,6 @@ message(${var_ref})
|
|||
(source_file
|
||||
(normal_command
|
||||
(identifier)
|
||||
(arguments
|
||||
(argument
|
||||
(unquoted_argument
|
||||
(variable_ref (normal_var (variable)))
|
||||
|
@ -97,5 +84,4 @@ message(${var_ref})
|
|||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
|
66
grammar.js
66
grammar.js
|
@ -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+/;
|
||||
}
|
||||
|
|
231
src/grammar.json
231
src/grammar.json
|
@ -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,8 +524,51 @@
|
|||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "PATTERN",
|
||||
"value": "[ \\t]+"
|
||||
},
|
||||
{
|
||||
"type": "PATTERN",
|
||||
"value": "\\n+"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "_seperation"
|
||||
"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+"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
|
@ -582,7 +576,18 @@
|
|||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "arguments"
|
||||
"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": [
|
||||
|
|
|
@ -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
|
||||
|
|
4478
src/parser.c
4478
src/parser.c
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue