aboutsummaryrefslogtreecommitdiff
blob: f4ca4e1c9e1274459e344f83339b8b6a2e40b64f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
   Please use git log for copyright holder and year information

   This file is part of libbash.

   libbash is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 2 of the License, or
   (at your option) any later version.

   libbash is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with libbash.  If not, see <http://www.gnu.org/licenses/>.
*/
gunit bashast;

cond_comparison:
"[[ -a this/is.afile ]]" -> (COMPOUND_COND (KEYWORD_TEST (a (STRING this / is . afile))))
"[[ -a this/is.afile]]" FAIL
"[[-a this/is.afile ]]" FAIL
"[[
-a this/is.afile ]]" -> (COMPOUND_COND (KEYWORD_TEST (a (STRING this / is . afile))))
"test ! -a this/is.afile" -> (COMPOUND_COND (BUILTIN_TEST (NEGATION (a (STRING this / is . afile)))))
"[[ asdf > qwert ]]" -> (COMPOUND_COND (KEYWORD_TEST (> (STRING asdf) (STRING qwert))))
"[ asdf \> qwert ]" -> (COMPOUND_COND (BUILTIN_TEST (\> (STRING asdf) (STRING qwert))))

arithmetic_expression:
"((5+3))" -> (ARITHMETIC_EXPRESSION (+ 5 3))
"(( 5+3 ))" -> (ARITHMETIC_EXPRESSION (+ 5 3))

current_shell:
"{ time cat; }" -> (CURRENT_SHELL (LIST (COMMAND (STRING cat) time)))
"{ time cat
}" -> (CURRENT_SHELL (LIST (COMMAND (STRING cat) time)))
// there's a tab on the empty line
"{
	echo
	
}" -> (CURRENT_SHELL (LIST (COMMAND (STRING echo))))
"{time cat}" FAIL
"{ time cat }" FAIL

subshell:
"(time cat)" -> (SUBSHELL (LIST (COMMAND (STRING cat) time)))
"(time cat )" ->(SUBSHELL (LIST (COMMAND (STRING cat) time)))
"( time cat)" -> (SUBSHELL (LIST (COMMAND (STRING cat) time)))
"( time cat;
)" -> (SUBSHELL (LIST (COMMAND (STRING cat) time)))

case_expr:
"case a in esac" -> (case (STRING a))
"case `echo asdf` in
esac" -> (case (STRING (COMMAND_SUB (LIST (COMMAND (STRING echo) (STRING asdf))))))

"case `echo asdf` in
gz)
echo yay
;;
bzip)
echo three
;;
*) echo woo
;;
esac" -> (case (STRING (COMMAND_SUB (LIST (COMMAND (STRING echo) (STRING asdf))))) (CASE_PATTERN (STRING gz) CASE_COMMAND (LIST (COMMAND (STRING echo) (STRING yay)))) (CASE_PATTERN (STRING bzip) CASE_COMMAND (LIST (COMMAND (STRING echo) (STRING three)))) (CASE_PATTERN (STRING MATCH_ALL) CASE_COMMAND (LIST (COMMAND (STRING echo) (STRING woo)))))

"case asdf in
	gz)
		echo yay
		;;
	bzip)
		echo three
		;;
	*) echo woo
esac" -> (case (STRING asdf) (CASE_PATTERN (STRING gz) CASE_COMMAND (LIST (COMMAND (STRING echo) (STRING yay)))) (CASE_PATTERN (STRING bzip) CASE_COMMAND (LIST (COMMAND (STRING echo) (STRING three)))) (CASE_PATTERN (STRING MATCH_ALL) CASE_COMMAND (LIST (COMMAND (STRING echo) (STRING woo)))))

"case `echo asdf` in gz|asdf) echo yay ;; bzip) echo three ;; *) echo woo esac" FAIL
"case asdf in gz|asdf) echo yay ;; bzip) echo three ;; *) echo woo ;; esac" -> (case (STRING asdf) (CASE_PATTERN (STRING gz) (STRING asdf) CASE_COMMAND (LIST (COMMAND (STRING echo) (STRING yay)))) (CASE_PATTERN (STRING bzip) CASE_COMMAND (LIST (COMMAND (STRING echo) (STRING three)))) (CASE_PATTERN (STRING MATCH_ALL) CASE_COMMAND (LIST (COMMAND (STRING echo) (STRING woo)))))

for_expr:
"for each in `ls |grep log`; do
	echo \"file found\"
done" -> (for each (STRING (COMMAND_SUB (LIST (| (COMMAND (STRING ls)) (COMMAND (STRING grep) (STRING log)))))) (LIST (COMMAND (STRING echo) (STRING (DOUBLE_QUOTED_STRING file   found)))))
"for each in `ls |grep log`; do echo \"file found\"; done" -> (for each (STRING (COMMAND_SUB (LIST (| (COMMAND (STRING ls)) (COMMAND (STRING grep) (STRING log)))))) (LIST (COMMAND (STRING echo) (STRING (DOUBLE_QUOTED_STRING file   found)))))
"for i in 'foo' 'bar'; do echo $i; done" -> (for i (STRING (SINGLE_QUOTED_STRING foo)) (STRING (SINGLE_QUOTED_STRING bar)) (LIST (COMMAND (STRING echo) (STRING (VAR_REF i)))))
"for i in foo$var bar; do echo $i; done" -> (for i (STRING foo (VAR_REF var)) (STRING bar) (LIST (COMMAND (STRING echo) (STRING (VAR_REF i)))))
"for each in `ls |grep log`; do echo file done" FAIL

"for ((5+3;5+3;5+3)); do echo yay; done" -> (CFOR (FOR_INIT (+ 5 3)) (FOR_COND (+ 5 3)) (LIST (COMMAND (STRING echo) (STRING yay))) (FOR_MOD (+ 5 3)))
"for ((5+3; ;5+3)); do echo yay; done" -> (CFOR (FOR_INIT (+ 5 3)) (LIST (COMMAND (STRING echo) (STRING yay))) (FOR_MOD (+ 5 3)))
"for ((;5+3 ;5+3)); do echo yay; done" -> (CFOR (FOR_COND (+ 5 3)) (LIST (COMMAND (STRING echo) (STRING yay))) (FOR_MOD (+ 5 3)))
"for ((5+3;;5+3)); do echo yay; done" -> (CFOR (FOR_INIT (+ 5 3)) (LIST (COMMAND (STRING echo) (STRING yay))) (FOR_MOD (+ 5 3)))

sel_expr:
"select each in `ls |grep log`; do
  echo \"file found\"
	done" -> (select each (STRING (COMMAND_SUB (LIST (| (COMMAND (STRING ls)) (COMMAND (STRING grep) (STRING log)))))) (LIST (COMMAND (STRING echo) (STRING (DOUBLE_QUOTED_STRING file   found)))))
"select each in `ls |grep log`; do echo \"file found\"; done" -> (select each (STRING (COMMAND_SUB (LIST (| (COMMAND (STRING ls)) (COMMAND (STRING grep) (STRING log)))))) (LIST (COMMAND (STRING echo) (STRING (DOUBLE_QUOTED_STRING file   found)))))
"select each in `ls |grep log`; do echo file done" FAIL

if_expr:
"if echo yay2; then
echo yay
fi" -> (IF_STATEMENT (if (LIST (COMMAND (STRING echo) (STRING yay2))) (LIST (COMMAND (STRING echo) (STRING yay)))))
"if echo yay2; then echo yay ;fi" -> (IF_STATEMENT (if (LIST (COMMAND (STRING echo) (STRING yay2))) (LIST (COMMAND (STRING echo) (STRING yay)))))
"if echo yay2 then echo yay; fi" FAIL

"if echo yay2;
then
echo yay
elif echo yay3; then
echo boo
fi" -> (IF_STATEMENT (if (LIST (COMMAND (STRING echo) (STRING yay2))) (LIST (COMMAND (STRING echo) (STRING yay)))) (if (LIST (COMMAND (STRING echo) (STRING yay3))) (LIST (COMMAND (STRING echo) (STRING boo)))))

"if echo yay2; then
echo yay
elif echo yay3; then
echo boo
elif echo yay4; then echo hurrah
else echo darn
fi" -> (IF_STATEMENT (if (LIST (COMMAND (STRING echo) (STRING yay2))) (LIST (COMMAND (STRING echo) (STRING yay)))) (if (LIST (COMMAND (STRING echo) (STRING yay3))) (LIST (COMMAND (STRING echo) (STRING boo)))) (if (LIST (COMMAND (STRING echo) (STRING yay4))) (LIST (COMMAND (STRING echo) (STRING hurrah)))) (else (LIST (COMMAND (STRING echo) (STRING darn)))))

"if true; then
        echo true
        # comment
    elif false; then
        echo false
    fi" -> (IF_STATEMENT (if (LIST (COMMAND (STRING true))) (LIST (COMMAND (STRING echo) (STRING true)))) (if (LIST (COMMAND (STRING false))) (LIST (COMMAND (STRING echo) (STRING false)))))

while_expr:
"while echo true; do
echo \"file found\"
done" -> (while (LIST (COMMAND (STRING echo) (STRING true))) (LIST (COMMAND (STRING echo) (STRING (DOUBLE_QUOTED_STRING file   found)))))
"while echo true; do echo \"file found\"; done" -> (while (LIST (COMMAND (STRING echo) (STRING true))) (LIST (COMMAND (STRING echo) (STRING (DOUBLE_QUOTED_STRING file   found)))))
"while(( 1>0 )); do echo ok; done" -> (while (LIST (COMMAND (ARITHMETIC_EXPRESSION (> 1 0)))) (LIST (COMMAND (STRING echo) (STRING ok))))
"while echo true`; do echo file done" FAIL
"while [[ -n \"$ver_str\" ]] ; do
		echo true
	done" -> (while (LIST (COMMAND (COMPOUND_COND (KEYWORD_TEST (n (STRING (DOUBLE_QUOTED_STRING (VAR_REF ver_str)))))))) (LIST (COMMAND (STRING echo) (STRING true))))

until_expr:
"until echo true; do
echo \"file found\"
done" -> (until (LIST (COMMAND (STRING echo) (STRING true))) (LIST (COMMAND (STRING echo) (STRING (DOUBLE_QUOTED_STRING file   found)))))
"until echo true; do echo \"file found\"; done" -> (until (LIST (COMMAND (STRING echo) (STRING true))) (LIST (COMMAND (STRING echo) (STRING (DOUBLE_QUOTED_STRING file   found)))))
"until echo true`; do echo file done" FAIL

case_expr:
"case \"$1\" in
*)
echo \"Usage: $0 start|stop\" >&2
exit 3
;;
esac" ->  (case (STRING (DOUBLE_QUOTED_STRING (VAR_REF 1))) (CASE_PATTERN (STRING MATCH_ALL) CASE_COMMAND (LIST (COMMAND (STRING echo) (STRING (DOUBLE_QUOTED_STRING Usage :   (VAR_REF 0)   start | stop)) (REDIR >& (FILE_DESCRIPTOR 2))) (COMMAND (STRING exit) (STRING 3)))))

"case $asdf in
a)
echo \"yay\"
;;
esac" -> (case (STRING (VAR_REF asdf)) (CASE_PATTERN (STRING a) CASE_COMMAND (LIST (COMMAND (STRING echo) (STRING (DOUBLE_QUOTED_STRING yay))))))
"case asdf in
asdf)
echo \"yay\"
;;
esac" -> (case (STRING asdf) (CASE_PATTERN (STRING asdf) CASE_COMMAND (LIST (COMMAND (STRING echo) (STRING (DOUBLE_QUOTED_STRING yay))))))
"case 1 in 1) echo yay ;; esac" -> (case (STRING 1) (CASE_PATTERN (STRING 1) CASE_COMMAND (LIST (COMMAND (STRING echo) (STRING yay)))))
"case /usr/bin in 1) echo yay ;; esac" -> (case (STRING / usr / bin) (CASE_PATTERN (STRING 1) CASE_COMMAND (LIST (COMMAND (STRING echo) (STRING yay)))))
"case \"$1\" in
stop)
;;
*)
echo \"Usage: $0 start|stop\" >&2
exit 3
;;
esac" -> (case (STRING (DOUBLE_QUOTED_STRING (VAR_REF 1))) (CASE_PATTERN (STRING stop)) (CASE_PATTERN (STRING MATCH_ALL) CASE_COMMAND (LIST (COMMAND (STRING echo) (STRING (DOUBLE_QUOTED_STRING Usage :   (VAR_REF 0)   start | stop)) (REDIR >& (FILE_DESCRIPTOR 2))) (COMMAND (STRING exit) (STRING 3)))))

command:
"[[ asdf > qwert ]] > /dev/null" -> (COMMAND (COMPOUND_COND (KEYWORD_TEST (> (STRING asdf) (STRING qwert)))) (REDIR > (STRING / dev / null)))
"(( 5+3 )) > /dev/null" -> (COMMAND (ARITHMETIC_EXPRESSION (+ 5 3)) (REDIR > (STRING / dev / null)))
"{ time cat; } > /dev/null" -> (COMMAND (CURRENT_SHELL (LIST (COMMAND (STRING cat) time))) (REDIR > (STRING / dev / null)))
"(time cat) > /dev/null" -> (COMMAND (SUBSHELL (LIST (COMMAND (STRING cat) time))) (REDIR > (STRING / dev / null)))
"case a in esac >/dev/null" -> (COMMAND (case (STRING a)) (REDIR > (STRING / dev / null)))
"for i in foo$var bar; do echo $i; done >/dev/null" -> (COMMAND (for i (STRING foo (VAR_REF var)) (STRING bar) (LIST (COMMAND (STRING echo) (STRING (VAR_REF i))))) (REDIR > (STRING / dev / null)))
"for ((5+3;;5+3)); do echo yay; done >/dev/null" -> (COMMAND (CFOR (FOR_INIT (+ 5 3)) (LIST (COMMAND (STRING echo) (STRING yay))) (FOR_MOD (+ 5 3))) (REDIR > (STRING / dev / null)))
"select each in `ls |grep log`; do echo \"file found\"; done >/dev/null" -> (COMMAND (select each (STRING (COMMAND_SUB (LIST (| (COMMAND (STRING ls)) (COMMAND (STRING grep) (STRING log)))))) (LIST (COMMAND (STRING echo) (STRING (DOUBLE_QUOTED_STRING file   found))))) (REDIR > (STRING / dev / null)))

"if echo yay2;
then
echo yay
elif echo yay3; then
echo boo
fi > /dev/null" -> (COMMAND (IF_STATEMENT (if (LIST (COMMAND (STRING echo) (STRING yay2))) (LIST (COMMAND (STRING echo) (STRING yay)))) (if (LIST (COMMAND (STRING echo) (STRING yay3))) (LIST (COMMAND (STRING echo) (STRING boo))))) (REDIR > (STRING / dev / null)))
"while echo true; do echo \"file found\"; done > /dev/null" -> (COMMAND (while (LIST (COMMAND (STRING echo) (STRING true))) (LIST (COMMAND (STRING echo) (STRING (DOUBLE_QUOTED_STRING file   found))))) (REDIR > (STRING / dev / null)))
"until echo true; do echo \"file found\"; done >/dev/null" -> (COMMAND (until (LIST (COMMAND (STRING echo) (STRING true))) (LIST (COMMAND (STRING echo) (STRING (DOUBLE_QUOTED_STRING file   found))))) (REDIR > (STRING / dev / null)))