-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add C and Go bindings, update swift
- Loading branch information
Showing
10 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
VERSION := 0.0.1 | ||
|
||
# Repository | ||
SRC_DIR := src | ||
|
||
PARSER_REPO_URL := $(shell git -C $(SRC_DIR) remote get-url origin ) | ||
|
||
ifeq (, $(capnp)) | ||
capnp := $(shell basename $(PARSER_REPO_URL)) | ||
capnp := $(subst tree-sitter-,,$(capnp)) | ||
capnp := $(subst .git,,$(capnp)) | ||
endif | ||
|
||
ifeq (, $(PARSER_URL)) | ||
PARSER_URL := $(subst :,/,$(PARSER_REPO_URL)) | ||
PARSER_URL := $(subst git@,https://,$(PARSER_URL)) | ||
PARSER_URL := $(subst .git,,$(PARSER_URL)) | ||
endif | ||
|
||
CAPNP := $(shell echo $(capnp) | tr a-z A-Z ) | ||
|
||
# install directory layout | ||
PREFIX ?= /usr/local | ||
INCLUDEDIR ?= $(PREFIX)/include | ||
LIBDIR ?= $(PREFIX)/lib | ||
PCLIBDIR ?= $(LIBDIR)/pkgconfig | ||
|
||
# collect C++ sources, and link if necessary | ||
CPPSRC := $(wildcard $(SRC_DIR)/*.cc) | ||
|
||
ifeq (, $(CPPSRC)) | ||
ADDITIONALLIBS := | ||
else | ||
ADDITIONALLIBS := -lc++ | ||
endif | ||
|
||
# collect sources | ||
SRC := $(wildcard $(SRC_DIR)/*.c) | ||
SRC += $(CPPSRC) | ||
OBJ := $(addsuffix .o,$(basename $(SRC))) | ||
|
||
# ABI versioning | ||
SONAME_MAJOR := 0 | ||
SONAME_MINOR := 0 | ||
|
||
CFLAGS ?= -O3 -Wall -Wextra -I$(SRC_DIR) | ||
CXXFLAGS ?= -O3 -Wall -Wextra -I$(SRC_DIR) | ||
override CFLAGS += -std=gnu99 -fPIC | ||
override CXXFLAGS += -fPIC | ||
|
||
# OS-specific bits | ||
ifeq ($(shell uname),Darwin) | ||
SOEXT = dylib | ||
SOEXTVER_MAJOR = $(SONAME_MAJOR).dylib | ||
SOEXTVER = $(SONAME_MAJOR).$(SONAME_MINOR).dylib | ||
LINKSHARED := $(LINKSHARED)-dynamiclib -Wl, | ||
ifneq ($(ADDITIONALLIBS),) | ||
LINKSHARED := $(LINKSHARED)$(ADDITIONALLIBS), | ||
endif | ||
LINKSHARED := $(LINKSHARED)-install_name,$(LIBDIR)/libtree-sitter-$(capnp).$(SONAME_MAJOR).dylib,-rpath,@executable_path/../Frameworks | ||
else | ||
SOEXT = so | ||
SOEXTVER_MAJOR = so.$(SONAME_MAJOR) | ||
SOEXTVER = so.$(SONAME_MAJOR).$(SONAME_MINOR) | ||
LINKSHARED := $(LINKSHARED)-shared -Wl, | ||
ifneq ($(ADDITIONALLIBS),) | ||
LINKSHARED := $(LINKSHARED)$(ADDITIONALLIBS) | ||
endif | ||
LINKSHARED := $(LINKSHARED)-soname,libtree-sitter-$(capnp).so.$(SONAME_MAJOR) | ||
endif | ||
ifneq (,$(filter $(shell uname),FreeBSD NetBSD DragonFly)) | ||
PCLIBDIR := $(PREFIX)/libdata/pkgconfig | ||
endif | ||
|
||
all: libtree-sitter-$(capnp).a libtree-sitter-$(capnp).$(SOEXTVER) bindings/c/$(capnp).h bindings/c/tree-sitter-$(capnp).pc | ||
|
||
libtree-sitter-$(capnp).a: $(OBJ) | ||
$(AR) rcs $@ $^ | ||
|
||
libtree-sitter-$(capnp).$(SOEXTVER): $(OBJ) | ||
$(CC) $(LDFLAGS) $(LINKSHARED) $^ $(LDLIBS) -o $@ | ||
ln -sf $@ libtree-sitter-$(capnp).$(SOEXT) | ||
ln -sf $@ libtree-sitter-$(capnp).$(SOEXTVER_MAJOR) | ||
|
||
bindings/c/$(capnp).h: | ||
sed -e 's|@UPPER_PARSERNAME@|$(CAPNP)|' \ | ||
-e 's|@PARSERNAME@|$(capnp)|' \ | ||
bindings/c/tree-sitter.h.in > $@ | ||
|
||
bindings/c/tree-sitter-$(capnp).pc: | ||
sed -e 's|@LIBDIR@|$(LIBDIR)|;s|@INCLUDEDIR@|$(INCLUDEDIR)|;s|@VERSION@|$(VERSION)|' \ | ||
-e 's|=$(PREFIX)|=$${prefix}|' \ | ||
-e 's|@PREFIX@|$(PREFIX)|' \ | ||
-e 's|@ADDITIONALLIBS@|$(ADDITIONALLIBS)|' \ | ||
-e 's|@PARSERNAME@|$(capnp)|' \ | ||
-e 's|@PARSERURL@|$(PARSER_URL)|' \ | ||
bindings/c/tree-sitter.pc.in > $@ | ||
|
||
install: all | ||
install -d '$(DESTDIR)$(LIBDIR)' | ||
install -m755 libtree-sitter-$(capnp).a '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(capnp).a | ||
install -m755 libtree-sitter-$(capnp).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(capnp).$(SOEXTVER) | ||
ln -sf libtree-sitter-$(capnp).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(capnp).$(SOEXTVER_MAJOR) | ||
ln -sf libtree-sitter-$(capnp).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(capnp).$(SOEXT) | ||
install -d '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter | ||
install -m644 bindings/c/$(capnp).h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/ | ||
install -d '$(DESTDIR)$(PCLIBDIR)' | ||
install -m644 bindings/c/tree-sitter-$(capnp).pc '$(DESTDIR)$(PCLIBDIR)'/ | ||
|
||
clean: | ||
rm -f $(OBJ) libtree-sitter-$(capnp).a libtree-sitter-$(capnp).$(SOEXT) libtree-sitter-$(capnp).$(SOEXTVER_MAJOR) libtree-sitter-$(capnp).$(SOEXTVER) | ||
rm -f bindings/c/$(capnp).h bindings/c/tree-sitter-$(capnp).pc | ||
|
||
.PHONY: all install clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef TREE_SITTER_@UPPER_PARSERNAME@_H_ | ||
#define TREE_SITTER_@UPPER_PARSERNAME@_H_ | ||
|
||
#include <tree_sitter/parser.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
extern TSLanguage *tree_sitter_@PARSERNAME@(); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // TREE_SITTER_@UPPER_PARSERNAME@_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
prefix=@PREFIX@ | ||
libdir=@LIBDIR@ | ||
includedir=@INCLUDEDIR@ | ||
additionallibs=@ADDITIONALLIBS@ | ||
|
||
Name: tree-sitter-@PARSERNAME@ | ||
Description: @PARSERNAME@ grammar for tree-sitter | ||
URL: @PARSERURL@ | ||
Version: @VERSION@ | ||
Libs: -L${libdir} ${additionallibs} -ltree-sitter-@PARSERNAME@ | ||
Cflags: -I${includedir} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package tree_sitter_capnp | ||
|
||
// #cgo CFLAGS: -O3 -Wall -Wextra -std=gnu99 -fPIC | ||
// #include "../../src/tree_sitter/parser.h" | ||
// #include "../../src/parser.c" | ||
// // if you have a scanner, you need to include it here too | ||
import "C" | ||
|
||
import ( | ||
"unsafe" | ||
|
||
tree_sitter "github.com/smacker/go-tree-sitter" | ||
) | ||
|
||
func GetLanguage() *tree_sitter.Language { | ||
ptr := unsafe.Pointer(C.tree_sitter_capnp()) | ||
return tree_sitter.NewLanguage(ptr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package tree_sitter_capnp_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/amaanq/tree-sitter-capnp" | ||
) | ||
|
||
func TestCanLoadGrammar(t *testing.T) { | ||
parser := tree_sitter_capnp.GetLanguage() | ||
if parser == nil { | ||
t.Errorf("Error loading Capnp grammar") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/amaanq/tree-sitter-capnp | ||
|
||
go 1.20 | ||
|
||
require github.com/smacker/go-tree-sitter v0.0.0-20230720070738-0d0a9f78d8f8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/smacker/go-tree-sitter v0.0.0-20230720070738-0d0a9f78d8f8 h1:DxgjlvWYsb80WEN2Zv3WqJFAg2DKjUQJO6URGdf1x6Y= | ||
github.com/smacker/go-tree-sitter v0.0.0-20230720070738-0d0a9f78d8f8/go.mod h1:q99oHDsbP0xRwmn7Vmob8gbSMNyvJ83OauXPSuHQuKE= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= | ||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,10 @@ | |
], | ||
"highlights": [ | ||
"queries/highlights.scm" | ||
], | ||
"bindings": [ | ||
"go", | ||
"swift" | ||
] | ||
} | ||
] | ||
|