Repository navigation
Add a system for assigning properties to syntax nodes - #204
Merged
Merged
Conversation
maxbrunsfeld
force-pushed
the
property-sheets
branch
from
October 4, 2018 01:13
e771bb4 to
e654039
Compare
maxbrunsfeld
force-pushed
the
property-sheets
branch
from
October 4, 2018 16:21
47f3c17 to
9c6f5c9
Compare
maxbrunsfeld
force-pushed
the
property-sheets
branch
from
October 14, 2018 21:54
b154d5b to
14e45a9
Compare
maxbrunsfeld
force-pushed
the
property-sheets
branch
from
October 15, 2018 16:42
14e45a9 to
2b6857b
Compare
Contributor
Author
|
There currently aren't any tests for this in the core library. It's easier to test at a higher level, because the CSS parsing is done in |
This was referenced Oct 16, 2018
Closed
Closed
11 of 12 tasks
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.
Motivation
There are several use cases where it'd be nice to be able to specify declaratively that certain nodes have certain properties. For example, I'd like to build a cross-language tool like
ctagsthat can be used to link definitions to their corresponding usages, so I'd like to indicate in a generic way:However, I'd like to avoid providing this information directly in the grammar. I'd like these properties to be an optional feature that you could layer on top of an existing syntax tree.
One analogous technology that we use is CSS, which provides a declarative language for indicating that certain nodes in a tree have certain properties.
Solution
This PR introduces the concept of a Property Sheet. A property sheet is like a stylesheet, but instead of specifying styling, you specify arbitrary properties that should be associated with particular syntax nodes.
Specifically, I've added one new public function to the compiler library (the library used by
tree-sitter-cli). The function is calledts_compile_property_sheet. Together with some functionality that is going to live intree-sitter-cli, this function will allow you to use the CSS language to write a property sheet like this:Then, when you run
tree-sitter generate, Tree-sitter will compile this stylesheet into an optimal state machine for matching these selectors against syntax nodes. This state machine has a simple JSON format that can be easily used from any language.The State Machine
The state machine is designed to allow a given node's properties to be computed with negligible overhead by descending to the node from the root of the tree. And by maintaining a stack of states, you can walk the tree and compute each node's properties along the way. Representing the state machine as a simple JSON object ensures that bindings like
node-tree-sitter,rust-tree-sitter, andhaskell-tree-sittercan all easily use it and abstract over it.