Learn R Programming

treesitter (version 0.3.0)

query-accessors: 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() and query_end_byte_for_pattern() return the byte where the ith pattern starts/ends in the query source.

Usage

query_pattern_count(x)

query_capture_count(x)

query_string_count(x)

query_start_byte_for_pattern(x, i)

query_end_byte_for_pattern(x, i)

Value

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

  • query_start_byte_for_pattern() and query_end_byte_for_pattern() return a single double for their respective byte if there was an ith pattern, otherwise they return NA.

Arguments

x

[tree_sitter_query]

A query.

i

[double(1)]

The ith pattern to extract the byte for.

Examples

Run this code
if (FALSE) { # rlang::is_installed("treesitter.r")
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)

query_start_byte_for_pattern(query, 1)
query_end_byte_for_pattern(query, 1)

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)
}

Run the code above in your browser using DataLab