1 |
commit: 9ad65f4f20c373da2517bef333ae4b4c664e1504 |
2 |
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org> |
3 |
AuthorDate: Tue Apr 5 13:22:39 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Wed Apr 6 00:53:24 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=9ad65f4f |
7 |
|
8 |
Use arithmetic inside substring expansion |
9 |
|
10 |
Bash manual claims that length and offset are arithmetic expressions. |
11 |
Meanwhile, a negative offset must be separated from the colon by at |
12 |
least one space to avoid being confused with the :- expansion. So a |
13 |
wspace rule is added. Arithmetic expression inside substring expansion |
14 |
with $(()) around is not supported yet. |
15 |
|
16 |
--- |
17 |
bashast/bashast.g | 2 +- |
18 |
bashast/gunit/param_main.gunit | 2 ++ |
19 |
2 files changed, 3 insertions(+), 1 deletions(-) |
20 |
|
21 |
diff --git a/bashast/bashast.g b/bashast/bashast.g |
22 |
index 2659ef5..60ad096 100644 |
23 |
--- a/bashast/bashast.g |
24 |
+++ b/bashast/bashast.g |
25 |
@@ -254,7 +254,7 @@ var_ref |
26 |
| DOLLAR '_' -> ^(VAR_REF '_'); |
27 |
//Variable expansions |
28 |
var_exp : var_name (USE_DEFAULT|USE_ALTERNATE|DISPLAY_ERROR|ASSIGN_DEFAULT)^ word |
29 |
- | var_name COLON os=num (COLON len=num)? -> ^(OFFSET var_name $os ^($len)?) |
30 |
+ | var_name COLON wspace* LPAREN? os=arithmetic RPAREN? (COLON len=arithmetic)? -> ^(OFFSET var_name $os ^($len)?) |
31 |
| BANG^ var_name (TIMES|AT) |
32 |
| BANG var_name LSQUARE (op=TIMES|op=AT) RSQUARE -> ^(LIST_EXPAND var_name $op) |
33 |
| POUND^ var_name |
34 |
|
35 |
diff --git a/bashast/gunit/param_main.gunit b/bashast/gunit/param_main.gunit |
36 |
index 769f19f..a582179 100644 |
37 |
--- a/bashast/gunit/param_main.gunit |
38 |
+++ b/bashast/gunit/param_main.gunit |
39 |
@@ -23,6 +23,8 @@ var_ref: |
40 |
"${asdf}" -> (VAR_REF asdf) |
41 |
"${asdf:-foo}" -> (VAR_REF (:- asdf (STRING foo))) |
42 |
"${bar:7}" -> (VAR_REF (OFFSET bar 7)) |
43 |
+"${bar: -10}" -> (VAR_REF (OFFSET bar (MINUS_SIGN 10))) |
44 |
+"${bar:(-10 + 5)}" -> (VAR_REF (OFFSET bar (+ (MINUS_SIGN 10) 5))) |
45 |
"${foo:5:2}" -> (VAR_REF (OFFSET foo 5 2)) |
46 |
"${!asdf*}" -> (VAR_REF (! asdf *)) |
47 |
"${!asdf@}" -> (VAR_REF (! asdf @)) |