Skip to content

Commit 57b464e

Browse files
committed
[arith] Add 'shopt -s parse_empty_arith' for compatibility.
1 parent 002848d commit 57b464e

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

frontend/option_def.py

+1
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ def _Init(opt_def):
232232
# TODO: Rename eval_unsafe_arith
233233
opt_def.Add('eval_unsafe_arith') # recursive parsing and evaluation (ble.sh)
234234
opt_def.Add('parse_dynamic_arith') # dyanmic LHS
235+
opt_def.Add('parse_empty_arith') # empty arithmetic expressions
235236

236237
# Two strict options that from bash's shopt
237238
for name in ['nullglob', 'inherit_errexit']:

osh/arith_parse.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ def __init__(self, w_parser, parse_opts):
190190
def Parse(self):
191191
# type: () -> arith_expr_t
192192
self.Next() # may raise ParseError
193-
if self.op_id in (Id.Arith_RParen, Id.Arith_RBrace, Id.Arith_RBracket, Id.Arith_Colon):
194-
return arith_expr.Empty()
193+
if self.parse_opts.parse_empty_arith():
194+
if self.op_id in (Id.Arith_RParen, Id.Arith_RBrace, Id.Arith_RBracket, Id.Arith_Colon):
195+
return arith_expr.Empty()
195196
return self.ParseUntil(0)

spec/arith.test.sh

+8-1
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ echo $((1?2?3:4:5))
580580

581581
#### Empty expression: $(())
582582
case $SH in (dash) exit 1;; esac
583+
shopt -s parse_empty_arith 2>/dev/null
583584
echo 1:$(())
584585
echo 2:$(( ))
585586
## STDOUT:
@@ -593,6 +594,7 @@ echo 2:$(( ))
593594
# dash/mksh doesn't support $[]
594595
# oil has different meaning for $[]
595596
case ${SH##*/} in (dash|mksh|osh) exit 1;; esac
597+
shopt -s parse_empty_arith 2>/dev/null
596598
echo 3:$[]
597599
echo 4:$[ ]
598600
## STDOUT:
@@ -604,6 +606,7 @@ echo 4:$[ ]
604606

605607
#### Empty expression: (())
606608
case $SH in (dash) exit 1;; esac
609+
shopt -s parse_empty_arith 2>/dev/null
607610
(()) && echo unexpected || echo OK
608611
(( )) && echo unexpected || echo OK
609612
! (()) && echo OK || echo unexpected
@@ -623,6 +626,7 @@ case $SH in
623626
(mksh|zsh) exit 1;; # mksh/zsh does not support.
624627
(bash) exit 1;; # bash supports this from bash-5.0.
625628
esac
629+
shopt -s parse_empty_arith 2>/dev/null
626630

627631
arr=(foo bar)
628632
echo "${arr[ ]}"
@@ -635,8 +639,10 @@ foo
635639
#### Empty expression: ${var::}
636640
case $SH in
637641
(dash) exit 1;; # dash does not support arrays.
638-
(dash) exit 1;; # zsh does not support empty offset/length.
642+
(zsh) exit 1;; # zsh does not support empty offset/length.
639643
esac
644+
shopt -s parse_empty_arith 2>/dev/null
645+
640646
var=abcd
641647
echo "1:[${var::1}]"
642648
echo "2:[${var: :1}]"
@@ -661,6 +667,7 @@ case $SH in
661667
(mksh) exit 1;; # mksh does not support ${array[@]:offset:length}
662668
(zsh) exit 1;; # zsh does not support empty offset/length
663669
esac
670+
shopt -s parse_empty_arith 2>/dev/null
664671
array=(1 2 3)
665672
argv.py "${array[@]::1}"
666673
argv.py "${array[@]: :1}"

0 commit comments

Comments
 (0)