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

`tree-sitter-python` parses file with errors, but tree has no errors

Open eaftan opened this issue 4 years ago • 2 comments

I would expect this not to parse/parse with errors:

def foo():
foo()

But AFAICT there are no ERROR nodes in the tree, nor are there any nodes for which hasError() returns true. It looks like this was intentionally added in #65 ("As is often the case, I think the most practical fix is to allow a superset of what Python really allows, and treat empty blocks as valid blocks.")

I have a higher-level question: if I want to know whether a file parses correctly, can I do that with tree-sitter? Or is that not what tree-sitter is for?

eaftan avatar Jun 07 '21 16:06 eaftan

You can check if a block is empty by looking at its children in the syntax tree. So if you wanted to add some special handling for empty blocks (like an error message that says "Blocks must contain one or more statement"), you could do that by checking if a block contains any statements.

maxbrunsfeld avatar Jun 07 '21 16:06 maxbrunsfeld

Thanks @maxbrunsfeld. Is it generally true that tree-sitter accepts a superset of what the programming language grammar allows? I'm trying to understand what to expect from tree-sitter, rather than trying to deal with this specific issue.

eaftan avatar Jun 07 '21 22:06 eaftan