Bindings for the tree-sitter library with some embedded grammars.
- C 99.9%
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 |
||
|---|---|---|
| _automation | ||
| _examples | ||
| bash | ||
| c | ||
| cpp | ||
| csharp | ||
| css | ||
| cue | ||
| dockerfile | ||
| elixir | ||
| elm | ||
| golang | ||
| groovy | ||
| hcl | ||
| html | ||
| java | ||
| javascript | ||
| kotlin | ||
| lua | ||
| markdown | ||
| ocaml | ||
| php | ||
| protobuf | ||
| python | ||
| ruby | ||
| rust | ||
| scala | ||
| sql | ||
| svelte | ||
| swift | ||
| toml | ||
| tree_sitter | ||
| typescript | ||
| yaml | ||
| .gitignore | ||
| benchmark_test.go | ||
| bindings.c | ||
| bindings.go | ||
| bindings.h | ||
| bindings_test.go | ||
| example_test.go | ||
| go.mod | ||
| go.sum | ||
| iter.go | ||
| LICENSE | ||
| predicates_test.go | ||
| README.md | ||
| test_grammar.go | ||
| test_grammar.js | ||
| test_grammar_generate.sh | ||
| tree_sitter.c | ||
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) orrevision(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