1
0
Fork 0
Bindings for the tree-sitter library with some embedded grammars.
Find a file
Sky Johnson e9038855db Add comprehensive benchmarks for Go binding overhead
Benchmark realistic workloads using the Go language grammar:

Parsing:
- BenchmarkParseGo: full parse with throughput (MB/s)
- BenchmarkParseGoScaling: 1KB/4KB/16KB/64KB inputs
- BenchmarkParseGoCancellable: cancellable context overhead
- BenchmarkParseInputGo: callback-based ParseInput path
- BenchmarkIncrementalParse: edit + re-parse cycle

Tree traversal:
- BenchmarkTreeCursorWalk: TreeCursor recursive DFS
- BenchmarkIteratorDFS/BFS: Iterator in both modes
- BenchmarkNamedIteratorDFS: named-only traversal

Node property access:
- BenchmarkNodeChildAccess: child iteration with property reads
- BenchmarkNodeNamedChildAccess: named child with point access
- BenchmarkNodeContent: extracting source text
- BenchmarkNodeSiblingTraversal: NextSibling chain
- BenchmarkNodeChildByFieldName: field-based child lookup
- BenchmarkNodeString: S-expression generation

Query execution:
- BenchmarkQueryNew: query compilation cost
- BenchmarkQueryExecMatch: NextMatch iteration
- BenchmarkQueryExecCapture: NextCapture iteration
- BenchmarkQueryMultiPattern: multi-pattern with content reads
- BenchmarkQueryWithPointRange: range-restricted queries

Lifecycle:
- BenchmarkLanguageSymbolLookup: symbol metadata access
- BenchmarkParserCreate: parser allocation + close
- BenchmarkTreeCopy: tree duplication
2026-04-03 11:07:35 -05:00
_automation Merge pull request #172 from asalvi0/improve-updater 2024-08-27 17:42:17 +08:00
_examples Update all Go import paths to git.sharkk.net/go/tree-sitter 2026-04-02 16:40:36 -05:00
bash Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
c Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
cpp Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
csharp Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
css Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
cue Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
dockerfile Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
elixir Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
elm Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
golang Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
groovy Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
hcl Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
html Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
java Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
javascript Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
kotlin Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
lua Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
markdown Fix typo: bingding_test.go -> binding_test.go 2026-04-02 16:50:43 -05:00
ocaml Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
php Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
protobuf Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
python Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
ruby Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
rust Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
scala Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
sql Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
svelte Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
swift Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
toml Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
tree_sitter Move tree-sitter C runtime files into tree_sitter/ subdirectory 2026-04-02 16:40:58 -05:00
typescript Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
yaml Replace testify with stdlib testing in all grammar tests 2026-04-02 16:50:14 -05:00
.gitignore updated _automation/main.go script to prefer commits tied to a latest release, if it exists. Added a README.md for this file. Added a subcommand called 'run-tests' which will run 'go test' against every grammar's 'binding_test.go' file. 2024-02-08 09:46:20 -08:00
benchmark_test.go Add comprehensive benchmarks for Go binding overhead 2026-04-03 11:07:35 -05:00
bindings.c Update CGo directives for tree_sitter/ subdirectory layout 2026-04-02 16:42:14 -05:00
bindings.go Update CGo directives for tree_sitter/ subdirectory layout 2026-04-02 16:42:14 -05:00
bindings.h Update CGo directives for tree_sitter/ subdirectory layout 2026-04-02 16:42:14 -05:00
bindings_test.go Replace testify with stdlib testing in bindings_test.go 2026-04-02 16:49:57 -05:00
example_test.go Fix example_test.go function naming and remove unused example 2026-04-02 16:50:07 -05:00
go.mod Update Go version to 1.24 2026-04-02 16:50:35 -05:00
go.sum Remove testify dependency from go.mod 2026-04-02 16:50:24 -05:00
iter.go NewIterator iterates over all children now. Added NewNamedIterator for only named children 2018-12-24 12:56:38 +01:00
LICENSE add license 2019-11-25 14:14:52 +01:00
predicates_test.go Replace testify with stdlib testing in predicates_test.go 2026-04-02 16:50:02 -05:00
README.md Update README for new module path and clean up 2026-04-02 16:51:32 -05:00
test_grammar.go fix test_grammar.go 2021-07-13 18:45:05 -03:00
test_grammar.js replace real grammars with a test on for tests 2019-12-04 17:58:05 +01:00
test_grammar_generate.sh remove additional files and folders created by test_grammar_generate.sh 2021-07-13 18:44:10 -03:00
tree_sitter.c Fix grammar scanner includes for tree_sitter/ layout 2026-04-02 16:43:54 -05:00

go tree-sitter

Go bindings for tree-sitter

Usage

Create a parser with a grammar:

import (
	"context"
	"fmt"

	sitter "git.sharkk.net/go/tree-sitter"
	"git.sharkk.net/go/tree-sitter/javascript"
)

parser := sitter.NewParser()
parser.SetLanguage(javascript.GetLanguage())

Parse some code:

sourceCode := []byte("let a = 1")
tree, _ := parser.ParseCtx(context.Background(), nil, sourceCode)

Inspect the syntax tree:

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

Editing

If your source code changes, you can update the syntax tree. This will take less time than the first parse.

// 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
fmt.Println(n.HasChanges())            // true
fmt.Println(n.Child(0).HasChanges())   // true

// generate new tree
newTree := parser.Parse(tree, newText)

Predicates

You can filter AST by using predicate S-expressions.

Supported predicates:

  • eq?, not-eq?
  • match?, not-match?

Usage example:

func main() {
	// Javascript code
	sourceCode := []byte(`
		const camelCaseConst = 1;
		const SCREAMING_SNAKE_CASE_CONST = 2;
		const lower_snake_case_const = 3;`)
	// Query with predicates
	screamingSnakeCasePattern := `(
		(identifier) @constant
		(#match? @constant "^[A-Z][A-Z_]+")
	)`

	// Parse source code
	lang := javascript.GetLanguage()
	n, _ := sitter.ParseCtx(context.Background(), sourceCode, lang)
	// Execute the query
	q, _ := sitter.NewQuery([]byte(screamingSnakeCasePattern), lang)
	qc := sitter.NewQueryCursor()
	qc.Exec(q, n)
	// Iterate over query results
	for {
		m, ok := qc.NextMatch()
		if !ok {
			break
		}
		// Apply predicates filtering
		m = qc.FilterPredicates(m, sourceCode)
		for _, c := range m.Captures {
			fmt.Println(c.Node.Content(sourceCode))
		}
	}
}

// Output of this program:
// SCREAMING_SNAKE_CASE_CONST

Development

Updating a grammar

Check if any updates for vendored files are available:

go run _automation/main.go check-updates

Update vendor files:

  • open _automation/grammars.json
  • modify reference (for tagged grammars) or revision (for grammars from a branch)
  • run go run _automation/main.go update <grammar-name>

It is also possible to update all grammars in one go using

go run _automation/main.go update-all