Skip to content

Commit edd6596

Browse files
authored
Merge pull request #3 from savetheclocktower/main
Massive update (details within)
2 parents 2efa374 + 9ec7dc3 commit edd6596

35 files changed

+20859
-926
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

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ build
88
package-lock.json
99
target
1010
Cargo.lock
11+
test.vto
12+
.envrc

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-vento
4+
VERSION "0.1.0"
5+
DESCRIPTION "Vento grammar for Tree-sitter"
6+
HOMEPAGE_URL "https://github.com/ventojs/tree-sitter-vento"
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-vento src/parser.c)
30+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/src/scanner.c)
31+
target_sources(tree-sitter-vento PRIVATE src/scanner.c)
32+
endif()
33+
target_include_directories(tree-sitter-vento
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-vento PRIVATE
39+
$<$<BOOL:${TREE_SITTER_REUSE_ALLOCATOR}>:TREE_SITTER_REUSE_ALLOCATOR>
40+
$<$<CONFIG:Debug>:TREE_SITTER_DEBUG>)
41+
42+
set_target_properties(tree-sitter-vento
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-vento.pc.in
50+
"${CMAKE_CURRENT_BINARY_DIR}/tree-sitter-vento.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-vento.pc"
56+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
57+
install(TARGETS tree-sitter-vento
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/vento")
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-vento
6+
HOMEPAGE_URL := https://github.com/ventojs/tree-sitter-vento
7+
VERSION := 0.1.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/vento '$(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/vento
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/vento
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

Package.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// swift-tools-version:5.3
2+
3+
import Foundation
4+
import PackageDescription
5+
6+
var sources = ["src/parser.c"]
7+
if FileManager.default.fileExists(atPath: "src/scanner.c") {
8+
sources.append("src/scanner.c")
9+
}
10+
11+
let package = Package(
12+
name: "TreeSitterVento",
13+
products: [
14+
.library(name: "TreeSitterVento", targets: ["TreeSitterVento"]),
15+
],
16+
dependencies: [
17+
.package(name: "SwiftTreeSitter", url: "https://github.com/tree-sitter/swift-tree-sitter", from: "0.9.0"),
18+
],
19+
targets: [
20+
.target(
21+
name: "TreeSitterVento",
22+
dependencies: [],
23+
path: ".",
24+
sources: sources,
25+
resources: [
26+
.copy("queries")
27+
],
28+
publicHeadersPath: "bindings/swift",
29+
cSettings: [.headerSearchPath("src")]
30+
),
31+
.testTarget(
32+
name: "TreeSitterVentoTests",
33+
dependencies: [
34+
"SwiftTreeSitter",
35+
"TreeSitterVento",
36+
],
37+
path: "bindings/swift/TreeSitterVentoTests"
38+
)
39+
],
40+
cLanguageStandard: .c11
41+
)

bindings/c/tree-sitter-vento.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-vento
6+
Description: @PROJECT_DESCRIPTION@
7+
URL: @PROJECT_HOMEPAGE_URL@
8+
Version: @PROJECT_VERSION@
9+
Libs: -L${libdir} -ltree-sitter-vento
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_VENTO_H_
2+
#define TREE_SITTER_VENTO_H_
3+
4+
typedef struct TSLanguage TSLanguage;
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
const TSLanguage *tree_sitter_vento(void);
11+
12+
#ifdef __cplusplus
13+
}
14+
#endif
15+
16+
#endif // TREE_SITTER_VENTO_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_vento
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_vento())
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_vento_test
2+
3+
import (
4+
"testing"
5+
6+
tree_sitter "github.com/tree-sitter/go-tree-sitter"
7+
tree_sitter_vento "github.com/ventojs/tree-sitter-vento/bindings/go"
8+
)
9+
10+
func TestCanLoadGrammar(t *testing.T) {
11+
language := tree_sitter.NewLanguage(tree_sitter_vento.Language())
12+
if language == nil {
13+
t.Errorf("Error loading Vento 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)