Skip to content

Commit 5d8a332

Browse files
fix: remove double parenthesis as valid opening to arithmetic expasion (#311)
* chore: checkpoint with formatted tests No changes for now, we only re-formatted tests to make it easier to diff them when we actually do changes. * feat: avoid spurious parsing of arithmetic expansion * test: add test for parenthesis in quote * chore: update known failures with master Update the `script/known-failures.txt` document with the latest grammar in `master`, to make it easier to check what our changes are impacting. * refactor: move (( expression )) from test_command to compound_statement With the introduced changes, some of the examples were failing with a specific pattern, namely the files: - `examples/wild-corpus/esoteric/lishp/evaluator.sh` - `examples/wild-corpus/esoteric/lishp/test.sh` - `examples/wild-corpus/esoteric/lishp/variables.map.sh` - `examples/wild-corpus/esoteric/lishp/variables.sh` When taking a closer look it was noted that the following program was failing to parse: ``` (( size=1, max_index=10 ) ``` After taking a closer look at bash's manual, it seems this should be possible and it should be considered a compound command instead. As such, this commit moves this pattern from `test_command` to `compound_statement`, updating it so that we can have multiple expressions separated by a comma. Note that these changes will very very very likely break the existing tests, as we recently updated some to expect `test_command`. However, for the time being, I'm keeping those failures since I'd like to have someone else take another look at the changes I'm introducing with this commit to see if they make sense. * refactor: revert changes to subscript This commit reverts the changes that had been done to `subscript` in order to fix failing examples for the following files: - `examples/gentoo/dev-lang/mlton/mlton-20180207.ebuild` - `examples/gentoo/eclass/cuda.eclass` - `examples/wild-corpus/cloud/kubernetes/cluster/lib/logging.sh` - `examples/wild-corpus/cloud/kubernetes/hack/update-godep-licenses.sh` - `examples/wild-corpus/git/t/t9902-completion.sh` - `examples/wild-corpus/shell/bashdb/test/unit/test-columns.sh` - `examples/wild-corpus/shell/bashdb/lib/frame.sh` - `examples/gentoo/dev-qt/qt-docs/qt-docs-6.10.0_p202510021201.ebuild` - `examples/gentoo/dev-qt/qt-docs/qt-docs-6.9.3_p202509261208.ebuild` - `examples/gentoo/eclass/linux-mod-r1.eclass` Namely, with the introduced changes, the following program could not be parsed: ``` echo "${array[${index}+1]}" ``` Even though I've confirmed this is valid bash syntax and, if the variables were set, this would actually be evaluated to the value in that specific index of the array. Once again, not touching tests right now as I want someone to double-check that my changes make sense. * test: commit updated good tests Add the updated tests for both `test/corpus/literals.txt` and `test/corpus/statements.txt`, while leaving the failure in `test/corpus/commands.txt`, as the two first simply updated the `test_command` node to `compound_statement`, which is expected. The latter broke the binary expression in a parenthesized expression in a subscript, which is simply returning word instead of actually parsing the binary or unary expression, still need to figure that out. * refactor: replace parenthesized_expression for compound_statement Replace `parenthesized_expression` for `compound_statement` in `subscript`, as that fixes the issue with patterns like `${array[(($number+1))]}` where `(($number+1))` was being parsed as a `(parenthesized_expression(word))`. This removes even more known failures, while introducing a new single on, which needs to be looked into. * fix: add subshell to possible options in subscript Not sure if we should add `subshell` but, since the last commit removed `parenthesized_expression`, we now need another one to allow `( ... )` in subscript. * refactor: remove unnecessary optional Co-authored-by: Max Brunsfeld <[email protected]> * ci: disable node tests Co-authored-by: Max Brunsfeld <[email protected]> --------- Co-authored-by: Max Brunsfeld <[email protected]>
1 parent 20f55fb commit 5d8a332

File tree

10 files changed

+196799
-205052
lines changed

10 files changed

+196799
-205052
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
with:
4949
generate: false
5050
test-rust: true
51-
test-node: true
51+
test-node: false
5252
test-python: true
5353
test-go: true
5454
test-swift: false

grammar.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,22 @@ module.exports = grammar({
386386
field('redirect', optional($._redirect)),
387387
)),
388388

389-
compound_statement: $ => seq(
390-
'{',
391-
optional($._terminated_statement),
392-
token(prec(-1, '}')),
389+
compound_statement: $ => choice(
390+
seq(
391+
'{',
392+
optional($._terminated_statement),
393+
token(prec(-1, '}')),
394+
),
395+
seq(
396+
'((',
397+
repeat(
398+
seq(
399+
$._arithmetic_expression,
400+
',',
401+
),
402+
),
403+
$._arithmetic_expression,
404+
'))'),
393405
),
394406

395407
subshell: $ => seq(
@@ -435,7 +447,6 @@ module.exports = grammar({
435447
),
436448
']]',
437449
),
438-
seq('((', optional($._expression), '))'),
439450
),
440451
),
441452

@@ -508,7 +519,7 @@ module.exports = grammar({
508519
subscript: $ => seq(
509520
field('name', $.variable_name),
510521
'[',
511-
field('index', choice($._literal, $.binary_expression, $.unary_expression, $.parenthesized_expression)),
522+
field('index', choice($._literal, $.binary_expression, $.unary_expression, $.compound_statement, $.subshell)),
512523
optional($._concat),
513524
']',
514525
optional($._concat),
@@ -701,7 +712,7 @@ module.exports = grammar({
701712
),
702713

703714
arithmetic_expansion: $ => choice(
704-
seq(choice('$((', '(('), commaSep1($._arithmetic_expression), '))'),
715+
seq('$((', commaSep1($._arithmetic_expression), '))'),
705716
seq('$[', $._arithmetic_expression, ']'),
706717
),
707718

@@ -731,6 +742,7 @@ module.exports = grammar({
731742
$._simple_variable_name,
732743
$.variable_name,
733744
$.string,
745+
$.raw_string,
734746
)),
735747

736748
_arithmetic_binary_expression: $ => {
@@ -1108,7 +1120,7 @@ module.exports = grammar({
11081120
$.variable_name,
11091121
),
11101122

1111-
_special_variable_name: $ => alias(choice('*', '@', '?', '!', '#', '-', '$', '0', '_'), $.special_variable_name),
1123+
_special_variable_name: $ => alias(choice('*', '@', '?', '!', '#', '-', '$', '_'), $.special_variable_name),
11121124

11131125
word: _ => token(seq(
11141126
choice(

script/known-failures.txt

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
examples/bash-it/completion/available/aliases.completion.bash
21
examples/bash-it/completion/available/bundler.completion.bash
32
examples/bash-it/completion/available/sqlmap.completion.bash
43
examples/bash-it/plugins/available/colors.plugin.bash
54
examples/bash-it/plugins/available/history-eternal.plugin.bash
65
examples/bash-it/themes/hawaii50/hawaii50.theme.bash
76
examples/bash/examples/functions/notify.bash
87
examples/bash/examples/shellmath/shellmath.sh
8+
examples/bash/tests/arith-for.tests
99
examples/bash/tests/arith.tests
1010
examples/bash/tests/array.tests
1111
examples/bash/tests/assoc.tests
12+
examples/bash/tests/braces.tests
1213
examples/bash/tests/case.tests
14+
examples/bash/tests/casemod.tests
1315
examples/bash/tests/comsub-posix.tests
16+
examples/bash/tests/comsub.tests
17+
examples/bash/tests/comsub2.tests
1418
examples/bash/tests/cond.tests
1519
examples/bash/tests/errors.tests
1620
examples/bash/tests/extglob.tests
@@ -20,6 +24,7 @@ examples/bash/tests/histexp.tests
2024
examples/bash/tests/misc/dev-tcp.tests
2125
examples/bash/tests/more-exp.tests
2226
examples/bash/tests/new-exp.tests
27+
examples/bash/tests/parser.tests
2328
examples/bash/tests/posixexp.tests
2429
examples/bash/tests/posixexp2.tests
2530
examples/bash/tests/printf.tests
@@ -29,23 +34,29 @@ examples/bash/tests/redir.tests
2934
examples/bash/tests/test.tests
3035
examples/gentoo/app-misc/editor-wrapper/files/editor-wrapper-4.sh
3136
examples/gentoo/dev-lang/rust/rust-1.74.1-r101.ebuild
37+
examples/gentoo/dev-libs/libffi/libffi-3.5.1.ebuild
38+
examples/gentoo/dev-libs/libffi/libffi-3.5.2.ebuild
39+
examples/gentoo/dev-libs/libffi/libffi-9999.ebuild
3240
examples/gentoo/eclass/rust.eclass
3341
examples/gentoo/eclass/tests/toolchain-funcs.sh
3442
examples/gentoo/eclass/texlive-common.eclass
3543
examples/gentoo/eclass/toolchain-funcs.eclass
3644
examples/gentoo/mail-client/thunderbird-bin/files/thunderbird-bin-r1.sh
3745
examples/gentoo/mail-client/thunderbird/files/thunderbird-r1.sh
38-
examples/gentoo/net-misc/asterisk/asterisk-18.26.1-r1.ebuild
39-
examples/gentoo/net-misc/asterisk/asterisk-20.12.0.ebuild
40-
examples/gentoo/net-misc/asterisk/asterisk-20.13.0.ebuild
41-
examples/gentoo/net-misc/asterisk/asterisk-21.7.0.ebuild
42-
examples/gentoo/net-misc/asterisk/asterisk-21.8.0.ebuild
43-
examples/gentoo/net-misc/asterisk/asterisk-22.2.0.ebuild
44-
examples/gentoo/net-misc/asterisk/asterisk-22.3.0.ebuild
46+
examples/gentoo/net-misc/asterisk/asterisk-18.26.2.ebuild
47+
examples/gentoo/net-misc/asterisk/asterisk-18.26.4.ebuild
48+
examples/gentoo/net-misc/asterisk/asterisk-20.15.2.ebuild
49+
examples/gentoo/net-misc/asterisk/asterisk-21.10.2.ebuild
50+
examples/gentoo/net-misc/asterisk/asterisk-22.5.2.ebuild
4551
examples/gentoo/sys-apps/dcfldd/dcfldd-1.9.2-r1.ebuild
4652
examples/gentoo/sys-apps/dcfldd/dcfldd-1.9.3.ebuild
4753
examples/gentoo/sys-apps/less/files/lesspipe-r3.sh
4854
examples/gentoo/sys-apps/less/files/lesspipe-r4.sh
55+
examples/gentoo/sys-apps/locale-gen/locale-gen-3.8.ebuild
56+
examples/gentoo/sys-apps/locale-gen/locale-gen-3.9-r1.ebuild
57+
examples/gentoo/sys-apps/locale-gen/locale-gen-9999.ebuild
58+
examples/gentoo/sys-fs/bcachefs-tools/bcachefs-tools-1.25.3.ebuild
59+
examples/gentoo/sys-fs/bcachefs-tools/bcachefs-tools-1.31.5.ebuild
4960
examples/gentoo/www-client/firefox-bin/files/firefox-bin-r1.sh
5061
examples/gentoo/www-client/firefox/files/firefox-r1.sh
5162
examples/wild-corpus/cloud/chef-bcpc/legacy_scripts/make_databag.sh
@@ -79,8 +90,6 @@ examples/wild-corpus/distro/woof-CE/woof-code/rootfs-skeleton/usr/local/petget/i
7990
examples/wild-corpus/distro/woof-CE/woof-code/rootfs-skeleton/usr/local/petget/pkg_chooser.sh
8091
examples/wild-corpus/esoteric/CmdlineGL/share/examples/FlightSim.sh
8192
examples/wild-corpus/esoteric/CmdlineGL/share/examples/Robot.sh
82-
examples/wild-corpus/esoteric/lishp/callable.lambda.sh
83-
examples/wild-corpus/esoteric/lishp/parser.sh
8493
examples/wild-corpus/esoteric/make-a-lisp-bash/core.sh
8594
examples/wild-corpus/esoteric/shasm/shasm.sh
8695
examples/wild-corpus/exp/shootout/timing.sh
@@ -97,8 +106,6 @@ examples/wild-corpus/freebsd-11.1/crypto/openssl/util/bat.sh
97106
examples/wild-corpus/freebsd-11.1/gnu/usr.bin/binutils/ld/genscripts.sh
98107
examples/wild-corpus/freebsd-11.1/share/examples/drivers/make_device_driver.sh
99108
examples/wild-corpus/freebsd-11.1/share/examples/drivers/make_pseudo_driver.sh
100-
examples/wild-corpus/freebsd-11.1/sys/dev/bhnd/tools/bus_macro.sh
101-
examples/wild-corpus/freebsd-11.1/sys/tools/bus_macro.sh
102109
examples/wild-corpus/freebsd-11.1/tools/tools/mctest/mctest_run.sh
103110
examples/wild-corpus/freebsd-11.1/tools/tools/nanobsd/fill_pkg.sh
104111
examples/wild-corpus/freebsd-11.1/tools/tools/shlib-compat/test/regress.sh
@@ -276,17 +283,17 @@ examples/wild-corpus/oil/test/smoke.sh
276283
examples/wild-corpus/sdk/tools/clang/scripts/update.sh
277284
examples/wild-corpus/shell/ast/src/cmd/3d/3d.sh
278285
examples/wild-corpus/shell/ast/src/cmd/3d/features/syscall.sh
279-
examples/wild-corpus/shell/ast/src/cmd/html/mm2bb.sh
280-
examples/wild-corpus/shell/ast/src/cmd/html/mm2html.sh
281-
examples/wild-corpus/shell/ast/src/cmd/html/mm2twiki.sh
282-
examples/wild-corpus/shell/ast/src/cmd/ie/ie.sh
283286
examples/wild-corpus/shell/ast/src/cmd/INIT/ditto.sh
284287
examples/wild-corpus/shell/ast/src/cmd/INIT/hurl.sh
285288
examples/wild-corpus/shell/ast/src/cmd/INIT/iffe.sh
286289
examples/wild-corpus/shell/ast/src/cmd/INIT/mktest.sh
287290
examples/wild-corpus/shell/ast/src/cmd/INIT/package.sh
288291
examples/wild-corpus/shell/ast/src/cmd/INIT/regress.sh
289292
examples/wild-corpus/shell/ast/src/cmd/INIT/rt.sh
293+
examples/wild-corpus/shell/ast/src/cmd/html/mm2bb.sh
294+
examples/wild-corpus/shell/ast/src/cmd/html/mm2html.sh
295+
examples/wild-corpus/shell/ast/src/cmd/html/mm2twiki.sh
296+
examples/wild-corpus/shell/ast/src/cmd/ie/ie.sh
290297
examples/wild-corpus/shell/ast/src/cmd/ksh93/data/bash_pre_rc.sh
291298
examples/wild-corpus/shell/ast/src/cmd/ksh93/tests/append.sh
292299
examples/wild-corpus/shell/ast/src/cmd/ksh93/tests/arith.sh
@@ -353,5 +360,4 @@ examples/wild-corpus/shell/mksh/Build.sh
353360
examples/wild-corpus/shell/mksh/test.sh
354361
examples/wild-corpus/shell/modernish/install.sh
355362
examples/wild-corpus/shell/mwc-sh/shql.sh
356-
examples/wild-corpus/shell/posixcube/posixcube.sh
357363
examples/wild-corpus/shell/posixcube/test.sh

src/grammar.json

Lines changed: 65 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)