Add case statements

This commit is contained in:
Max Brunsfeld 2017-07-14 16:29:28 -07:00
parent 9f38e36bc3
commit 5b26947ca8
4 changed files with 4834 additions and 2942 deletions

View File

@ -43,3 +43,26 @@ fi
(command (command_name) (argument)))
(else_clause
(command (command_name)))))
====================================
Case statements
====================================
case "opt" in
a)
echo a
;;
b)
echo b
;;
esac
---
(program
(case_statement (argument)
(case_item (argument)
(command (command_name) (argument)))
(case_item (argument)
(command (command_name) (argument)))))

View File

@ -1,7 +1,7 @@
module.exports = grammar({
name: 'bash',
inline: $ => [$.statement],
inline: $ => [$.statement, $.terminator],
externals: $ => [
$._simple_heredoc,
@ -20,13 +20,14 @@ module.exports = grammar({
_terminated_statement: $ => seq(
$.statement,
choice(';', ';;', '\n')
$.terminator
),
statement: $ => choice(
$.command,
$.while_statement,
$.if_statement,
$.case_statement,
$.pipeline,
$.list
),
@ -37,6 +38,12 @@ module.exports = grammar({
$.do_group
),
do_group: $ => seq(
'do',
repeat($._terminated_statement),
'done'
),
if_statement: $ => seq(
'if',
$._terminated_statement,
@ -59,10 +66,21 @@ module.exports = grammar({
repeat($._terminated_statement)
),
do_group: $ => seq(
'do',
case_statement: $ => seq(
'case',
rename($.word, 'argument'),
optional($.terminator),
'in',
$.terminator,
repeat($.case_item),
'esac'
),
case_item: $ => seq(
rename($.word, 'argument'),
')',
repeat($._terminated_statement),
'done'
optional(';;')
),
command: $ => seq(
@ -149,8 +167,10 @@ module.exports = grammar({
leading_word: $ => /[^\\\s#=|;:{}]+/,
word: $ => /[^#\\\s$<>{}&;]+/,
word: $ => /[^#\\\s$<>{}&;)]+/,
comment: $ => /#.*/,
terminator: $ => choice(';', ';;', '\n'),
}
});

View File

@ -16,21 +16,8 @@
"name": "statement"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ";"
},
{
"type": "STRING",
"value": ";;"
},
{
"type": "STRING",
"value": "\n"
}
]
"type": "SYMBOL",
"name": "terminator"
}
]
},
@ -49,6 +36,10 @@
"type": "SYMBOL",
"name": "if_statement"
},
{
"type": "SYMBOL",
"name": "case_statement"
},
{
"type": "SYMBOL",
"name": "pipeline"
@ -76,6 +67,26 @@
}
]
},
"do_group": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "do"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_terminated_statement"
}
},
{
"type": "STRING",
"value": "done"
}
]
},
"if_statement": {
"type": "SEQ",
"members": [
@ -163,12 +174,68 @@
}
]
},
"do_group": {
"case_statement": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "do"
"value": "case"
},
{
"type": "RENAME",
"content": {
"type": "SYMBOL",
"name": "word"
},
"value": "argument"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "terminator"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "in"
},
{
"type": "SYMBOL",
"name": "terminator"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "case_item"
}
},
{
"type": "STRING",
"value": "esac"
}
]
},
"case_item": {
"type": "SEQ",
"members": [
{
"type": "RENAME",
"content": {
"type": "SYMBOL",
"name": "word"
},
"value": "argument"
},
{
"type": "STRING",
"value": ")"
},
{
"type": "REPEAT",
@ -177,9 +244,17 @@
"name": "_terminated_statement"
}
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "done"
"value": ";;"
},
{
"type": "BLANK"
}
]
}
]
},
@ -566,11 +641,28 @@
},
"word": {
"type": "PATTERN",
"value": "[^#\\\\\\s$<>{}&;]+"
"value": "[^#\\\\\\s$<>{}&;)]+"
},
"comment": {
"type": "PATTERN",
"value": "#.*"
},
"terminator": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ";"
},
{
"type": "STRING",
"value": ";;"
},
{
"type": "STRING",
"value": "\n"
}
]
}
},
"extras": [
@ -615,6 +707,7 @@
}
],
"inline": [
"statement"
"statement",
"terminator"
]
}

File diff suppressed because it is too large Load Diff