Final have foreach working

This commit is contained in:
Uy Ha 2021-06-07 23:02:35 +02:00
parent 3ff964a361
commit 4ef112e6ce
5 changed files with 1568 additions and 1817 deletions

View file

@ -9,10 +9,16 @@ endforeach()
(source_file (source_file
(command_invocation (command_invocation
(foreach_loop (foreach_command
(foreach)
(variable) (variable)
) )
) )
(command_invocation
(endforeach_command
(endforeach)
)
)
) )
============================================== ==============================================
@ -26,9 +32,61 @@ endforeach(var)
(source_file (source_file
(command_invocation (command_invocation
(foreach_loop (foreach_command
(foreach)
(variable) (variable)
)
)
(command_invocation
(endforeach_command
(endforeach)
(variable) (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)
)
)
)

View file

@ -8,6 +8,7 @@ module.exports = grammar({
seperation: ($) => choice($.space, $.line_ending), seperation: ($) => choice($.space, $.line_ending),
space: ($) => /[ \t]+/, space: ($) => /[ \t]+/,
newline: ($) => /\n+/, newline: ($) => /\n+/,
...commands("foreach", "endforeach"),
identifier: ($) => /[A-Za-z_][A-Za-z0-9_]*/, identifier: ($) => /[A-Za-z_][A-Za-z0-9_]*/,
integer: ($) => /[+-]*\d+/, integer: ($) => /[+-]*\d+/,
@ -38,23 +39,27 @@ module.exports = grammar({
arguments: ($) => seq($.argument, repeat($._seperated_arguments)), arguments: ($) => seq($.argument, repeat($._seperated_arguments)),
_seperated_arguments: ($) => prec.left(seq(repeat1($.seperation), optional($.argument))), _seperated_arguments: ($) => prec.left(seq(repeat1($.seperation), optional($.argument))),
foreach_loop: ($) => foreach_command: ($) =>
seq( seq($.foreach, "(", repeat($.seperation), $.variable, ")"),
"foreach", endforeach_command: ($) =>
repeat($.space), seq($.endforeach, "(", repeat($.seperation), optional($.variable), ")"),
"(",
$.variable,
repeat($.seperation),
optional($.arguments),
")",
"endforeach",
"(",
optional($.variable),
")"
),
normal_command: ($) => 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));
}

View file

@ -33,6 +33,14 @@
"type": "PATTERN", "type": "PATTERN",
"value": "\\n+" "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": { "identifier": {
"type": "PATTERN", "type": "PATTERN",
"value": "[A-Za-z_][A-Za-z0-9_]*" "value": "[A-Za-z_][A-Za-z0-9_]*"
@ -382,28 +390,17 @@
] ]
} }
}, },
"foreach_loop": { "foreach_command": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "STRING",
"value": "foreach"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL", "type": "SYMBOL",
"name": "space" "name": "foreach"
}
}, },
{ {
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
}, },
{
"type": "SYMBOL",
"name": "variable"
},
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
@ -411,30 +408,34 @@
"name": "seperation" "name": "seperation"
} }
}, },
{
"type": "CHOICE",
"members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "arguments" "name": "variable"
},
{
"type": "BLANK"
}
]
}, },
{ {
"type": "STRING", "type": "STRING",
"value": ")" "value": ")"
}
]
}, },
"endforeach_command": {
"type": "SEQ",
"members": [
{ {
"type": "STRING", "type": "SYMBOL",
"value": "endforeach" "name": "endforeach"
}, },
{ {
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
}, },
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "seperation"
}
},
{ {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
@ -460,13 +461,6 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "identifier" "name": "identifier"
}, },
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "space"
}
},
{ {
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
@ -501,11 +495,15 @@
"members": [ "members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "normal_command" "name": "foreach_command"
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "foreach_loop" "name": "endforeach_command"
},
{
"type": "SYMBOL",
"name": "normal_command"
} }
] ]
} }

View file

@ -85,7 +85,11 @@
"required": true, "required": true,
"types": [ "types": [
{ {
"type": "foreach_loop", "type": "endforeach_command",
"named": true
},
{
"type": "foreach_command",
"named": true "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", "type": "env_var",
"named": true, "named": true,
@ -116,7 +143,7 @@
"fields": {} "fields": {}
}, },
{ {
"type": "foreach_loop", "type": "foreach_command",
"named": true, "named": true,
"fields": {}, "fields": {},
"children": { "children": {
@ -124,17 +151,13 @@
"required": true, "required": true,
"types": [ "types": [
{ {
"type": "arguments", "type": "foreach",
"named": true "named": true
}, },
{ {
"type": "seperation", "type": "seperation",
"named": true "named": true
}, },
{
"type": "space",
"named": true
},
{ {
"type": "variable", "type": "variable",
"named": true "named": true
@ -176,10 +199,6 @@
{ {
"type": "seperation", "type": "seperation",
"named": true "named": true
},
{
"type": "space",
"named": true
} }
] ]
} }
@ -382,11 +401,11 @@
}, },
{ {
"type": "endforeach", "type": "endforeach",
"named": false "named": true
}, },
{ {
"type": "foreach", "type": "foreach",
"named": false "named": true
}, },
{ {
"type": "identifier", "type": "identifier",

File diff suppressed because it is too large Load diff