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

View File

@ -16,21 +16,8 @@
"name": "statement" "name": "statement"
}, },
{ {
"type": "CHOICE", "type": "SYMBOL",
"members": [ "name": "terminator"
{
"type": "STRING",
"value": ";"
},
{
"type": "STRING",
"value": ";;"
},
{
"type": "STRING",
"value": "\n"
}
]
} }
] ]
}, },
@ -49,6 +36,10 @@
"type": "SYMBOL", "type": "SYMBOL",
"name": "if_statement" "name": "if_statement"
}, },
{
"type": "SYMBOL",
"name": "case_statement"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "pipeline" "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": { "if_statement": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
@ -163,12 +174,68 @@
} }
] ]
}, },
"do_group": { "case_statement": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "STRING", "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", "type": "REPEAT",
@ -178,8 +245,16 @@
} }
}, },
{ {
"type": "STRING", "type": "CHOICE",
"value": "done" "members": [
{
"type": "STRING",
"value": ";;"
},
{
"type": "BLANK"
}
]
} }
] ]
}, },
@ -566,11 +641,28 @@
}, },
"word": { "word": {
"type": "PATTERN", "type": "PATTERN",
"value": "[^#\\\\\\s$<>{}&;]+" "value": "[^#\\\\\\s$<>{}&;)]+"
}, },
"comment": { "comment": {
"type": "PATTERN", "type": "PATTERN",
"value": "#.*" "value": "#.*"
},
"terminator": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": ";"
},
{
"type": "STRING",
"value": ";;"
},
{
"type": "STRING",
"value": "\n"
}
]
} }
}, },
"extras": [ "extras": [
@ -615,6 +707,7 @@
} }
], ],
"inline": [ "inline": [
"statement" "statement",
"terminator"
] ]
} }

File diff suppressed because it is too large Load Diff