Skip to content

dougthor42/go-tree-sitter

This branch is up to date with smacker/go-tree-sitter:master.

Folders and files

NameName
Last commit message
Last commit date
Aug 27, 2024
Aug 27, 2024
Feb 23, 2023
Apr 22, 2024
Aug 25, 2024
Aug 25, 2024
Aug 25, 2024
Aug 25, 2024
Apr 22, 2024
Aug 26, 2024
Apr 22, 2024
Apr 22, 2024
Aug 26, 2024
Aug 25, 2024
Aug 25, 2024
Aug 25, 2024
Apr 22, 2024
Aug 25, 2024
Aug 26, 2024
Apr 22, 2024
Jun 14, 2024
Aug 26, 2024
Aug 25, 2024
Nov 14, 2021
Aug 26, 2024
Aug 25, 2024
Aug 25, 2024
Aug 25, 2024
Aug 25, 2024
Apr 22, 2024
Aug 25, 2024
Nov 14, 2021
Aug 26, 2024
Mar 13, 2022
Feb 8, 2024
Nov 25, 2019
Oct 11, 2019
Feb 7, 2024
Apr 20, 2024
Apr 20, 2024
Apr 20, 2024
Apr 20, 2024
Jan 30, 2024
Feb 5, 2020
Jun 14, 2024
Feb 5, 2020
Apr 23, 2023
Dec 1, 2020
Dec 17, 2023
Nov 25, 2019
Mar 24, 2022
Jan 30, 2024
Nov 25, 2019
Aug 26, 2024
Aug 25, 2024
Oct 23, 2022
Dec 24, 2018
Apr 20, 2024
Apr 20, 2024
Oct 23, 2022
Jan 30, 2024
Apr 20, 2024
Apr 20, 2024
Apr 20, 2024
Apr 20, 2024
Oct 23, 2022
May 28, 2023
Nov 25, 2019
Apr 20, 2024
Nov 25, 2019
Dec 1, 2020
Apr 20, 2024
Oct 23, 2022
Apr 20, 2024
Apr 20, 2024
Jul 13, 2021
Dec 4, 2019
Jul 13, 2021
Apr 20, 2024
Dec 17, 2023
Apr 20, 2024
Apr 20, 2024
Nov 25, 2019
Nov 25, 2019
Nov 25, 2019
Nov 25, 2019
Nov 25, 2019
Nov 25, 2019
Apr 20, 2024
Apr 20, 2024

Repository files navigation

go tree-sitter

Build Status GoDoc

Golang bindings for tree-sitter

Usage

Create a parser with a grammar:

import (
	"context"
	"fmt"

	sitter "github.com/smacker/go-tree-sitter"
	"github.com/smacker/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

Custom grammars

This repository provides grammars for many common languages out of the box.

But if you need support for any other language you can keep it inside your own project or publish it as a separate repository to share with the community.

See explanation on how to create a grammar for go-tree-sitter here.

Known external grammars:

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
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.Parse(tree, newText)

Predicates

You can filter AST by using predicate S-expressions.

Similar to Rust or WebAssembly bindings we support filtering on a few common 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

Packages

No packages published

Languages

  • C 99.9%
  • Other 0.1%