From 986a39e09d963cb741acd25a6328892bd7e275ad Mon Sep 17 00:00:00 2001 From: Andy C Date: Wed, 26 Jun 2024 10:45:35 -0400 Subject: [PATCH] [osh] Allow ${a[@]:} in addition to ${a[@]: } This makes our parser shorter and more consistent. bash and zsh have a quirk where they don't allow the first, without a space. --- osh/word_parse.py | 14 +++++++------- spec/var-op-slice.test.sh | 9 ++++++++- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/osh/word_parse.py b/osh/word_parse.py index 95c8d63818..46856d0032 100644 --- a/osh/word_parse.py +++ b/osh/word_parse.py @@ -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 diff --git a/spec/var-op-slice.test.sh b/spec/var-op-slice.test.sh index 57ec455cf9..ffdeafd152 100644 --- a/spec/var-op-slice.test.sh +++ b/spec/var-op-slice.test.sh @@ -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[@]: }' @@ -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