Skip to content

Commit e01b171

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

File tree

15 files changed

+687
-1
lines changed

15 files changed

+687
-1
lines changed

.editorconfig

Lines changed: 39 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 94 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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

Lines changed: 11 additions & 0 deletions
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

Lines changed: 13 additions & 0 deletions
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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"Python grammar for tree-sitter"
2+
3+
from ._binding import language
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def language() -> int: ...

0 commit comments

Comments
 (0)