Query#

class tree_sitter.Query(language, source, *, timeout_micros=None)#

A set of patterns that match nodes in a syntax tree.

Raises:

QueryError – If any error occurred while creating the query.

See also

Query Syntax

Note

The following predicates are supported by default:

  • #eq?, #not-eq?, #any-eq?, #any-not-eq?

  • #match?, #not-match?, #any-match?, #any-not-match?

  • #any-of?, #not-any-of?

  • #is?, #is-not?

  • #set!

Methods#

captures(node, /, predicate=None)#

Get a list of captures within the given node.

Returns:

A dict where the keys are the names of the captures and the values are lists of the captured nodes.

Hint

This method returns all of thecaptures while matches() only returns the last match.

Important

Predicates cannot be used if the tree was parsed from a callback.

disable_capture(capture)#

Disable a certain capture within a query.

Important

Currently, there is no way to undo this.

disable_pattern(index)#

Disable a certain pattern within a query.

Important

Currently, there is no way to undo this.

end_byte_for_pattern(index)#

Get the byte offset where the given pattern ends in the query’s source.

is_pattern_guaranteed_at_step(index)#

Check if a pattern is guaranteed to match once a given byte offset is reached.

is_pattern_non_local(index)#

Check if the pattern with the given index is “non-local”.

Note

A non-local pattern has multiple root nodes and can match within a repeating sequence of nodes, as specified by the grammar. Non-local patterns disable certain optimizations that would otherwise be possible when executing a query on a specific range of a syntax tree.

is_pattern_rooted(index)#

Check if the pattern with the given index has a single root node.

matches(node, /, predicate=None)#

Get a list of matches within the given node.

Returns:

A list of tuples where the first element is the pattern index and the second element is a dictionary that maps capture names to nodes.

Important

Predicates cannot be used if the tree was parsed from a callback.

pattern_assertions(index)#

Get the property assertions for the given pattern index.

Assertions are performed using the #is? and #is-not? predicates.

Returns:

A dictionary of assertions, where the first item is the optional property value and the second item indicates whether the assertion was positive or negative.

pattern_settings(index)#

Get the property settings for the given pattern index.

Properties are set using the #set! predicate.

Returns:

A dictionary of properties with optional values.

set_byte_range(byte_range)#

Set the range of bytes in which the query will be executed.

set_point_range(point_range)#

Set the range of points in which the query will be executed.

start_byte_for_pattern(index)#

Get the byte offset where the given pattern starts in the query’s source.

set_match_limit(match_limit)#

Set the maximum number of in-progress matches.

Raises:

ValueError – If set to 0.

set_max_start_depth(max_start_depth)#

Set the maximum start depth for the query.

set_timeout_micros(timeout_micros)#

Set the maximum duration in microseconds that query execution should be allowed to take before halting.

Attributes#

capture_count#

The number of captures in the query.

did_exceed_match_limit#

Check if the query exceeded its maximum number of in-progress matches during its last execution.

match_limit#

The maximum number of in-progress matches.

pattern_count#

The number of patterns in the query.

timeout_micros#

The maximum duration in microseconds that query execution should be allowed to take before halting.