Skip to content

Commit

Permalink
syntax: strictly check variable names of "for"-statements
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Dec 6, 2021
1 parent 60d244f commit d056547
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -120,6 +120,7 @@
- main: suppress non-interactive warnings from manually sourced startup files (reported by andreclerigo) `#D1676` 0525528 88e2df5
- mandb: integrate `mandb` with `bash-completion` (motivated by Shahabaz-Bagwan, bbyfacekiller and EmilySeville7cfg) `#D1688` 0000000
- syntax: do not start argument completions immediately after previous word (reported by EmilySeville7cfg) `#D1690` 0000000
- syntax: strictly check variable names of `for`-statements `#D1692` 0000000

## Fixes

Expand Down
27 changes: 16 additions & 11 deletions lib/core-syntax.sh
Expand Up @@ -2114,6 +2114,10 @@ function ble/syntax:bash/check-glob {
if ((ctx==CTX_VRHS||ctx==CTX_ARGVR||ctx==CTX_ARGER||ctx==CTX_VALR||ctx==CTX_RDRS)); then
force_attr=$ctx
ntype="glob_attr=$force_attr"
elif ((ctx==CTX_FARGX1||ctx==CTX_FARGI1)); then
# for [xxx] / for a[xxx] の場合
force_attr=$ATTR_ERR
ntype="glob_attr=$force_attr"
elif ((ctx==CTX_PWORD||ctx==CTX_PWORDE||ctx==CTX_PWORDR)); then
ntype="glob_ctx=$ctx"
elif ((ctx==CTX_PATN||ctx==CTX_BRAX)); then
Expand Down Expand Up @@ -3422,7 +3426,7 @@ _ble_syntax_bash_command_EndWtype[CTX_CMDXE]=$CTX_CMDI
_ble_syntax_bash_command_EndWtype[CTX_CMDXD]=$CTX_CMDI
_ble_syntax_bash_command_EndWtype[CTX_CMDXD0]=$CTX_CMDI
_ble_syntax_bash_command_EndWtype[CTX_CMDXV]=$CTX_CMDI
_ble_syntax_bash_command_EndWtype[CTX_FARGX1]=$CTX_ARGI
_ble_syntax_bash_command_EndWtype[CTX_FARGX1]=$CTX_FARGI1 # 変数名
_ble_syntax_bash_command_EndWtype[CTX_SARGX1]=$CTX_ARGI
_ble_syntax_bash_command_EndWtype[CTX_FARGX2]=$CTX_FARGI2 # in
_ble_syntax_bash_command_EndWtype[CTX_FARGX3]=$CTX_ARGI # in
Expand Down Expand Up @@ -3907,16 +3911,7 @@ function ble/syntax:bash/ctx-command {
elif local rex='^([^'${_ble_syntax_bash_chars[CTX_ARGI]}']+|\\.)'; [[ $tail =~ $rex ]]; then
local rematch=$BASH_REMATCH
local attr=$ctx
if ((attr==CTX_FARGI1)); then
# for var in ... の var の部分は変数名をチェックして着色
if rex='^[a-zA-Z_][a-zA-Z_0-9]*'; ((i==wbegin)) && [[ $rematch =~ $rex ]]; then
local ret; ble/syntax/highlight/vartype "$BASH_REMATCH"; attr=$ret
else
attr=$ATTR_ERR
fi
elif [[ $BASH_REMATCH == '\'? ]]; then
attr=$ATTR_QESC
fi
[[ $BASH_REMATCH == '\'? ]] && attr=$ATTR_QESC
((_ble_syntax_attr[i]=attr,i+=${#rematch}))
flagConsume=1
elif ble/syntax:bash/check-process-subst; then
Expand All @@ -3939,6 +3934,16 @@ function ble/syntax:bash/ctx-command {

if ((flagConsume)); then
ble/util/assert '((wtype0>=0))'

if ((ctx==CTX_FARGI1)); then
# for var in ... の var の部分は変数名をチェックして着色
local rex='^[a-zA-Z_][a-zA-Z_0-9]*$' attr=$ATTR_ERR
if ((i0==wbegin)) && [[ ${text:i0:i-i0} =~ $rex ]]; then
local ret; ble/syntax/highlight/vartype "$BASH_REMATCH"; attr=$ret
fi
((_ble_syntax_attr[i0]=attr))
fi

[[ ${_ble_syntax_bash_command_Expect[wtype0]} ]] &&
((_ble_syntax_attr[i0]=ATTR_ERR))
if ((unexpectedWbegin>=0)); then
Expand Down
16 changes: 16 additions & 0 deletions note.txt
Expand Up @@ -5517,6 +5517,22 @@ bash_tips

2021-12-06

* highlight: for の第一引数のファイル名着色はしない。変数名着色はする [#D1692]

for - の word highlighting で option を除外。というか for の highlighting
を普通のコマンドとして処理しているのはおかしい。

for - in のエラー着色は - だからしていると考えていたがそうではなかった。唯
単に for 文が不完全であることによるエラー着色だった。というか for の第一引
数は変数名に合致しない物を指定した時にはエラー着色になる様にするべきである。

うーん。どうやら for の変数名の部分は here-document の word と同じで、切り
出しは文法的に行われる物の、実際にはコマンド置換もクォート除去も何も解釈さ
れずに直接変数名かどうかの判定対象となる様である。

取り敢えず arr[xxx] 等に対してはエラー着色が出る様にしたが、その他の入れ子
構造 (例えば "for $(echo var)" など) に対してはエラー着色になっていない。

* 2021-09-08 complete: 'fo で補完すると 'for' になってしまうが for はキーワードなので駄目 [#D1691]

元々の compgen では対応しているのに ble.sh が勝手に quote を変更するから
Expand Down

0 comments on commit d056547

Please sign in to comment.