From e67cee655cb964ab1c65949617db163b46337dd0 Mon Sep 17 00:00:00 2001 From: Amaan Qureshi Date: Sat, 24 Aug 2024 18:12:33 -0400 Subject: [PATCH] docs: add README --- README.md | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..00c32ac --- /dev/null +++ b/README.md @@ -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: + +```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).