Skip to content

Commit

Permalink
FIX: global declarations were error nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
leath-dub committed Jul 15, 2024
1 parent 4af5d82 commit 8639a08
Show file tree
Hide file tree
Showing 8 changed files with 9,036 additions and 8,657 deletions.
22 changes: 17 additions & 5 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,29 @@
"targets": [
{
"target_name": "tree_sitter_hare_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",
# 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",
]
}
]
}
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_hare();

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_hare());

Nan::Set(instance, Nan::New("name").ToLocalChecked(),
Nan::New("hare").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, "hare");
auto language = Napi::External<TSLanguage>::New(env, tree_sitter_hare());
language.TypeTag(&LANGUAGE_TYPE_TAG);
exports["language"] = language;
return exports;
}

NODE_MODULE(tree_sitter_hare_binding, Init)

} // namespace
NODE_API_MODULE(tree_sitter_hare_binding, Init)
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_hare_binding');
} catch (error1) {
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require('../../build/Debug/tree_sitter_hare_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 (_) {}
3 changes: 3 additions & 0 deletions bindings/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ fn main() {
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable")
.flag_if_supported("-Wno-trigraphs");
#[cfg(target_env = "msvc")]
c_config.flag("-utf-8");

let parser_path = src_dir.join("parser.c");
c_config.file(&parser_path);

Expand Down
2 changes: 1 addition & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ module.exports = grammar({

global_binding: $ => seq(
optional($.declaration_attribute),
$.identifier, ':', $.type, optional(seq('=', $.expression)),
$.identifier, optional(seq(':', $.type)), optional(seq('=', $.expression)),
),

declaration_attribute: $ => seq(
Expand Down
27 changes: 24 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,53 @@
"version": "1.0.0",
"description": "Hare grammar for tree-sitter",
"main": "bindings/node",
"types": "bindings/node",
"keywords": [
"parser",
"lexer",
"hare"
],
"files": [
"grammar.js",
"binding.gyp",
"prebuilds/**",
"bindings/node/*",
"queries/*",
"src/**"
],
"author": "Amaan Qureshi <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/amaanq/tree-sitter-hare/issues"
},
"homepage": "https://github.com/amaanq/tree-sitter-hare#readme",
"dependencies": {
"nan": "^2.17.0"
"node-addon-api": "^7.1.0",
"node-gyp-build": "^4.8.0"
},
"peerDependencies": {
"tree-sitter": "^0.21.0"
},
"peerDependenciesMeta": {
"tree_sitter": {
"optional": true
}
},
"devDependencies": {
"eslint": "^8.40.0",
"eslint-config-google": "^0.14.0",
"tree-sitter-cli": "^0.20.8"
"tree-sitter-cli": "^0.20.8",
"prebuildify": "^6.0.0"
},
"repository": "https://github.com/amaanq/tree-sitter-hare",
"scripts": {
"build": "tree-sitter generate && node-gyp build",
"lint": "eslint grammar.js",
"parse": "tree-sitter parse",
"test": "tree-sitter test && script/parse-examples",
"test-windows": "tree-sitter test"
"test-windows": "tree-sitter test",
"install": "node-gyp-build",
"prebuildify": "prebuildify --napi --strip"
},
"tree-sitter": [
{
Expand Down
25 changes: 19 additions & 6 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,25 @@
"name": "identifier"
},
{
"type": "STRING",
"value": ":"
},
{
"type": "SYMBOL",
"name": "type"
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ":"
},
{
"type": "SYMBOL",
"name": "type"
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "CHOICE",
Expand Down
Loading

0 comments on commit 8639a08

Please sign in to comment.