bonsaisitter

Bindings to the tree-sitter parsing library for R. The API mirrors the treesitter package, so bonsaisitter:: can stand in for treesitter:: in existing code. The package depends on nothing beyond base R.

bonsaisitter is the runtime only. Grammars come from separate packages:

Language Package Where
R treesitter.r CRAN
Python treesitter.python GitHub, drat
C++ treesitter.cpp GitHub, drat
Go treesitter.go GitHub, drat
Rust treesitter.rust GitHub, drat
JavaScript treesitter.javascript GitHub, drat

Installation

install.packages("bonsaisitter")

Development builds and the grammar packages that are not on CRAN are in the cornball.ai drat repository:

install.packages(c("bonsaisitter", "treesitter.python"),
                 repos = "https://cornball-ai.github.io/drat")

Usage

library(bonsaisitter)

lang <- treesitter.r::language()
tree <- text_parse("x <- f(1, 2)", lang)
root <- tree_root_node(tree)

node_type(root)
#> [1] "program"

q <- query(lang, "(call function: (identifier) @fn)")
vapply(query_captures(q, root)$node, node_text, character(1))
#> [1] "f"

cursor <- tree_walk(tree)
cursor$goto_first_child()
node_type(cursor$node())
#> [1] "binary_operator"

Differences from treesitter

Extras

License

MIT, copyright cornball.ai. The bundled tree-sitter sources are MIT, copyright Max Brunsfeld; every holder is listed in inst/COPYRIGHTS.