Add if statements

This commit is contained in:
Max Brunsfeld 2017-07-14 16:18:46 -07:00
parent cce4a65d33
commit 9f38e36bc3
4 changed files with 3007 additions and 1836 deletions

View File

@ -15,3 +15,31 @@ done
(do_group (do_group
(command (command_name) (argument)) (command (command_name) (argument))
(command (command_name) (argument))))) (command (command_name) (argument)))))
====================================
If statements
====================================
if cat some_file | grep -v ok; then
echo one
elif cat other_file | grep -v ok; then
echo two
else
exit
fi
---
(program
(if_statement
(pipeline
(command (command_name) (argument))
(command (command_name) (argument) (argument)))
(command (command_name) (argument))
(elif_clause
(pipeline
(command (command_name) (argument))
(command (command_name) (argument) (argument)))
(command (command_name) (argument)))
(else_clause
(command (command_name)))))

View File

@ -26,6 +26,7 @@ module.exports = grammar({
statement: $ => choice( statement: $ => choice(
$.command, $.command,
$.while_statement, $.while_statement,
$.if_statement,
$.pipeline, $.pipeline,
$.list $.list
), ),
@ -36,6 +37,28 @@ module.exports = grammar({
$.do_group $.do_group
), ),
if_statement: $ => seq(
'if',
$._terminated_statement,
'then',
repeat($._terminated_statement),
repeat($.elif_clause),
optional($.else_clause),
'fi'
),
elif_clause: $ => seq(
'elif',
$._terminated_statement,
'then',
repeat($._terminated_statement)
),
else_clause: $ => seq(
'else',
repeat($._terminated_statement)
),
do_group: $ => seq( do_group: $ => seq(
'do', 'do',
repeat($._terminated_statement), repeat($._terminated_statement),

View File

@ -45,6 +45,10 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "while_statement" "name": "while_statement"
}, },
{
"type": "SYMBOL",
"name": "if_statement"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "pipeline" "name": "pipeline"
@ -72,6 +76,93 @@
} }
] ]
}, },
"if_statement": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "if"
},
{
"type": "SYMBOL",
"name": "_terminated_statement"
},
{
"type": "STRING",
"value": "then"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_terminated_statement"
}
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "elif_clause"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "else_clause"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "fi"
}
]
},
"elif_clause": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "elif"
},
{
"type": "SYMBOL",
"name": "_terminated_statement"
},
{
"type": "STRING",
"value": "then"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_terminated_statement"
}
}
]
},
"else_clause": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "else"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_terminated_statement"
}
}
]
},
"do_group": { "do_group": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [

File diff suppressed because it is too large Load Diff