tree-sitter-bash icon indicating copy to clipboard operation
tree-sitter-bash copied to clipboard

Fails to parse one-line code block with return statement

Open hinell opened this issue 5 years ago • 3 comments

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

hinell avatar Oct 14 '20 21:10 hinell

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.

issue-label-bot[bot] avatar Oct 14 '20 21:10 issue-label-bot[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

mklcp avatar Jan 08 '21 21:01 mklcp

@melopee It does work well with earlier 5.0.1.17 though. May be this should be an error, not an incorrect tree.

hinell avatar Feb 09 '21 22:02 hinell

@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!

hinell avatar Aug 22 '23 17:08 hinell

this grammar should not be used for zsh.

amaanq avatar Aug 22 '23 17:08 amaanq

@amaanq Yeah, I know, but it's used by VS Code sadly lol.

hinell avatar Aug 22 '23 18:08 hinell

If it's any consolation, I am writing a zsh grammar currently

amaanq avatar Aug 22 '23 19:08 amaanq

@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?

hinell avatar Aug 23 '23 13:08 hinell