Skip to content

Commit 3c1344e

Browse files
committed
feat: update bindings
1 parent e0149e0 commit 3c1344e

File tree

21 files changed

+585
-21
lines changed

21 files changed

+585
-21
lines changed

.editorconfig

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
6+
[*.{json,toml,yml,gyp}]
7+
indent_style = space
8+
indent_size = 2
9+
10+
[*.js]
11+
indent_style = space
12+
indent_size = 2
13+
14+
[*.scm]
15+
indent_style = space
16+
indent_size = 2
17+
18+
[*.{c,cc,h}]
19+
indent_style = space
20+
indent_size = 4
21+
22+
[*.rs]
23+
indent_style = space
24+
indent_size = 4
25+
26+
[*.{py,pyi}]
27+
indent_style = space
28+
indent_size = 4
29+
30+
[*.swift]
31+
indent_style = space
32+
indent_size = 4
33+
34+
[*.go]
35+
indent_style = tab
36+
indent_size = 8
37+
38+
[Makefile]
39+
indent_style = tab
40+
indent_size = 8
41+
42+
[parser.c]
43+
indent_size = 2
44+
45+
[{alloc,array,parser}.h]
46+
indent_size = 2

.gitattributes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ src/parser.c linguist-generated
88
src/grammar.json -diff
99
src/node-types.json -diff
1010
src/parser.c -diff
11+
12+
# Zig bindings
13+
build.zig linguist-generated
14+
build.zig.zon linguist-generated

.gitignore

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,50 @@
1+
# Rust artifacts
2+
target/
13
Cargo.lock
2-
node_modules
3-
build
4+
5+
# Node artifacts
6+
build/
7+
prebuilds/
8+
node_modules/
49
package-lock.json
5-
/target/
6-
.build/
10+
11+
# Swift artifacts
12+
.build/
13+
Package.resolved
14+
15+
# Go artifacts
16+
_obj/
17+
18+
# Python artifacts
19+
.venv/
20+
dist/
21+
*.egg-info
22+
*.whl
23+
24+
# C artifacts
25+
*.a
26+
*.so
27+
*.so.*
28+
*.dylib
29+
*.dll
30+
*.pc
31+
*.exp
32+
*.lib
33+
34+
# Zig artifacts
35+
.zig-cache/
36+
zig-cache/
37+
zig-out/
38+
39+
# Example dirs
40+
/examples/*/
41+
42+
# Grammar volatiles
43+
*.wasm
44+
*.obj
45+
*.o
46+
47+
# Archives
48+
*.tar.gz
49+
*.tgz
50+
*.zip

CMakeLists.txt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
3+
project(tree-sitter-csv
4+
VERSION "1.2.0"
5+
DESCRIPTION "CSV, PSV, TSV grammars for tree-sitter"
6+
HOMEPAGE_URL "https://github.com/tree-sitter-grammars/tree-sitter-csv"
7+
LANGUAGES C)
8+
9+
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
10+
option(TREE_SITTER_REUSE_ALLOCATOR "Reuse the library allocator" OFF)
11+
12+
set(TREE_SITTER_ABI_VERSION 15 CACHE STRING "Tree-sitter ABI version")
13+
if(NOT ${TREE_SITTER_ABI_VERSION} MATCHES "^[0-9]+$")
14+
unset(TREE_SITTER_ABI_VERSION CACHE)
15+
message(FATAL_ERROR "TREE_SITTER_ABI_VERSION must be an integer")
16+
endif()
17+
18+
include(GNUInstallDirs)
19+
20+
find_program(TREE_SITTER_CLI tree-sitter DOC "Tree-sitter CLI")
21+
22+
add_custom_command(OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/src/parser.c"
23+
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/grammar.json"
24+
COMMAND "${TREE_SITTER_CLI}" generate src/grammar.json
25+
--abi=${TREE_SITTER_ABI_VERSION}
26+
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
27+
COMMENT "Generating parser.c")
28+
29+
add_library(tree-sitter-csv src/parser.c)
30+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/scanner.c)
31+
target_sources(tree-sitter-csv PRIVATE src/scanner.c)
32+
endif()
33+
target_include_directories(tree-sitter-csv
34+
PRIVATE src
35+
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/bindings/c>
36+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
37+
38+
target_compile_definitions(tree-sitter-csv PRIVATE
39+
$<$<BOOL:${TREE_SITTER_REUSE_ALLOCATOR}>:TREE_SITTER_REUSE_ALLOCATOR>
40+
$<$<CONFIG:Debug>:TREE_SITTER_DEBUG>)
41+
42+
set_target_properties(tree-sitter-csv
43+
PROPERTIES
44+
C_STANDARD 11
45+
POSITION_INDEPENDENT_CODE ON
46+
SOVERSION "${TREE_SITTER_ABI_VERSION}.${PROJECT_VERSION_MAJOR}"
47+
DEFINE_SYMBOL "")
48+
49+
configure_file(bindings/c/tree-sitter-csv.pc.in
50+
"${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-csv.pc" @ONLY)
51+
52+
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bindings/c/tree_sitter"
53+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
54+
FILES_MATCHING PATTERN "*.h")
55+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-csv.pc"
56+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
57+
install(TARGETS tree-sitter-csv
58+
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
59+
60+
file(GLOB QUERIES queries/*.scm)
61+
install(FILES ${QUERIES}
62+
DESTINATION "${CMAKE_INSTALL_DATADIR}/tree-sitter/queries/csv")
63+
64+
add_custom_target(ts-test "${TREE_SITTER_CLI}" test
65+
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
66+
COMMENT "tree-sitter test")

Makefile

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
ifeq ($(OS),Windows_NT)
2+
$(error Windows is not supported)
3+
endif
4+
5+
LANGUAGE_NAME := tree-sitter-csv
6+
HOMEPAGE_URL := https://github.com/tree-sitter-grammars/tree-sitter-csv
7+
VERSION := 1.2.0
8+
9+
# repository
10+
SRC_DIR := src
11+
12+
TS ?= tree-sitter
13+
14+
# install directory layout
15+
PREFIX ?= /usr/local
16+
DATADIR ?= $(PREFIX)/share
17+
INCLUDEDIR ?= $(PREFIX)/include
18+
LIBDIR ?= $(PREFIX)/lib
19+
PCLIBDIR ?= $(LIBDIR)/pkgconfig
20+
21+
# source/object files
22+
PARSER := $(SRC_DIR)/parser.c
23+
EXTRAS := $(filter-out $(PARSER),$(wildcard $(SRC_DIR)/*.c))
24+
OBJS := $(patsubst %.c,%.o,$(PARSER) $(EXTRAS))
25+
26+
# flags
27+
ARFLAGS ?= rcs
28+
override CFLAGS += -I$(SRC_DIR) -std=c11 -fPIC
29+
30+
# ABI versioning
31+
SONAME_MAJOR = $(shell sed -n 's/\#define LANGUAGE_VERSION //p' $(PARSER))
32+
SONAME_MINOR = $(word 1,$(subst ., ,$(VERSION)))
33+
34+
# OS-specific bits
35+
ifeq ($(shell uname),Darwin)
36+
SOEXT = dylib
37+
SOEXTVER_MAJOR = $(SONAME_MAJOR).$(SOEXT)
38+
SOEXTVER = $(SONAME_MAJOR).$(SONAME_MINOR).$(SOEXT)
39+
LINKSHARED = -dynamiclib -Wl,-install_name,$(LIBDIR)/lib$(LANGUAGE_NAME).$(SOEXTVER),-rpath,@executable_path/../Frameworks
40+
else
41+
SOEXT = so
42+
SOEXTVER_MAJOR = $(SOEXT).$(SONAME_MAJOR)
43+
SOEXTVER = $(SOEXT).$(SONAME_MAJOR).$(SONAME_MINOR)
44+
LINKSHARED = -shared -Wl,-soname,lib$(LANGUAGE_NAME).$(SOEXTVER)
45+
endif
46+
ifneq ($(filter $(shell uname),FreeBSD NetBSD DragonFly),)
47+
PCLIBDIR := $(PREFIX)/libdata/pkgconfig
48+
endif
49+
50+
all: lib$(LANGUAGE_NAME).a lib$(LANGUAGE_NAME).$(SOEXT) $(LANGUAGE_NAME).pc
51+
52+
lib$(LANGUAGE_NAME).a: $(OBJS)
53+
$(AR) $(ARFLAGS) $@ $^
54+
55+
lib$(LANGUAGE_NAME).$(SOEXT): $(OBJS)
56+
$(CC) $(LDFLAGS) $(LINKSHARED) $^ $(LDLIBS) -o $@
57+
ifneq ($(STRIP),)
58+
$(STRIP) $@
59+
endif
60+
61+
$(LANGUAGE_NAME).pc: bindings/c/$(LANGUAGE_NAME).pc.in
62+
sed -e 's|@PROJECT_VERSION@|$(VERSION)|' \
63+
-e 's|@CMAKE_INSTALL_LIBDIR@|$(LIBDIR:$(PREFIX)/%=%)|' \
64+
-e 's|@CMAKE_INSTALL_INCLUDEDIR@|$(INCLUDEDIR:$(PREFIX)/%=%)|' \
65+
-e 's|@PROJECT_DESCRIPTION@|$(DESCRIPTION)|' \
66+
-e 's|@PROJECT_HOMEPAGE_URL@|$(HOMEPAGE_URL)|' \
67+
-e 's|@CMAKE_INSTALL_PREFIX@|$(PREFIX)|' $< > $@
68+
69+
$(PARSER): $(SRC_DIR)/grammar.json
70+
$(TS) generate $^
71+
72+
install: all
73+
install -d '$(DESTDIR)$(DATADIR)'/tree-sitter/queries/csv '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter '$(DESTDIR)$(PCLIBDIR)' '$(DESTDIR)$(LIBDIR)'
74+
install -m644 bindings/c/tree_sitter/$(LANGUAGE_NAME).h '$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/$(LANGUAGE_NAME).h
75+
install -m644 $(LANGUAGE_NAME).pc '$(DESTDIR)$(PCLIBDIR)'/$(LANGUAGE_NAME).pc
76+
install -m644 lib$(LANGUAGE_NAME).a '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).a
77+
install -m755 lib$(LANGUAGE_NAME).$(SOEXT) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER)
78+
ln -sf lib$(LANGUAGE_NAME).$(SOEXTVER) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR)
79+
ln -sf lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXT)
80+
ifneq ($(wildcard queries/*.scm),)
81+
install -m644 queries/*.scm '$(DESTDIR)$(DATADIR)'/tree-sitter/queries/csv
82+
endif
83+
84+
uninstall:
85+
$(RM) '$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).a \
86+
'$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER) \
87+
'$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXTVER_MAJOR) \
88+
'$(DESTDIR)$(LIBDIR)'/lib$(LANGUAGE_NAME).$(SOEXT) \
89+
'$(DESTDIR)$(INCLUDEDIR)'/tree_sitter/$(LANGUAGE_NAME).h \
90+
'$(DESTDIR)$(PCLIBDIR)'/$(LANGUAGE_NAME).pc
91+
$(RM) -r '$(DESTDIR)$(DATADIR)'/tree-sitter/queries/csv
92+
93+
clean:
94+
$(RM) $(OBJS) $(LANGUAGE_NAME).pc lib$(LANGUAGE_NAME).a lib$(LANGUAGE_NAME).$(SOEXT)
95+
96+
test:
97+
$(TS) test
98+
99+
.PHONY: all install uninstall clean test

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
prefix=@CMAKE_INSTALL_PREFIX@
2+
libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
3+
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
4+
5+
Name: tree-sitter-csv
6+
Description: @PROJECT_DESCRIPTION@
7+
URL: @PROJECT_HOMEPAGE_URL@
8+
Version: @PROJECT_VERSION@
9+
Libs: -L${libdir} -ltree-sitter-csv
10+
Cflags: -I${includedir}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef TREE_SITTER_CSV_H_
2+
#define TREE_SITTER_CSV_H_
3+
4+
typedef struct TSLanguage TSLanguage;
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
const TSLanguage *tree_sitter_csv(void);
11+
12+
#ifdef __cplusplus
13+
}
14+
#endif
15+
16+
#endif // TREE_SITTER_CSV_H_

bindings/go/binding.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package tree_sitter_csv
2+
3+
// #cgo CFLAGS: -std=c11 -fPIC
4+
// #include "../../src/parser.c"
5+
// #if __has_include("../../src/scanner.c")
6+
// #include "../../src/scanner.c"
7+
// #endif
8+
import "C"
9+
10+
import "unsafe"
11+
12+
// Get the tree-sitter Language for this grammar.
13+
func Language() unsafe.Pointer {
14+
return unsafe.Pointer(C.tree_sitter_csv())
15+
}

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_csv_test
2+
3+
import (
4+
"testing"
5+
6+
tree_sitter "github.com/tree-sitter/go-tree-sitter"
7+
tree_sitter_csv "github.com/tree-sitter-grammars/tree-sitter-csv/bindings/go"
8+
)
9+
10+
func TestCanLoadGrammar(t *testing.T) {
11+
language := tree_sitter.NewLanguage(tree_sitter_csv.Language())
12+
if language == nil {
13+
t.Errorf("Error loading Csv grammar")
14+
}
15+
}

bindings/node/binding_test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const assert = require("node:assert");
2+
const { test } = require("node:test");
3+
4+
const Parser = require("tree-sitter");
5+
6+
test("can load grammar", () => {
7+
const parser = new Parser();
8+
assert.doesNotThrow(() => parser.setLanguage(require(".")));
9+
});

0 commit comments

Comments
 (0)