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,11 +8,9 @@ message([[]])
(source_file (source_file
(normal_command (normal_command
(identifier) (identifier)
(arguments
(argument (bracket_argument)) (argument (bracket_argument))
) )
) )
)
======================================= =======================================
One bracket argument [bracket_argument] One bracket argument [bracket_argument]
@ -24,11 +22,9 @@ message([[an argument]])
(source_file (source_file
(normal_command (normal_command
(identifier) (identifier)
(arguments
(argument (bracket_argument (bracket_content))) (argument (bracket_argument (bracket_content)))
) )
) )
)
======================================== ========================================
Two bracket arguments [bracket_argument] Two bracket arguments [bracket_argument]
@ -40,12 +36,10 @@ message([[first argument]] [[second argument]])
(source_file (source_file
(normal_command (normal_command
(identifier) (identifier)
(arguments
(argument (bracket_argument (bracket_content))) (argument (bracket_argument (bracket_content)))
(argument (bracket_argument (bracket_content))) (argument (bracket_argument (bracket_content)))
) )
) )
)
======================================================== ========================================================
Two bracket with two equals arguments [bracket_argument] Two bracket with two equals arguments [bracket_argument]
@ -60,12 +54,10 @@ message(
(source_file (source_file
(normal_command (normal_command
(identifier) (identifier)
(arguments
(argument (bracket_argument (bracket_content))) (argument (bracket_argument (bracket_content)))
(argument (bracket_argument (bracket_content))) (argument (bracket_argument (bracket_content)))
) )
) )
)
=================================================== ===================================================
Bracket argument with line break [bracket_argument] Bracket argument with line break [bracket_argument]
@ -79,9 +71,7 @@ with line break
(source_file (source_file
(normal_command (normal_command
(identifier) (identifier)
(arguments
(argument (bracket_argument (bracket_content))) (argument (bracket_argument (bracket_content)))
) )
) )
)

View file

@ -8,11 +8,9 @@ message("")
(source_file (source_file
(normal_command (normal_command
(identifier) (identifier)
(arguments
(argument (quoted_argument)) (argument (quoted_argument))
) )
) )
)
===================================== =====================================
One quoted argument [quoted_argument] One quoted argument [quoted_argument]
@ -24,11 +22,9 @@ message("An argument")
(source_file (source_file
(normal_command (normal_command
(identifier) (identifier)
(arguments
(argument (quoted_argument (quoted_element))) (argument (quoted_argument (quoted_element)))
) )
) )
)
====================================== ======================================
Two quoted arguments [quoted_argument] Two quoted arguments [quoted_argument]
@ -40,12 +36,10 @@ message("First argument" "Second argument")
(source_file (source_file
(normal_command (normal_command
(identifier) (identifier)
(arguments
(argument (quoted_argument (quoted_element))) (argument (quoted_argument (quoted_element)))
(argument (quoted_argument (quoted_element))) (argument (quoted_argument (quoted_element)))
) )
) )
)
=================================================== ===================================================
A quoted argument with line break [quoted_argument] A quoted argument with line break [quoted_argument]
@ -58,11 +52,9 @@ with line break")
(source_file (source_file
(normal_command (normal_command
(identifier) (identifier)
(arguments
(argument (quoted_argument (quoted_element))) (argument (quoted_argument (quoted_element)))
) )
) )
)
======================================== ========================================
One variable reference [quoted_argument] One variable reference [quoted_argument]
@ -74,7 +66,6 @@ message("${var}")
(source_file (source_file
(normal_command (normal_command
(identifier) (identifier)
(arguments
(argument (argument
(quoted_argument (quoted_argument
(quoted_element (quoted_element
@ -84,7 +75,6 @@ message("${var}")
) )
) )
) )
)
========================================= =========================================
Two Variable references [quoted_argument] Two Variable references [quoted_argument]
@ -96,7 +86,6 @@ message("${var} ${var}")
(source_file (source_file
(normal_command (normal_command
(identifier) (identifier)
(arguments
(argument (argument
(quoted_argument (quoted_argument
(quoted_element (quoted_element
@ -107,4 +96,3 @@ message("${var} ${var}")
) )
) )
) )
)

View file

@ -9,11 +9,9 @@ message(STATUS)
(source_file (source_file
(normal_command (normal_command
(identifier) (identifier)
(arguments
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
) )
)
========================================================= =========================================================
Command invocation with two arguments [unquoted_argument] Command invocation with two arguments [unquoted_argument]
@ -26,12 +24,10 @@ message(STATUS Hello)
(source_file (source_file
(normal_command (normal_command
(identifier) (identifier)
(arguments
(argument (unquoted_argument)) (argument (unquoted_argument))
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
) )
)
=============================================================== ===============================================================
Command invocations with leading seperation [unquoted_argument] Command invocations with leading seperation [unquoted_argument]
@ -45,17 +41,13 @@ STATUS)
(source_file (source_file
(normal_command (normal_command
(identifier) (identifier)
(arguments
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
)
(normal_command (normal_command
(identifier) (identifier)
(arguments
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
) )
)
============================================================ ============================================================
Command invocations with escape sequence [unquoted_argument] Command invocations with escape sequence [unquoted_argument]
============================================================ ============================================================
@ -68,17 +60,13 @@ STATUS)
(source_file (source_file
(normal_command (normal_command
(identifier) (identifier)
(arguments
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
)
(normal_command (normal_command
(identifier) (identifier)
(arguments
(argument (unquoted_argument)) (argument (unquoted_argument))
) )
) )
)
======================================== ========================================
Variable referencing [unquoted_argument] Variable referencing [unquoted_argument]
@ -89,7 +77,6 @@ message(${var_ref})
(source_file (source_file
(normal_command (normal_command
(identifier) (identifier)
(arguments
(argument (argument
(unquoted_argument (unquoted_argument
(variable_ref (normal_var (variable))) (variable_ref (normal_var (variable)))
@ -97,5 +84,4 @@ message(${var_ref})
) )
) )
) )
)

View file

@ -4,14 +4,6 @@ module.exports = grammar({
rules: { rules: {
source_file: ($) => repeat($._command_invocation), 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_sequence: ($) => choice($._escape_identity, $._escape_encoded, $._escape_semicolon),
_escape_identity: ($) => /\\[^A-Za-z0-9;]/, _escape_identity: ($) => /\\[^A-Za-z0-9;]/,
_escape_encoded: ($) => choice("\\t", "\\r", "\\n"), _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: ($) => prec.left(repeat1(choice(/[a-zA-Z0-9/_.+-]/, $.escape_sequence))),
variable_ref: ($) => choice($.normal_var, $.env_var, $.cache_var), variable_ref: ($) => choice($.normal_var, $.env_var, $.cache_var),
normal_var: ($) => seq("${", $.variable, "}"), normal_var: ($) => seq("${", $.variable, "}"),
env_var: ($) => seq("$ENV{", $.variable, "}"), env_var: ($) => seq("$ENV", "{", $.variable, "}"),
cache_var: ($) => seq("$CACHE{", $.variable, "}"), cache_var: ($) => seq("$CACHE", "{", $.variable, "}"),
argument: ($) => choice($.bracket_argument, $.quoted_argument, $.unquoted_argument), argument: ($) => choice($.bracket_argument, $.quoted_argument, $.unquoted_argument),
bracket_argument: ($) => seq($._bracket_open, optional($.bracket_content), $._bracket_close), bracket_argument: ($) => seq($._bracket_open, optional($.bracket_content), $._bracket_close),
_bracket_open: ($) => seq("[", repeat("="), "["), _bracket_open: (_) => seq("[", repeat("="), "["),
bracket_content: ($) => repeat1(/[^\]]/), bracket_content: (_) => repeat1(/[^\]]/),
_bracket_close: ($) => seq("]", repeat("="), "]"), _bracket_close: (_) => seq("]", repeat("="), "]"),
quoted_argument: ($) => seq('"', optional($.quoted_element), '"'), quoted_argument: ($) => seq('"', optional($.quoted_element), '"'),
quoted_element: ($) => quoted_element: ($) => repeat1(choice($.variable_ref, /[^\\"]/, $.escape_sequence, seq("\\", newline()))),
repeat1(choice($.variable_ref, /[^\\"]/, $.escape_sequence, seq("\\", $._newline))),
unquoted_argument: ($) => repeat1(choice($.variable_ref, /[^ ()#\"\\]/, $.escape_sequence)), unquoted_argument: ($) => repeat1(choice($.variable_ref, /[^ ()#\"\\]/, $.escape_sequence)),
arguments: ($) => args($, $.argument), foreach_command: ($) => command($.foreach, args(choice($.argument, "IN", "RANGE", "ZIP_LISTS", "LISTS", "ITEMS"))),
endforeach_command: ($) => command($.endforeach, optional($.argument)),
foreach_command: ($) => foreach_loop: ($) => seq($.foreach_command, repeat($._command_invocation), $.endforeach_command),
seq($.foreach, "(", args($, $.argument, "IN", "ZIP_LIST", "RANGE", "LISTS", "ITEMS"), ")"), normal_command: ($) => command($.identifier, optional(args($.argument))),
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), ")"),
_command_invocation: ($) => choice($.normal_command, $.foreach_loop), _command_invocation: ($) => choice($.normal_command, $.foreach_loop),
...commandNames("foreach", "endforeach"),
identifier: ($) => /[A-Za-z_][A-Za-z0-9_]*/,
integer: ($) => /[+-]*\d+/,
}, },
}); });
function iregex(s) { function iregex(s) {
return new RegExp( return new RegExp(Array.from(s).reduce((acc, value) => acc + `[${value.toLowerCase()}${value.toUpperCase()}]`, ""));
Array.from(s).reduce((acc, value) => acc + `[${value.toLowerCase()}${value.toUpperCase()}]`, "")
);
} }
function commandName(name) { function commandName(name) {
return { [name]: ($) => iregex(name) }; return { [name]: (_) => iregex(name) };
} }
function commands(...names) { function commandNames(...names) {
return Object.assign({}, ...names.map(commandName)); return Object.assign({}, ...names.map(commandName));
} }
function args($, ...rules) { function args(rule) {
return seq( return seq(rule, repeat(prec.left(seq(repeat1(seperation()), optional(rule)))));
choice(...rules), }
repeat(prec.left(seq(repeat1($._seperation), optional(choice(...rules))))) 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" "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": { "escape_sequence": {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
@ -150,7 +109,11 @@
"members": [ "members": [
{ {
"type": "STRING", "type": "STRING",
"value": "$ENV{" "value": "$ENV"
},
{
"type": "STRING",
"value": "{"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -167,7 +130,11 @@
"members": [ "members": [
{ {
"type": "STRING", "type": "STRING",
"value": "$CACHE{" "value": "$CACHE"
},
{
"type": "STRING",
"value": "{"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
@ -318,8 +285,8 @@
"value": "\\" "value": "\\"
}, },
{ {
"type": "SYMBOL", "type": "PATTERN",
"name": "_newline" "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": { "foreach_command": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -407,6 +324,22 @@
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
}, },
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[ \\t]+"
},
{
"type": "PATTERN",
"value": "\\n+"
}
]
}
},
{ {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -423,11 +356,11 @@
}, },
{ {
"type": "STRING", "type": "STRING",
"value": "ZIP_LIST" "value": "RANGE"
}, },
{ {
"type": "STRING", "type": "STRING",
"value": "RANGE" "value": "ZIP_LISTS"
}, },
{ {
"type": "STRING", "type": "STRING",
@ -450,8 +383,17 @@
{ {
"type": "REPEAT1", "type": "REPEAT1",
"content": { "content": {
"type": "SYMBOL", "type": "CHOICE",
"name": "_seperation" "members": [
{
"type": "PATTERN",
"value": "[ \\t]+"
},
{
"type": "PATTERN",
"value": "\\n+"
}
]
} }
}, },
{ {
@ -470,11 +412,11 @@
}, },
{ {
"type": "STRING", "type": "STRING",
"value": "ZIP_LIST" "value": "RANGE"
}, },
{ {
"type": "STRING", "type": "STRING",
"value": "RANGE" "value": "ZIP_LISTS"
}, },
{ {
"type": "STRING", "type": "STRING",
@ -517,8 +459,17 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "SYMBOL", "type": "CHOICE",
"name": "_seperation" "members": [
{
"type": "PATTERN",
"value": "[ \\t]+"
},
{
"type": "PATTERN",
"value": "\\n+"
}
]
} }
}, },
{ {
@ -573,8 +524,51 @@
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
"type": "CHOICE",
"members": [
{
"type": "PATTERN",
"value": "[ \\t]+"
},
{
"type": "PATTERN",
"value": "\\n+"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL", "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": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "arguments" "name": "argument"
},
{
"type": "BLANK"
}
]
}
]
}
}
}
]
}, },
{ {
"type": "BLANK" "type": "BLANK"
@ -607,6 +612,22 @@
"name": "foreach_loop" "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": [ "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", "type": "bracket_argument",
"named": true, "named": true,
@ -166,7 +151,7 @@
"required": true, "required": true,
"types": [ "types": [
{ {
"type": "arguments", "type": "argument",
"named": true "named": true
}, },
{ {
@ -306,11 +291,11 @@
"named": false "named": false
}, },
{ {
"type": "$CACHE{", "type": "$CACHE",
"named": false "named": false
}, },
{ {
"type": "$ENV{", "type": "$ENV",
"named": false "named": false
}, },
{ {
@ -346,7 +331,7 @@
"named": false "named": false
}, },
{ {
"type": "ZIP_LIST", "type": "ZIP_LISTS",
"named": false "named": false
}, },
{ {
@ -385,6 +370,10 @@
"type": "identifier", "type": "identifier",
"named": true "named": true
}, },
{
"type": "{",
"named": false
},
{ {
"type": "}", "type": "}",
"named": false "named": false

File diff suppressed because it is too large Load diff