Skip to content

Tree-queries - add optional node syntax, improve handling of repeated nodes - #612

Merged
maxbrunsfeld merged 7 commits into
masterfrom
optionals-in-queries
May 11, 2020
Merged

maxbrunsfeld merged 7 commits into
masterfrom
optionals-in-queries

Conversation

@maxbrunsfeld

@maxbrunsfeld maxbrunsfeld commented May 7, 2020 •

Copy link
Copy Markdown
Contributor

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 TSQuery objects 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 optional alternative_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:

(
  (comment)* @doc
  (function_declaration
    name: (identifier) @name) @function
)

Note that there is no parent node written that contains both the comment and the function_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:

(array
  (string) @element
  ("," (string) @element)*)

I plan to do these things ☝️ in follow-up PRs, so that this PR will be non-breaking.

@maxbrunsfeld
maxbrunsfeld force-pushed the optionals-in-queries branch from 46c8100 to 1011be7 Compare May 7, 2020 19:41
@maxbrunsfeld
maxbrunsfeld merged commit e2271ac into master May 11, 2020
@maxbrunsfeld
maxbrunsfeld deleted the optionals-in-queries branch May 11, 2020 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant