Repository navigation
Add an API for associating field names with child nodes - #271
Merged
Merged
Conversation
maxbrunsfeld
force-pushed
the
node-fields
branch
from
February 8, 2019 22:28
850350c to
9fbb55d
Compare
Co-Authored-By: Ayman Nadeem <[email protected]>
maxbrunsfeld
force-pushed
the
node-fields
branch
from
February 8, 2019 23:47
9fbb55d to
51a9f14
Compare
Fields aren't stored in sequences now, so the max length is back to being just for aliases.
This allows you to assert about fields in tests. But if your test s-expression does *not* include fields, the fields will be stripped from the regexp before comparison.
Closed
1 of 6 tasks
|
This one would be really nice to get in - would be very useful for several stages after parsing in an IDE pipeline - nice stuff! |
|
Wait, so I won't have to iterate everything just to skip possible comments now? Also would this provide / have integer values for the fields, like how you can use |
Contributor
Author
|
Yeah the fields have numerical ids that you can use; you don’t have to use the field name strings. |
Even though normal aliases don't cause this, simple (single-use) aliases still do cause it.
maxbrunsfeld
force-pushed
the
master
branch
4 times, most recently
from
March 21, 2019 00:02
10d1ff6 to
3340168
Compare
maxbrunsfeld
force-pushed
the
node-fields
branch
from
March 27, 2019 23:32
52346b7 to
eb96dd6
Compare
This was referenced Mar 29, 2019
This was referenced Jul 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Previously, syntax nodes' children were structured as a simple ordered list. This is useful for some kinds of processing (e.g. syntax highlighting), but is different than the way syntax trees are commonly structured, in which there is a label associated with each child node.
Solution
Grammar API
This PR adds a new function called
fieldto the grammar API. You can use it like this:Node API
Then, when parsing, you can retrieve nodes' children based on their field name:
Tree Cursor API
You can also retrieve the field name associated with the current node when walking a tree using a tree cursor:
Testing API
The
tree-sitter testcommand now allows you to optionally assert about nodes' fields using a notation like this:But if you don't want to change your existing tests, you can also omit the field names from the test file. In that case, the fields will be ignored for the purpose of the assertion.
Command Line Output
The field names are also shown when you run the
tree-sitter parsecommand:Static Node Types
The
tree-sitter generatecommand now generates a JSON file calledsrc/node-types.jsonwhich provides static information about nodes' fields based on the grammar. Here is an example file:By reading this JSON, applications can use meta-programming to generate a specific data types for each kind of node. The
required,multiple, andtypesproperties in the JSON can be used to determine the appropriate data type for each field.Property Sheet API
The new Property Sheet feature can also make use of these named fields. You can select nodes based on their field name using CSS class-selector syntax (dot notation).
Example:
For any
functionnode, this will associate the propertycolor: bluewith the node'snamechild, and associate the propertycolor: redto anyidentifiernode within theparameterschild.Follow-on Work
We'll need to update Tree-sitter's language binding libraries like
node-tree-sitterandhaskell-tree-sitterto take advantage of this new feature.In the JavaScript case, I believe that we can use the new
node-interface.jsonfile and the newts_node_child_by_field_nameC API to automatically generate a set ofNodesubclasses with accessor methods for their children.Something like this: