From 92ce433a51a3ed929b7faaef1fcf88e09d88ece0 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sat, 4 Mar 2023 22:15:39 +0900 Subject: [PATCH] syntax: check alias expansions of "coproc" variable names --- docs/ChangeLog.md | 1 + lib/core-syntax.sh | 40 +++++++++++++++++++++++++++++++++------- note.txt | 16 ++++++++++++++++ 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/docs/ChangeLog.md b/docs/ChangeLog.md index 3e6e049c..b7a27e23 100644 --- a/docs/ChangeLog.md +++ b/docs/ChangeLog.md @@ -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 diff --git a/lib/core-syntax.sh b/lib/core-syntax.sh index 6638d448..b8fd6599 100644 --- a/lib/core-syntax.sh +++ b/lib/core-syntax.sh @@ -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 diff --git a/note.txt b/note.txt index 344ac6e2..c271b4f3 100644 --- a/note.txt +++ b/note.txt @@ -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]