Final have foreach working
This commit is contained in:
parent
3ff964a361
commit
4ef112e6ce
|
@ -9,10 +9,16 @@ endforeach()
|
|||
|
||||
(source_file
|
||||
(command_invocation
|
||||
(foreach_loop
|
||||
(foreach_command
|
||||
(foreach)
|
||||
(variable)
|
||||
)
|
||||
)
|
||||
(command_invocation
|
||||
(endforeach_command
|
||||
(endforeach)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
==============================================
|
||||
|
@ -26,9 +32,61 @@ endforeach(var)
|
|||
|
||||
(source_file
|
||||
(command_invocation
|
||||
(foreach_loop
|
||||
(foreach_command
|
||||
(foreach)
|
||||
(variable)
|
||||
)
|
||||
)
|
||||
(command_invocation
|
||||
(endforeach_command
|
||||
(endforeach)
|
||||
(variable)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
==========================================
|
||||
[foreach, uppercase, empty_for, empty_end]
|
||||
==========================================
|
||||
|
||||
FOREACH(var)
|
||||
ENDFOREACH()
|
||||
|
||||
---
|
||||
|
||||
(source_file
|
||||
(command_invocation
|
||||
(foreach_command
|
||||
(foreach)
|
||||
(variable)
|
||||
)
|
||||
)
|
||||
(command_invocation
|
||||
(endforeach_command
|
||||
(endforeach)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
===========================================
|
||||
[foreach, mixed_case, empty_for, empty_end]
|
||||
===========================================
|
||||
|
||||
forEach(var)
|
||||
endForEach()
|
||||
|
||||
---
|
||||
|
||||
(source_file
|
||||
(command_invocation
|
||||
(foreach_command
|
||||
(foreach)
|
||||
(variable)
|
||||
)
|
||||
)
|
||||
(command_invocation
|
||||
(endforeach_command
|
||||
(endforeach)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
|
37
grammar.js
37
grammar.js
|
@ -8,6 +8,7 @@ module.exports = grammar({
|
|||
seperation: ($) => choice($.space, $.line_ending),
|
||||
space: ($) => /[ \t]+/,
|
||||
newline: ($) => /\n+/,
|
||||
...commands("foreach", "endforeach"),
|
||||
identifier: ($) => /[A-Za-z_][A-Za-z0-9_]*/,
|
||||
integer: ($) => /[+-]*\d+/,
|
||||
|
||||
|
@ -38,23 +39,27 @@ module.exports = grammar({
|
|||
arguments: ($) => seq($.argument, repeat($._seperated_arguments)),
|
||||
_seperated_arguments: ($) => prec.left(seq(repeat1($.seperation), optional($.argument))),
|
||||
|
||||
foreach_loop: ($) =>
|
||||
seq(
|
||||
"foreach",
|
||||
repeat($.space),
|
||||
"(",
|
||||
$.variable,
|
||||
repeat($.seperation),
|
||||
optional($.arguments),
|
||||
")",
|
||||
"endforeach",
|
||||
"(",
|
||||
optional($.variable),
|
||||
")"
|
||||
),
|
||||
foreach_command: ($) =>
|
||||
seq($.foreach, "(", repeat($.seperation), $.variable, ")"),
|
||||
endforeach_command: ($) =>
|
||||
seq($.endforeach, "(", repeat($.seperation), optional($.variable), ")"),
|
||||
normal_command: ($) =>
|
||||
seq($.identifier, repeat($.space), "(", repeat($.seperation), optional($.arguments), ")"),
|
||||
seq($.identifier, "(", repeat($.seperation), optional($.arguments), ")"),
|
||||
|
||||
command_invocation: ($) => choice($.normal_command, $.foreach_loop),
|
||||
command_invocation: ($) => choice($.normal_command, $.foreach_command, $.endforeach_command),
|
||||
},
|
||||
});
|
||||
|
||||
function iregex(s) {
|
||||
return new RegExp(
|
||||
Array.from(s).reduce((acc, value) => acc + `[${value.toLowerCase()}${value.toUpperCase()}]`, "")
|
||||
);
|
||||
}
|
||||
|
||||
function commandName(name) {
|
||||
return { [name]: ($) => iregex(name) };
|
||||
}
|
||||
|
||||
function commands(...names) {
|
||||
return Object.assign({}, ...names.map(commandName));
|
||||
}
|
||||
|
|
|
@ -33,6 +33,14 @@
|
|||
"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_]*"
|
||||
|
@ -382,28 +390,17 @@
|
|||
]
|
||||
}
|
||||
},
|
||||
"foreach_loop": {
|
||||
"foreach_command": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "foreach"
|
||||
},
|
||||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "space"
|
||||
}
|
||||
"type": "SYMBOL",
|
||||
"name": "foreach"
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "("
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "variable"
|
||||
},
|
||||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
|
@ -412,29 +409,33 @@
|
|||
}
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "arguments"
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
}
|
||||
]
|
||||
"type": "SYMBOL",
|
||||
"name": "variable"
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": ")"
|
||||
},
|
||||
}
|
||||
]
|
||||
},
|
||||
"endforeach_command": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "endforeach"
|
||||
"type": "SYMBOL",
|
||||
"name": "endforeach"
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "("
|
||||
},
|
||||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "seperation"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
|
@ -460,13 +461,6 @@
|
|||
"type": "SYMBOL",
|
||||
"name": "identifier"
|
||||
},
|
||||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "space"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "("
|
||||
|
@ -501,11 +495,15 @@
|
|||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "normal_command"
|
||||
"name": "foreach_command"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "foreach_loop"
|
||||
"name": "endforeach_command"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "normal_command"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -85,7 +85,11 @@
|
|||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "foreach_loop",
|
||||
"type": "endforeach_command",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "foreach_command",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
|
@ -95,6 +99,29 @@
|
|||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "endforeach_command",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
"multiple": true,
|
||||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "endforeach",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "seperation",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "variable",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "env_var",
|
||||
"named": true,
|
||||
|
@ -116,7 +143,7 @@
|
|||
"fields": {}
|
||||
},
|
||||
{
|
||||
"type": "foreach_loop",
|
||||
"type": "foreach_command",
|
||||
"named": true,
|
||||
"fields": {},
|
||||
"children": {
|
||||
|
@ -124,17 +151,13 @@
|
|||
"required": true,
|
||||
"types": [
|
||||
{
|
||||
"type": "arguments",
|
||||
"type": "foreach",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "seperation",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "space",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "variable",
|
||||
"named": true
|
||||
|
@ -176,10 +199,6 @@
|
|||
{
|
||||
"type": "seperation",
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "space",
|
||||
"named": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -382,11 +401,11 @@
|
|||
},
|
||||
{
|
||||
"type": "endforeach",
|
||||
"named": false
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "foreach",
|
||||
"named": false
|
||||
"named": true
|
||||
},
|
||||
{
|
||||
"type": "identifier",
|
||||
|
|
3171
src/parser.c
3171
src/parser.c
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue