Skip to content

Commit 3999862

Browse files
authored
[ShellScript] Add support for arithmethic expansions in square brackets (#4409)
This commit moves implementation for arithmetic expansions in square brackets from Zsh to Bash as depsite not being document, it is supported by current versions of Bash and actively used in Linux kernel repository.
1 parent bbf1a81 commit 3999862

File tree

3 files changed

+105
-23
lines changed

3 files changed

+105
-23
lines changed

ShellScript/Bash.sublime-syntax

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2816,6 +2816,11 @@ contexts:
28162816
branch:
28172817
- arithmetic-expansion
28182818
- command-expansion
2819+
- match: (\$)(\[)
2820+
captures:
2821+
1: punctuation.definition.variable.shell.shell
2822+
2: punctuation.section.interpolation.begin.shell.shell
2823+
push: arithmetic-expansion-bracket-body
28192824

28202825
arithmetic-expansion:
28212826
- match: (\$)(\(\()
@@ -2834,6 +2839,14 @@ contexts:
28342839
- include: expression-content
28352840
- include: expression-illegals
28362841

2842+
arithmetic-expansion-bracket-body:
2843+
- meta_scope: meta.interpolation.arithmetic.shell.shell
2844+
- match: \]
2845+
scope: punctuation.section.interpolation.end.shell.shell
2846+
pop: 1
2847+
- include: expression-content
2848+
- include: expression-illegals
2849+
28372850
###[ BRACE EXPANSIONS ]########################################################
28382851

28392852
brace-interpolations:
@@ -3405,7 +3418,7 @@ variables:
34053418
case_clause_end: ;;&?|;&
34063419

34073420
# Parameter expansions
3408-
is_interpolation: (?=\$[({{{identifier_char}}{{special_variables}}]|`)
3421+
is_interpolation: (?=\$[({\[{{identifier_char}}{{special_variables}}]|`)
34093422
parameter_switch: '[AEKLPQUaku]'
34103423

34113424
# Filename expansions

ShellScript/Bash/tests/syntax_test_scope.bash

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ $((
117117
# ^^^^^^^^^^ - comment
118118
))
119119

120+
$[ # comment ]
121+
#^^^^^^^^^^^^^ - comment
122+
120123
cmd \
121124
# comment after line continuation
122125
# <- comment.line.number-sign.shell punctuation.definition.comment.shell
@@ -4153,6 +4156,13 @@ x="$(( foo++ ))"
41534156
# ^^ keyword
41544157
# ^^ punctuation.section.interpolation.end.shell
41554158

4159+
x="$[ foo++ ]"
4160+
#^ keyword.operator.assignment.shell
4161+
# ^ punctuation.definition.variable.shell
4162+
# ^ punctuation.section.interpolation.begin.shell
4163+
# ^^ keyword
4164+
# ^ punctuation.section.interpolation.end.shell
4165+
41564166
# These are all legal identifiers for variables.
41574167
alias=hello
41584168
# <- - storage - keyword
@@ -9164,11 +9174,31 @@ stash) || true)
91649174
# https://www.gnu.org/software/bash/manual/bash.html#Arithmetic-Expansion #
91659175
###############################################################################
91669176

9177+
$(())
9178+
# <- meta.function-call.identifier.shell meta.command.shell meta.interpolation.arithmetic.shell punctuation.definition.variable.shell
9179+
#^^^^ meta.function-call.identifier.shell meta.command.shell meta.interpolation.arithmetic.shell
9180+
#^^ punctuation.section.interpolation.begin.shell
9181+
# ^^ punctuation.section.interpolation.end.shell
9182+
9183+
$(( var + 1 ))
9184+
# <- meta.function-call.identifier.shell meta.command.shell meta.interpolation.arithmetic.shell punctuation.definition.variable.shell
9185+
#^^^^^^^^^^^^^ meta.function-call.identifier.shell meta.command.shell meta.interpolation.arithmetic.shell
9186+
#^^ punctuation.section.interpolation.begin.shell
9187+
# ^^^ variable.other.readwrite.shell
9188+
# ^ keyword.operator.arithmetic.shell
9189+
# ^ meta.number.integer.decimal.shell constant.numeric.value.shell
9190+
# ^^ punctuation.section.interpolation.end.shell
9191+
91679192
: $(())
9193+
#^^^^^^ meta.function-call.arguments.shell
91689194
# ^^^^^ meta.string.glob.shell meta.interpolation.arithmetic.shell
9195+
# ^ punctuation.definition.variable.shell
9196+
# ^^ punctuation.section.interpolation.begin.shell
9197+
# ^^ punctuation.section.interpolation.end.shell
91699198

91709199
: $(( ))
9171-
# ^^^^^^^ meta.interpolation.arithmetic.shell
9200+
#^^^^^^^^ meta.function-call.arguments.shell
9201+
# ^^^^^^^ meta.string.glob.shell meta.interpolation.arithmetic.shell
91729202
# ^ punctuation.definition.variable.shell
91739203
# ^^ punctuation.section.interpolation.begin.shell
91749204
# ^^ punctuation.section.interpolation.end.shell
@@ -9205,6 +9235,66 @@ stash) || true)
92059235
# ^ punctuation.section.interpolation.end.shell
92069236
# ^^ punctuation.section.interpolation.end.shell
92079237

9238+
$[]
9239+
# <- meta.function-call.identifier.shell meta.command.shell meta.interpolation.arithmetic.shell.shell punctuation.definition.variable.shell.shell
9240+
#^^ meta.function-call.identifier.shell meta.command.shell meta.interpolation.arithmetic.shell.shell
9241+
#^ punctuation.section.interpolation.begin.shell.shell
9242+
# ^ punctuation.section.interpolation.end.shell.shell
9243+
9244+
$[var + 1]
9245+
# <- meta.function-call.identifier.shell meta.command.shell meta.interpolation.arithmetic.shell.shell punctuation.definition.variable.shell.shell
9246+
#^^^^^^^^^ meta.function-call.identifier.shell meta.command.shell meta.interpolation.arithmetic.shell.shell
9247+
#^ punctuation.section.interpolation.begin.shell.shell
9248+
# ^^^ variable.other.readwrite.shell
9249+
# ^ keyword.operator.arithmetic.shell
9250+
# ^ meta.number.integer.decimal.shell constant.numeric.value.shell
9251+
# ^ punctuation.section.interpolation.end.shell.shell
9252+
9253+
: $[]
9254+
#^^^^ meta.function-call.arguments.shell
9255+
# ^^^ meta.string.glob.shell meta.interpolation.arithmetic.shell.shell
9256+
# ^ punctuation.definition.variable.shell.shell
9257+
# ^ punctuation.section.interpolation.begin.shell.shell
9258+
# ^ punctuation.section.interpolation.end.shell.shell
9259+
9260+
: $[ ]
9261+
# ^^^^^ meta.string.glob.shell meta.interpolation.arithmetic.shell.shell
9262+
# ^ punctuation.definition.variable.shell.shell
9263+
# ^ punctuation.section.interpolation.begin.shell.shell
9264+
# ^ punctuation.section.interpolation.end.shell.shell
9265+
9266+
: $[ `date +%Y`[2] ]
9267+
# ^^^^^^^^^^^^^^^^^^ meta.string.glob.shell meta.interpolation.arithmetic.shell
9268+
# ^ punctuation.definition.variable.shell
9269+
# ^ punctuation.section.interpolation.begin.shell
9270+
# ^^^^^^^^^^ meta.interpolation.command.shell
9271+
# ^ punctuation.section.interpolation.begin.shell
9272+
# ^ punctuation.section.interpolation.end.shell
9273+
# ^^^ meta.item-access.shell
9274+
# ^ punctuation.section.interpolation.end.shell
9275+
9276+
: $[ ${var} + 5 * ($var-1) ]
9277+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.string.glob.shell meta.interpolation.arithmetic.shell.shell
9278+
# ^ punctuation.definition.variable.shell.shell
9279+
# ^ punctuation.section.interpolation.begin.shell.shell
9280+
# ^^^^^^ meta.interpolation.parameter.shell
9281+
# ^ punctuation.definition.variable.shell
9282+
# ^ punctuation.section.interpolation.begin.shell
9283+
# ^^^ variable.other.readwrite.shell
9284+
# ^ punctuation.section.interpolation.end.shell
9285+
# ^ keyword.operator.arithmetic.shell
9286+
# ^ meta.number.integer.decimal.shell constant.numeric.value.shell
9287+
# ^ keyword.operator.arithmetic.shell
9288+
# ^^^^^^^^ meta.group.shell
9289+
# ^ punctuation.section.group.begin.shell
9290+
# ^^^^ meta.interpolation.parameter.shell variable.other.readwrite.shell
9291+
# ^ punctuation.definition.variable.shell
9292+
# ^ keyword.operator.arithmetic.shell
9293+
# ^ meta.number.integer.decimal.shell constant.numeric.value.shell
9294+
# ^ punctuation.section.group.end.shell
9295+
# ^ punctuation.section.interpolation.end.shell.shell
9296+
9297+
92089298
###############################################################################
92099299
# 3.5.8.1 Pattern Matching #
92109300
# https://www.gnu.org/software/bash/manual/bash.html#Pattern-Matching #

ShellScript/Zsh.sublime-syntax

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -670,24 +670,6 @@ contexts:
670670
- include: eregexp-group-content
671671
- include: word-end
672672

673-
###[ ARITHMETIC EXPANSIONS ]###################################################
674-
675-
arithmetic-expansions:
676-
- meta_prepend: true
677-
- match: (\$)(\[)
678-
captures:
679-
1: punctuation.definition.variable.shell.zsh
680-
2: punctuation.section.interpolation.begin.shell.zsh
681-
push: zsh-arithmetic-expansion-body
682-
683-
zsh-arithmetic-expansion-body:
684-
- meta_scope: meta.interpolation.arithmetic.shell.zsh
685-
- match: \]
686-
scope: punctuation.section.interpolation.end.shell.zsh
687-
pop: 1
688-
- include: expression-content
689-
- include: expression-illegals
690-
691673
###[ BRACE EXPANSIONS ]########################################################
692674

693675
brace-interpolations:
@@ -1756,9 +1738,6 @@ variables:
17561738

17571739
case_clause_end: ;[;&|]
17581740

1759-
# Parameter expansions
1760-
is_interpolation: (?=\$[({\[{{identifier_char}}{{special_variables}}]|`)
1761-
17621741
# A character that, when unquoted, separates words.
17631742
# Bash ones + `{` and `}`
17641743
metachar: '[\s|&;(){}<>]'

0 commit comments

Comments
 (0)