Skip to content

Commit e1384e2

Browse files
committed
build: update bindings
1 parent 2146294 commit e1384e2

21 files changed

+429
-52
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

Makefile

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
VERSION := 0.20.0
2+
3+
LANGUAGE_NAME := tree-sitter-c_sharp
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+
TS ?= tree-sitter
19+
20+
# ABI versioning
21+
SONAME_MAJOR := $(word 1,$(subst ., ,$(VERSION)))
22+
SONAME_MINOR := $(word 2,$(subst ., ,$(VERSION)))
23+
24+
# install directory layout
25+
PREFIX ?= /usr/local
26+
INCLUDEDIR ?= $(PREFIX)/include
27+
LIBDIR ?= $(PREFIX)/lib
28+
PCLIBDIR ?= $(LIBDIR)/pkgconfig
29+
30+
# source/object files
31+
PARSER := $(SRC_DIR)/parser.c
32+
EXTRAS := $(filter-out $(PARSER),$(wildcard $(SRC_DIR)/*.c))
33+
OBJS := $(patsubst %.c,%.o,$(PARSER) $(EXTRAS))
34+
35+
# flags
36+
ARFLAGS ?= rcs
37+
override CFLAGS += -I$(SRC_DIR) -std=c11 -fPIC
38+
39+
# OS-specific bits
40+
ifeq ($(OS),Windows_NT)
41+
$(error "Windows is not supported")
42+
else ifeq ($(shell uname),Darwin)
43+
SOEXT = dylib
44+
SOEXTVER_MAJOR = $(SONAME_MAJOR).dylib
45+
SOEXTVER = $(SONAME_MAJOR).$(SONAME_MINOR).dylib
46+
LINKSHARED := $(LINKSHARED)-dynamiclib -Wl,
47+
ifneq ($(ADDITIONAL_LIBS),)
48+
LINKSHARED := $(LINKSHARED)$(ADDITIONAL_LIBS),
49+
endif
50+
LINKSHARED := $(LINKSHARED)-install_name,$(LIBDIR)/lib$(LANGUAGE_NAME).$(SONAME_MAJOR).dylib,-rpath,@executable_path/../Frameworks
51+
else
52+
SOEXT = so
53+
SOEXTVER_MAJOR = so.$(SONAME_MAJOR)
54+
SOEXTVER = so.$(SONAME_MAJOR).$(SONAME_MINOR)
55+
LINKSHARED := $(LINKSHARED)-shared -Wl,
56+
ifneq ($(ADDITIONAL_LIBS),)
57+
LINKSHARED := $(LINKSHARED)$(ADDITIONAL_LIBS)
58+
endif
59+
LINKSHARED := $(LINKSHARED)-soname,lib$(LANGUAGE_NAME).so.$(SONAME_MAJOR)
60+
endif
61+
ifneq ($(filter $(shell uname),FreeBSD NetBSD DragonFly),)
62+
PCLIBDIR := $(PREFIX)/libdata/pkgconfig
63+
endif
64+
65+
all: lib$(LANGUAGE_NAME).a lib$(LANGUAGE_NAME).$(SOEXT) $(LANGUAGE_NAME).pc
66+
67+
lib$(LANGUAGE_NAME).a: $(OBJS)
68+
$(AR) $(ARFLAGS) $@ $^
69+
70+
lib$(LANGUAGE_NAME).$(SOEXT): $(OBJS)
71+
$(CC) $(LDFLAGS) $(LINKSHARED) $^ $(LDLIBS) -o $@
72+
ifneq ($(STRIP),)
73+
$(STRIP) $@
74+
endif
75+
76+
$(LANGUAGE_NAME).pc: bindings/c/$(LANGUAGE_NAME).pc.in
77+
sed -e 's|@URL@|$(PARSER_URL)|' \
78+
-e 's|@VERSION@|$(VERSION)|' \
79+
-e 's|@LIBDIR@|$(LIBDIR)|' \
80+
-e 's|@INCLUDEDIR@|$(INCLUDEDIR)|' \
81+
-e 's|@REQUIRES@|$(REQUIRES)|' \
82+
-e 's|@ADDITIONAL_LIBS@|$(ADDITIONAL_LIBS)|' \
83+
-e 's|=$(PREFIX)|=$${prefix}|' \
84+
-e 's|@PREFIX@|$(PREFIX)|' $< > $@
85+
86+
$(PARSER): $(SRC_DIR)/grammar.json
87+
$(TS) generate --no-bindings $^
88+
89+
install: all
90+
install -d '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter '$(DESTDIR)$(PCLIBDIR)' '$(DESTDIR)$(LIBDIR)'
91+
install -m644 bindings/c/$(LANGUAGE_NAME).h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/$(LANGUAGE_NAME).h
92+
install -m644 $(LANGUAGE_NAME).pc '$(DESTDIR)$(PCLIBDIR)'/$(LANGUAGE_NAME).pc
93+
install -m644 lib$(LANGUAGE_NAME).a '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).a
94+
install -m755 lib$(LANGUAGE_NAME).$(SOEXT) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER)
95+
ln -sf lib$(LANGUAGE_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR)
96+
ln -sf lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXT)
97+
98+
uninstall:
99+
$(RM) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).a \
100+
'$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER) \
101+
'$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR) \
102+
'$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXT) \
103+
'$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/$(LANGUAGE_NAME).h \
104+
'$(DESTDIR)$(PCLIBDIR)'/$(LANGUAGE_NAME).pc
105+
106+
clean:
107+
$(RM) $(OBJS) $(LANGUAGE_NAME).pc lib$(LANGUAGE_NAME).a lib$(LANGUAGE_NAME).$(SOEXT)
108+
109+
test:
110+
$(TS) test
111+
112+
.PHONY: all install uninstall clean test

binding.gyp

+17-6
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,29 @@
22
"targets": [
33
{
44
"target_name": "tree_sitter_c_sharp_binding",
5+
"dependencies": [
6+
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
7+
],
58
"include_dirs": [
6-
"<!(node -e \"require('nan')\")",
7-
"src"
9+
"src",
810
],
911
"sources": [
12+
"bindings/node/binding.cc",
1013
"src/parser.c",
1114
"src/scanner.c",
12-
"bindings/node/binding.cc"
1315
],
14-
"cflags_c": [
15-
"-std=c99",
16-
]
16+
"conditions": [
17+
["OS!='win'", {
18+
"cflags_c": [
19+
"-std=c11",
20+
],
21+
}, { # OS == "win"
22+
"cflags_c": [
23+
"/std:c11",
24+
"/utf-8",
25+
],
26+
}],
27+
],
1728
}
1829
]
1930
}

bindings/c/tree-sitter-c_sharp.h

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

bindings/c/tree-sitter-c_sharp.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-c_sharp
6+
Description: C# grammar for tree-sitter
7+
URL: @URL@
8+
Version: @VERSION@
9+
Requires: @REQUIRES@
10+
Libs: -L${libdir} @ADDITIONAL_LIBS@ -ltree-sitter-c_sharp
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_c_sharp
2+
3+
// #cgo CFLAGS: -std=c11 -fPIC
4+
// #include "../../src/parser.c"
5+
// #include "../../src/scanner.c"
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_c_sharp())
13+
}

bindings/go/binding_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package tree_sitter_c_sharp_test
2+
3+
import (
4+
"testing"
5+
6+
tree_sitter "github.com/smacker/go-tree-sitter"
7+
"github.com/tree-sitter/tree-sitter-c_sharp"
8+
)
9+
10+
func TestCanLoadGrammar(t *testing.T) {
11+
language := tree_sitter.NewLanguage(tree_sitter_c_sharp.Language())
12+
if language == nil {
13+
t.Errorf("Error loading C# 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-c_sharp
2+
3+
go 1.22
4+
5+
require github.com/smacker/go-tree-sitter v0.0.0-20230720070738-0d0a9f78d8f8

bindings/node/binding.cc

+14-22
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
1-
#include "tree_sitter/parser.h"
2-
#include <node.h>
3-
#include "nan.h"
1+
#include <napi.h>
42

5-
using namespace v8;
3+
typedef struct TSLanguage TSLanguage;
64

7-
extern "C" TSLanguage * tree_sitter_c_sharp();
5+
extern "C" TSLanguage *tree_sitter_c_sharp();
86

9-
namespace {
7+
// "tree-sitter", "language" hashed with BLAKE2
8+
const napi_type_tag LANGUAGE_TYPE_TAG = {
9+
0x8AF2E5212AD58ABF, 0xD5006CAD83ABBA16
10+
};
1011

11-
NAN_METHOD(New) {}
12-
13-
void Init(Local<Object> exports, Local<Object> module) {
14-
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
15-
tpl->SetClassName(Nan::New("Language").ToLocalChecked());
16-
tpl->InstanceTemplate()->SetInternalFieldCount(1);
17-
18-
Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
19-
Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
20-
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_c_sharp());
21-
22-
Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("c_sharp").ToLocalChecked());
23-
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
12+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
13+
exports["name"] = Napi::String::New(env, "c_sharp");
14+
auto language = Napi::External<TSLanguage>::New(env, tree_sitter_c_sharp());
15+
language.TypeTag(&LANGUAGE_TYPE_TAG);
16+
exports["language"] = language;
17+
return exports;
2418
}
2519

26-
NODE_MODULE(tree_sitter_c_sharp_binding, Init)
27-
28-
} // namespace
20+
NODE_API_MODULE(tree_sitter_c_sharp_binding, Init)

bindings/node/index.d.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
type BaseNode = {
2+
type: string;
3+
named: boolean;
4+
};
5+
6+
type ChildNode = {
7+
multiple: boolean;
8+
required: boolean;
9+
types: BaseNode[];
10+
};
11+
12+
type NodeInfo =
13+
| (BaseNode & {
14+
subtypes: BaseNode[];
15+
})
16+
| (BaseNode & {
17+
fields: { [name: string]: ChildNode };
18+
children: ChildNode[];
19+
});
20+
21+
type Language = {
22+
name: string;
23+
language: unknown;
24+
nodeTypeInfo: NodeInfo[];
25+
};
26+
27+
declare const language: Language;
28+
export = language;

bindings/node/index.js

+3-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
try {
2-
module.exports = require("../../build/Release/tree_sitter_c_sharp_binding");
3-
} catch (error1) {
4-
if (error1.code !== 'MODULE_NOT_FOUND') {
5-
throw error1;
6-
}
7-
try {
8-
module.exports = require("../../build/Debug/tree_sitter_c_sharp_binding");
9-
} catch (error2) {
10-
if (error2.code !== 'MODULE_NOT_FOUND') {
11-
throw error2;
12-
}
13-
throw error1
14-
}
15-
}
1+
const root = require("path").join(__dirname, "..", "..");
2+
3+
module.exports = require("node-gyp-build")(root);
164

175
try {
186
module.exports.nodeTypeInfo = require("../../src/node-types.json");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"C# grammar for tree-sitter"
2+
3+
from ._binding import language
4+
5+
__all__ = ["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+
TSLanguage *tree_sitter_c_sharp(void);
6+
7+
static PyObject* _binding_language(PyObject *self, PyObject *args) {
8+
return PyLong_FromVoidPtr(tree_sitter_c_sharp());
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_c_sharp/py.typed

Whitespace-only changes.

bindings/rust/build.rs

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ fn main() {
1010
.flag_if_supported("-Wno-unused-parameter")
1111
.flag_if_supported("-Wno-unused-but-set-variable")
1212
.flag_if_supported("-Wno-trigraphs");
13+
#[cfg(target_env = "msvc")]
14+
c_config.flag("-utf-8");
15+
1316
let parser_path = src_dir.join("parser.c");
1417
c_config.file(&parser_path);
1518
let scanner_path = src_dir.join("scanner.c");

0 commit comments

Comments
 (0)