Skip to content

Commit 4932594

Browse files
committed
fix: update tree-sitter version and also start working on more luaCATS
1 parent acb3f36 commit 4932594

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2198
-1046
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+
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+
[*.{c,cc,h}]
15+
indent_style = space
16+
indent_size = 4
17+
18+
[*.rs]
19+
indent_style = space
20+
indent_size = 4
21+
22+
[*.{py,pyi}]
23+
indent_style = space
24+
indent_size = 4
25+
26+
[*.swift]
27+
indent_style = space
28+
indent_size = 4
29+
30+
[*.go]
31+
indent_style = tab
32+
indent_size = 8
33+
34+
[Makefile]
35+
indent_style = tab
36+
indent_size = 8
37+
38+
[parser.c]
39+
indent_size = 2

Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ include = [
2020
path = "bindings/rust/lib.rs"
2121

2222
[dependencies]
23-
tree-sitter = "0.17"
23+
tree-sitter-language = "0.1.0"
2424

2525
[build-dependencies]
2626
cc = "1.0"
27+
28+
[dev-dependencies]
29+
tree-sitter = "0.23"

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ build_parser: generate
2525
mkdir -p build
2626
cc -o ./build/parser.so -I./src src/parser.c src/scanner.c -shared -Os -fPIC
2727
mkdir -p parser
28-
cp -u ./build/parser.so ./parser/lua.so || exit 0
28+
cp ./build/parser.so ./parser/lua.so || exit 0
2929

3030
gen_howto:
3131
nvim --headless --noplugin -u tests/init.lua -c "luafile ./scratch/gen_howto.lua" -c 'qa'

Package.swift

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// swift-tools-version:5.3
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "TreeSitterLua",
6+
products: [
7+
.library(name: "TreeSitterLua", targets: ["TreeSitterLua"]),
8+
],
9+
dependencies: [
10+
.package(url: "https://github.com/ChimeHQ/SwiftTreeSitter", from: "0.8.0"),
11+
],
12+
targets: [
13+
.target(
14+
name: "TreeSitterLua",
15+
dependencies: [],
16+
path: ".",
17+
sources: [
18+
"src/parser.c",
19+
// NOTE: if your language has an external scanner, add it here.
20+
],
21+
resources: [
22+
.copy("queries")
23+
],
24+
publicHeadersPath: "bindings/swift",
25+
cSettings: [.headerSearchPath("src")]
26+
),
27+
.testTarget(
28+
name: "TreeSitterLuaTests",
29+
dependencies: [
30+
"SwiftTreeSitter",
31+
"TreeSitterLua",
32+
],
33+
path: "bindings/swift/TreeSitterLuaTests"
34+
)
35+
],
36+
cLanguageStandard: .c11
37+
)

binding.gyp

+18-6
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,29 @@
22
"targets": [
33
{
44
"target_name": "tree_sitter_lua_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",
11-
"bindings/node/binding.cc"
14+
# NOTE: if your language has an external scanner, add it here.
15+
],
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+
}],
1227
],
13-
"cflags_c": [
14-
"-std=c99",
15-
]
1628
}
1729
]
1830
}

bindings/c/tree-sitter-lua.h

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

bindings/c/tree-sitter-lua.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-lua
6+
Description: Lua grammar for tree-sitter
7+
URL: @URL@
8+
Version: @VERSION@
9+
Requires: @REQUIRES@
10+
Libs: -L${libdir} @ADDITIONAL_LIBS@ -ltree-sitter-lua
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_lua
2+
3+
// #cgo CFLAGS: -std=c11 -fPIC
4+
// #include "../../src/parser.c"
5+
// // NOTE: if your language has an external scanner, add it here.
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_lua())
13+
}

bindings/go/binding_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package tree_sitter_lua_test
2+
3+
import (
4+
"testing"
5+
6+
tree_sitter "github.com/tree-sitter/go-tree-sitter"
7+
tree_sitter_lua "github.com/tree-sitter/tree-sitter-lua/bindings/go"
8+
)
9+
10+
func TestCanLoadGrammar(t *testing.T) {
11+
language := tree_sitter.NewLanguage(tree_sitter_lua.Language())
12+
if language == nil {
13+
t.Errorf("Error loading Lua grammar")
14+
}
15+
}

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_lua();
5+
extern "C" TSLanguage *tree_sitter_lua();
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_lua());
21-
22-
Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("lua").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, "lua");
14+
auto language = Napi::External<TSLanguage>::New(env, tree_sitter_lua());
15+
language.TypeTag(&LANGUAGE_TYPE_TAG);
16+
exports["language"] = language;
17+
return exports;
2418
}
2519

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

bindings/node/binding_test.js

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

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_lua_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_lua_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");

bindings/python/tests/test_binding.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from unittest import TestCase
2+
3+
import tree_sitter, tree_sitter_lua
4+
5+
6+
class TestLanguage(TestCase):
7+
def test_can_load_grammar(self):
8+
try:
9+
tree_sitter.Language(tree_sitter_lua.language())
10+
except Exception:
11+
self.fail("Error loading Lua grammar")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Lua grammar for tree-sitter"""
2+
3+
from importlib.resources import files as _files
4+
5+
from ._binding import language
6+
7+
8+
def _get_query(name, file):
9+
query = _files(f"{__package__}.queries") / file
10+
globals()[name] = query.read_text()
11+
return globals()[name]
12+
13+
14+
def __getattr__(name):
15+
# NOTE: uncomment these to include any queries that this grammar contains:
16+
17+
# if name == "HIGHLIGHTS_QUERY":
18+
# return _get_query("HIGHLIGHTS_QUERY", "highlights.scm")
19+
# if name == "INJECTIONS_QUERY":
20+
# return _get_query("INJECTIONS_QUERY", "injections.scm")
21+
# if name == "LOCALS_QUERY":
22+
# return _get_query("LOCALS_QUERY", "locals.scm")
23+
# if name == "TAGS_QUERY":
24+
# return _get_query("TAGS_QUERY", "tags.scm")
25+
26+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
27+
28+
29+
__all__ = [
30+
"language",
31+
# "HIGHLIGHTS_QUERY",
32+
# "INJECTIONS_QUERY",
33+
# "LOCALS_QUERY",
34+
# "TAGS_QUERY",
35+
]
36+
37+
38+
def __dir__():
39+
return sorted(__all__ + [
40+
"__all__", "__builtins__", "__cached__", "__doc__", "__file__",
41+
"__loader__", "__name__", "__package__", "__path__", "__spec__",
42+
])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from typing import Final
2+
3+
# NOTE: uncomment these to include any queries that this grammar contains:
4+
5+
# HIGHLIGHTS_QUERY: Final[str]
6+
# INJECTIONS_QUERY: Final[str]
7+
# LOCALS_QUERY: Final[str]
8+
# TAGS_QUERY: Final[str]
9+
10+
def language() -> object: ...
+27
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_lua(void);
6+
7+
static PyObject* _binding_language(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) {
8+
return PyCapsule_New(tree_sitter_lua(), "tree_sitter.Language", NULL);
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_lua/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)