fix 'always'

This commit is contained in:
romkatv 2020-01-12 11:26:48 +01:00
parent b59f74a7bd
commit 60d00e9e75

View file

@ -1,5 +1,6 @@
typeset -gA __pb_cmd_skip=( typeset -gA __pb_cmd_skip=(
'}' '' '}' ''
'{' ''
'|' '' '|' ''
'||' '' '||' ''
'&' '' '&' ''
@ -9,7 +10,6 @@ typeset -gA __pb_cmd_skip=(
'&|' '' '&|' ''
')' '' ')' ''
'(' '' '(' ''
'{' ''
'()' '' '()' ''
'!' '' '!' ''
';' '' ';' ''
@ -84,34 +84,24 @@ typeset -gA __pb_term=(
';|' '' ';|' ''
'(' '' '(' ''
')' '' ')' ''
'{' ''
'}' ''
'()' '' '()' ''
'}' ''
) )
typeset -gA __pb_term_skip=( typeset -gA __pb_term_skip=(
'()' ''
'(' '\)' '(' '\)'
';;' '\)|esac' ';;' '\)|esac'
';&' '\)|esac' ';&' '\)|esac'
';|' '\)|esac' ';|' '\)|esac'
) )
# False positives: # Broken:
#
# {} always {}
#
# False negatives:
# #
# --------------- # ---------------
# : $(x) # : $(x)
# --------------- # ---------------
# : `x` # : `x`
# --------------- # ---------------
#
# Broken:
#
# ---------------
# ${x/} # ${x/}
# --------------- # ---------------
# - -- x # - -- x
@ -122,6 +112,8 @@ typeset -gA __pb_term_skip=(
# --------------- # ---------------
# x=$y; $x # x=$y; $x
# --------------- # ---------------
# alias x=y; y
# ---------------
# x <<END # x <<END
# ; END # ; END
# END # END
@ -135,6 +127,8 @@ typeset -gA __pb_term_skip=(
# #
# More brokenness with non-standard options (ignore_braces, ignore_close_braces, etc.). # More brokenness with non-standard options (ignore_braces, ignore_close_braces, etc.).
function _parse_buffer() { function _parse_buffer() {
[[ ${2:-0} == <-> ]] || return
local rcquotes local rcquotes
[[ -o rcquotes ]] && rcquotes=(-o rcquotes) [[ -o rcquotes ]] && rcquotes=(-o rcquotes)
@ -145,7 +139,7 @@ function _parse_buffer() {
local -r id='(<->|[[:alpha:]_][[:IDENT:]]#)' local -r id='(<->|[[:alpha:]_][[:IDENT:]]#)'
local -r var="\$$id|\${$id}|\"\$$id\"|\"\${$id}\"" local -r var="\$$id|\${$id}|\"\$$id\"|\"\${$id}\""
local -i e ic c=1024 local -i e ic c=${2:-'1 << 62'}
local skip n s r state local skip n s r state
local -a aln alp alf v commands local -a aln alp alf v commands
@ -156,19 +150,17 @@ function _parse_buffer() {
local tokens=(${(z)1}) local tokens=(${(z)1})
fi fi
() { {
while (( $#tokens )); do while (( $#tokens )); do
if (( $#tokens == alp[-1] )); then (( e = $#state ))
if (( $#alp && $#tokens == alp[-1] )); then
aln[-1]=() aln[-1]=()
alp[-1]=() alp[-1]=()
if (( $#tokens == alf[-1] )); then if (( $#tokens == alf[-1] )); then
alf[-1]=() alf[-1]=()
(( e = 0 )) (( e = 0 ))
else
(( e = $#state ))
fi fi
else
(( e = $#state ))
fi fi
while (( c-- > 0 )) || return; do while (( c-- > 0 )) || return; do
@ -204,24 +196,50 @@ function _parse_buffer() {
fi fi
case $state in case $state in
a)
if [[ $token == $skip ]]; then
if [[ $token == '{' ]]; then
_buffer_commands+=($commands)
commands=()
state=
else
skip='{'
fi
continue
else
state=t
fi
;& # fall through
t|p*) t|p*)
if (( $+__pb_term[$token] )); then if (( $+__pb_term[$token] )); then
skip=$__pb_term_skip[$token] if [[ $token == '()' ]]; then
state=${skip:+s} state=
[[ $token == '()' ]] || _buffer_commands+=($commands) else
_buffer_commands+=($commands)
if [[ $token == '}' ]]; then
state=a
skip=always
else
skip=$__pb_term_skip[$token]
state=${skip:+s}
fi
fi
commands=() commands=()
continue continue
elif [[ $state == t ]]; then elif [[ $state == t ]]; then
continue continue
fi;; fi
;;
s) s)
if [[ $token == $~skip ]]; then if [[ $token == $~skip ]]; then
state= state=
fi fi
continue;; continue
;;
*r) *r)
state[1]= state[1]=
continue;; continue
;;
h) h)
while (( $#tokens )); do while (( $#tokens )); do
(( e = ${tokens[(i)$token]} )) (( e = ${tokens[(i)$token]} ))
@ -237,7 +255,8 @@ function _parse_buffer() {
alp[-1]=() alp[-1]=()
done done
state=t state=t
continue;; continue
;;
esac esac
if (( $+__pb_redirect[${token#<0-255>}] )); then if (( $+__pb_redirect[${token#<0-255>}] )); then
@ -271,7 +290,8 @@ function _parse_buffer() {
continue continue
fi fi
fi fi
: ${token::=${(Q)${~token}}};; : ${token::=${(Q)${~token}}}
;;
p) p)
: ${token::=${(Q)${~token}}} : ${token::=${(Q)${~token}}}
case $token in case $token in
@ -279,11 +299,12 @@ function _parse_buffer() {
--) state=p1; continue;; --) state=p1; continue;;
$~skip) state=p2; continue;; $~skip) state=p2; continue;;
*) continue;; *) continue;;
esac;; esac
p1) ;; ;;
p2) p2)
state=p state=p
continue;; continue
;;
esac esac
commands+=$token commands+=$token
@ -294,8 +315,8 @@ function _parse_buffer() {
state=t state=t
fi fi
done done
} always {
_buffer_commands+=($commands)
_buffer_commands=(${(u)_buffer_commands:#('(('*'))'|'`'*'`'|'$'*)})
} }
_buffer_commands+=($commands)
_buffer_commands=(${(u)_buffer_commands:#('(('*'))'|'`'*'`'|'$'*)})
} }