Getting Started
This guide will walk you through setting up your development environment and creating your first Tree-sitter parser.Dependencies
To develop a Tree-sitter parser, you need to install two dependencies:JavaScript Runtime
Tree-sitter grammars are written in JavaScript, and Tree-sitter uses a JavaScript runtime (the default being Node.js) to interpret JavaScript files.The runtime command (default:
node) must be in one of the directories in your PATH.C Compiler
Tree-sitter creates parsers that are written in C. To run and test these parsers with thetree-sitter parse or tree-sitter test commands, you must have a C/C++ compiler installed.
Installation
You can install the Tree-sitter CLI in several ways:Project Setup
The preferred convention is to name your parser repository “tree-sitter-” followed by the name of the language in lowercase.Initialize the project
- Language name
- Parser description
- Author information
- License
Your First Grammar
After initialization, you’ll have agrammar.js file with the following structure:
grammar.js
The triple-slash reference and
@ts-check comments enable TypeScript type checking and autocompletion in your editor.Understanding the Template
Let’s break down the key parts:- File header - Documentation comments with description, author, and license
- Type reference - Enables IDE support for the grammar DSL
- Export - The grammar object is exported as the default export
- Name - Must match your language name
- Rules - Define the structure of your language
Generate the Parser
Now generate the C code for your parser:Run the generate command
src/ directory:parser.c- The generated parsertree_sitter/parser.h- Header filenode-types.json- Node type definitions
Enable IDE Support
For the best development experience, install the Tree-sitter TypeScript definitions:- Autocompletion - Suggestions for grammar DSL functions
- Type checking - Catch errors before generating
- Documentation - Inline docs for all functions
Development Commands
Here are the essential commands you’ll use during development:tree-sitter generate
Generate the parser from your grammar
tree-sitter test
Run all tests in
test/corpus/tree-sitter parse
Parse a file and display the syntax tree
tree-sitter build
Compile the parser as a shared library
Parse Command Options
Next Steps
Now that you have a working parser, you’re ready to:- Learn about the Grammar DSL functions
- Start Writing Grammar rules
- Add Tests for your parser