tree-sitter-bash
tree-sitter-bash copied to clipboard
Parse error when redirection occurs before text
This is a parse error:
echo >&2 "foobar"
This is not a parse error:
echo "foobar" >&2
Both are equally valid bash syntax.
I am experiencing a similar issue involving a parse error and redirects.
From the bash manpage:
A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator.
The following line is a simple command with no variable assignments and no words, a single redirection, and terminated by a newline. This command creates or truncates the file t. (or, more specifically, opens file t for writing on file descriptor 1.)
>t
This is my result:
(program (ERROR (file_redirect (word))))
I believe the source of our issue lies between lines 270 and 283 of grammar.js:
command: $ => prec.left(seq(
repeat(choice(
$.variable_assignment,
$.file_redirect
)),
$.command_name,
repeat(choice(
$._literal,
seq(
choice('=~', '=='),
choice($._literal, $.regex)
)
))
)),
The above appears to specify that all simple commands must include at least one word, and does not specify anything about redirections that follow or are within a simple command.
Addressing this will likely break backwards-compatibility, but in my opinion, it needs to be done.
Same problem here:
#!/usr/bin/env bash
if [ "${2}" == '' ]; then
echo >&2 "${1}"
else
echo >&2 "${1}" <<<"${2}"
fi
echo "${@}"
Hi @amaanq
Can you please take a look at this? This issue has been reported at least 3 times:
- #72
- #125
- #140 (closed but not fixed)
@amaanq
I ran :TSUpdate and still getting the wrong syntax highlights
Hi @ZiViZiViZ
I rebuilt .../nvim-treesitter/parser/bash.so using the latest commit from master (c0f5797a728fc4ebd78a8b0e436b1494a8ab5f51) and the TS ERRORs were gone.
Try doing the same and if you still have issues, you might have a different problem.
Thanks @amaanq 😄
@amaanq I ran
:TSUpdateand still getting the wrong syntax highlights
or you can just wait until nvim-ts updates the bash revision (happens nightly)