Add basic support for foreach

This commit is contained in:
Uy Ha 2021-06-06 20:38:18 +02:00
parent 265b10cf72
commit 3ff964a361
5 changed files with 2043 additions and 1823 deletions

34
corpus/foreach.txt Normal file
View file

@ -0,0 +1,34 @@
==========================================
[foreach, lowercase, empty_for, empty_end]
==========================================
foreach(var)
endforeach()
---
(source_file
(command_invocation
(foreach_loop
(variable)
)
)
)
==============================================
[foreach, lowercase, empty_for, non_empty_end]
==============================================
foreach(var)
endforeach(var)
---
(source_file
(command_invocation
(foreach_loop
(variable)
(variable)
)
)
)

View file

@ -40,24 +40,20 @@ module.exports = grammar({
foreach_loop: ($) => foreach_loop: ($) =>
seq( seq(
repeat($.space), "foreach",
/foreach/i,
repeat($.space), repeat($.space),
"(", "(",
$.variable,
repeat($.seperation), repeat($.seperation),
optional($.arguments), optional($.arguments),
")",
"endforeach",
"(",
optional($.variable),
")" ")"
), ),
normal_command: ($) => normal_command: ($) =>
seq( seq($.identifier, repeat($.space), "(", repeat($.seperation), optional($.arguments), ")"),
repeat($.space),
$.identifier,
repeat($.space),
"(",
repeat($.seperation),
optional($.arguments),
")"
),
command_invocation: ($) => choice($.normal_command, $.foreach_loop), command_invocation: ($) => choice($.normal_command, $.foreach_loop),
}, },

View file

@ -386,14 +386,7 @@
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "REPEAT", "type": "STRING",
"content": {
"type": "SYMBOL",
"name": "space"
}
},
{
"type": "PATTERN",
"value": "foreach" "value": "foreach"
}, },
{ {
@ -407,6 +400,10 @@
"type": "STRING", "type": "STRING",
"value": "(" "value": "("
}, },
{
"type": "SYMBOL",
"name": "variable"
},
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
@ -426,6 +423,30 @@
} }
] ]
}, },
{
"type": "STRING",
"value": ")"
},
{
"type": "STRING",
"value": "endforeach"
},
{
"type": "STRING",
"value": "("
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "variable"
},
{
"type": "BLANK"
}
]
},
{ {
"type": "STRING", "type": "STRING",
"value": ")" "value": ")"
@ -435,13 +456,6 @@
"normal_command": { "normal_command": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "space"
}
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "identifier" "name": "identifier"

View file

@ -121,7 +121,7 @@
"fields": {}, "fields": {},
"children": { "children": {
"multiple": true, "multiple": true,
"required": false, "required": true,
"types": [ "types": [
{ {
"type": "arguments", "type": "arguments",
@ -134,6 +134,10 @@
{ {
"type": "space", "type": "space",
"named": true "named": true
},
{
"type": "variable",
"named": true
} }
] ]
} }
@ -376,6 +380,14 @@
"type": "]", "type": "]",
"named": false "named": false
}, },
{
"type": "endforeach",
"named": false
},
{
"type": "foreach",
"named": false
},
{ {
"type": "identifier", "type": "identifier",
"named": true "named": true

File diff suppressed because it is too large Load diff