Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jimhester/rtreesitter

R bindings to the Tree-sitter parsing library
https://github.com/jimhester/rtreesitter

r tree-sitter

Last synced: about 2 months ago
JSON representation

R bindings to the Tree-sitter parsing library

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# rtreesitter

[![Codecov test coverage](https://codecov.io/gh/jimhester/rtreesitter/branch/main/graph/badge.svg)](https://codecov.io/gh/jimhester/rtreesitter?branch=main)
[![R-CMD-check](https://github.com/jimhester/rtreesitter/workflows/R-CMD-check/badge.svg)](https://github.com/jimhester/rtreesitter/actions)

rtreesitter provides R bindings to the [tree-sitter](https://tree-sitter.github.io/tree-sitter/) parsing library.

## Installation

You can install the development version from [GitHub](https://github.com/) with:

``` r
# install.packages("remotes")
remotes::install_github("jimhester/rtreesitter")
```

## Usage

```bash
mkdir vendor
cd vendor
git clone https://github.com/jimhester/tree-sitter-r
git clone https://github.com/tree-sitter/tree-sitter-python
git clone https://github.com/tree-sitter/tree-sitter-javascript
git clone https://github.com/tree-sitter/tree-sitter-go
```

```{r}
library(rtreesitter)

lib_dir <- file.path(tempdir(), "rtreesitter")
lib <- file.path(lib_dir, "languages.so")

repos <- c(
"tree-sitter/tree-sitter-python",
"tree-sitter/tree-sitter-javascript",
"jimhester/tree-sitter-r"
)
for (repo in repos) {
processx::run("git", c("clone", "--depth=1", sprintf("https://github.com/%s", repo), file.path(lib_dir, basename(repo))))
}

build_library(lib, file.path(lib_dir, basename(repos)))

p_r <- rts_parser$new("tree_sitter_R")
p_r$parse("x <- function(x) 1")

p_py <- rts_parser$new("tree_sitter_python")
p_py$parse("
def foo():
if bar:
baz()
")

p_js <- rts_parser$new("tree_sitter_javascript")
p_js$parse('
let name = "world";
alert("hello " + name);')
```