smacker/go-tree-sitter

Handle predicates

grouville opened this issue · 3 comments

When playing with the queries of go-tree-sitter, I realized (not 100% sure, but 95%) that it doesn't handle predicates such as:

(
  (identifier) @constant
  (#match? @constant "^[A-Z][A-Z_]+")
)

It gets matched, but not perfectly: there are duplicates, and the constraint of the match doesn't get applied.

According to the docs:

Note - Predicates are not handled directly by the Tree-sitter C library. They are just exposed in a structured form so that higher-level code can perform the filtering. However, higher-level bindings to Tree-sitter like the Rust crate or the WebAssembly binding implement a few common predicates like #eq? and #match?.

As this binding is relying on the C libraries (from what I understand), there's a great chance that this is not implemented at all.
I don't know where to start, if it's hard or not. But this would be an awesome addition, and would love to help on that 😇

PS: tested on the cue binding

Hi @grouville !

You are correct. The predicates are not implemented in this library. Currently, the binding just exposes Tree-sitter C library in Go without (almost) any logic on top.

I agree it would be nice to implement predicates to be compatible with Rust and JS bindings.

If you are willing to contribute the starting point would be query method in JS binding (or you can find the same in the rust one if you are more comfortable with rust). The logic there is somewhat straightforward and probably can be ported almost as-is to Go.

This was implemented, thanks to @PKuebler.