Skip to main content

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 the tree-sitter parse or tree-sitter test commands, you must have a C/C++ compiler installed.
Tree-sitter will try to look for compilers in the standard places for each platform (gcc, clang, or MSVC).

Installation

You can install the Tree-sitter CLI in several ways:
The npm installation method is fast but only works on certain platforms because it relies on pre-built binaries.

Project Setup

The preferred convention is to name your parser repository “tree-sitter-” followed by the name of the language in lowercase.
1

Create the project directory

Replace mylang with the lowercase name of your language.
2

Initialize the project

This command will prompt you for:
  • Language name
  • Parser description
  • Author information
  • License
The CLI will create the necessary project structure and files.
3

Review the generated files

The init command creates several files:
  • grammar.js - Your grammar definition
  • package.json - Node.js package configuration
  • Cargo.toml - Rust package configuration
  • bindings/ - Language bindings
  • src/ - Generated parser code (created later)

Your First Grammar

After initialization, you’ll have a grammar.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:
1

Run the generate command

This creates the parser code in the src/ directory:
  • parser.c - The generated parser
  • tree_sitter/parser.h - Header file
  • node-types.json - Node type definitions
2

Test the parser

Create a test file:
You should see:
The numbers in brackets [0, 0] - [1, 0] represent the position in the file: [row, column] for start and end.

Enable IDE Support

For the best development experience, install the Tree-sitter TypeScript definitions:
This downloads the TypeScript API and enables:
  • 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:
  1. Learn about the Grammar DSL functions
  2. Start Writing Grammar rules
  3. Add Tests for your parser
Start by examining existing Tree-sitter parsers on GitHub to see real-world examples. The tree-sitter organization maintains parsers for many popular languages.