Skip to content

Commit

Permalink
feat: add starlark binding
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Feb 14, 2024
1 parent 1f283e2 commit ad88e90
Show file tree
Hide file tree
Showing 6 changed files with 92,951 additions and 0 deletions.
10 changes: 10 additions & 0 deletions _automation/grammars.json
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,16 @@
"reference": "v0.19.1",
"revision": "918f0fb948405181707a1772cab639f2d278d384"
},
{
"language": "starlark",
"url": "https://github.com/tree-sitter-grammars/tree-sitter-starlark",
"files": [
"parser.c",
"scanner.c"
],
"reference": "v1.0.0",
"revision": "b31a616aac5d05f927f3f9dd809789db7805b632"
},
{
"language": "svelte",
"url": "https://github.com/Himujjal/tree-sitter-svelte",
Expand Down
15 changes: 15 additions & 0 deletions starlark/binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package starlark

//#include "parser.h"
//TSLanguage *tree_sitter_starlark();
import "C"
import (
"unsafe"

sitter "github.com/smacker/go-tree-sitter"
)

func GetLanguage() *sitter.Language {
ptr := unsafe.Pointer(C.tree_sitter_starlark())
return sitter.NewLanguage(ptr)
}
25 changes: 25 additions & 0 deletions starlark/binding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package starlark_test

import (
"context"
"testing"

sitter "github.com/smacker/go-tree-sitter"
"github.com/smacker/go-tree-sitter/starlark"
"github.com/stretchr/testify/assert"
)

func TestGrammar(t *testing.T) {
assert := assert.New(t)

code := `def myfunc(a):
pass
`

n, err := sitter.ParseCtx(context.Background(), []byte(code), starlark.GetLanguage())
assert.NoError(err)
assert.Equal(
"(module (function_definition name: (identifier) parameters: (parameters (identifier)) body: (block (pass_statement))))",
n.String(),
)
}
Loading

0 comments on commit ad88e90

Please sign in to comment.