tree-sitter-bash
tree-sitter-bash copied to clipboard
Fails to parse one-line code block with return statement
foo(){ return 0 }
brings you the following syntax-tree
[
CommandNode {
type: command,
...
},
SyntaxNode {
type: ERROR, // <--
...
}
]
Expected output
[
FunctionDefinitionNode {
type: function_definition,
...
}
]
Steps to reproduce
Assuming node and modules are installed.
$ clear; cat <<eol | node
let Parser = require("tree-sitter");
let Bash = require("tree-sitter-bash");
let parser = new Parser();
parser.setLanguage(Bash);
let r = parser.parse("foo(){ return 0 }")
console.log(r.rootNode.children);
r = parser.parse("foo(){\n return 0 \n}")
console.log(r.rootNode.children);
eol
Discovery place
Bash-IDE extension for VS Code, which failed to show me up nested symbols of approximately following code:
foo(){ return 0 }
bar(){
...
foo || { ... ; return 0 } # the code block also fails the outer function 'bar' somehow
}
Workaround
Put a semicolon after the last expression:
... || { ... ; return 0; };
Versions affected
- Node.js: v14.13.0
- tree-sitter: v0.17.1
- tree-sitter-bash: v0.16.1
Issue-Label Bot is automatically applying the label bug to this issue, with a confidence of 0.95. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback!
Links: app homepage, dashboard and code for this bot.
it seems that foo(){ return 0 } is incorrect by design ( https://stackoverflow.com/questions/38595559/how-to-define-a-function-on-one-line )
and when I try to execute it with bash 5.1.4(1), it fails
@melopee It does work well with earlier 5.0.1.17 though. May be this should be an error, not an incorrect tree.
@mklcp Well, it fails in bash 5.2.15 but it doesn't fail in zsh. Sadly, this grammar parser is used for both in many places, which is not correct!
this grammar should not be used for zsh.
@amaanq Yeah, I know, but it's used by VS Code sadly lol.
If it's any consolation, I am writing a zsh grammar currently
@amaanq Nice, but I think it's better to map its native parser to tree-sitter api than writing entire grammar cause this way you will be in sync with original codebase.
You got a repo link to share btw?