tree-sitter-r
An incremental tree-sitter grammar for the R programming language
Repository Health
Technical Analysis
tree-sitter-r is the official tree-sitter grammar for the R programming language, maintained by the r-lib / Posit team. It parses R source into a concrete syntax tree that updates incrementally as code changes, making it well suited to editors, linters, and code-analysis tooling that need fast, error-tolerant parsing.
The grammar ships with bindings for Rust, Node, Python, Go, Swift, and C, plus a set of highlight, injection, and local queries. It closely follows the R language reference while making pragmatic deviations (such as treating ]] as a literal token) that keep syntax trees consistent and easy to consume.
What You Get
- A complete tree-sitter grammar for R (
grammar.js) with generated C parser sources - Language bindings for Rust, Node, Python, Go, Swift, and C
- Highlight, injection, and local query files for editor integration
- A well-documented set of known deviations from the raw R grammar for predictable trees
Common Use Cases
- Syntax highlighting for R in editors such as Neovim, Helix, and Zed
- Structural code navigation, folding, and selection in IDEs
- Static analysis, linting, and refactoring tools that need an R syntax tree
- Language-server and code-intelligence backends for R
Under The Hood
Architecture - The grammar is authored in grammar.js using tree-sitter’s DSL and compiled to a C parser (src/parser.c) plus an external scanner. Language bindings under bindings/ wrap the generated parser for Rust, Node, Python, Go, Swift, and C, each exposing a language() entry point that consumers register with a tree-sitter runtime.
Tech Stack - Core parsing logic is generated C; the grammar definition is JavaScript. The Rust binding depends on tree-sitter-language with a cc build step, and dev/testing uses the tree-sitter crate. Node, Python, Swift, and Go bindings are packaged alongside via their native build systems (node-gyp, setup.py, Package.swift).
Code Quality - The repo includes a test/ corpus of parser tests, a CHANGELOG, CONTRIBUTING guide, editorconfig and clang-format configuration, and CI under .github/. Deliberate grammar deviations are documented in the README, signalling careful, maintained work.
API Design - Consuming the grammar is a one-call affair: import the binding and hand its language object to a tree-sitter parser. Shipped highlight/injection/local queries mean editors need little glue code, and the multi-language bindings give a consistent surface across ecosystems.