Repository navigation
Node.is_named returning true while it should not #47
|
May be it’s just me messing up something, so needs to be confirmed. With this Lua sample: while true do
break
endFor the This is with version 0.2.0+dev |
Replies: 5 comments 1 reply
|
From your comments in #41 I think you may be confusing node names with field names. Tree sitter has some docs here explaining and I'll try to elaborate on what I said there: As an example, consider this code (no particular language) It could get parsed by tree-sitter into something like Here the field names are
Hope this helps! |
|
Ok, thanks for the enlightenment. I should have open a discussion rather than an issue. I see what you mean, but was confused for multiple reasons. Will try to enumerate some of it. A node always has a type, so some name attached to it. For this reason, saying anonymous was a bit confusing to me and I was unsure. Later, for some reason (the ones explained below) I though it must be something which it may not have, and the field-name was a good candidate to me. I had read the documentation (at least two times, will read it again) and the wording saying an anonymous node is part of the concrete syntax while a named node is part of the abstract syntax, got my attention, because it is important. At first, I used There are reasons to be confused. I’m not saying it's a fault from you. A pity I have not opened a discussion instead of an issue, because I may be not the only once to be confused this way. Will have another look at the parsed trees I looked at as interesting examples. |
The example of My though was as follow. This applies to leaf nodes, because only a leaf node can be said a purely syntactic node. There are leaf nodes such as With some other cases too, not only the Lua grammar, some other grammars to, I had the feeling some tree nodes have to be re-interpreted. The TS documentation itself says the distinction is important for lexer colorization. How I still see confusion, while feel less confused. Thanks again for the notice. |
|
An example of the opposite, which is as much confusing. With Python: a = 1 + 2Gives this tree, where “+” means abstract node ( The operator seems obviously needed as data in the abstract tree, but it is marked as purely syntactic, like the “=” special operator. The “=” can be said purely syntactic, because the information is present in the type of the enclosing node, “assignment”. However, with an abstract node whose type is “binary_operator”, how to know what the operator is, if it is not there as data. There are multiple binary operators, so it is more expected there is a This is another case which can easily introduce confusion, making you wonder the way your own procedure sees the tree is wrong and why. When something is reasonably seen as an issue, it’s not always easy to know where the cause is, especially when there are multiple actors (tree-sitter, grammar provider, client library, binding, end-application, at least). I could show other examples like this, but it would be too annoying. I would just say, take what euclidianAce said, as a starting point, don’t try to fix around |
A good point and something which help to know. The good point, is that using query, at least with my case, is too times faster than navigating the tree (but I have not yet tried to use a cursor, which is supposed to be faster than using node properties). Then there is something worth mentioning to people interested in this topic: take care these query files can produce overlaps. This is at least the case for the one provided for the Lua language (also, not all language grammar author provide query files). Ex. there may be two consecutive equal start-end (1). In this case, may assume the last is the most specific (if the query file was designed this way, which is a guess), but it may also be something like start1-end followed by start2-end where both end are equal but start2 is greater than start1 (2), in this case, which one to choose? Merging style could be an option for the the overlapping part, but not all editor provides this capability. I choose to split in two parts, start1-start2 minus 1 then start2-end. It is not immediately well suited to all editor API or it needs either looking ahead or delaying with check, to apply operations as the editor API expect, like colorizing in increasing positions, which disallow overlaps. It is feasible, but knowing help (I have not seen this mentioned anywhere). (1) An example of when it can occurs, is due to this: it means for "and", "not" and "or" in an expression, there will be first a matching as (2) Still investigating on this, so cannot say which rules are involved (will edit the message later if found). |
From your comments in #41 I think you may be confusing node names with field names. Tree sitter has some docs here explaining and I'll try to elaborate on what I said there:
As an example, consider this code (no particular language)
It could get parsed by tree-sitter into something like
Here the field names are
condition,consequent,alternate,lhs, andrhs. But these are not properties of the nodes themselves. These are just labeled slots of theifandaddnodes. The children have no knowledge of their field names.is_n…