Empryoempryo.beta
← Glossary
definition

Tree-sitter

Tree-sitter is an open-source incremental parser generator and parsing library that turns source code into a concrete syntax tree (CST), fast enough to re-parse on every keystroke and resilient enough to produce a usable tree even when the code has errors.

Created at GitHub, Tree-sitter has two parts: a CLI/build system that compiles a grammar (a JavaScript-defined grammar.js) into a fast C parser, and a runtime library that uses those parsers. Each language is a separate grammar (tree-sitter-python, tree-sitter-rust, and so on), and there are community grammars for well over a hundred languages. The runtime has bindings for many hosts, including a WebAssembly build (web-tree-sitter) that lets the same grammars run in browsers and JS/Bun runtimes without native compilation.

Two properties make it well-suited to editors and tooling. It is incremental: after an edit, it re-parses only the changed region instead of the whole file, which keeps parsing cheap on large files. And it is error-tolerant: malformed or half-typed code still yields a tree with ERROR nodes rather than failing outright, so features keep working while you type. This is why editors like Neovim, Helix, and Zed use it for syntax highlighting, indentation, and structural selection.

Tree-sitter also ships a query language: S-expression patterns that match nodes in the tree and bind @captures to them. A query like (function_declaration name: (identifier) @name) extracts every function name in a file regardless of formatting or whitespace. Queries are the standard way tools pull structured facts (functions, classes, imports, call sites) out of code without writing a bespoke parser per language.

The trade-off is that Tree-sitter is purely syntactic. It produces a parse tree, not a semantic model: it does not resolve types, follow imports across files, or know that two User symbols refer to the same definition. That deeper, cross-file understanding is the domain of a language server (LSP) or a type-aware tool like ts-morph. Tree-sitter is the fast, language-agnostic foundation those higher tiers build on.