A Tree-sitter query consists of one or more patterns written as S-expressions. Each pattern matches a certain set of nodes in a syntax tree.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.
Basic Node Matching
The expression to match a given node consists of a pair of parentheses containing the node’s type and optionally, patterns for the node’s children.Matching with All Children
This pattern matches anybinary_expression node whose children are both number_literal nodes:
Matching with Some Children
Children can be omitted. This matches anybinary_expression where at least one child is a string_literal node:
Fields
It’s good practice to make patterns more specific by specifying field names associated with child nodes. Prefix a child pattern with a field name followed by a colon.Using Field Names
This pattern matches anassignment_expression node where the left child is a member_expression whose object is a call_expression:
Using field names makes your queries more robust and easier to understand. They document the semantic role of each child in the pattern.
Negated Fields
You can constrain a pattern to only match nodes that lack a certain field by prefixing the field name with!.
This pattern matches a class declaration with no type parameters:
Anonymous Nodes
The parenthesized syntax only applies to named nodes. To match specific anonymous nodes (like operators or punctuation), write their name between double quotes. This pattern matches anybinary_expression where the operator is != and the right side is null:
Common Anonymous Nodes
Anonymous nodes typically include:- Operators:
"+","-","*","/","==","!=" - Punctuation:
"(",")","{","}","[","]",",",";" - Keywords: Language-specific keywords that appear literally in code
Special Nodes
The Wildcard Node
A wildcard node matches any node, similar to. in regular expressions.
- Named Wildcard
- Any Wildcard
(_) matches any named node:The ERROR Node
When the parser encounters text it does not recognize, it creates an ERROR node. These can be queried to detect syntax errors:
The MISSING Node
If the parser recovers from errors by inserting a missing token, it creates a MISSING node. These are zero-width nodes that represent inserted tokens.
Query all missing nodes:
MISSING nodes are not captured by (ERROR) queries. Use both to detect all syntax errors in a parse tree.Supertype Nodes
Some node types are marked as supertypes in a grammar. A supertype contains multiple subtypes, allowing you to match any of them without listing each individually.Matching Any Subtype
For example,expression is often a supertype that can represent any kind of expression:
binary_expression, call_expression, identifier, and any other expression subtype.
Matching Specific Subtypes
Use the syntaxsupertype/subtype to query specific subtypes:
binary_expression only if it is a child of the expression supertype.
Supertypes with Anonymous Nodes
This also works with anonymous nodes:Best Practices
Use Field Names When Available
Use Field Names When Available
Field names make queries more maintainable and self-documenting. They also make queries more resilient to grammar changes that affect child ordering but not field names.
Start Specific, Then Generalize
Start Specific, Then Generalize
Begin with specific patterns that match exactly what you need. Generalize only if you need broader matches. This reduces false positives.
Leverage Supertypes
Leverage Supertypes
Use supertypes when you want to match multiple related node types. This keeps queries concise and easier to maintain.
Handle Error Nodes
Handle Error Nodes
Consider whether your query should match on partial or erroneous code. Use
ERROR and MISSING nodes to handle syntax errors gracefully.Next Steps
Query Operators
Learn how to capture nodes, use quantifiers, and create complex patterns
Predicates
Add conditions and metadata to your patterns