aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMu Qiao <qiaomuf@gentoo.org>2011-07-22 16:10:59 +0800
committerMu Qiao <qiaomuf@gentoo.org>2011-08-02 15:46:29 +0800
commitb6a7f5f3676b27710c8e47ed1a37c90915e75e1c (patch)
tree2736c30bdc34071ee67997cb2761f85279f07313
parentParser: fix the rule for expansion pattern (diff)
downloadlibbash-b6a7f5f3676b27710c8e47ed1a37c90915e75e1c.tar.gz
libbash-b6a7f5f3676b27710c8e47ed1a37c90915e75e1c.tar.bz2
libbash-b6a7f5f3676b27710c8e47ed1a37c90915e75e1c.zip
Parser: fix array size expansion
The token for "##" is removed in order to support ${##}.
-rw-r--r--bashast/bashast.g13
-rw-r--r--bashast/features_script/features.sh.tokens2
-rw-r--r--bashast/gunit/param_main.gunit8
-rw-r--r--bashast/gunit/simp_command.gunit2
4 files changed, 16 insertions, 9 deletions
diff --git a/bashast/bashast.g b/bashast/bashast.g
index fae2137..c4ab527 100644
--- a/bashast/bashast.g
+++ b/bashast/bashast.g
@@ -678,7 +678,7 @@ ns_string_part
|OTHER|EQUALS|PCT|PCTPCT|PLUS|MINUS|DOT|DOTDOT|COLON|TEST_EXPR
|TILDE|MUL_ASSIGN|DIVIDE_ASSIGN|MOD_ASSIGN
|LSHIFT_ASSIGN|RSHIFT_ASSIGN|AND_ASSIGN|XOR_ASSIGN|LSQUARE|RSQUARE
- |OR_ASSIGN|CARET|POUND|POUNDPOUND|COMMA|EXPORT|LOCAL|AT
+ |OR_ASSIGN|CARET|POUND|COMMA|EXPORT|LOCAL|AT
// The following is for filename expansion
|TIMES|QMARK;
@@ -774,10 +774,10 @@ parameter_expansion
| LSQUARE (op=TIMES|op=AT) RSQUARE -> ^(LIST_EXPAND variable_name_for_bang $op)
| -> ^(VAR_REF variable_name_for_bang)
)
- | variable_size_ref;
+ | {LA(1) == POUND && LA(2) != RBRACE }? => variable_size_ref;
parameter_delete_operator
- : POUND -> LAZY_REMOVE_AT_START
- | POUNDPOUND -> REPLACE_AT_START
+ : (POUND POUND) => POUND POUND -> REPLACE_AT_START
+ | POUND -> LAZY_REMOVE_AT_START
| PCT -> LAZY_REMOVE_AT_END
| PCTPCT -> REPLACE_AT_END;
parameter_value_operator
@@ -840,8 +840,8 @@ variable_name_no_digit
variable_name_for_bang
: num|name|POUND;
variable_size_ref
- : POUND name LSQUARE array_size_index RSQUARE -> ^(POUND ^(name array_size_index))
- | POUND^ name;
+ : (POUND name LSQUARE) => POUND name LSQUARE array_size_index RSQUARE -> ^(POUND ^(name array_size_index))
+ | POUND^ variable_name;
array_size_index
: DIGIT+
| (AT|TIMES) -> ARRAY_SIZE;
@@ -1038,7 +1038,6 @@ ALPHANUM : (DIGIT|LETTER);
TILDE : '~';
HERE_STRING_OP : '<<<';
POUND : '#';
-POUNDPOUND : '##';
PCT : '%';
PCTPCT : '%%';
SLASH : '/';
diff --git a/bashast/features_script/features.sh.tokens b/bashast/features_script/features.sh.tokens
index 25c95d3..af1b075 100644
--- a/bashast/features_script/features.sh.tokens
+++ b/bashast/features_script/features.sh.tokens
@@ -105,7 +105,7 @@
105 DOLLAR LBRACE POUND NAME RBRACE EOL
106 DOLLAR LBRACE NAME SLASH NAME SLASH NAME RBRACE EOL
107 DOLLAR LBRACE NAME POUND NAME RBRACE EOL
-108 DOLLAR LBRACE NAME POUNDPOUND NAME RBRACE EOL
+108 DOLLAR LBRACE NAME POUND POUND NAME RBRACE EOL
109 DOLLAR LBRACE NAME PCT NAME RBRACE EOL
110 DOLLAR LBRACE NAME PCT NAME RBRACE EOL
111 DOLLAR DIGIT BLANK DOLLAR AT BLANK DOLLAR TIMES EOL
diff --git a/bashast/gunit/param_main.gunit b/bashast/gunit/param_main.gunit
index c83ccc2..27fc273 100644
--- a/bashast/gunit/param_main.gunit
+++ b/bashast/gunit/param_main.gunit
@@ -57,6 +57,7 @@ variable_reference:
"$*" -> (VAR_REF *)
"${@}" -> (VAR_REF @)
"${#}" -> (VAR_REF #)
+"$#" -> (VAR_REF #)
"${!foo}" -> (VAR_REF (VAR_REF foo))
"${!#}" -> (VAR_REF (VAR_REF #))
"${3}" -> (VAR_REF 3)
@@ -75,6 +76,13 @@ variable_reference:
"${PN/wrong#/#correct}" -> (VAR_REF (REPLACE_FIRST PN (STRING wrong #) (STRING # correct)))
"${a/b/\}c}" -> (VAR_REF (REPLACE_FIRST a (STRING b) (STRING \ } c)))
"${a/b/a\}c}" -> (VAR_REF (REPLACE_FIRST a (STRING b) (STRING a \ } c)))
+"${#var}" -> (VAR_REF (# var))
+"${#var[@]}" -> (VAR_REF (# (var ARRAY_SIZE)))
+"${#var[*]}" -> (VAR_REF (# (var ARRAY_SIZE)))
+"${#@}" -> (VAR_REF (# @))
+"${#*}" -> (VAR_REF (# *))
+"${##}" -> (VAR_REF (# #))
+"${#$}" -> (VAR_REF (# $))
variable_definition_atom:
"MY_PN=${PN/asterisk-}" -> (= MY_PN (STRING (VAR_REF (REPLACE_FIRST PN (STRING asterisk -)))))
diff --git a/bashast/gunit/simp_command.gunit b/bashast/gunit/simp_command.gunit
index 965b351..8e9a443 100644
--- a/bashast/gunit/simp_command.gunit
+++ b/bashast/gunit/simp_command.gunit
@@ -29,7 +29,7 @@ command_atom:
"dodir ${foo}/${bar}" -> (STRING dodir) (STRING (VAR_REF foo) / (VAR_REF bar))
"local a=123 b=(1 2 3) c" -> (VARIABLE_DEFINITIONS local (= a (STRING 123)) (= b (ARRAY (STRING 1) (STRING 2) (STRING 3))) (EQUALS c))
"echo {}{}}{{{}}{{}" -> (STRING echo) (STRING { } { } } { { { } } { { })
-"echo \"ab#af ###\" #abc" -> (STRING echo) (STRING (DOUBLE_QUOTED_STRING ab # af ## #))
+"echo \"ab#af ###\" #abc" -> (STRING echo) (STRING (DOUBLE_QUOTED_STRING ab # af # # #))
command:
"asdf=5 cat out.log > result" -> (COMMAND (STRING cat) (STRING out . log) (= asdf (STRING 5)) (REDIR > (STRING result)))