-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Go Tree-sitter | ||
|
||
[![CI][ci]](https://github.com/tree-sitter/py-tree-sitter/actions/workflows/ci.yml) | ||
[![Go version][go version]](https://github.com/tree-sitter/go-tree-sitter/blob/master/go.mod) | ||
[![Version][version]](https://github.com/tree-sitter/go-tree-sitter/tags) | ||
[![Docs][docs]](https://pkg.go.dev/github.com/tree-sitter/go-tree-sitter) | ||
|
||
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: | ||
|
||
```sh | ||
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: | ||
|
||
```sh | ||
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). | ||
|
||
[ci]: https://img.shields.io/github/actions/workflow/status/tree-sitter/go-tree-sitter/ci.yml?logo=github&label=CI | ||
[go version]: https://img.shields.io/github/go-mod/go-version/tree-sitter/go-tree-sitter | ||
[version]: https://img.shields.io/github/v/tag/tree-sitter/go-tree-sitter?label=version | ||
[docs]: https://pkg.go.dev/badge/github.com/tree-sitter/go-tree-sitter.svg?style=flat-square |