1 |
commit: 95c2c86fdd4a52250393e05bbe5648367d5759ee |
2 |
Author: Petteri Räty <petsku <AT> petteriraty <DOT> eu> |
3 |
AuthorDate: Fri Apr 8 18:57:03 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Sat Apr 9 06:13:53 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=95c2c86f |
7 |
|
8 |
Improve brace expansion |
9 |
|
10 |
The parser assumed brace expansion could only happen once for a word but |
11 |
this not the case. Writing a{a,b}b{c,d} is valid bash and expands to |
12 |
four words. |
13 |
|
14 |
--- |
15 |
bashast/bashast.g | 15 ++++++--------- |
16 |
bashast/gunit/brace.gunit | 19 +++++++++---------- |
17 |
bashast/gunit/expansions.gunit | 2 +- |
18 |
3 files changed, 16 insertions(+), 20 deletions(-) |
19 |
|
20 |
diff --git a/bashast/bashast.g b/bashast/bashast.g |
21 |
index bf07b7f..5214b65 100644 |
22 |
--- a/bashast/bashast.g |
23 |
+++ b/bashast/bashast.g |
24 |
@@ -27,7 +27,6 @@ options |
25 |
tokens{ |
26 |
ARG; |
27 |
ARRAY; |
28 |
- BRACE; |
29 |
BRACE_EXP; |
30 |
COMMAND_SUB; |
31 |
CASE_PATTERN; |
32 |
@@ -111,8 +110,7 @@ bash_command |
33 |
: fname_no_res_word (BLANK+ arg)* -> ^(COMMAND fname_no_res_word arg*); |
34 |
//An argument to a command |
35 |
arg |
36 |
- : brace_expansion |
37 |
- | var_ref |
38 |
+ : var_ref |
39 |
| fname |
40 |
| command_sub |
41 |
| var_ref; |
42 |
@@ -144,16 +142,14 @@ redir_op: AMP LESS_THAN -> OP["&<"] |
43 |
| GREATER_THAN |
44 |
| DIGIT redir_op; |
45 |
brace_expansion |
46 |
- : pre=fname? brace post=fname? -> ^(BRACE_EXP ($pre)? brace ($post)?); |
47 |
-brace |
48 |
- : LBRACE BLANK* brace_expansion_inside BLANK?RBRACE -> ^(BRACE brace_expansion_inside); |
49 |
+ : LBRACE BLANK* brace_expansion_inside BLANK* RBRACE -> ^(BRACE_EXP brace_expansion_inside); |
50 |
brace_expansion_inside |
51 |
: commasep|range; |
52 |
range : DIGIT DOTDOT^ DIGIT |
53 |
| LETTER DOTDOT^ LETTER; |
54 |
brace_expansion_part |
55 |
- : fname |
56 |
- | brace |
57 |
+ : brace_expansion |
58 |
+ | fname |
59 |
| var_ref |
60 |
| command_sub; |
61 |
commasep: brace_expansion_part(COMMA! brace_expansion_part)+; |
62 |
@@ -388,13 +384,14 @@ no_res_word_part |
63 |
| var_ref |
64 |
| command_sub |
65 |
| arithmetic_expansion |
66 |
+ | brace_expansion |
67 |
| dqstr |
68 |
| sqstr |
69 |
| ns_str_part_no_res |
70 |
| SLASH |
71 |
| pattern_match_trigger; |
72 |
//non-quoted string rule, allows expansions |
73 |
-nqstr : (bracket_pattern_match|extended_pattern_match|var_ref|command_sub|arithmetic_expansion|dqstr|sqstr|(str_part str_part_with_pound*)|pattern_match_trigger|BANG)+; |
74 |
+nqstr : (bracket_pattern_match|extended_pattern_match|var_ref|command_sub|arithmetic_expansion|brace_expansion|dqstr|sqstr|(str_part str_part_with_pound*)|pattern_match_trigger|BANG)+; |
75 |
//double quoted string rule, allows expansions |
76 |
dqstr : QUOTE dqstr_part* QUOTE -> ^(DOUBLE_QUOTED_STRING dqstr_part*); |
77 |
dqstr_part |
78 |
|
79 |
diff --git a/bashast/gunit/brace.gunit b/bashast/gunit/brace.gunit |
80 |
index a5a5620..937e594 100644 |
81 |
--- a/bashast/gunit/brace.gunit |
82 |
+++ b/bashast/gunit/brace.gunit |
83 |
@@ -18,14 +18,13 @@ |
84 |
*/ |
85 |
gunit bashast; |
86 |
|
87 |
-brace: |
88 |
-"{a,b}"-> (BRACE (STRING a) (STRING b)) |
89 |
-"{a,b,c}" -> (BRACE (STRING a) (STRING b) (STRING c)) |
90 |
-"{a..d}" -> (BRACE (.. a d)) |
91 |
-"{{a,b},c,d}" -> (BRACE (BRACE (STRING a) (STRING b)) (STRING c) (STRING d)) |
92 |
- |
93 |
brace_expansion: |
94 |
-"{a,b}" -> (BRACE_EXP (BRACE (STRING a) (STRING b))) |
95 |
-"a{b,c}" -> (BRACE_EXP (STRING a) (BRACE (STRING b) (STRING c))) |
96 |
-"{c..d}f" -> (BRACE_EXP (BRACE (.. c d)) (STRING f)) |
97 |
-"z{{a,b},c}" -> (BRACE_EXP (STRING z) (BRACE (BRACE (STRING a) (STRING b)) (STRING c))) |
98 |
+"{a,b}"-> (BRACE_EXP (STRING a) (STRING b)) |
99 |
+"{a,b,c}" -> (BRACE_EXP (STRING a) (STRING b) (STRING c)) |
100 |
+"{a..d}" -> (BRACE_EXP (.. a d)) |
101 |
+"{{a,b},c,d}" -> (BRACE_EXP (BRACE_EXP (STRING a) (STRING b)) (STRING c) (STRING d)) |
102 |
+ |
103 |
+arg: |
104 |
+"a{b,c}" -> (STRING a (BRACE_EXP (STRING b) (STRING c))) |
105 |
+"{c..d}f" -> (STRING (BRACE_EXP (.. c d)) f) |
106 |
+"a{a,b}b{c,d}" -> (STRING a (BRACE_EXP (STRING a) (STRING b)) b (BRACE_EXP (STRING c) (STRING d))) |
107 |
|
108 |
diff --git a/bashast/gunit/expansions.gunit b/bashast/gunit/expansions.gunit |
109 |
index 2bce986..252c6bf 100644 |
110 |
--- a/bashast/gunit/expansions.gunit |
111 |
+++ b/bashast/gunit/expansions.gunit |
112 |
@@ -19,7 +19,7 @@ |
113 |
gunit bashast; |
114 |
|
115 |
list: |
116 |
-"echo a{b,c,d}" -> (LIST (COMMAND (STRING echo) (BRACE_EXP (STRING a) (BRACE (STRING b) (STRING c) (STRING d))))) |
117 |
+"echo a{b,c,d}" -> (LIST (COMMAND (STRING echo) (STRING a (BRACE_EXP (STRING b) (STRING c) (STRING d))))) |
118 |
"((5+5))" -> (LIST (COMPOUND_ARITH (+ 5 5))) |
119 |
"(( 4 + $asdf ))" -> (LIST (COMPOUND_ARITH (+ 4 (VAR_REF asdf)))) |
120 |
"[[ while=while ]] && echo true" -> (LIST (&& (COMPOUND_COND (KEYWORD_TEST (= (STRING while) (STRING while)))) (COMMAND (STRING echo) (STRING true)))) |