Add length operator in variable expansions

This commit is contained in:
Max Brunsfeld 2017-07-15 23:12:22 -07:00
parent 3abfbbd7bd
commit 403361626d
5 changed files with 24833 additions and 22964 deletions

View File

@ -136,3 +136,21 @@ find "`dirname $file`" -name "$base"'*'
(word)
(string (simple_expansion (variable_name)))
(raw_string)))
=========================================
Arrays and array expansions
=========================================
a=()
b=(1 2 3)
echo ${a[@]}
echo ${#b[@]}
---
(program
(environment_variable_assignment (variable_name) (array))
(environment_variable_assignment (variable_name) (array (word) (word) (word)))
(command (command_name) (expansion (variable_name)))
(command (command_name) (expansion (variable_name))))

View File

@ -14,7 +14,8 @@ module.exports = grammar({
$._heredoc_middle,
$._heredoc_end,
$.file_descriptor,
$._empty_value
$._empty_value,
'#'
],
extras: $ => [
@ -241,11 +242,15 @@ module.exports = grammar({
expansion: $ => seq(
'${',
$._variable_name,
optional(seq(
choice(':', ':?', '=', ':-'),
$._expression
)),
choice(
$._variable_name,
seq('#', $._variable_name),
seq(
$._variable_name,
choice(':', ':?', '=', ':-'),
$._expression
)
),
'}'
),

32
src/grammar.json vendored
View File

@ -847,16 +847,33 @@
"type": "STRING",
"value": "${"
},
{
"type": "SYMBOL",
"name": "_variable_name"
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_variable_name"
},
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "#"
},
{
"type": "SYMBOL",
"name": "_variable_name"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_variable_name"
},
{
"type": "CHOICE",
"members": [
@ -883,9 +900,6 @@
"name": "_expression"
}
]
},
{
"type": "BLANK"
}
]
},
@ -1111,6 +1125,10 @@
{
"type": "SYMBOL",
"name": "_empty_value"
},
{
"type": "STRING",
"value": "#"
}
],
"inline": [

47719
src/parser.c vendored

File diff suppressed because it is too large Load Diff

11
src/scanner.cc vendored
View File

@ -13,6 +13,7 @@ enum TokenType {
HEREDOC_END,
FILE_DESCRIPTOR,
EMPTY_VALUE,
LENGTH_OPERATOR
};
struct Scanner {
@ -115,6 +116,16 @@ struct Scanner {
}
}
if (valid_symbols[LENGTH_OPERATOR]) {
if (lexer->lookahead == '#') {
advance(lexer);
if (iswalpha(lexer->lookahead)) {
lexer->result_symbol = LENGTH_OPERATOR;
return true;
}
}
}
return false;
}