Skip to content

Commit

Permalink
[ShellScript] Add special treatment of mapfile built-in
Browse files Browse the repository at this point in the history
  • Loading branch information
deathaxe committed Aug 7, 2024
1 parent dd8ed90 commit 497e9d2
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 1 deletion.
37 changes: 36 additions & 1 deletion ShellScript/Bash.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ contexts:
- include: cmd-exec
- include: cmd-export
- include: cmd-historic
- include: cmd-mapfile
- include: cmd-readonly
- include: cmd-sudo
- include: cmd-test
Expand Down Expand Up @@ -537,6 +538,35 @@ contexts:
set: cmd-args-end-of-options-then-function-declarations
- include: cmd-args-end-of-options-then-variables-literal-values

###[ MAPFILE BUILTINS ]########################################################

cmd-mapfile:
# bash only
- match: mapfile{{cmd_break}}
scope:
meta.function-call.identifier.shell
support.function.shell
set:
- cmd-args-meta
- cmd-mapfile-args

cmd-mapfile-args:
- match: ([-+])t*C{{opt_break}}
scope:
meta.parameter.option.shell
variable.parameter.option.shell
captures:
1: punctuation.definition.parameter.shell
push: cmd-args-option-maybe-command
- match: ([-+])[t]*[cdnOsu]{{opt_break}}
scope:
meta.parameter.option.shell
variable.parameter.option.shell
captures:
1: punctuation.definition.parameter.shell
push: cmd-args-option-maybe-value
- include: cmd-args-end-of-options-then-variables-literal-values

###[ READONLY BUILTINS ]#######################################################

cmd-readonly:
Expand Down Expand Up @@ -907,6 +937,11 @@ contexts:
- include: line-continuations
- include: immediately-pop

cmd-args-option-maybe-command:
- include: cmd-args-end
- include: statements-word
- include: else-pop

cmd-args-option-maybe-value:
# NOTE: this context is used in commands-builtin-shell-bash.sublime-syntax
- meta_include_prototype: false
Expand Down Expand Up @@ -2984,7 +3019,7 @@ variables:
# Note: Contains thosw, which are not present as dedicated context
builtin_cmds: |-
(?x: \. | \: | bg | bind | caller | cd | disown | enable | eval | fg
| getopts | hash | help | jobs | kill | logout | mapfile | printf | pwd
| getopts | hash | help | jobs | kill | logout | printf | pwd
| read | readarray | set | shift | shopt | source | su | suspend | times
| type | ulimit | umask | wait ){{cmd_break}}
Expand Down
64 changes: 64 additions & 0 deletions ShellScript/Bash/tests/syntax_test_scope.bash
Original file line number Diff line number Diff line change
Expand Up @@ -10823,6 +10823,70 @@ f() {
# <- meta.function.body.shell meta.block.shell punctuation.section.block.end.shell


###############################################################################
# 4.2 Bash Builtin Commands (mapfile) #
# https://www.gnu.org/software/bash/manual/bash.html#index-mapfile #
# mapfile [-d delim] [-n count] [-O origin] [-s count] #
# [-t] [-u fd] [-C callback] [-c quantum] [array] #
###############################################################################

mapfile
# <- meta.function-call.identifier.shell support.function.shell
#^^^^^^ meta.function-call.identifier.shell support.function.shell
# ^ - meta.function-call

mapfile array
# <- meta.function-call.identifier.shell support.function.shell
#^^^^^^ meta.function-call.identifier.shell support.function.shell
# ^^^^^^ meta.function-call.arguments.shell
# ^^^^^ variable.other.readwrite.shell
# ^ - meta.function-call

mapfile -C ; result=10
# ^^^ meta.function-call.arguments.shell
# ^^^^^^^^^^^^^ - meta.function-call
# ^^ meta.parameter.option.shell variable.parameter.option.shell
# ^ punctuation.terminator.statement.shell
# ^^^^^^ variable.other.readwrite.shell
# ^ keyword.operator.assignment.shell
# ^^ string.unquoted.shell

mapfile -C "~/.bin/app -p $val arg" array
# <- meta.function-call.identifier.shell support.function.shell
#^^^^^^ meta.function-call.identifier.shell support.function.shell
# ^^^^ meta.function-call.arguments.shell - meta.string
# ^^ meta.parameter.option.shell variable.parameter.option.shell
# ^ meta.function-call.arguments.shell meta.string.shell string.quoted.double.shell punctuation.section.string.begin.shell
# ^^^^^^^^^^ meta.function-call.arguments.shell meta.string.shell meta.function-call.identifier.shell
# ^^^^^^^^^^^^ meta.function-call.arguments.shell meta.string.shell meta.function-call.arguments.shell
# ^ meta.function-call.arguments.shell meta.string.shell string.quoted.double.shell punctuation.section.string.end.shell
# ^ meta.function-call.arguments.shell - meta.string - variable
# ^^^^^ meta.function-call.arguments.shell meta.variable.shell variable.other.readwrite.shell
# ^ - meta.function-call

mapfile -d ";" -n 10 -O $origin -s 5 -t -u 20 -C callback -c 10 array
# <- meta.function-call.identifier.shell support.function.shell
#^^^^^^ meta.function-call.identifier.shell support.function.shell
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function-call.arguments.shell
# ^ - meta.function-call
# ^^ meta.parameter.option.shell variable.parameter.option.shell
# ^^^ meta.string.shell string.quoted.double.shell
# ^^ meta.parameter.option.shell variable.parameter.option.shell
# ^^ meta.number.integer.decimal.shell constant.numeric.value.shell
# ^^ meta.parameter.option.shell variable.parameter.option.shell
# ^^^^^^^ meta.string.shell meta.interpolation.parameter.shell variable.other.readwrite.shell
# ^^ meta.parameter.option.shell variable.parameter.option.shell
# ^ meta.number.integer.decimal.shell constant.numeric.value.shell
# ^^ meta.parameter.option.shell variable.parameter.option.shell
# ^^ meta.parameter.option.shell variable.parameter.option.shell
# ^^ meta.number.integer.decimal.shell constant.numeric.value.shell
# ^^ meta.parameter.option.shell variable.parameter.option.shell
# ^^^^^^^^ meta.string.shell variable.function.shell
# ^^ meta.parameter.option.shell variable.parameter.option.shell
# ^^ meta.number.integer.decimal.shell constant.numeric.value.shell
# ^^^^^ meta.variable.shell variable.other.readwrite.shell


###############################################################################
# 4.2 Bash Builtin Commands (readonly) #
# https://www.gnu.org/software/bash/manual/bash.html#index-readonly #
Expand Down
2 changes: 2 additions & 0 deletions ShellScript/Zsh.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ contexts:
- cmd-args-meta
- cmd-args-end-of-options-then-function-references

cmd-mapfile: []

###[ COMMANDS ]################################################################

cmd-compound:
Expand Down
10 changes: 10 additions & 0 deletions ShellScript/Zsh/tests/syntax_test_scope.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -3707,3 +3707,13 @@ echo $var[1] World
# ^^^^^^^^^^^^^^^^^^^^^^^ variable.language.builtin.shell
: $ZLE_RPROMPT_INDENT
# ^^^^^^^^^^^^^^^^^^^ variable.language.builtin.shell
##############################################################################
# 17 Shell Builtin Commands
# https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Commands.html
##############################################################################
mapfile # mapfile is not a known/built-in command in ZSH
# <- meta.function-call.identifier.shell variable.function.shell - support.function
#^^^^^^ meta.function-call.identifier.shell variable.function.shell - support.function

0 comments on commit 497e9d2

Please sign in to comment.