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) (word)
(string (simple_expansion (variable_name))) (string (simple_expansion (variable_name)))
(raw_string))) (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_middle,
$._heredoc_end, $._heredoc_end,
$.file_descriptor, $.file_descriptor,
$._empty_value $._empty_value,
'#'
], ],
extras: $ => [ extras: $ => [
@ -241,11 +242,15 @@ module.exports = grammar({
expansion: $ => seq( expansion: $ => seq(
'${', '${',
$._variable_name, choice(
optional(seq( $._variable_name,
choice(':', ':?', '=', ':-'), seq('#', $._variable_name),
$._expression seq(
)), $._variable_name,
choice(':', ':?', '=', ':-'),
$._expression
)
),
'}' '}'
), ),

32
src/grammar.json vendored
View File

@ -847,16 +847,33 @@
"type": "STRING", "type": "STRING",
"value": "${" "value": "${"
}, },
{
"type": "SYMBOL",
"name": "_variable_name"
},
{ {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
{
"type": "SYMBOL",
"name": "_variable_name"
},
{ {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{
"type": "STRING",
"value": "#"
},
{
"type": "SYMBOL",
"name": "_variable_name"
}
]
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_variable_name"
},
{ {
"type": "CHOICE", "type": "CHOICE",
"members": [ "members": [
@ -883,9 +900,6 @@
"name": "_expression" "name": "_expression"
} }
] ]
},
{
"type": "BLANK"
} }
] ]
}, },
@ -1111,6 +1125,10 @@
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_empty_value" "name": "_empty_value"
},
{
"type": "STRING",
"value": "#"
} }
], ],
"inline": [ "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, HEREDOC_END,
FILE_DESCRIPTOR, FILE_DESCRIPTOR,
EMPTY_VALUE, EMPTY_VALUE,
LENGTH_OPERATOR
}; };
struct Scanner { 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; return false;
} }