Skip to content

Commit 2c1b487

Browse files
committed
[arith] Add 'shopt -s parse_empty_arith' for compatibility.
1 parent d481482 commit 2c1b487

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
@@ -231,6 +231,7 @@ def _Init(opt_def):
231231

232232
opt_def.Add('eval_unsafe_arith') # recursive parsing and evaluation (ble.sh)
233233
opt_def.Add('parse_dynamic_arith') # dynamic LHS
234+
opt_def.Add('parse_empty_arith') # empty arithmetic expressions
234235
opt_def.Add('compat_array') # ${array} is ${array[0]}
235236

236237
# Two strict options that from bash's shopt

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
@@ -600,6 +600,7 @@ a=1
600600

601601
#### Empty expression: $(())
602602
case $SH in (dash) exit 1;; esac
603+
shopt -s parse_empty_arith 2>/dev/null
603604
echo 1:$(())
604605
echo 2:$(( ))
605606
## STDOUT:
@@ -613,6 +614,7 @@ echo 2:$(( ))
613614
# dash/mksh doesn't support $[]
614615
# oil has different meaning for $[]
615616
case ${SH##*/} in (dash|mksh|osh) exit 1;; esac
617+
shopt -s parse_empty_arith 2>/dev/null
616618
echo 3:$[]
617619
echo 4:$[ ]
618620
## STDOUT:
@@ -624,6 +626,7 @@ echo 4:$[ ]
624626

625627
#### Empty expression: (())
626628
case $SH in (dash) exit 1;; esac
629+
shopt -s parse_empty_arith 2>/dev/null
627630
(()) && echo unexpected || echo OK
628631
(( )) && echo unexpected || echo OK
629632
! (()) && echo OK || echo unexpected
@@ -643,6 +646,7 @@ case $SH in
643646
(mksh|zsh) exit 1;; # mksh/zsh does not support.
644647
(bash) exit 1;; # bash supports this from bash-5.0.
645648
esac
649+
shopt -s parse_empty_arith 2>/dev/null
646650

647651
arr=(foo bar)
648652
echo "${arr[ ]}"
@@ -655,8 +659,10 @@ foo
655659
#### Empty expression: ${var::}
656660
case $SH in
657661
(dash) exit 1;; # dash does not support arrays.
658-
(dash) exit 1;; # zsh does not support empty offset/length.
662+
(zsh) exit 1;; # zsh does not support empty offset/length.
659663
esac
664+
shopt -s parse_empty_arith 2>/dev/null
665+
660666
var=abcd
661667
echo "1:[${var::1}]"
662668
echo "2:[${var: :1}]"
@@ -681,6 +687,7 @@ case $SH in
681687
(mksh) exit 1;; # mksh does not support ${array[@]:offset:length}
682688
(zsh) exit 1;; # zsh does not support empty offset/length
683689
esac
690+
shopt -s parse_empty_arith 2>/dev/null
684691
array=(1 2 3)
685692
argv.py "${array[@]::1}"
686693
argv.py "${array[@]: :1}"

0 commit comments

Comments
 (0)