Skip to content

Guide · Open source

How to use tree-sitter: parse code and update syntax trees

Updated · written with AI from the project's own documentation and checked against our catalogue data

Tree-sitter is a parser generator tool and an incremental parsing library. It builds a syntax tree for a source file and updates that tree as the file changes, so tool makers can work with code structure without re-reading the whole file each time. This guide is for developers building editors, IDE features, or analysis tools who want to understand what tree-sitter does and how to start with it.

tree-sittertree-sitter/tree-sitter · ★ 27.4k · MIT

Quick start

  1. 1Clone the repository. Run the git clone command from the README to get the source code locally.
  2. 2Decide which binding you need. Pick the Rust binding, the Wasm binding for the web, or the command-line interface, depending on where your code runs.
  3. 3Read the binding README. Open the README for your chosen binding in the repository to see how to load a language and parse text.
  4. 4Get or write a grammar. Confirm a grammar exists for your language. If not, plan to write one, since tree-sitter is a parser generator.
  5. 5Parse a file. Use the binding API to parse a source file into a syntax tree.
  6. 6Apply edits incrementally. When the file changes, update the existing tree instead of re-parsing from scratch.

What tree-sitter does

Tree-sitter reads source code and builds a concrete syntax tree, a structured map of the code's grammatical structure. When the file is edited, it can efficiently update that tree instead of starting over.

The project aims to be general enough to parse any programming language, fast enough to parse on every keystroke in a text editor, robust enough to give useful results even when code contains syntax errors, and dependency-free so the runtime library can be embedded in any application.

  • Builds a concrete syntax tree for a source file
  • Updates the syntax tree efficiently as the file is edited
  • Runtime library is written in pure C
  • Designed to keep working when code has syntax errors

Who tree-sitter is for

Tree-sitter is a building block for tool makers, not an end-user application. It suits people building code editors or IDE features, tool authors who want syntax-aware highlighting, navigation, or analysis for a language, and teams embedding a small parsing runtime in their own software.

It is not a ready-made linter or formatter, and the README does not describe those features. If you only want to use an editor, you do not need to work with tree-sitter directly.

  • Developers building code editors or IDE features
  • Tool authors who need syntax-aware highlighting, navigation, or analysis
  • Teams embedding a parsing runtime in their own applications
  • Not suited to people who only want an editor or a ready-made linter or formatter

Install tree-sitter

The README lists a package-manager install method and no prerequisites. The one install command given in the README clones the repository from GitHub. Check the documentation at the project homepage for how to add the library to your own project.

Bindings are listed for Rust, for the web via WebAssembly, and a command-line interface. Each has its own README in the repository.

  • Rust binding: lib/binding_rust/README.md
  • Wasm binding: lib/binding_web/README.md
  • Command-line interface: crates/cli/README.md
git clone https://github.com/tree-sitter/tree-sitter

Parse a source file with a grammar

Tree-sitter needs a grammar for the language you want to parse. The README says the project is a parser generator, so a grammar is what turns a language definition into a working parser. If a grammar already exists for your language, you can use it; otherwise you will need to write one.

Consult the binding README for your language to see the exact API for loading a language and parsing text in your environment.

  • Choose the binding that matches your application: Rust, Wasm, or C runtime
  • Make sure a grammar exists for your language before you start
  • Read the binding README for the parse API

Update the tree as code changes

The main reason to use tree-sitter is incremental updating. When the source file is edited, the existing syntax tree is updated rather than rebuilt from scratch, which is what makes parsing on every keystroke practical.

The README describes this as the design goal. Check the binding documentation for the specific calls that apply the edit to the tree.

Find the documentation and get help

The full documentation is at tree-sitter.github.io. The README also links to a Discord server and a Matrix chat room for the project community.

Because the README is the only source for this guide, details beyond what is listed here should be confirmed in the documentation and binding READMEs.

  • Documentation: https://tree-sitter.github.io
  • Discord and Matrix chat are linked from the README

What people use it for

  • Building syntax-aware highlighting in a code editor
  • Adding navigation features to an IDE for a specific programming language
  • Running syntax-aware analysis inside a custom developer tool
  • Embedding a small parsing runtime in a Rust, C, or web application

Frequently asked questions

Is tree-sitter free to use?

Yes, tree-sitter is released under the MIT licence, as listed in the project details. You can check the LICENSE file in the repository for the full terms.

What do I need to install tree-sitter?

The README lists no prerequisites. It gives a package-manager install method and a git clone command for the repository.

Is tree-sitter a code editor?

No. It is a parsing system for tool makers. The README says it is a building block for editors and IDE features, not an end-user app.

Can tree-sitter find errors or format my code?

The README does not describe linting or formatting features. Tree-sitter is designed to give useful results even when code has syntax errors, but it is not presented as a linter or formatter.

Which languages does tree-sitter support?

The README says the project aims to parse any programming language, but each language needs a grammar. If one does not exist for your language, you will need to write it.

Are there alternatives to tree-sitter?

The project lists no comparable projects. Check the documentation for how tree-sitter fits with other parsing approaches.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Tree-sitter suits developers building editors, IDE features, or syntax-aware tools who need fast incremental parsing and can use or write a grammar for their language. It is not the right choice if you want a finished editor, linter, or formatter, or if you need a parser for a language with no grammar and no plan to write one.

Install options & downloads