Skip to content

Commit

Permalink
[osh] Allow ${a[@]:} in addition to ${a[@]: }
Browse files Browse the repository at this point in the history
This makes our parser shorter and more consistent.

bash and zsh have a quirk where they don't allow the first, without a
space.
  • Loading branch information
Andy C committed Jun 26, 2024
1 parent 5d2e6a6 commit 986a39e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
14 changes: 7 additions & 7 deletions osh/word_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,19 +264,19 @@ def _ReadVarOpArg2(self, arg_lex_mode, eof_type, empty_ok):
def _ReadSliceVarOp(self):
# type: () -> suffix_op.Slice
"""
Looking token after first ':'
ArithExpr? (':' ArithExpr? )? '}'
"""
# Looking token after first ':'
self._SetNext(lex_mode_e.Arith)
self._GetToken()
self._NextNonSpace()

cur_id = self.token_type

begin = arith_expr.EmptyZero # type: arith_expr_t
if cur_id != Id.Arith_Colon: # A pun for Id.VOp2_Colon
if cur_id in (Id.Arith_RBrace, Id.Arith_Colon): # ${a:} or ${a::}
begin = arith_expr.EmptyZero # type: arith_expr_t
else:
begin = self.a_parser.Parse()
cur_id = self.a_parser.CurrentId()
#log('after begin %s', Id_str(cur_id))
cur_id = self.a_parser.CurrentId() # advance

if cur_id == Id.Arith_RBrace: # ${a:1} or ${@:1}
no_length = None # type: Optional[arith_expr_t] # No length specified
Expand Down
9 changes: 8 additions & 1 deletion spec/var-op-slice.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ argv.py "${s:0:0}"
['1', '2', '3']
## END

#### Inconsistent - ${array[@]:} is not allowed but ${array[@]: } is
#### ${array[@]:} vs ${array[@]: } - bash and zsh inconsistent

$SH -c 'array=(1 2 3); argv.py ${array[@]:}'
$SH -c 'array=(1 2 3); argv.py space ${array[@]: }'
Expand All @@ -363,6 +363,13 @@ $SH -c 's=123; argv.py space ${s: }'
['space', '123']
## END

## OK osh STDOUT:
['1', '2', '3']
['space', '1', '2', '3']
['123']
['space', '123']
## END

## BUG mksh STDOUT:
['space', '123']
## END
Expand Down

0 comments on commit 986a39e

Please sign in to comment.