Add function definitions

This commit is contained in:
Max Brunsfeld 2017-07-14 17:35:51 -07:00
parent 5446533c20
commit 6d341b8314
4 changed files with 13211 additions and 11686 deletions

View File

@ -97,3 +97,21 @@ Subshells
(program
(subshell (command (command_name) (argument))))
===============================
Function definitions
===============================
do_something() {
echo ok
}
function do_something_else() {
echo ok
}
---
(program
(function_definition (command_name) (compound_command (command (command_name) (argument))))
(function_definition (command_name) (compound_command (command (command_name) (argument)))))

View File

@ -33,7 +33,8 @@ module.exports = grammar({
$.case_statement,
$.pipeline,
$.list,
$.subshell
$.subshell,
$.function_definition
),
while_statement: $ => seq(
@ -87,6 +88,20 @@ module.exports = grammar({
';;'
),
function_definition: $ => seq(
optional('function'),
rename($.leading_word, 'command_name'),
'(',
')',
$.compound_command
),
compound_command: $ => seq(
'{',
repeat($._terminated_statement),
'}'
),
bracket_command: $ => choice(
seq('[', repeat1($.value), ']'),
seq('[[', repeat1($.value), ']]')

View File

@ -59,6 +59,10 @@
{
"type": "SYMBOL",
"name": "subshell"
},
{
"type": "SYMBOL",
"name": "function_definition"
}
]
},
@ -254,6 +258,63 @@
}
]
},
"function_definition": {
"type": "SEQ",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "function"
},
{
"type": "BLANK"
}
]
},
{
"type": "RENAME",
"content": {
"type": "SYMBOL",
"name": "leading_word"
},
"value": "command_name"
},
{
"type": "STRING",
"value": "("
},
{
"type": "STRING",
"value": ")"
},
{
"type": "SYMBOL",
"name": "compound_command"
}
]
},
"compound_command": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "{"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_terminated_statement"
}
},
{
"type": "STRING",
"value": "}"
}
]
},
"bracket_command": {
"type": "CHOICE",
"members": [

24801
src/parser.c

File diff suppressed because it is too large Load Diff