-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No distinction between a function declaration and a variable declaration #124
Comments
https://godbolt.org/z/PxfYhdEYn parsing variable and function declarations is highly context dependent. When you see an identifier it could be a variable, function or type, and that affects the parsing of the code. In your example (demonstrated in the godbolt link), if The last example is known as the most vexing parse, where even though n is a value in scope, the So there doesn't seem to be a way to get it right 100%, but perhaps precedence could be changed to make it parse as a variable declaration in cases like this. local function declarations are exceedingly rare anyway |
Okay, I see. So, I suppose there are two solutions?
|
@IndianBoy42 is absolutely right. We could try to solve this by trying to take the context into account in our queries (not guess function declaration within function bodies) or just leave it the way it is. I guess even with locals this will be almost impossible to distinguish |
It is actually undecidable to solve this problem, as described in this blog post. Not only would the parser have to keep track of existing (local and global) variables, but it would also have to perform template instantiation (i. e. arbitrary computation). |
Any updates on this? I would much rather prefer treesitter to parse them as variables, because when I'm using treesitter textobjects, go to next function makes me land on the variable declaration. And also, who would ever write a local function declaration 😭. |
The tree-sitter parser makes no distinction between a function declaration and this constructor call.
It even thinks that
x
orn
in my example are types. So this is probably a tree-sitter-cpp issue.Originally posted by @theHamsta in nvim-treesitter/nvim-treesitter#1625 (comment)
The text was updated successfully, but these errors were encountered: