Favicon of Tree-sitter

Tree-sitter

Tree-sitter builds and incrementally updates concrete syntax trees for source code, with a dependency-free C core and Rust and Wasm bindings.

Tree-sitter GitHub repository preview

Tree-sitter is a parser generator and incremental parsing library used by editors, linters, and other developer tools that need an up-to-date syntax tree of a source file as it's being edited. It's aimed at people building language tooling (editor plugins, syntax highlighters, static analyzers, code navigation features) rather than end users writing application code.

The core parsing engine is written in pure C with no external dependencies, so it can be embedded almost anywhere. Language grammars are written in a declarative DSL and compiled into standalone C parsers, which are then consumed through bindings in Rust, WebAssembly, or other host languages. Tree-sitter is the engine behind syntax highlighting and code folding in editors like Neovim, Atom, and many others.

Key features

  • Incremental parsing: re-parses only the parts of a file that changed after an edit, instead of reprocessing the whole document, which keeps it fast enough to run on every keystroke.
  • General grammar support: designed to parse any programming language, not tied to a specific syntax style.
  • Error tolerance: produces a usable syntax tree even when the source has syntax errors, which matters for editors where code is often incomplete while typing.
  • Dependency-free C runtime: the parsing library itself is pure C with no external dependencies, making it easy to embed in editors, servers, or other applications.
  • Multiple language bindings: official Rust and WebAssembly (Wasm) bindings, alongside a command-line interface for generating and testing grammars.
  • Grammar DSL: a declarative format for defining language grammars that compiles down to a standalone C parser.

Ideal use cases

Tree-sitter fits well when you're building an editor feature, IDE extension, or code-analysis tool that needs a live, structured view of source code: syntax highlighting, code folding, structural selection, symbol navigation, or refactoring tools. It's also a good base for static analysis and linting tools that need to walk a real syntax tree instead of relying on regex-based heuristics.

It's a strong fit for text editors and language servers where source files are edited interactively and re-parsing must be cheap, and for projects that already have (or want to write) a grammar for a specific language.

It's not the right tool if you need a full semantic analyzer, type checker, or compiler backend. Tree-sitter produces a concrete syntax tree, not a fully resolved AST with type information or scope resolution. If you just need to parse a file once and don't care about incremental re-parsing or error recovery, a simpler dedicated parser for that specific language may be less overhead.

Installation

Tree-sitter is distributed as a runtime library plus a set of language grammars, and it can be consumed in different ways depending on your stack:

  • Rust: add the tree-sitter crate as a dependency, then use the Rust binding to load a compiled language grammar and parse source text. See the Rust binding README for API details.
  • WebAssembly: use the Wasm binding to run Tree-sitter in a browser or JS runtime without a native build step. See the Wasm binding README for setup.
  • Command-line interface: install the tree-sitter CLI (via the cli crate) to generate parsers from grammar files, run test suites against grammars, and inspect parse trees from the terminal.

A typical workflow for working with a grammar looks like this once the CLI is installed:

tree-sitter generate
tree-sitter test
tree-sitter parse path/to/file

For language-specific parsers (for example a grammar for Python, JavaScript, or another language), you'd typically depend on that language's separate tree-sitter-<language> grammar package alongside the core tree-sitter runtime. Full setup details for each binding, including build requirements for the underlying C compiler toolchain, are in the linked binding READMEs and the project documentation site.

Categories:

Frequently asked questions

Share:

Stars
27.2K
Forks
3K
Last commit
14 hours ago
Repository age
13 years
License
MIT
Self-hosted
No
Activity score
83/100
View Repository
Ad
Favicon

 

  
 

Similar to Tree-sitter

Favicon

 

  
 
Favicon

 

  
 
Favicon