Skip to content

Commit e01b171

Browse files
committed
feat: improve bindings
1 parent 94b477e commit e01b171

15 files changed

+687
-1
lines changed

.editorconfig

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
trim_trailing_whitespace = true
8+
9+
[*.{json,toml,yml,gyp}]
10+
indent_style = space
11+
indent_size = 2
12+
13+
[*.js]
14+
indent_style = space
15+
indent_size = 2
16+
17+
[*.rs]
18+
indent_style = space
19+
indent_size = 4
20+
21+
[*.{c,cc,h}]
22+
indent_style = space
23+
indent_size = 4
24+
25+
[*.{py,pyi}]
26+
indent_style = space
27+
indent_size = 4
28+
29+
[*.swift]
30+
indent_style = space
31+
indent_size = 4
32+
33+
[*.go]
34+
indent_style = tab
35+
indent_size = 8
36+
37+
[Makefile]
38+
indent_style = tab
39+
indent_size = 8

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ include = ["bindings/rust/*", "grammar.js", "queries/*", "src/*"]
2121
path = "bindings/rust/lib.rs"
2222

2323
[dependencies]
24-
tree-sitter = "~0.20.10"
24+
tree-sitter = ">=0.20.10"
2525

2626
[build-dependencies]
2727
cc = "~1.0"

Makefile

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
VERSION := 0.0.1
2+
3+
LANGUAGE_NAME := tree-sitter-python
4+
5+
# repository
6+
SRC_DIR := src
7+
8+
PARSER_REPO_URL := $(shell git -C $(SRC_DIR) remote get-url origin 2>/dev/null)
9+
10+
ifeq ($(PARSER_URL),)
11+
PARSER_URL := $(subst .git,,$(PARSER_REPO_URL))
12+
ifeq ($(shell echo $(PARSER_URL) | grep '^[a-z][-+.0-9a-z]*://'),)
13+
PARSER_URL := $(subst :,/,$(PARSER_URL))
14+
PARSER_URL := $(subst git@,https://,$(PARSER_URL))
15+
endif
16+
endif
17+
18+
# ABI versioning
19+
SONAME_MAJOR := $(word 1,$(subst ., ,$(VERSION)))
20+
SONAME_MINOR := $(word 2,$(subst ., ,$(VERSION)))
21+
22+
# install directory layout
23+
PREFIX ?= /usr/local
24+
INCLUDEDIR ?= $(PREFIX)/include
25+
LIBDIR ?= $(PREFIX)/lib
26+
PCLIBDIR ?= $(LIBDIR)/pkgconfig
27+
28+
# object files
29+
OBJS := $(patsubst %.c,%.o,$(wildcard $(SRC_DIR)/*.c))
30+
31+
# flags
32+
ARFLAGS := rcs
33+
override CFLAGS += -I$(SRC_DIR) -std=c11
34+
35+
# OS-specific bits
36+
ifeq ($(shell uname),Darwin)
37+
SOEXT = dylib
38+
SOEXTVER_MAJOR = $(SONAME_MAJOR).dylib
39+
SOEXTVER = $(SONAME_MAJOR).$(SONAME_MINOR).dylib
40+
LINKSHARED := $(LINKSHARED)-dynamiclib -Wl,
41+
ifneq ($(ADDITIONAL_LIBS),)
42+
LINKSHARED := $(LINKSHARED)$(ADDITIONAL_LIBS),
43+
endif
44+
LINKSHARED := $(LINKSHARED)-install_name,$(LIBDIR)/lib$(LANGUAGE_NAME).$(SONAME_MAJOR).dylib,-rpath,@executable_path/../Frameworks
45+
else ifneq ($(filter $(shell uname),Linux FreeBSD NetBSD DragonFly),)
46+
SOEXT = so
47+
SOEXTVER_MAJOR = so.$(SONAME_MAJOR)
48+
SOEXTVER = so.$(SONAME_MAJOR).$(SONAME_MINOR)
49+
LINKSHARED := $(LINKSHARED)-shared -Wl,
50+
ifneq ($(ADDITIONAL_LIBS),)
51+
LINKSHARED := $(LINKSHARED)$(ADDITIONAL_LIBS)
52+
endif
53+
LINKSHARED := $(LINKSHARED)-soname,lib$(LANGUAGE_NAME).so.$(SONAME_MAJOR)
54+
else ifeq ($(OS),Windows_NT)
55+
$(error "Windows is not supported")
56+
endif
57+
ifneq ($(filter $(shell uname),FreeBSD NetBSD DragonFly),)
58+
PCLIBDIR := $(PREFIX)/libdata/pkgconfig
59+
endif
60+
61+
all: lib$(LANGUAGE_NAME).a lib$(LANGUAGE_NAME).$(SOEXT) $(LANGUAGE_NAME).pc
62+
63+
$(SRC_DIR)/%.o: $(SRC_DIR)/%.c
64+
$(CC) -c $^ -o $@
65+
66+
lib$(LANGUAGE_NAME).a: $(OBJS)
67+
$(AR) $(ARFLAGS) $@ $^
68+
69+
lib$(LANGUAGE_NAME).$(SOEXT): $(OBJS)
70+
$(CC) -fPIC $(LDFLAGS) $(LINKSHARED) $^ $(LDLIBS) -o $@
71+
72+
$(LANGUAGE_NAME).pc:
73+
sed > $@ bindings/c/$(LANGUAGE_NAME).pc.in \
74+
-e 's|@URL@|$(PARSER_URL)|' \
75+
-e 's|@VERSION@|$(VERSION)|' \
76+
-e 's|@LIBDIR@|$(LIBDIR)|;' \
77+
-e 's|@INCLUDEDIR@|$(INCLUDEDIR)|;' \
78+
-e 's|=$(PREFIX)|=$${prefix}|' \
79+
-e 's|@PREFIX@|$(PREFIX)|' \
80+
-e 's|@REQUIRES@|$(REQUIRES)|' \
81+
-e 's|@ADDITIONAL_LIBS@|$(ADDITIONAL_LIBS)|'
82+
83+
install: all
84+
install -Dm644 bindings/c/$(LANGUAGE_NAME).h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/$(LANGUAGE_NAME).h
85+
install -Dm644 $(LANGUAGE_NAME).pc '$(DESTDIR)$(PCLIBDIR)'/$(LANGUAGE_NAME).pc
86+
install -Dm755 lib$(LANGUAGE_NAME).a '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).a
87+
install -Dm755 lib$(LANGUAGE_NAME).$(SOEXT) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER)
88+
ln -sf lib$(LANGUAGE_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR)
89+
ln -sf lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXT)
90+
91+
clean:
92+
$(RM) $(OBJS) $(LANGUAGE_NAME).pc lib$(LANGUAGE_NAME).a lib$(LANGUAGE_NAME).$(SOEXT)
93+
94+
.PHONY: all install clean

bindings/c/tree-sitter-python.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef TREE_SITTER_PYTHON_H_
2+
#define TREE_SITTER_PYTHON_H_
3+
4+
typedef struct TSLanguage TSLanguage;
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
extern const TSLanguage *tree_sitter_python(void);
11+
12+
#ifdef __cplusplus
13+
}
14+
#endif
15+
16+
#endif // TREE_SITTER_PYTHON_H_

bindings/c/tree-sitter-python.pc.in

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
prefix=@PREFIX@
2+
libdir=@LIBDIR@
3+
includedir=@INCLUDEDIR@
4+
5+
Name: tree-sitter-python
6+
Description: python grammar for tree-sitter
7+
URL: @URL@
8+
Version: @VERSION@
9+
Requires: @REQUIRES@
10+
Libs: -L${libdir} @ADDITIONAL_LIBS@ -ltree-sitter-python
11+
Cflags: -I${includedir}

bindings/go/binding.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package tree_sitter_python
2+
3+
// #cgo CFLAGS: -std=c11 -fPIC
4+
// #include "../../src/parser.c"
5+
// // NOTE: if your language has an external scanner, add it here.
6+
import "C"
7+
8+
import "unsafe"
9+
10+
// Get the tree-sitter Language for this grammar.
11+
func Language() unsafe.Pointer {
12+
return unsafe.Pointer(C.tree_sitter_python())
13+
}

bindings/go/binding_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package tree_sitter_python_test
2+
3+
import (
4+
"testing"
5+
6+
tree_sitter "github.com/smacker/go-tree-sitter"
7+
"github.com/tree-sitter/tree-sitter-python"
8+
)
9+
10+
func TestCanLoadGrammar(t *testing.T) {
11+
language := tree_sitter.NewLanguage(tree_sitter_python.Language())
12+
if language == nil {
13+
t.Errorf("Error loading Python grammar")
14+
}
15+
}

bindings/go/go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/tree-sitter/tree-sitter-python
2+
3+
go 1.22
4+
5+
require github.com/smacker/go-tree-sitter v0.0.0-20230720070738-0d0a9f78d8f8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"Python grammar for tree-sitter"
2+
3+
from ._binding import language
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def language() -> int: ...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <Python.h>
2+
3+
typedef struct TSLanguage TSLanguage;
4+
5+
extern const TSLanguage *tree_sitter_python(void);
6+
7+
static PyObject* _binding_language(PyObject *self, PyObject *args) {
8+
return PyLong_FromVoidPtr((void *)tree_sitter_python());
9+
}
10+
11+
static PyMethodDef methods[] = {
12+
{"language", _binding_language, METH_NOARGS,
13+
"Get the tree-sitter language for this grammar."},
14+
{NULL, NULL, 0, NULL}
15+
};
16+
17+
static struct PyModuleDef module = {
18+
.m_base = PyModuleDef_HEAD_INIT,
19+
.m_name = "_binding",
20+
.m_doc = NULL,
21+
.m_size = -1,
22+
.m_methods = methods
23+
};
24+
25+
PyMODINIT_FUNC PyInit__binding(void) {
26+
return PyModule_Create(&module);
27+
}

bindings/python/tree_sitter_python/py.typed

Whitespace-only changes.

pyproject.toml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[build-system]
2+
requires = ["setuptools>=42", "wheel"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "tree-sitter-python"
7+
description = "Python grammar for tree-sitter"
8+
version = "0.0.1"
9+
keywords = ["parsing", "incremental", "python"]
10+
classifiers = [
11+
"Development Status :: 4 - Beta",
12+
"Intended Audience :: Developers",
13+
"License :: OSI Approved :: MIT License",
14+
"Topic :: Software Development :: Compilers",
15+
"Topic :: Text Processing :: Linguistic",
16+
]
17+
requires-python = ">=3.8"
18+
license.file = "LICENSE"
19+
readme = "README.md"
20+
21+
[project.optional-dependencies]
22+
core = ["tree-sitter~=0.21"]
23+
24+
[tool.cibuildwheel]
25+
build = "cp38-*"
26+
build-frontend = "build"

setup.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from os.path import isdir, join
2+
from platform import system
3+
4+
from setuptools import Extension, find_packages, setup
5+
from setuptools.command.build import build
6+
from wheel.bdist_wheel import bdist_wheel
7+
8+
9+
class Build(build):
10+
def run(self):
11+
if isdir("queries"):
12+
dest = join(self.build_lib, "tree_sitter_python", "queries")
13+
self.copy_tree("queries", dest)
14+
super().run()
15+
16+
17+
class BdistWheel(bdist_wheel):
18+
def get_tag(self):
19+
python, abi, platform = super().get_tag()
20+
if python.startswith("cp"):
21+
python, abi = "cp38", "abi3"
22+
return python, abi, platform
23+
24+
25+
setup(
26+
packages=find_packages("bindings/python"),
27+
package_dir={"": "bindings/python"},
28+
package_data={
29+
"tree_sitter_python": ["*.pyi", "py.typed"],
30+
"tree_sitter_python.queries": ["*.scm"],
31+
},
32+
ext_package="tree_sitter_python",
33+
ext_modules=[
34+
Extension(
35+
name="_binding",
36+
sources=[
37+
"bindings/python/tree_sitter_python/binding.c",
38+
"src/parser.c",
39+
"src/scanner.c",
40+
],
41+
extra_compile_args=(
42+
["-std=c11"] if system() != 'Windows' else []
43+
),
44+
define_macros=[
45+
("Py_LIMITED_API", "0x03080000"),
46+
("PY_SSIZE_T_CLEAN", None)
47+
],
48+
include_dirs=["src"],
49+
py_limited_api=True,
50+
)
51+
],
52+
cmdclass={
53+
"build": Build,
54+
"bdist_wheel": BdistWheel
55+
},
56+
zip_safe=False,
57+
)

0 commit comments

Comments
 (0)