feat: enable go & python bindings
This commit is contained in:
15
bindings/go/binding.go
generated
Normal file
15
bindings/go/binding.go
generated
Normal file
@@ -0,0 +1,15 @@
|
||||
package tree_sitter_hcl
|
||||
|
||||
// #cgo CFLAGS: -std=c11 -fPIC
|
||||
// #include "../../src/parser.c"
|
||||
// #if __has_include("../../src/scanner.c")
|
||||
// #include "../../src/scanner.c"
|
||||
// #endif
|
||||
import "C"
|
||||
|
||||
import "unsafe"
|
||||
|
||||
// Get the tree-sitter Language for this grammar.
|
||||
func Language() unsafe.Pointer {
|
||||
return unsafe.Pointer(C.tree_sitter_hcl())
|
||||
}
|
||||
15
bindings/go/binding_test.go
generated
Normal file
15
bindings/go/binding_test.go
generated
Normal file
@@ -0,0 +1,15 @@
|
||||
package tree_sitter_hcl_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
tree_sitter "github.com/tree-sitter/go-tree-sitter"
|
||||
tree_sitter_hcl "github.com/tree-sitter-grammars/tree-sitter-hcl/bindings/go"
|
||||
)
|
||||
|
||||
func TestCanLoadGrammar(t *testing.T) {
|
||||
language := tree_sitter.NewLanguage(tree_sitter_hcl.Language())
|
||||
if language == nil {
|
||||
t.Errorf("Error loading HCL grammar")
|
||||
}
|
||||
}
|
||||
12
bindings/python/tests/test_binding.py
generated
Normal file
12
bindings/python/tests/test_binding.py
generated
Normal file
@@ -0,0 +1,12 @@
|
||||
from unittest import TestCase
|
||||
|
||||
import tree_sitter
|
||||
import tree_sitter_hcl
|
||||
|
||||
|
||||
class TestLanguage(TestCase):
|
||||
def test_can_load_grammar(self):
|
||||
try:
|
||||
tree_sitter.Language(tree_sitter_hcl.language())
|
||||
except Exception:
|
||||
self.fail("Error loading HCL grammar")
|
||||
5
bindings/python/tree_sitter_hcl/__init__.py
generated
Normal file
5
bindings/python/tree_sitter_hcl/__init__.py
generated
Normal file
@@ -0,0 +1,5 @@
|
||||
"""HCL and terraform grammar for tree-sitter"""
|
||||
|
||||
from ._binding import language
|
||||
|
||||
__all__ = ["language"]
|
||||
1
bindings/python/tree_sitter_hcl/__init__.pyi
generated
Normal file
1
bindings/python/tree_sitter_hcl/__init__.pyi
generated
Normal file
@@ -0,0 +1 @@
|
||||
def language() -> object: ...
|
||||
35
bindings/python/tree_sitter_hcl/binding.c
generated
Normal file
35
bindings/python/tree_sitter_hcl/binding.c
generated
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <Python.h>
|
||||
|
||||
typedef struct TSLanguage TSLanguage;
|
||||
|
||||
TSLanguage *tree_sitter_hcl(void);
|
||||
|
||||
static PyObject* _binding_language(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) {
|
||||
return PyCapsule_New(tree_sitter_hcl(), "tree_sitter.Language", NULL);
|
||||
}
|
||||
|
||||
static struct PyModuleDef_Slot slots[] = {
|
||||
#ifdef Py_GIL_DISABLED
|
||||
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
|
||||
#endif
|
||||
{0, NULL}
|
||||
};
|
||||
|
||||
static PyMethodDef methods[] = {
|
||||
{"language", _binding_language, METH_NOARGS,
|
||||
"Get the tree-sitter language for this grammar."},
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
|
||||
static struct PyModuleDef module = {
|
||||
.m_base = PyModuleDef_HEAD_INIT,
|
||||
.m_name = "_binding",
|
||||
.m_doc = NULL,
|
||||
.m_size = 0,
|
||||
.m_methods = methods,
|
||||
.m_slots = slots,
|
||||
};
|
||||
|
||||
PyMODINIT_FUNC PyInit__binding(void) {
|
||||
return PyModuleDef_Init(&module);
|
||||
}
|
||||
0
bindings/python/tree_sitter_hcl/py.typed
generated
Normal file
0
bindings/python/tree_sitter_hcl/py.typed
generated
Normal file
Reference in New Issue
Block a user