/
parse-errors.sh
executable file
·507 lines (373 loc) · 8.91 KB
/
parse-errors.sh
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#!/bin/bash
#
# Usage:
# ./parse-errors.sh <function name>
set -o nounset
set -o pipefail
set -o errexit
source test/common.sh
# Run with SH=bash too
SH=${SH:-bin/osh}
banner() {
echo
echo ===== CASE: "$@" =====
echo
}
_error-case() {
banner "$@"
echo
$SH -c "$@"
# NOTE: This works with osh, not others.
local status=$?
if test $status != 2; then
die "Expected status 2, got $?"
fi
}
# All in osh/word_parse.py
patsub() {
set +o errexit
_error-case 'echo ${x/}' # pattern must not be empty
# These are a little odd
_error-case 'echo ${x//}'
#_error-case 'echo ${x///}'
#_error-case 'echo ${x/foo}'
#_error-case 'echo ${x//foo}'
# This should be a different error? It should be an empty pattern?
_error-case 'echo ${x///foo}'
# Newline in replacement pattern
#_error-case 'echo ${x//foo/replace
#}'
#_error-case 'echo ${x//foo/replace$foo}'
}
# osh/word_parse.py
word-parse() {
set +o errexit
_error-case 'echo ${'
# This parses like a slice, but that's OK. Maybe talk about arithmetic
# expression. Maybe say where it started?
_error-case '${foo:}'
_error-case 'echo ${a[@Z'
_error-case 'echo ${x.}'
_error-case 'echo ${!x.}'
# Slicing
_error-case 'echo ${a:1;}'
_error-case 'echo ${a:1:2;}'
# I don't seem to be able to tickle errors here
#_error-case 'echo ${a:-}'
#_error-case 'echo ${a#}'
_error-case 'echo ${#a.'
# for (( ))
_error-case 'for (( i = 0; i < 10; i++ ;'
# Hm not sure about this
_error-case 'for (( i = 0; i < 10; i++ /'
_error-case 'echo @(extglob|foo'
# Copied from osh/word_parse_test.py. Bugs were found while writing
# core/completion_test.py.
_error-case '${undef:-'
_error-case '${undef:-$'
_error-case '${undef:-$F'
_error-case '${x@'
_error-case '${x@Q'
_error-case '${x%'
_error-case '${x/'
_error-case '${x/a/'
_error-case '${x/a/b'
_error-case '${x:'
}
array-literal() {
set +o errexit
# Array literal with invalid TokenWord.
_error-case 'a=(1 & 2)'
_error-case 'a= (1 2)'
_error-case 'a=(1 2'
_error-case 'a=(1 ${2@} )' # error in word inside array literal
}
arith-context() {
set +o errexit
# $(( ))
_error-case 'echo $(( 1 + 2 ;'
_error-case 'echo $(( 1 + 2 );'
_error-case 'echo $(( '
_error-case 'echo $(( 1'
# Non-standard arith sub $[1 + 2]
_error-case 'echo $[ 1 + 2 ;'
# What's going on here? No location info?
_error-case 'echo $[ 1 + 2 /'
_error-case 'echo $[ 1 + 2 / 3'
_error-case 'echo $['
# (( ))
_error-case '(( 1 + 2 /'
_error-case '(( 1 + 2 )/'
_error-case '(( 1'
_error-case '(('
# Should be an error
_error-case 'a[x+]=1'
_error-case 'a[]=1'
_error-case 'a[*]=1'
}
arith-expr() {
set +o errexit
# BUG: the token is off here
_error-case '$(( 1 + + ))'
# BUG: not a great error either
_error-case '$(( 1 2 ))'
# Triggered a crash!
_error-case '$(( - ; ))'
# NOTE: This is confusing, should point to ` for command context?
_error-case '$(( ` ))'
_error-case '$(( $ ))'
# "Can't assign to None" is misleading.
# From wild/src/distro/portage/bin/helper-functions.sh
_error-case '$(( ${var} = fd ))'
}
command-sub() {
set +o errexit
_error-case '
echo line 2
echo $( echo '
_error-case '
echo line 2
echo ` echo '
# Both unclosed
_error-case '
echo line 2
echo ` echo \` '
# Only the inner one is unclosed
_error-case '
echo line 2
echo ` echo \`unclosed ` '
}
bool-expr() {
set +o errexit
# Extra word
_error-case '[[ a b ]]'
_error-case '[[ a "a"$(echo hi)"b" ]]'
# Wrong error message
_error-case '[[ a == ]]'
# Invalid regex
_error-case '[[ $var =~ * ]]'
_error-case '[[ $var =~ \\ ]]'
# Unbalanced parens
_error-case '[[ ( 1 == 2 - ]]'
_error-case '[[ == ]]'
_error-case '[[ ) ]]'
_error-case '[[ ( ]]'
_error-case '[[ ;;; ]]'
_error-case '[['
}
# These don't have any location information.
test-builtin() {
set +o errexit
# Some of these come from osh/bool_parse.py, and some from
# osh/builtin_bracket.py.
# Extra token
_error-case '[ x -a y f ]'
_error-case 'test x -a y f'
# Missing closing ]
_error-case '[ x '
# Hm some of these errors are wonky. Need positions.
_error-case '[ x x ]'
_error-case '[ x x "a b" ]'
# This is a runtime error but is handled similarly
_error-case '[ -t xxx ]'
_error-case '[ \( x -a -y -a z ]'
# -o tests if an option is enabled.
#_error-case '[ -o x ]'
}
printf-builtin() {
set +o errexit
_error-case 'printf %'
_error-case 'printf [%Z]'
_error-case 'printf -v "-invalid-" %s foo'
}
other-builtins() {
set +o errexit
_error-case 'shift 1 2'
_error-case 'shift zzz'
_error-case 'pushd x y'
_error-case 'pwd -x'
_error-case 'repr foo a-x'
_error-case 'wait zzz'
_error-case 'wait %jobspec-not-supported'
}
quoted-strings() {
set +o errexit
_error-case '"unterminated double'
_error-case "'unterminated single"
_error-case '
"unterminated double multiline
line 1
line 2'
_error-case "
'unterminated single multiline
line 1
line 2"
}
cmd-parse() {
set +o errexit
_error-case 'FOO=1 break'
_error-case 'break 1 2'
_error-case 'break >out'
_error-case 'for x in &'
_error-case 'for (( i=0; i<10; i++ )) ls'
_error-case 'for $x in 1 2 3; do echo $i; done'
_error-case 'for x.y in 1 2 3; do echo $i; done'
_error-case 'for x in 1 2 3; &'
_error-case 'for foo BAD'
_error-case 'x"y"() { echo hi; }'
_error-case 'function x"y" { echo hi; }'
_error-case '}'
_error-case 'case foo in *) echo '
_error-case 'ls foo|'
_error-case 'ls foo&&'
_error-case 'foo()'
# Unquoted (. What happened here?
_error-case '[ ( x ]'
}
append() {
# from spec/append.test.sh. bash treats this as a runtime error, but it's a
# parse error in OSH.
_error-case 'a[-1]+=(4 5)'
}
redirect() {
set +o errexit
_error-case 'echo < <<'
_error-case 'echo $( echo > >> )'
_error-case 'FOO=1 BAR=2 > out'
_error-case '> out FOO=1 BAR=2'
_error-case 'local BAR=2 > out'
}
simple-command() {
set +o errexit
_error-case 'PYTHONPATH=. FOO=(1 2) python'
_error-case 'echo foo FOO=(1 2)'
_error-case 'PYTHONPATH+=1 python'
}
assign() {
set +o errexit
_error-case 'local name$x'
_error-case 'local "ab"'
_error-case 'local a.b'
_error-case 'FOO=1 local foo=1'
}
# I can't think of any other here doc error conditions except arith/var/command
# substitution, and unterminated.
here-doc() {
set +o errexit
# Arith in here doc
_error-case 'cat <<EOF
$(( 1 * ))
EOF
'
# Varsub in here doc
_error-case 'cat <<EOF
invalid: ${a!}
EOF
'
_error-case 'cat <<EOF
$(for x in )
EOF
'
}
here-doc-delimiter() {
set +o errexit
# NOTE: This is more like the case where.
_error-case 'cat << $(invalid here end)'
# TODO: Arith parser doesn't have location information
_error-case 'cat << $((1+2))'
_error-case 'cat << a=(1 2 3)'
_error-case 'cat << \a$(invalid)'
# Actually the $invalid part should be highlighted... yeah an individual
# part is the problem.
#"cat << 'single'$(invalid)"
_error-case 'cat << "double"$(invalid)'
_error-case 'cat << ~foo/$(invalid)'
_error-case 'cat << $var/$(invalid)'
}
args-parse-builtin() {
set +o errexit
_error-case 'read -x' # invalid
_error-case 'read -n' # expected argument for -n
_error-case 'read -n x' # expected integer
_error-case 'set -o errexit +o oops'
# not implemented yet
#_error-case 'read -t x' # expected floating point number
# TODO:
# - invalid choice
# - Oil flags: invalid long flag, boolean argument, etc.
}
# aiding the transition
args-parse-more() {
set +o errexit
_error-case 'set -z'
_error-case 'shopt -s foo'
_error-case 'shopt -z'
}
args-parse-main() {
set +o errexit
bin/osh --ast-format x
bin/osh -o errexit +o oops
# TODO: opy/opy_main.py uses OilFlags, which has Go-like boolean syntax
}
strict_backslash_warnings() {
echo $'\A'
echo -e '\A'
}
invalid-brace-ranges() {
set +o errexit
_error-case 'echo {1..3..-1}'
_error-case 'echo {1..3..0}'
_error-case 'echo {3..1..1}'
_error-case 'echo {3..1..0}'
_error-case 'echo {a..Z}'
_error-case 'echo {a..z..0}'
_error-case 'echo {a..z..-1}'
_error-case 'echo {z..a..1}'
}
cases-in-strings() {
set +o errexit
cmd-parse
simple-command
command-sub
redirect
here-doc
here-doc-delimiter
append
# Word
word-parse
array-literal
patsub
quoted-strings
# Arith
arith-context
arith-expr
bool-expr
test-builtin
printf-builtin
other-builtins
# frontend/args.py
args-parse-builtin
args-parse-main
invalid-brace-ranges # osh/braces.py
}
# Cases in their own file
cases-in-files() {
set +o errexit # Don't fail
for t in test/parse-errors/*.sh; do
banner $t
$SH $t
done
}
all() {
cases-in-strings
echo
echo ----------------------
echo
cases-in-files
# Always passes
return 0
}
run-for-release() {
run-other-suite-for-release parse-errors all
}
"$@"