Skip to content

Commit

Permalink
docs: add README
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq committed Aug 24, 2024
1 parent a39bcbe commit d72dbf9
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Go Tree-sitter

[![CI][ci]](https://github.com/tree-sitter/py-tree-sitter/actions/workflows/ci.yml)
[![Go version][version]](https://github.com/tree-sitter/go-tree-sitter/blob/master/go.mod)
[![Version](https://img.shields.io/github/v/tag/tree-sitter/go-tree-sitter?label=version)](https://github.com/tree-sitter/go-tree-sitter/tags)
[![Docs][docs]](https://pkg.go.dev/github.com/tree-sitter/go-tree-sitter)

[ci]: https://img.shields.io/github/actions/workflow/status/tree-sitter/go-tree-sitter/ci.yml?logo=github&label=CI
[version]: https://img.shields.io/github/go-mod/go-version/tree-sitter/go-tree-sitter
[docs]: https://pkg.go.dev/badge/github.com/tree-sitter/go-tree-sitter.svg

This repository contains Go bindings for the [Tree-sitter](https://tree-sitter.github.io/tree-sitter/) parsing library.

To use this in your Go project, run:

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

Example usage:

```go
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:

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

Due to [bugs with `runtime.SetFinalizer` and CGO](https://groups.google.com/g/golang-nuts/c/LIWj6Gl--es), 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](https://pkg.go.dev/github.com/tree-sitter/go-tree-sitter).

0 comments on commit d72dbf9

Please sign in to comment.