Add if statements
This commit is contained in:
parent
cce4a65d33
commit
9f38e36bc3
|
@ -15,3 +15,31 @@ done
|
|||
(do_group
|
||||
(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)))))
|
||||
|
|
23
grammar.js
23
grammar.js
|
@ -26,6 +26,7 @@ module.exports = grammar({
|
|||
statement: $ => choice(
|
||||
$.command,
|
||||
$.while_statement,
|
||||
$.if_statement,
|
||||
$.pipeline,
|
||||
$.list
|
||||
),
|
||||
|
@ -36,6 +37,28 @@ module.exports = grammar({
|
|||
$.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',
|
||||
repeat($._terminated_statement),
|
||||
|
|
|
@ -45,6 +45,10 @@
|
|||
"type": "SYMBOL",
|
||||
"name": "while_statement"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "if_statement"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"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": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
|
|
4701
src/parser.c
4701
src/parser.c
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue