From 69fcb40275dc7dbc51f46097483cb1328db16686 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 25 Sep 2015 21:18:37 +0000 Subject: [PATCH 1/6] Highlight backslash escapes within $'' strings. Correct highlighting of backslash escapes within "" strings: highlight only the four specific escape sequences defined there. Fixes zsh-users/zsh-syntax-highlighting#196. --- highlighters/main/README.md | 1 + highlighters/main/main-highlighter.zsh | 35 +++++++++++++++-- .../main/test-data/dollar-quoted2.zsh | 38 +++++++++++++++++++ highlighters/main/test-data/double-quoted.zsh | 4 +- .../main/test-data/double-quoted3.zsh | 2 + 5 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 highlighters/main/test-data/dollar-quoted2.zsh diff --git a/highlighters/main/README.md b/highlighters/main/README.md index 5bdc017..fff62de 100644 --- a/highlighters/main/README.md +++ b/highlighters/main/README.md @@ -45,6 +45,7 @@ This highlighter defines the following styles: * `dollar-quoted-argument` - dollar quoted arguments (`` $'foo' ``) * `dollar-double-quoted-argument` - dollar double quoted arguments ($foo inside "") * `back-double-quoted-argument` - back double quoted arguments (\x inside "") +* `back-dollar-quoted-argument` - back dollar quoted arguments (\x inside $'') * `assign` - variable assignments * `default` - parts of the buffer that do not match anything diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index b576c46..0284a3b 100755 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -54,6 +54,7 @@ : ${ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]:=fg=yellow} : ${ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]:=fg=cyan} : ${ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]:=fg=cyan} +: ${ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]:=fg=cyan} : ${ZSH_HIGHLIGHT_STYLES[assign]:=none} # Whether the highlighter should be called or not. @@ -103,7 +104,7 @@ _zsh_highlight_main_highlighter() for arg in ${(z)buf}; do # substr_color is set to 1 to disable adding an entry to region_highlight - # for this iteration. Currently, that is done for "" strings, + # for this iteration. Currently, that is done for "" and $'' strings, # which add the entry early so escape sequences within the string override # the string's color. integer substr_color=0 @@ -204,6 +205,9 @@ _zsh_highlight_main_highlighter() substr_color=1 ;; \$\'*) style=$ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument] + _zsh_highlight_main_add_region_highlight $start_pos $end_pos $style + _zsh_highlight_main_highlighter_highlight_dollar_string + substr_color=1 ;; '`'*) style=$ZSH_HIGHLIGHT_STYLES[back-quoted-argument];; *[*?]*) $highlight_glob && style=$ZSH_HIGHLIGHT_STYLES[globbing] || style=$ZSH_HIGHLIGHT_STYLES[default];; @@ -265,8 +269,6 @@ _zsh_highlight_main_highlighter_highlight_string() { setopt localoptions noksharrays local i j k style - local AA - integer c # Starting quote is at 1, so start parsing at offset 2 in the string. for (( i = 2 ; i < end_pos - start_pos ; i += 1 )) ; do (( j = i + start_pos - 1 )) @@ -282,6 +284,33 @@ _zsh_highlight_main_highlighter_highlight_string() fi ;; "\\") style=$ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument] + if [[ \\\`\"\$ == *$arg[$i+1]* ]]; then + (( k += 1 )) # Color following char too. + (( i += 1 )) # Skip parsing the escaped char. + else + continue + fi + ;; + *) continue ;; + + esac + _zsh_highlight_main_add_region_highlight $j $k $style + done +} + +# Highlight special chars inside dollar-quoted strings +_zsh_highlight_main_highlighter_highlight_dollar_string() +{ + setopt localoptions noksharrays + local i j k style + local AA + integer c + # Starting dollar-quote is at 1:2, so start parsing at offset 3 in the string. + for (( i = 3 ; i < end_pos - start_pos ; i += 1 )) ; do + (( j = i + start_pos - 1 )) + (( k = j + 1 )) + case "$arg[$i]" in + "\\") style=$ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument] for (( c = i + 1 ; c < end_pos - start_pos ; c += 1 )); do [[ "$arg[$c]" != ([0-9,xX,a-f,A-F]) ]] && break done diff --git a/highlighters/main/test-data/dollar-quoted2.zsh b/highlighters/main/test-data/dollar-quoted2.zsh new file mode 100644 index 0000000..21df3c8 --- /dev/null +++ b/highlighters/main/test-data/dollar-quoted2.zsh @@ -0,0 +1,38 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2015 zsh-syntax-highlighting contributors +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, are permitted +# provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this list of conditions +# and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, this list of +# conditions and the following disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors +# may be used to endorse or promote products derived from this software without specific prior +# written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# ------------------------------------------------------------------------------------------------- +# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*- +# vim: ft=zsh sw=2 ts=2 et +# ------------------------------------------------------------------------------------------------- + +ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]=$unused_highlight +BUFFER=": \$'foo\xbar'" + +expected_region_highlight=( + "3 7 $ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]" # $'foo + "8 11 $ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]" # \xba + "12 13 $ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]" # r' +) diff --git a/highlighters/main/test-data/double-quoted.zsh b/highlighters/main/test-data/double-quoted.zsh index 0847dca..35178a2 100644 --- a/highlighters/main/test-data/double-quoted.zsh +++ b/highlighters/main/test-data/double-quoted.zsh @@ -29,7 +29,7 @@ # ------------------------------------------------------------------------------------------------- BUFFER=': "foo$bar:\`:\":\$:' -BUFFER+=\\\':\" +BUFFER+=\\\\:\" expected_region_highlight=( "3 6 $ZSH_HIGHLIGHT_STYLES[double-quoted-argument]" # "foo @@ -41,6 +41,6 @@ expected_region_highlight=( "17 17 $ZSH_HIGHLIGHT_STYLES[double-quoted-argument]" # : "18 19 $ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]" # \" "20 20 $ZSH_HIGHLIGHT_STYLES[double-quoted-argument]" # : - "21 22 $ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]" # \' + "21 22 $ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]" # \\ "23 24 $ZSH_HIGHLIGHT_STYLES[double-quoted-argument]" # :" ) diff --git a/highlighters/main/test-data/double-quoted3.zsh b/highlighters/main/test-data/double-quoted3.zsh index 4a91c85..2d861cb 100644 --- a/highlighters/main/test-data/double-quoted3.zsh +++ b/highlighters/main/test-data/double-quoted3.zsh @@ -29,10 +29,12 @@ # ------------------------------------------------------------------------------------------------- BUFFER=': "$" "$42foo"' +BUFFER+=\ \"\\\'\\x\" expected_region_highlight=( "3 5 $ZSH_HIGHLIGHT_STYLES[double-quoted-argument]" # "$" "7 7 $ZSH_HIGHLIGHT_STYLES[double-quoted-argument]" # " "8 10 $ZSH_HIGHLIGHT_STYLES[dollar-double-quoted-argument]" # $42 "11 14 $ZSH_HIGHLIGHT_STYLES[double-quoted-argument]" # foo" + "16 21 $ZSH_HIGHLIGHT_STYLES[double-quoted-argument]" # "\'\x" - \' and \x are not escape sequences ) From 78ad649d479f677bf9ff555ea42cb3ef7b56b1ba Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 25 Sep 2015 21:36:20 +0000 Subject: [PATCH 2/6] back-dollar-quoted-argument: Don't consider comma a hex character. Character class syntax does not ignore commas between ranges. --- highlighters/main/main-highlighter.zsh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 0284a3b..2222943 100755 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -312,11 +312,11 @@ _zsh_highlight_main_highlighter_highlight_dollar_string() case "$arg[$i]" in "\\") style=$ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument] for (( c = i + 1 ; c < end_pos - start_pos ; c += 1 )); do - [[ "$arg[$c]" != ([0-9,xX,a-f,A-F]) ]] && break + [[ "$arg[$c]" != ([0-9xXa-fA-F]) ]] && break done AA=$arg[$i+1,$c-1] # Matching for HEX and OCT values like \0xA6, \xA6 or \012 - if [[ "$AA" =~ "^(0*(x|X)[0-9,a-f,A-F]{1,2})" || "$AA" =~ "^(0[0-7]{1,3})" ]];then + if [[ "$AA" =~ "^(0*(x|X)[0-9a-fA-F]{1,2})" || "$AA" =~ "^(0[0-7]{1,3})" ]];then (( k += $#MATCH )) (( i += $#MATCH )) else From 98366ade118e16e2a831602493a38fd40ec31cc9 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 25 Sep 2015 21:36:20 +0000 Subject: [PATCH 3/6] back-dollar-quoted-argument: Don't consider \0xHH a hex escape sequence. A leading zero is not permitted in the '\xHH' syntax. --- highlighters/main/main-highlighter.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 2222943..77d7785 100755 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -316,7 +316,7 @@ _zsh_highlight_main_highlighter_highlight_dollar_string() done AA=$arg[$i+1,$c-1] # Matching for HEX and OCT values like \0xA6, \xA6 or \012 - if [[ "$AA" =~ "^(0*(x|X)[0-9a-fA-F]{1,2})" || "$AA" =~ "^(0[0-7]{1,3})" ]];then + if [[ "$AA" =~ "^((x|X)[0-9a-fA-F]{1,2})" || "$AA" =~ "^(0[0-7]{1,3})" ]];then (( k += $#MATCH )) (( i += $#MATCH )) else From 8a43bd2cd98033a7951f43ecf730c8e9498900df Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 25 Sep 2015 21:36:20 +0000 Subject: [PATCH 4/6] back-dollar-quoted-argument: Highlight \uHHHH and \UHHHHHHHH escapes. --- highlighters/main/main-highlighter.zsh | 8 ++++++-- highlighters/main/test-data/dollar-quoted2.zsh | 6 ++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 77d7785..bf76e98 100755 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -312,11 +312,15 @@ _zsh_highlight_main_highlighter_highlight_dollar_string() case "$arg[$i]" in "\\") style=$ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument] for (( c = i + 1 ; c < end_pos - start_pos ; c += 1 )); do - [[ "$arg[$c]" != ([0-9xXa-fA-F]) ]] && break + [[ "$arg[$c]" != ([0-9xXuUa-fA-F]) ]] && break done AA=$arg[$i+1,$c-1] # Matching for HEX and OCT values like \0xA6, \xA6 or \012 - if [[ "$AA" =~ "^((x|X)[0-9a-fA-F]{1,2})" || "$AA" =~ "^(0[0-7]{1,3})" ]];then + if [[ "$AA" =~ "^(x|X)[0-9a-fA-F]{1,2}" + || "$AA" =~ "^0[0-7]{1,3}" + || "$AA" =~ "^u[0-9a-fA-F]{1,4}" + || "$AA" =~ "^U[0-9a-fA-F]{1,8}" + ]]; then (( k += $#MATCH )) (( i += $#MATCH )) else diff --git a/highlighters/main/test-data/dollar-quoted2.zsh b/highlighters/main/test-data/dollar-quoted2.zsh index 21df3c8..ecaaa39 100644 --- a/highlighters/main/test-data/dollar-quoted2.zsh +++ b/highlighters/main/test-data/dollar-quoted2.zsh @@ -29,10 +29,12 @@ # ------------------------------------------------------------------------------------------------- ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]=$unused_highlight -BUFFER=": \$'foo\xbar'" +BUFFER=": \$'foo\xbar\udeadbeef'" expected_region_highlight=( "3 7 $ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]" # $'foo "8 11 $ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]" # \xba - "12 13 $ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]" # r' + "12 12 $ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]" # r + "13 18 $ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]" # \dead + "19 23 $ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]" # beef' ) From 0788514cbd3e6165a498cf64ddfae50fd0834b54 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 25 Sep 2015 21:36:20 +0000 Subject: [PATCH 5/6] back-dollar-quoted-argument: Correct octal escape syntax. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A leading zero, as in '\0NNN', is permitted by 'echo' but not by 'print', and «$''» follows 'print'. --- highlighters/main/main-highlighter.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index bf76e98..483d285 100755 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -317,7 +317,7 @@ _zsh_highlight_main_highlighter_highlight_dollar_string() AA=$arg[$i+1,$c-1] # Matching for HEX and OCT values like \0xA6, \xA6 or \012 if [[ "$AA" =~ "^(x|X)[0-9a-fA-F]{1,2}" - || "$AA" =~ "^0[0-7]{1,3}" + || "$AA" =~ "^[0-7]{1,3}" || "$AA" =~ "^u[0-9a-fA-F]{1,4}" || "$AA" =~ "^U[0-9a-fA-F]{1,8}" ]]; then From 6732783d46baa15e0b48a1e24922ec79640af243 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 25 Sep 2015 21:36:20 +0000 Subject: [PATCH 6/6] back-dollar-quoted-argument: Correct an off-by-one. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Highlight the last character of a «\xHH» escape when it is the last thing in LBUFFER. This is similar to what b0cc02ed86e3586ab92cc1082fb97b94cd5f584f did for issue #186. --- highlighters/main/main-highlighter.zsh | 2 +- .../main/test-data/dollar-quoted3.zsh | 39 +++++++++++++++++++ .../main/test-data/double-quoted2.zsh | 1 + 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 highlighters/main/test-data/dollar-quoted3.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 483d285..ef42709 100755 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -311,7 +311,7 @@ _zsh_highlight_main_highlighter_highlight_dollar_string() (( k = j + 1 )) case "$arg[$i]" in "\\") style=$ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument] - for (( c = i + 1 ; c < end_pos - start_pos ; c += 1 )); do + for (( c = i + 1 ; c <= end_pos - start_pos ; c += 1 )); do [[ "$arg[$c]" != ([0-9xXuUa-fA-F]) ]] && break done AA=$arg[$i+1,$c-1] diff --git a/highlighters/main/test-data/dollar-quoted3.zsh b/highlighters/main/test-data/dollar-quoted3.zsh new file mode 100644 index 0000000..2632ff9 --- /dev/null +++ b/highlighters/main/test-data/dollar-quoted3.zsh @@ -0,0 +1,39 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2015 zsh-syntax-highlighting contributors +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, are permitted +# provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this list of conditions +# and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, this list of +# conditions and the following disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors +# may be used to endorse or promote products derived from this software without specific prior +# written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# ------------------------------------------------------------------------------------------------- +# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*- +# vim: ft=zsh sw=2 ts=2 et +# ------------------------------------------------------------------------------------------------- + +# Similar to double-quoted2.zsh +ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]=$unused_highlight +# This test checks that the '1' gets highlighted correctly. Do not append to the BUFFER. +BUFFER=": \$'\xa1" + +expected_region_highlight=( + "3 4 $ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]" # $' + "5 8 $ZSH_HIGHLIGHT_STYLES[back-dollar-quoted-argument]" # \xa1 +) diff --git a/highlighters/main/test-data/double-quoted2.zsh b/highlighters/main/test-data/double-quoted2.zsh index e596c6d..fd202fe 100644 --- a/highlighters/main/test-data/double-quoted2.zsh +++ b/highlighters/main/test-data/double-quoted2.zsh @@ -28,6 +28,7 @@ # vim: ft=zsh sw=2 ts=2 et # ------------------------------------------------------------------------------------------------- +# Similar to dollar-quoted3.zsh # This test checks that the 'r' gets highlighted correctly. Do not append to the BUFFER. BUFFER=': "foo$bar'