query-accessors: Query accessors

query-accessorsR Documentation

Query accessors

Description

  • query_pattern_count() returns the number of patterns in a query.

  • query_capture_count() returns the number of captures in a query.

  • query_string_count() returns the number of string literals in a query.

  • query_start_byte_for_pattern() returns the byte where the ith pattern starts in the query source.

Usage

query_pattern_count(x)

query_capture_count(x)

query_string_count(x)

query_start_byte_for_pattern(x, i)

Arguments

x

⁠[tree_sitter_query]⁠

A query.

i

⁠[double(1)]⁠

The ith pattern to extract the start byte for.

Value

  • query_pattern_count(), query_capture_count(), and query_string_count() return a single double count value.

  • query_start_byte_for_pattern() returns a single double for the start byte if there was an ith pattern, otherwise it returns NA.

Examples


source <- '(binary_operator
  lhs: (identifier) @lhs
  operator: _ @operator
  rhs: (function_definition) @rhs
  (#eq? @lhs "fn")
)'
language <- treesitter.r::language()

query <- query(language, source)

query_pattern_count(query)
query_capture_count(query)
query_string_count(query)

text <- "
  fn <- function() {}
  fn2 <- function() {}
  fn <- 5
  fn <- function(a, b, c) { a + b + c }
"
parser <- parser(language)
tree <- parser_parse(parser, text)
node <- tree_root_node(tree)

query_matches(query, node)


treesitter documentation built on June 24, 2024, 5:07 p.m.