From d6a5792ce2529e24dd2303319e2e330dd9dbd753 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Thu, 22 Nov 2018 17:52:07 -0800 Subject: [PATCH] Allow nested square brackets again Code like echo $list[$var[1]] was producing an error because of nested square brackets. Allow these brackets again. Fixes #5362 --- src/tokenizer.cpp | 11 +---------- tests/expansion.err | 3 +++ tests/expansion.in | 7 +++++++ tests/expansion.out | 5 +++++ 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index 68b1d38e9..fd997a361 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -205,17 +205,8 @@ tok_t tokenizer_t::read_string() { expecting.pop_back(); } else if (c == L'[') { if (this->buff != buff_start) { - if ((mode & tok_modes::array_brackets) == tok_modes::array_brackets) { - // Nested brackets should not overwrite the existing slice_offset - // mqudsi: TOK_ILLEGAL_SLICE is the right error here, but the shell - // prints an error message with the caret pointing at token_start, - // not err_loc, making the TOK_ILLEGAL_SLICE message misleading. - // return call_error(TOK_ILLEGAL_SLICE, buff_start, this->buff); - return this->call_error(tokenizer_error_t::unterminated_slice, this->start, - this->buff); - } - slice_offset = this->buff - this->start; mode |= tok_modes::array_brackets; + slice_offset = this->buff - this->start; } else { // This is actually allowed so the test operator `[` can be used as the head of a // command diff --git a/tests/expansion.err b/tests/expansion.err index 8469cfc76..753d83cf8 100644 --- a/tests/expansion.err +++ b/tests/expansion.err @@ -1,3 +1,6 @@ + +#################### +# Slices fish: Invalid index value echo "$foo[d]" ^ diff --git a/tests/expansion.in b/tests/expansion.in index e31dc9723..961d811fe 100644 --- a/tests/expansion.in +++ b/tests/expansion.in @@ -60,6 +60,8 @@ set -l fooer '' expansion $$foo expansion prefix$$foo +logmsg Slices + set -l foo bar '' fooest expansion "$$foo" expansion $$foo @@ -101,6 +103,11 @@ echo $foo[d] echo ()[1] echo ()[d] +set -l outer out1 out2 +set -l inner 1 2 +echo $outer[$inner[2]] # out2 +echo $outer[$inner[2..1]] # out2 out1 + logmsg Percent self echo %selfNOT NOT%self \%self "%self" '%self' echo %self | string match -qr '^\\d+$' diff --git a/tests/expansion.out b/tests/expansion.out index 194906487..d46187a22 100644 --- a/tests/expansion.out +++ b/tests/expansion.out @@ -32,6 +32,9 @@ 2 prefixbaz prefixquux 3 baz quux 3 prefixbaz prefixquux prefix + +#################### +# Slices 1 baz quux fooest 2 baz quux 1 prefixbaz quux fooest @@ -62,6 +65,8 @@ # foo = a 0 +out2 +out2 out1 #################### # Percent self