Add quoted arguments, bracket expressions
This commit is contained in:
parent
5b26947ca8
commit
86e67f9403
|
@ -22,6 +22,21 @@ cat -n file1.txt file2.txt
|
|||
(command (command_name) (argument))
|
||||
(command (command_name) (argument) (argument) (argument)))
|
||||
|
||||
===============================
|
||||
Commands with string arguments
|
||||
===============================
|
||||
|
||||
echo "hello $(whoami), this is $(uname)"
|
||||
echo 'hi'
|
||||
|
||||
---
|
||||
|
||||
(program
|
||||
(command (command_name) (quoted_argument
|
||||
(command_substitution (command (command_name)))
|
||||
(command_substitution (command (command_name)))))
|
||||
(command (command_name) (single_quoted_argument)))
|
||||
|
||||
===================================
|
||||
Commands with environment variables
|
||||
===================================
|
||||
|
@ -37,7 +52,7 @@ VAR1=a VAR2="ok" git diff --word-diff=color
|
|||
(command_name))
|
||||
(command
|
||||
(environment_variable_assignment (variable_name) (argument))
|
||||
(environment_variable_assignment (variable_name) (argument))
|
||||
(environment_variable_assignment (variable_name) (quoted_argument))
|
||||
(command_name)
|
||||
(argument)
|
||||
(argument)))
|
||||
|
|
|
@ -44,6 +44,24 @@ fi
|
|||
(else_clause
|
||||
(command (command_name)))))
|
||||
|
||||
====================================
|
||||
If statements with conditional expressions
|
||||
====================================
|
||||
|
||||
if [ "$(uname)" == 'Darwin' ]; then
|
||||
echo one
|
||||
fi
|
||||
|
||||
---
|
||||
|
||||
(program
|
||||
(if_statement
|
||||
(bracket_command
|
||||
(quoted_argument (command_substitution (command (command_name))))
|
||||
(argument)
|
||||
(single_quoted_argument))
|
||||
(command (command_name) (argument))))
|
||||
|
||||
====================================
|
||||
Case statements
|
||||
====================================
|
||||
|
@ -61,7 +79,7 @@ esac
|
|||
---
|
||||
|
||||
(program
|
||||
(case_statement (argument)
|
||||
(case_statement (quoted_argument)
|
||||
(case_item (argument)
|
||||
(command (command_name) (argument)))
|
||||
(case_item (argument)
|
||||
|
|
57
grammar.js
57
grammar.js
|
@ -1,7 +1,7 @@
|
|||
module.exports = grammar({
|
||||
name: 'bash',
|
||||
|
||||
inline: $ => [$.statement, $.terminator],
|
||||
inline: $ => [$.statement, $.terminator, $.value],
|
||||
|
||||
externals: $ => [
|
||||
$._simple_heredoc,
|
||||
|
@ -24,7 +24,9 @@ module.exports = grammar({
|
|||
),
|
||||
|
||||
statement: $ => choice(
|
||||
$.environment_variable_assignment,
|
||||
$.command,
|
||||
$.bracket_command,
|
||||
$.while_statement,
|
||||
$.if_statement,
|
||||
$.case_statement,
|
||||
|
@ -68,7 +70,7 @@ module.exports = grammar({
|
|||
|
||||
case_statement: $ => seq(
|
||||
'case',
|
||||
rename($.word, 'argument'),
|
||||
$.value,
|
||||
optional($.terminator),
|
||||
'in',
|
||||
$.terminator,
|
||||
|
@ -77,12 +79,17 @@ module.exports = grammar({
|
|||
),
|
||||
|
||||
case_item: $ => seq(
|
||||
rename($.word, 'argument'),
|
||||
$.value,
|
||||
')',
|
||||
repeat($._terminated_statement),
|
||||
optional(';;')
|
||||
),
|
||||
|
||||
bracket_command: $ => choice(
|
||||
seq('[', repeat1($.value), ']'),
|
||||
seq('[[', repeat1($.value), ']]')
|
||||
),
|
||||
|
||||
command: $ => seq(
|
||||
repeat(choice(
|
||||
$.environment_variable_assignment,
|
||||
|
@ -91,11 +98,7 @@ module.exports = grammar({
|
|||
rename($.leading_word, 'command_name'),
|
||||
optional(seq(
|
||||
/\s+/,
|
||||
repeat(choice(
|
||||
rename($.word, 'argument'),
|
||||
$.expansion,
|
||||
$.operator_expansion
|
||||
))
|
||||
repeat($.value)
|
||||
)),
|
||||
repeat(choice(
|
||||
$.file_redirect,
|
||||
|
@ -118,9 +121,33 @@ module.exports = grammar({
|
|||
environment_variable_assignment: $ => seq(
|
||||
rename($.leading_word, 'variable_name'),
|
||||
'=',
|
||||
rename($.word, 'argument')
|
||||
$.value
|
||||
),
|
||||
|
||||
value: $ => choice(
|
||||
rename($.word, 'argument'),
|
||||
$.command_substitution,
|
||||
$.quoted_argument,
|
||||
$.single_quoted_argument,
|
||||
$.expansion,
|
||||
$.operator_expansion
|
||||
),
|
||||
|
||||
quoted_argument: $ => seq(
|
||||
'"',
|
||||
repeat(choice(
|
||||
$._quoted_chars,
|
||||
$.expansion,
|
||||
$.operator_expansion,
|
||||
$.command_substitution
|
||||
)),
|
||||
'"'
|
||||
),
|
||||
|
||||
_quoted_chars: $ => /[^"$]+/,
|
||||
|
||||
single_quoted_argument: $ => /'[^']*'/,
|
||||
|
||||
expansion: $ => seq(
|
||||
'$',
|
||||
rename($.word, 'variable_name')
|
||||
|
@ -131,11 +158,17 @@ module.exports = grammar({
|
|||
rename($.leading_word, 'variable_name'),
|
||||
optional(seq(
|
||||
choice(':', ':?', '='),
|
||||
rename($.word, 'argument')
|
||||
$.value
|
||||
)),
|
||||
'}'
|
||||
),
|
||||
|
||||
command_substitution: $ => seq(
|
||||
'$(',
|
||||
$.command,
|
||||
')'
|
||||
),
|
||||
|
||||
file_redirect: $ => seq(
|
||||
optional($.file_descriptor),
|
||||
choice('<', '>', '<&', '>&'),
|
||||
|
@ -165,9 +198,9 @@ module.exports = grammar({
|
|||
|
||||
file_descriptor: $ => token(prec(1, /\d+/)),
|
||||
|
||||
leading_word: $ => /[^\\\s#=|;:{}]+/,
|
||||
leading_word: $ => /[^"\\\s#=|;:{}()]+/,
|
||||
|
||||
word: $ => /[^#\\\s$<>{}&;)]+/,
|
||||
word: $ => /[^"#\\\s$<>{}&;()]+/,
|
||||
|
||||
comment: $ => /#.*/,
|
||||
|
||||
|
|
196
src/grammar.json
196
src/grammar.json
|
@ -24,10 +24,18 @@
|
|||
"statement": {
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "environment_variable_assignment"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "command"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "bracket_command"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "while_statement"
|
||||
|
@ -182,12 +190,8 @@
|
|||
"value": "case"
|
||||
},
|
||||
{
|
||||
"type": "RENAME",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "word"
|
||||
},
|
||||
"value": "argument"
|
||||
"type": "SYMBOL",
|
||||
"name": "value"
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
|
@ -226,12 +230,8 @@
|
|||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "RENAME",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "word"
|
||||
},
|
||||
"value": "argument"
|
||||
"type": "SYMBOL",
|
||||
"name": "value"
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
|
@ -258,6 +258,51 @@
|
|||
}
|
||||
]
|
||||
},
|
||||
"bracket_command": {
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "["
|
||||
},
|
||||
{
|
||||
"type": "REPEAT1",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "value"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "]"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "[["
|
||||
},
|
||||
{
|
||||
"type": "REPEAT1",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "value"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "]]"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"command": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
|
@ -298,25 +343,8 @@
|
|||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "RENAME",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "word"
|
||||
},
|
||||
"value": "argument"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "expansion"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "operator_expansion"
|
||||
}
|
||||
]
|
||||
"type": "SYMBOL",
|
||||
"name": "value"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
@ -419,6 +447,15 @@
|
|||
"type": "STRING",
|
||||
"value": "="
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "value"
|
||||
}
|
||||
]
|
||||
},
|
||||
"value": {
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "RENAME",
|
||||
"content": {
|
||||
|
@ -426,9 +463,74 @@
|
|||
"name": "word"
|
||||
},
|
||||
"value": "argument"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "command_substitution"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "quoted_argument"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "single_quoted_argument"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "expansion"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "operator_expansion"
|
||||
}
|
||||
]
|
||||
},
|
||||
"quoted_argument": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "\""
|
||||
},
|
||||
{
|
||||
"type": "REPEAT",
|
||||
"content": {
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "_quoted_chars"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "expansion"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "operator_expansion"
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "command_substitution"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "\""
|
||||
}
|
||||
]
|
||||
},
|
||||
"_quoted_chars": {
|
||||
"type": "PATTERN",
|
||||
"value": "[^\"$]+"
|
||||
},
|
||||
"single_quoted_argument": {
|
||||
"type": "PATTERN",
|
||||
"value": "'[^']*'"
|
||||
},
|
||||
"expansion": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
|
@ -485,12 +587,8 @@
|
|||
]
|
||||
},
|
||||
{
|
||||
"type": "RENAME",
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "word"
|
||||
},
|
||||
"value": "argument"
|
||||
"type": "SYMBOL",
|
||||
"name": "value"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
@ -505,6 +603,23 @@
|
|||
}
|
||||
]
|
||||
},
|
||||
"command_substitution": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "$("
|
||||
},
|
||||
{
|
||||
"type": "SYMBOL",
|
||||
"name": "command"
|
||||
},
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": ")"
|
||||
}
|
||||
]
|
||||
},
|
||||
"file_redirect": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
|
@ -637,11 +752,11 @@
|
|||
},
|
||||
"leading_word": {
|
||||
"type": "PATTERN",
|
||||
"value": "[^\\\\\\s#=|;:{}]+"
|
||||
"value": "[^\"\\\\\\s#=|;:{}()]+"
|
||||
},
|
||||
"word": {
|
||||
"type": "PATTERN",
|
||||
"value": "[^#\\\\\\s$<>{}&;)]+"
|
||||
"value": "[^\"#\\\\\\s$<>{}&;()]+"
|
||||
},
|
||||
"comment": {
|
||||
"type": "PATTERN",
|
||||
|
@ -708,6 +823,7 @@
|
|||
],
|
||||
"inline": [
|
||||
"statement",
|
||||
"terminator"
|
||||
"terminator",
|
||||
"value"
|
||||
]
|
||||
}
|
14889
src/parser.c
14889
src/parser.c
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue