Go bindings for tree-sitter
  • C 68.4%
  • Go 30.5%
  • C++ 1%
  • Nix 0.1%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2025-11-12 19:31:52 +01:00
.github ci: add nix build 2025-11-12 16:08:25 +01:00
include/tree_sitter build: update C source files 2025-02-02 16:10:42 -05:00
src build: update C source files 2025-02-02 16:10:42 -05:00
tree-sitter@f5afe475de build: bump submodule to 0.25.1 2025-02-02 16:10:42 -05:00
.envrc feat: add nix flake 2025-11-12 16:08:25 +01:00
.gitignore feat: add nix flake 2025-11-12 16:08:25 +01:00
.gitmodules fix(gitmodules): use https for submodule 2025-11-12 16:08:25 +01:00
allocator.c feat: Initial commit 2024-08-23 23:32:17 -04:00
allocator.go perf: use the C allocator functions directly if no allocator is set 2025-11-12 18:36:26 +01:00
allocator.h feat: Initial commit 2024-08-23 23:32:17 -04:00
copy.sh fix: copy script 2024-08-26 12:35:45 -04:00
dup_unix.go feat: Initial commit 2024-08-23 23:32:17 -04:00
dup_windows.go feat: Initial commit 2024-08-23 23:32:17 -04:00
edit.go feat: bump lib to 0.24 2024-10-14 16:36:00 -04:00
edit_test.go feat: Initial commit 2024-08-23 23:32:17 -04:00
flake.lock feat: add nix flake 2025-11-12 16:08:25 +01:00
flake.nix feat: add nix flake 2025-11-12 16:08:25 +01:00
go.mod build: bump dependencies 2025-02-02 16:10:42 -05:00
go.sum build: bump dependencies 2025-02-02 16:10:42 -05:00
language.go feat!: update to 0.25 2025-02-02 16:10:42 -05:00
language_test.go feat: bump lib to 0.24 2024-10-14 16:36:00 -04:00
LICENSE feat: Initial commit 2024-08-23 23:32:17 -04:00
logger.go feat: Initial commit 2024-08-23 23:32:17 -04:00
lookahead_iterator.go feat: bump lib to 0.24 2024-10-14 16:36:00 -04:00
lookahead_iterator_test.go feat: Initial commit 2024-08-23 23:32:17 -04:00
node.go feat!: update to 0.25 2025-02-02 16:10:42 -05:00
node_test.go build: bump dependencies 2025-02-02 16:10:42 -05:00
parser.go feat!: update to 0.25 2025-02-02 16:10:42 -05:00
parser_test.go feat!: update to 0.25 2025-02-02 16:10:42 -05:00
point.go feat: bump lib to 0.24 2024-10-14 16:36:00 -04:00
query.go fix!: make satisfiesTextPredicate private 2025-11-12 19:31:52 +01:00
query_test.go feat!: update to 0.25 2025-02-02 16:10:42 -05:00
ranges.go feat: bump lib to 0.24 2024-10-14 16:36:00 -04:00
README.md docs: correct README badge 2025-11-12 12:38:02 -05:00
tree.go feat!: update to 0.25 2025-02-02 16:10:42 -05:00
tree_cursor.go feat!: update to 0.25 2025-02-02 16:10:42 -05:00
tree_cursor_test.go build: bump dependencies 2025-02-02 16:10:42 -05:00
tree_sitter.go fix: move lib.c include to its own file 2025-02-02 16:10:42 -05:00
tree_test.go feat: bump lib to 0.24 2024-10-14 16:36:00 -04:00

Go Tree-sitter

CI Go version Version Docs

This repository contains Go bindings for the Tree-sitter parsing library.

To use this in your Go project, run:

go get github.com/tree-sitter/go-tree-sitter@latest

Example usage:

package main

import (
    "fmt"

    tree_sitter "github.com/tree-sitter/go-tree-sitter"
    tree_sitter_javascript "github.com/tree-sitter/tree-sitter-javascript/bindings/go"
)

func main() {
    code := []byte("const foo = 1 + 2")

    parser := tree_sitter.NewParser()
    defer parser.Close()
    parser.SetLanguage(tree_sitter.NewLanguage(tree_sitter_javascript.Language()))

    tree := parser.Parse(code, nil)
    defer tree.Close()

    root := tree.RootNode()
    fmt.Println(root.ToSexp())
}

By default, none of the grammars are included in this package. This way, you can only bring in what you need, but it's at the slight cost of having to call go get n times.

In the example above, to fetch the JavaScript grammar, you can run the following:

go get github.com/tree-sitter/tree-sitter-javascript@latest

Alternatively you can also load grammars at runtime from a shared library via purego.

The example below shows how to load the JavaScript grammar from a shared library (libtree-sitter-PARSER_NAME.so) at runtime on Linux & macOS:

For more information on other platforms, see the purego documentation

package main

import (
	tree_sitter "github.com/tree-sitter/go-tree-sitter"
	"github.com/ebitengine/purego"
)

func main() {
	path := "/path/to/your/parser.so"
	lib, err := purego.Dlopen(path, purego.RTLD_NOW|purego.RTLD_GLOBAL)
	if err != nil {
        // handle error
    }

	var javascriptLanguage func() uintptr
	purego.RegisterLibFunc(&javascriptLanguage, lib, "tree_sitter_javascript")

	language := tree_sitter.NewLanguage(unsafe.Pointer(javascriptLanguage()))
}

Note

Due to bugs with runtime.SetFinalizer and CGO, you must always call Close on an object that allocates memory from C. This must be done for the Parser, Tree, TreeCursor, Query, QueryCursor, and LookaheadIterator objects.

For more information, see the documentation.