Repository navigation
Tree-queries - add optional node syntax, improve handling of repeated nodes - #612
Merged
Merged
Conversation
maxbrunsfeld
force-pushed
the
optionals-in-queries
branch
from
May 7, 2020 19:41
46c8100 to
1011be7
Compare
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.
Summary
This PR adds a new postfix
?operator to the tree-query syntax, which indicates that a node in a pattern is optional. It also fixes bugs in the handling of the postfix+operator in tree queries.Motivation
In #583, we introduced a system for computing
ctags-style tags, using Tree-queries to specify how they should work for each language. As part of that effort, we realized that in order to capture documentation comments, we would need a repetition operator in tree queries. It is also very convenient to have an optional operator, so that we don't have to specify each pattern twice: once with doc comments and once without.Implementation Notes
The compiled
TSQueryobjects now have a structure that's a bit more general, and a bit more like a classic NFA. Branching in the state graph is expressed by the "query step" structs having an optionalalternative_index, which stores the index of another query step. This more general structure allows for things like nested repetitions/optionals to be handled correctly.Next Steps
With these structural improvements in place, it will be easy to make the query syntax more powerful and convenient.
Changing the meaning of
*Currently, the
+operator means one-or-more, and the?means optional. So in order to get a zero-or-more repetition, you need to write+?. This is dumb. But unfortunately, the*operator is already used as a 'wildcard' matcher (match any node type).We should change the wildcard syntax to use a different character, probably
_, a la Rust's pattern syntax. Then we could change*to mean zero-or-more. That would match regex syntax, which is familiar to many people.Fortunately, the
*operator is not yet documented, so it's unlikely people outside of GitHub are using it.Parenthesized sequences
Currently, you can't write a pattern that matches multiple sibling nodes without also including a parent node that wraps them all. In the future, I'd like you to be able to write siblings as a simple parenthesized list, like this:
Note that there is no parent node written that contains both the
commentand thefunction_declaration. Today, we work around this missing feature by using a wildcard (*) for the parent node.This could also be useful if you wanted to apply repetition to a sibling sequence. For example, you could capture an arbitrary number of strings in an array like this:
I plan to do these things ☝️ in follow-up PRs, so that this PR will be non-breaking.