Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kiteco/go-tree-sitter
https://github.com/kiteco/go-tree-sitter
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/kiteco/go-tree-sitter
- Owner: kiteco
- License: other
- Created: 2019-12-04T21:54:30.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-11-09T17:48:49.000Z (about 4 years ago)
- Last Synced: 2024-06-21T05:03:52.974Z (6 months ago)
- Language: C
- Size: 7.86 MB
- Stars: 3
- Watchers: 4
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go tree-sitter
[![Build Status](https://travis-ci.org/kiteco/go-tree-sitter.svg?branch=master)](https://travis-ci.org/kiteco/go-tree-sitter)
[![GoDoc](https://godoc.org/github.com/kiteco/go-tree-sitter?status.svg)](https://godoc.org/github.com/kiteco/go-tree-sitter)Golang bindings for [tree-sitter](https://github.com/tree-sitter/tree-sitter)
## Usage
Create a parser with that grammar:
```go
import (
sitter "github.com/kiteco/go-tree-sitter"
"github.com/kiteco/go-tree-sitter/javascript"
)parser := sitter.NewParser()
parser.SetLanguage(javascript.GetLanguage())
```Parse some code:
```go
sourceCode = []byte("let a = 1")
tree := parser.Parse(sourceCode)
```Inspect the syntax tree:
```go
n := tree.RootNode()fmt.Println(n) // (program (lexical_declaration (variable_declarator (identifier) (number))))
child := n.NamedChild(0)
fmt.Println(child.Type()) // lexical_declaration
fmt.Println(child.StartByte()) // 0
fmt.Println(child.EndByte()) // 9
```If your source code changes, you can update the syntax tree. This will take less time than the first parse.
```go
// change 1 -> true
newText := []byte("let a = true")
tree.Edit(sitter.EditInput{
StartIndex: 8,
OldEndIndex: 9,
NewEndIndex: 12,
StartPoint: sitter.Point{
Row: 0,
Column: 8,
},
OldEndPoint: sitter.Point{
Row: 0,
Column: 9,
},
NewEndPoint: sitter.Point{
Row: 0,
Column: 12,
},
})// check that it changed tree
assert.True(n.HasChanges())
assert.True(n.Child(0).HasChanges())
assert.False(n.Child(0).Child(0).HasChanges()) // left side of the tree didn't change
assert.True(n.Child(0).Child(1).HasChanges())// generate new tree
newTree := parser.ParseWithTree(newText, tree)
```