go-tree-sitter icon indicating copy to clipboard operation
go-tree-sitter copied to clipboard

Handle predicates

Open grouville opened this issue 2 years ago • 2 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

grouville avatar Jul 08 '22 15:07 grouville

Repro: https://gist.github.com/grouville/e6268cda701881921e9cc9fb1fe4f9f2

grouville avatar Jul 08 '22 16:07 grouville

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.

smacker avatar Jul 10 '22 07:07 smacker

This was implemented, thanks to @PKuebler.

smacker avatar Feb 23 '23 06:02 smacker