Bindings to the 'Tree-Sitter' Parsing Library

A runtime for the 'Tree-sitter' parsing library < https://tree-sitter.github.io/tree-sitter/> that mirrors the API of the 'treesitter' package, so it can serve as a drop-in replacement. Parses source code into concrete syntax trees and updates them incrementally as the source changes. Grammars are supplied by separate packages such as 'treesitter.r', so the runtime itself depends on nothing beyond base R.


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

  • The R6 TreeCursor is replaced by tree_walk() and node_walk(), which return an environment of closures with the same method names (cursor$goto_first_child(), cursor$node(), and so on).
  • query_matches() returns a flat list of matches instead of nesting them by pattern.
  • Query predicates such as #eq? and #match? are not applied; query_captures() returns every capture.

Extras

  • literals_and_calls() and audit_translation() compare the numeric literals and call names of a source file and its port, for example Python code and its R translation, and report the constants that drifted.
  • as.data.frame() on a node gives one row per descendant, with type, text and position columns.

License

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

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("bonsaisitter")

0.1.2 by Troy Hernandez, a month ago


https://github.com/cornball-ai/bonsaisitter


Report a bug at https://github.com/cornball-ai/bonsaisitter/issues


Browse source code at https://github.com/cran/bonsaisitter


Authors: Troy Hernandez [aut, cre] (ORCID: , cornball.ai [cph] , Max Brunsfeld [ctb, cph] (tree-sitter C library , bundled under src/tree-sitter) , International Business Machines Corporation [cph] (ICU UTF-8 and UTF-16 headers , src/tree-sitter/lib/src/unicode) , Unicode , Inc. [cph] (ICU license , src/tree-sitter/lib/src/unicode/LICENSE) , Mathias Panzenböck [ctb] (src/tree-sitter/lib/src/portable/endian.h , public domain)


Documentation:   PDF Manual  


MIT + file LICENSE license


Suggests tinytest, treesitter, treesitter.r


See at CRAN