feat: add block command and tests for function, macro, and block

This commit is contained in:
Uy Ha 2022-12-03 13:09:00 +01:00
parent 6e51463ef3
commit 686ad8b040
5 changed files with 24373 additions and 17168 deletions

140
corpus/block_commands.txt Normal file
View file

@ -0,0 +1,140 @@
======================================================
Function definition with no arguments [block_commands]
======================================================
function(fn)
endfunction()
---
(source_file
(function_def
(function_command
(function)
(argument
(unquoted_argument)
)
)
(endfunction_command
(endfunction)
)
)
)
========================================================
Function definition with many arguments [block_commands]
========================================================
function(fn arg1 arg2 arg3)
endfunction()
---
(source_file
(function_def
(function_command
(function)
(argument
(unquoted_argument)
)
(argument
(unquoted_argument)
)
(argument
(unquoted_argument)
)
(argument
(unquoted_argument)
)
)
(endfunction_command
(endfunction)
)
)
)
===================================================
Macro definition with no arguments [block_commands]
===================================================
macro(fn)
endmacro()
---
(source_file
(macro_def
(macro_command
(macro)
(argument
(unquoted_argument)
)
)
(endmacro_command
(endmacro)
)
)
)
========================================================
macro definition with many arguments [block_commands]
========================================================
macro(fn arg1 arg2 arg3)
endmacro()
---
(source_file
(macro_def
(macro_command
(macro)
(argument
(unquoted_argument)
)
(argument
(unquoted_argument)
)
(argument
(unquoted_argument)
)
(argument
(unquoted_argument)
)
)
(endmacro_command
(endmacro)
)
)
)
============================
Block scope [block_commands]
============================
block(SCOPE_FOR POLICIES VARIABLES PROPAGATE var)
endblock()
---
(source_file
(block_def
(block_command
(block)
(argument
(unquoted_argument)
)
(argument
(unquoted_argument)
)
(argument
(unquoted_argument)
)
(argument
(unquoted_argument)
)
(argument
(unquoted_argument)
)
)
(endblock_command
(endblock)
)
)
)

View file

@ -11,6 +11,8 @@ commands = [
"endfunction",
"macro",
"endmacro",
"block",
"endblock"
];
module.exports = grammar({
@ -75,10 +77,14 @@ module.exports = grammar({
endmacro_command: ($) => command($.endmacro, repeat($._untrimmed_argument)),
macro_def: ($) => seq($.macro_command, repeat($._untrimmed_command_invocation), $.endmacro_command),
block_command: ($) => command($.block, repeat($._untrimmed_argument)),
endblock_command: ($) => command($.endblock, repeat($._untrimmed_argument)),
block_def: ($) => seq($.block_command, repeat($._untrimmed_command_invocation), $.endblock_command),
normal_command: ($) => command($.identifier, repeat($._untrimmed_argument)),
_command_invocation: ($) =>
choice($.normal_command, $.if_condition, $.foreach_loop, $.while_loop, $.function_def, $.macro_def),
choice($.normal_command, $.if_condition, $.foreach_loop, $.while_loop, $.function_def, $.macro_def, $.block_def),
_untrimmed_command_invocation: ($) => choice(/\s/, $.bracket_comment, $.line_comment, $._command_invocation),
...commandNames(...commands),

View file

@ -941,6 +941,88 @@
}
]
},
"block_command": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "block"
},
{
"type": "REPEAT",
"content": {
"type": "PATTERN",
"value": "[\\t ]"
}
},
{
"type": "STRING",
"value": "("
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_untrimmed_argument"
}
},
{
"type": "STRING",
"value": ")"
}
]
},
"endblock_command": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "endblock"
},
{
"type": "REPEAT",
"content": {
"type": "PATTERN",
"value": "[\\t ]"
}
},
{
"type": "STRING",
"value": "("
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_untrimmed_argument"
}
},
{
"type": "STRING",
"value": ")"
}
]
},
"block_def": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "block_command"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_untrimmed_command_invocation"
}
},
{
"type": "SYMBOL",
"name": "endblock_command"
}
]
},
"normal_command": {
"type": "SEQ",
"members": [
@ -998,6 +1080,10 @@
{
"type": "SYMBOL",
"name": "macro_def"
},
{
"type": "SYMBOL",
"name": "block_def"
}
]
},
@ -1070,6 +1156,14 @@
"type": "PATTERN",
"value": "[eE][nN][dD][mM][aA][cC][rR][oO]"
},
"block": {
"type": "PATTERN",
"value": "[bB][lL][oO][cC][kK]"
},
"endblock": {
"type": "PATTERN",
"value": "[eE][nN][dD][bB][lL][oO][cC][kK]"
},
"identifier": {
"type": "PATTERN",
"value": "[A-Za-z_][A-Za-z0-9_]*"

View file

@ -22,6 +22,88 @@
]
}
},
{
"type": "block_command",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "argument",
"named": true
},
{
"type": "block",
"named": true
},
{
"type": "bracket_comment",
"named": true
},
{
"type": "line_comment",
"named": true
}
]
}
},
{
"type": "block_def",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "block_command",
"named": true
},
{
"type": "block_def",
"named": true
},
{
"type": "bracket_comment",
"named": true
},
{
"type": "endblock_command",
"named": true
},
{
"type": "foreach_loop",
"named": true
},
{
"type": "function_def",
"named": true
},
{
"type": "if_condition",
"named": true
},
{
"type": "line_comment",
"named": true
},
{
"type": "macro_def",
"named": true
},
{
"type": "normal_command",
"named": true
},
{
"type": "while_loop",
"named": true
}
]
}
},
{
"type": "cache_var",
"named": true,
@ -91,6 +173,33 @@
]
}
},
{
"type": "endblock_command",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "argument",
"named": true
},
{
"type": "bracket_comment",
"named": true
},
{
"type": "endblock",
"named": true
},
{
"type": "line_comment",
"named": true
}
]
}
},
{
"type": "endforeach_command",
"named": true,
@ -265,6 +374,10 @@
"multiple": true,
"required": true,
"types": [
{
"type": "block_def",
"named": true
},
{
"type": "bracket_comment",
"named": true
@ -343,6 +456,10 @@
"multiple": true,
"required": true,
"types": [
{
"type": "block_def",
"named": true
},
{
"type": "bracket_comment",
"named": true
@ -436,6 +553,10 @@
"multiple": true,
"required": true,
"types": [
{
"type": "block_def",
"named": true
},
{
"type": "bracket_comment",
"named": true
@ -522,6 +643,10 @@
"multiple": true,
"required": true,
"types": [
{
"type": "block_def",
"named": true
},
{
"type": "bracket_comment",
"named": true
@ -653,6 +778,10 @@
"multiple": true,
"required": false,
"types": [
{
"type": "block_def",
"named": true
},
{
"type": "bracket_comment",
"named": true
@ -788,6 +917,10 @@
"multiple": true,
"required": true,
"types": [
{
"type": "block_def",
"named": true
},
{
"type": "bracket_comment",
"named": true
@ -887,6 +1020,10 @@
"type": "\\t",
"named": false
},
{
"type": "block",
"named": true
},
{
"type": "bracket_argument",
"named": true
@ -903,6 +1040,10 @@
"type": "elseif",
"named": true
},
{
"type": "endblock",
"named": true
},
{
"type": "endforeach",
"named": true

41158
src/parser.c

File diff suppressed because it is too large Load diff