Skip to content

Commit

Permalink
Merge pull request #146 from jochil/groovy-support
Browse files Browse the repository at this point in the history
feat: add Groovy support
  • Loading branch information
smacker authored Feb 14, 2024
2 parents 746cc92 + 2315621 commit 1f283e2
Show file tree
Hide file tree
Showing 6 changed files with 183,344 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 @@ -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

0 comments on commit 1f283e2

Please sign in to comment.