Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Groovy support #146

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions _automation/grammars.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@
"reference": "master",
"revision": "64457ea6b73ef5422ed1687178d4545c3e91334a"
},
{
"language": "groovy",
"url": "https://github.com/murtaza64/tree-sitter-groovy",
"files": [
"parser.c",
"scanner.c"
],
"reference": "main",
"revision": "235009aad0f580211fc12014bb0846c3910130c1"
},
{
"language": "hcl",
"url": "https://github.com/MichaHoffmann/tree-sitter-hcl",
Expand Down
15 changes: 15 additions & 0 deletions groovy/binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package groovy

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

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

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

import (
"context"
"testing"

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

var gradleGroovyCode = `
plugins {
id 'application'
id 'foo'
}
`

var testCases = []struct {
name string
input string
expected string
}{
{
name: "hello-world",
input: `
class Example {
static void main(String[] args) {
println('Hello World');
}
}`,
expected: "(source_file (class_definition name: (identifier) body: (closure (function_definition (modifier) type: (builtintype) function: (identifier) parameters: (parameter_list (parameter type: (array_type (identifier)) name: (identifier))) body: (closure (function_call function: (identifier) args: (argument_list (string (string_content)))))))))",
},
{
name: "gradle",
input: `
plugins {
id 'application'
}

repositories {
mavenCentral()
}

application {
mainClass = 'example.App'
}
`,
expected: "(source_file (juxt_function_call function: (identifier) args: (argument_list (closure (juxt_function_call function: (identifier) args: (argument_list (string (string_content))))))) (juxt_function_call function: (identifier) args: (argument_list (closure (function_call function: (identifier) args: (argument_list))))) (juxt_function_call function: (identifier) args: (argument_list (closure (assignment (identifier) (string (string_content)))))))",
},
}

func TestGrammar(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
n, err := sitter.ParseCtx(context.Background(), []byte(tc.input), groovy.GetLanguage())
assert.Nil(t, err)
assert.Equal(t, tc.expected, n.String())
})
}
}
Loading