Query#

class tree_sitter.Query(language, source)#

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#

capture_name(index)#

Get the name of the capture at the given index.

Added in version 0.25.0.

capture_quantifier(pattern_index, capture_index)#

Get the quantifier of the capture at the given indexes.

Added in version 0.25.0.

disable_capture(name)#

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.

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.

start_byte_for_pattern(index)#

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

string_value(index)#

Get the string literal at the given index.

Added in version 0.25.0.

Attributes#

capture_count#

The number of captures in the query.

pattern_count#

The number of patterns in the query.

string_count#

The number of string literals in the query.

Added in version 0.25.0.