Skip to content

Commit

Permalink
syntax: check alias expansions of "coproc" variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Mar 4, 2023
1 parent 36efbb7 commit 92ce433
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -130,6 +130,7 @@
- syntax: support context after `((...))` and `[[ ... ]]` in bash-5.2 `#D1962` 67cb967
- edit: support the readline variable `search-ignore-case` of bash-5.3 `#D1976` e3ad110
- menu-complete: add `insert_unique` option to the `complete` widget `#D1995` xxxxxxx
- syntax: check alias expansions of `coproc` variable names `#D1996` xxxxxxx

## Changes

Expand Down
40 changes: 33 additions & 7 deletions lib/core-syntax.sh
Expand Up @@ -3452,14 +3452,40 @@ function ble/syntax:bash/ctx-coproc/check-word-end {
local word=${text:wbegin:wlen}
local wt=$wtype

if local rex='^[_a-zA-Z0-9]+$'; [[ $word =~ $rex ]]; then
if local rex='^[_a-zA-Z][_a-zA-Z]*$'; [[ $word =~ $rex ]]; then
if ble/syntax:bash/ctx-coproc/.is-next-compound; then
# Note: [_a-zA-Z0-9]+ は一回の読み取りの筈なので、
# 此処で遡って代入しても問題ない筈。
((_ble_syntax_attr[wbegin]=ATTR_VAR))
((ctx=CTX_CMDXC,type=CTX_ARGVI))
ble/syntax/parse/word-pop
return 0
# 構文: 変数名 複合コマンド
local attr=$ATTR_VAR

# alias だった場合は解釈が変わり得る。alias が厳密に変数名に展開された時
# にのみ変数名と判定する。複合コマンド開始に展開された場合は通常処理にフォー
# ルバック。それ以外はエラー。
if ble/alias#active "$word"; then
attr=
local ret; ble/alias#expand "$word"
case $word in
# 通常処理にフォールバックする
('if'|'while'|'until'|'for'|'select'|'case'|'{'|'[[') ;;
# 通常処理(構文エラー)
('fi'|'done'|'esac'|'then'|'elif'|'else'|'do'|'}'|'!'|'coproc'|'function'|'in') ;;
(*)
if ble/string#match "$word" '^[_a-zA-Z][_a-zA-Z0-9]*$'; then
# 変数名に展開される場合はOK
attr=$ATTR_CMD_ALIAS
else
attr=$ATTR_ERR
fi
esac
fi

if [[ $attr ]]; then
# Note: [_a-zA-Z0-9]+ は一回の読み取りの筈なので、
# 此処で遡って代入しても問題ない筈。
_ble_syntax_attr[wbegin]=$attr
((ctx=CTX_CMDXC,wtype=CTX_ARGVI))
ble/syntax/parse/word-pop
return 0
fi
fi
fi

Expand Down
16 changes: 16 additions & 0 deletions note.txt
Expand Up @@ -6678,6 +6678,22 @@ bash_tips
Done (実装ログ)
-------------------------------------------------------------------------------

2023-03-04

* syntax: coproc 変数名が alias に一致する時、着色を変更する [#D1996]

うーん。実はもっと詳しく調べてエラー着色にするべきなのではないか? →多少
alias の中身もチェックする事にした。alias の中身が複数の単語に展開される場
合にはエラー着色になる様にした。

展開前の時点でキーワードに一致している場合にはどうするのか。内部での解釈と
は独立に構文解析が行われる事になるので、内部でキーワードに展開されたからキー
ワード着色にするなどの工夫をした所で外側の構文解析が壊れてしまう。

→alias が展開されてキーワードになる場合には、当初はその場で簡単な物だけ処
理する様にしてみたが、結局通常のコマンド処理に fallback する様にした。元々
変数名に一致しない場合にはその様に処理していたので問題ない。

2023-03-03

* menu-complete: enter_menu であっても一意確定なら補完完了する機能 [#D1995]
Expand Down

0 comments on commit 92ce433

Please sign in to comment.