Query operators extend the basic pattern syntax with powerful features for capturing specific nodes, repeating patterns, creating alternatives, and controlling match behavior.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.
Capturing Nodes
When matching patterns, you often want to process specific nodes within the pattern. Captures allow you to associate names with specific nodes so you can refer to them later. Capture names are written after the nodes they refer to and start with an@ character.
Basic Capture
This pattern matches any assignment of afunction to an identifier, capturing the identifier as the-function-name:
Multiple Captures
You can capture multiple nodes in a single pattern. This matches all method definitions, capturing both the method name and class name:Capture names can use hyphens, underscores, dots, and alphanumeric characters. Common conventions include
@function.name, @variable.builtin, or @keyword.Quantification Operators
Quantification operators match repeating sequences of sibling nodes, similar to regular expression quantifiers.One or More: +
The + operator matches one or more repetitions of a pattern.
Match a sequence of one or more comments:
Zero or More: *
The * operator matches zero or more repetitions of a pattern.
Match a class declaration, capturing all decorators if any are present:
Optional: ?
The ? operator marks a node as optional.
Match all function calls, capturing a string argument if one is present:
- + (One or More)
- * (Zero or More)
- ? (Optional)
Use
+ when the pattern must appear at least once:Grouping Sibling Nodes
Use parentheses to group a sequence of sibling nodes. Groups can have quantification operators applied to them.Basic Grouping
Match a comment followed by a function declaration:Quantified Groups
Match a comma-separated series of numbers:- Matches the first number
- Matches zero or more groups of comma + number
Alternations
An alternation is written as square brackets[] containing a list of alternative patterns, similar to character classes in regular expressions.
Alternations with Captures
Match a call to either a variable or an object property:- If the function is an identifier, capture it as
@function - If the function is a member expression, capture the property as
@method
Matching Multiple Tokens
Match a set of keyword tokens:Anchors
The anchor operator. constrains how child patterns are matched based on their position among siblings.
First Child Anchor
When. is placed before the first child, that child only matches when it’s the first named node in the parent.
Match only the first identifier in an array:
Last Child Anchor
When. is placed after the last child, that child only matches when it’s the last named node in the parent.
Match only the last expression in a block:
Immediate Sibling Anchor
When. is placed between two child patterns, they only match immediate siblings.
Given a dotted name like a.b.c.d, match only consecutive identifier pairs:
(a, b), (b, c), (c, d)
Without the anchor, it would also match: (a, c), (b, d), (a, d) - all combinations.
Anchors ignore anonymous nodes when determining position. This ensures anchors work based on semantic structure, not just syntax tokens.
Combining Operators
Operators can be combined to create sophisticated patterns.Quantified Captures with Alternations
Match multiple imports of specific types:Anchored Groups
Match a JSDoc comment immediately before a function:Nested Captures with Quantifiers
Match all parameters in a function:Practical Examples
Find All TODO Comments
Find All TODO Comments
Match Function Calls with Specific Arguments
Match Function Calls with Specific Arguments
Find Export Declarations
Find Export Declarations
Match Chained Method Calls
Match Chained Method Calls
obj.method1().method2().Best Practices
Name Captures Semantically
Use descriptive names like
@function.name or @variable.builtin that indicate the role or meaning of captured nodes.Use Anchors Judiciously
Anchors make patterns more specific but also more brittle. Use them when position truly matters.
Combine Quantifiers with Predicates
Use quantifiers to capture multiple nodes, then predicates to filter them based on content.
Test Alternations Thoroughly
Each alternative branch should be tested to ensure it matches the intended nodes.
Next Steps
Predicates
Add conditions and filters to your captured nodes
Query API
Execute queries programmatically using the C API