Skip to content

Commit

Permalink
idac
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq committed Apr 18, 2024
1 parent 212a80f commit 014e8e0
Show file tree
Hide file tree
Showing 29 changed files with 68,190 additions and 68,736 deletions.
39 changes: 39 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.{json,toml,yml,gyp}]
indent_style = space
indent_size = 2

[*.js]
indent_style = space
indent_size = 2

[*.rs]
indent_style = space
indent_size = 4

[*.{c,cc,h}]
indent_style = space
indent_size = 4

[*.{py,pyi}]
indent_style = space
indent_size = 4

[*.swift]
indent_style = space
indent_size = 4

[*.go]
indent_style = tab
indent_size = 8

[Makefile]
indent_style = tab
indent_size = 8
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "tree-sitter-c"
name = "tree-sitter-idac"
description = "C grammar for the tree-sitter parsing library"
version = "0.20.6"
authors = ["Max Brunsfeld <[email protected]>"]
license = "MIT"
readme = "bindings/rust/README.md"
keywords = ["incremental", "parsing", "c"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/tree-sitter/tree-sitter-c"
repository = "https://github.com/tree-sitter/tree-sitter-idac"
edition = "2021"
autoexamples = false

Expand All @@ -18,7 +18,7 @@ include = ["bindings/rust/*", "grammar.js", "queries/*", "src/*"]
path = "bindings/rust/lib.rs"

[dependencies]
tree-sitter = "0.20.10"
tree-sitter = ">=0.21.0"

[build-dependencies]
cc = "~1.0"
cc = "1.0.94"
113 changes: 113 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
VERSION := 0.0.1

# Repository
SRC_DIR := src

LANGUAGE_NAME := c
UPPER_LANGUAGE_NAME := C

PARSER_REPO_URL := $(shell git -C $(SRC_DIR) remote get-url origin )

ifeq (, $(PARSER_URL))
PARSER_URL := $(subst .git,,$(PARSER_REPO_URL))
ifeq ($(shell echo $(PARSER_URL) | grep '^[a-z][-+.0-9a-z]*://'),)
PARSER_URL := $(subst :,/,$(PARSER_URL))
PARSER_URL := $(subst git@,https://,$(PARSER_URL))
endif
endif

# 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-$(LANGUAGE_NAME).$(SONAME_MAJOR).dylib,-rpath,@executable_path/../Frameworks
else ifneq (,$(filter $(shell uname),Linux FreeBSD NetBSD DragonFly))
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-$(LANGUAGE_NAME).so.$(SONAME_MAJOR)
else ifeq ($(OS),Windows_NT)
$(error "Windows is not supported")
endif
ifneq (,$(filter $(shell uname),FreeBSD NetBSD DragonFly))
PCLIBDIR := $(PREFIX)/libdata/pkgconfig
endif

all: libtree-sitter-$(LANGUAGE_NAME).a libtree-sitter-$(LANGUAGE_NAME).$(SOEXTVER) bindings/c/$(LANGUAGE_NAME).h bindings/c/tree-sitter-$(LANGUAGE_NAME).pc

libtree-sitter-$(LANGUAGE_NAME).a: $(OBJ)
$(AR) rcs $@ $^

libtree-sitter-$(LANGUAGE_NAME).$(SOEXTVER): $(OBJ)
$(CC) $(LDFLAGS) $(LINKSHARED) $^ $(LDLIBS) -o $@
ln -sf $@ libtree-sitter-$(LANGUAGE_NAME).$(SOEXT)
ln -sf $@ libtree-sitter-$(LANGUAGE_NAME).$(SOEXTVER_MAJOR)

bindings/c/$(LANGUAGE_NAME).h:
sed -e 's|@UPPER_LANGUAGENAME@|$(UPPER_LANGUAGE_NAME)|' \
-e 's|@LANGUAGENAME@|$(LANGUAGE_NAME)|' \
bindings/c/tree-sitter.h.in > $@

bindings/c/tree-sitter-$(LANGUAGE_NAME).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|@LANGUAGENAME@|$(LANGUAGE_NAME)|' \
-e 's|@PARSERURL@|$(PARSER_URL)|' \
bindings/c/tree-sitter.pc.in > $@

install: all
install -d '$(DESTDIR)$(LIBDIR)'
install -m755 libtree-sitter-$(LANGUAGE_NAME).a '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(LANGUAGE_NAME).a
install -m755 libtree-sitter-$(LANGUAGE_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(LANGUAGE_NAME).$(SOEXTVER)
ln -sf libtree-sitter-$(LANGUAGE_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(LANGUAGE_NAME).$(SOEXTVER_MAJOR)
ln -sf libtree-sitter-$(LANGUAGE_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/libtree-sitter-$(LANGUAGE_NAME).$(SOEXT)
install -d '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter
install -m644 bindings/c/$(LANGUAGE_NAME).h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/
install -d '$(DESTDIR)$(PCLIBDIR)'
install -m644 bindings/c/tree-sitter-$(LANGUAGE_NAME).pc '$(DESTDIR)$(PCLIBDIR)'/

clean:
rm -f $(OBJ) libtree-sitter-$(LANGUAGE_NAME).a libtree-sitter-$(LANGUAGE_NAME).$(SOEXT) libtree-sitter-$(LANGUAGE_NAME).$(SOEXTVER_MAJOR) libtree-sitter-$(LANGUAGE_NAME).$(SOEXTVER)
rm -f bindings/c/$(LANGUAGE_NAME).h bindings/c/tree-sitter-$(LANGUAGE_NAME).pc

.PHONY: all install clean
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# tree-sitter-c
# tree-sitter-idac

[![CI](https://github.com/tree-sitter/tree-sitter-c/actions/workflows/ci.yml/badge.svg)](https://github.com/tree-sitter/tree-sitter-c/actions/workflows/ci.yml)
[![Discord](https://img.shields.io/discord/1063097320771698699?logo=discord)](https://discord.gg/w7nTvsVJhm)
Expand Down
24 changes: 18 additions & 6 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@
"targets": [
{
"target_name": "tree_sitter_c_binding",
"dependencies": [
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
],
"include_dirs": [
"<!(node -e \"require('nan')\")",
"src"
"src",
],
"sources": [
"bindings/node/binding.cc",
"src/parser.c",
"bindings/node/binding.cc"
# NOTE: if your language has an external scanner, add it here.
],
"conditions": [
["OS!='win'", {
"cflags_c": [
"-std=c11",
],
}, { # OS == "win"
"cflags_c": [
"/std:c11",
"/utf-8",
],
}],
],
"cflags_c": [
"-std=c99",
]
}
]
}
16 changes: 16 additions & 0 deletions bindings/c/tree-sitter-idac.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef TREE_SITTER_IDAC_H_
#define TREE_SITTER_IDAC_H_

typedef struct TSLanguage TSLanguage;

#ifdef __cplusplus
extern "C" {
#endif

const TSLanguage *tree_sitter_idac(void);

#ifdef __cplusplus
}
#endif

#endif // TREE_SITTER_IDAC_H_
11 changes: 11 additions & 0 deletions bindings/c/tree-sitter-idac.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
prefix=@PREFIX@
libdir=@LIBDIR@
includedir=@INCLUDEDIR@

Name: tree-sitter-idac
Description: Idac grammar for tree-sitter
URL: @URL@
Version: @VERSION@
Requires: @REQUIRES@
Libs: -L${libdir} @ADDITIONAL_LIBS@ -ltree-sitter-idac
Cflags: -I${includedir}
19 changes: 19 additions & 0 deletions bindings/go/binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tree_sitter_c

// #cgo CFLAGS: -O3 -Wall -Wextra -std=gnu99 -fPIC -I../../src
// #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"
)

// Get the tree-sitter Language for this grammar.
func Language() *tree_sitter.Language {
ptr := unsafe.Pointer(C.tree_sitter_c())
return tree_sitter.NewLanguage(ptr)
}
14 changes: 14 additions & 0 deletions bindings/go/binding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package tree_sitter_c_test

import (
"testing"

"github.com/tree-sitter/tree-sitter-idac"
)

func TestCanLoadGrammar(t *testing.T) {
parser := tree_sitter_c.Language()
if parser == nil {
t.Errorf("Error loading C grammar")
}
}
5 changes: 5 additions & 0 deletions bindings/go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/tree-sitter/tree-sitter-c

go 1.20

require github.com/smacker/go-tree-sitter v0.0.0-20230720070738-0d0a9f78d8f8
38 changes: 14 additions & 24 deletions bindings/node/binding.cc
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
#include "nan.h"
#include "tree_sitter/parser.h"
#include <node.h>
#include <napi.h>

using namespace v8;
typedef struct TSLanguage TSLanguage;

extern "C" TSLanguage *tree_sitter_c();

namespace {

NAN_METHOD(New) {}

void Init(Local<Object> exports, Local<Object> module) {
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->SetClassName(Nan::New("Language").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);

Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
Local<Object> instance =
constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_c());

Nan::Set(instance, Nan::New("name").ToLocalChecked(),
Nan::New("c").ToLocalChecked());
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
// "tree-sitter", "language" hashed with BLAKE2
const napi_type_tag LANGUAGE_TYPE_TAG = {
0x8AF2E5212AD58ABF, 0xD5006CAD83ABBA16
};

Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports["name"] = Napi::String::New(env, "c");
auto language = Napi::External<TSLanguage>::New(env, tree_sitter_c());
language.TypeTag(&LANGUAGE_TYPE_TAG);
exports["language"] = language;
return exports;
}

NODE_MODULE(tree_sitter_c_binding, Init)

} // namespace
NODE_API_MODULE(tree_sitter_c_binding, Init)
28 changes: 28 additions & 0 deletions bindings/node/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
type BaseNode = {
type: string;
named: boolean;
};

type ChildNode = {
multiple: boolean;
required: boolean;
types: BaseNode[];
};

type NodeInfo =
| (BaseNode & {
subtypes: BaseNode[];
})
| (BaseNode & {
fields: { [name: string]: ChildNode };
children: ChildNode[];
});

type Language = {
name: string;
language: unknown;
nodeTypeInfo: NodeInfo[];
};

declare const language: Language;
export = language;
20 changes: 4 additions & 16 deletions bindings/node/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
try {
module.exports = require('../../build/Release/tree_sitter_c_binding');
} catch (error1) {
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require('../../build/Debug/tree_sitter_c_binding');
} catch (error2) {
if (error2.code !== 'MODULE_NOT_FOUND') {
throw error2;
}
throw error1;
}
}
const root = require("path").join(__dirname, "..", "..");

module.exports = require("node-gyp-build")(root);

try {
module.exports.nodeTypeInfo = require('../../src/node-types.json');
module.exports.nodeTypeInfo = require("../../src/node-types.json");
} catch (_) {}
5 changes: 5 additions & 0 deletions bindings/python/tree_sitter_idac/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"Idac grammar for tree-sitter"

from ._binding import language

__all__ = ["language"]
1 change: 1 addition & 0 deletions bindings/python/tree_sitter_idac/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def language() -> int: ...
Loading

0 comments on commit 014e8e0

Please sign in to comment.