Predicates and directives allow you to specify arbitrary metadata and conditions associated with a pattern. They enable sophisticated filtering and annotation of query matches.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/tree-sitter/tree-sitter/llms.txt
Use this file to discover all available pages before exploring further.
What are Predicates?
Predicates are S-expressions added to patterns that specify conditions for matching. They:- Start with a predicate name beginning with
#and ending with? - Contain
@-prefixed capture names or strings as arguments - Filter matches based on captured node content
! instead of ? and associate metadata with patterns.
Predicates and directives are not handled directly by the Tree-sitter C library. They’re exposed in structured form for higher-level bindings to implement. The predicates documented here are supported by the CLI and most language bindings.
The eq? Predicate
The eq? predicate family allows matching against a single capture or string value.
Basic Usage
The first argument must be a capture. The second can be either:- Another capture (to compare their text)
- A string literal (to compare against a fixed value)
self.
Comparing Captures
Match key-value pairs where the value identifier has the same text as the key:{ name: name } in JavaScript.
Variants
There are four variants in theeq? family:
- #eq?
- #not-eq?
- #any-eq?
- #any-not-eq?
Exact MatchAll captured nodes must match the value.
The match? Predicate
The match? predicate family uses regular expressions to match against capture text.
Basic Usage
The first argument must be a capture, and the second must be a string containing a regular expression. Match identifiers inSCREAMING_SNAKE_CASE:
Documentation Comments
Match C documentation comments that begin with///:
Complex Patterns
Match Cgo comments in Go (C code in comments beforeimport "C"):
Variants
Likeeq?, the match? family has four variants:
#match?- All captured nodes must match the regex#not-match?- No captured nodes can match the regex#any-match?- At least one captured node must match#any-not-match?- At least one captured node must not match
The any-of? Predicate
The any-of? predicate matches a capture against multiple strings, succeeding if the capture equals any of them.
Matching Multiple Values
Match builtin variables in JavaScript:#eq? predicates or a complex regex.
#any-of? performs exact string matching, not regex matching. Use #match? with alternation (a|b|c) for regex-based alternatives.The is? and is-not? Predicates
The is? predicate asserts that a capture has a given property. This is used for semantic properties that the query system provides.
Local Variable Check
Match builtin variables that are not local:Directives
Directives are similar to predicates but end with! instead of ?. They associate metadata with patterns rather than filtering matches.
The set! Directive
The set! directive associates arbitrary key-value pairs with a pattern.
Setting Injection Language
Mark a comment as containing Doxygen syntax:injection.language property to parse the comment with the Doxygen parser.
Common Use Cases
Language Injection
Language Injection
Scope Metadata
Scope Metadata
Priority Hints
Priority Hints
The select-adjacent! Directive
The select-adjacent! directive filters capture text to preserve only nodes adjacent to another capture.
It takes two capture names as arguments:
- The capture to filter
- The capture to check adjacency against
The strip! Directive
The strip! directive removes text from a capture using a regular expression.
It takes two arguments:
- The capture to strip text from
- A regex pattern matching text to remove
The
strip! directive is commonly used to clean up captured text by removing comment markers, whitespace, or other syntactic noise.Practical Examples
Finding Test Functions
Matching Builtin Types
Deprecated API Usage
SQL Injection Detection
Predicate Summary
#eq?
Direct match against capture or string. All nodes must match.
#match?
Regular expression match. All nodes must match the pattern.
#any-of?
Match against a list of strings. Node must equal one string.
#is?
Check for semantic property. Node must have the property.
Directive Summary
#set!
Associate key-value metadata with pattern
#select-adjacent!
Filter capture to adjacent nodes only
#strip!
Remove text matching regex from capture
Predicate Modifiers
- Default (All)
- not- (Negation)
- any- (Existential)
Best Practices
Prefer Specific Predicates
Use
#any-of? instead of complex regex alternations when matching fixed strings. It’s more readable and performant.Combine Predicates
Use multiple predicates to create precise conditions. They’re evaluated as AND conditions.
Use Directives for Metadata
Don’t use predicates to filter when you just want to tag matches. Use
#set! for metadata.Next Steps
Query API
Learn how to execute queries and access predicates programmatically
Query Syntax
Review basic query syntax and pattern matching