update to newer syntax

This commit is contained in:
Daniella / Tove 2024-09-30 19:04:31 +02:00
parent 99b2e4c19a
commit ba3ec6277d
10 changed files with 2696 additions and 2359 deletions

View file

@ -2,18 +2,29 @@
"targets": [ "targets": [
{ {
"target_name": "tree_sitter_spl_binding", "target_name": "tree_sitter_spl_binding",
"dependencies": [
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
],
"include_dirs": [ "include_dirs": [
"<!(node -e \"require('nan')\")", "src",
"src"
], ],
"sources": [ "sources": [
"bindings/node/binding.cc", "bindings/node/binding.cc",
"src/parser.c", "src/parser.c",
# If your language uses an external scanner, add it here. # NOTE: if your language has an external scanner, add it here.
], ],
"conditions": [
["OS!='win'", {
"cflags_c": [ "cflags_c": [
"-std=c99", "-std=c11",
] ],
}, { # OS == "win"
"cflags_c": [
"/std:c11",
"/utf-8",
],
}],
],
} }
] ]
} }

View file

@ -1,28 +1,20 @@
#include "tree_sitter/parser.h" #include <napi.h>
#include <node.h>
#include "nan.h"
using namespace v8; typedef struct TSLanguage TSLanguage;
extern "C" TSLanguage * tree_sitter_spl(); extern "C" TSLanguage *tree_sitter_spl();
namespace { // "tree-sitter", "language" hashed with BLAKE2
const napi_type_tag LANGUAGE_TYPE_TAG = {
0x8AF2E5212AD58ABF, 0xD5006CAD83ABBA16
};
NAN_METHOD(New) {} Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports["name"] = Napi::String::New(env, "spl");
void Init(Local<Object> exports, Local<Object> module) { auto language = Napi::External<TSLanguage>::New(env, tree_sitter_spl());
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New); language.TypeTag(&LANGUAGE_TYPE_TAG);
tpl->SetClassName(Nan::New("Language").ToLocalChecked()); exports["language"] = language;
tpl->InstanceTemplate()->SetInternalFieldCount(1); return exports;
Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_spl());
Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("spl").ToLocalChecked());
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
} }
NODE_MODULE(tree_sitter_spl_binding, Init) NODE_API_MODULE(tree_sitter_spl_binding, Init)
} // namespace

View file

@ -1,18 +1,6 @@
try { const root = require("path").join(__dirname, "..", "..");
module.exports = require("../../build/Release/tree_sitter_spl_binding");
} catch (error1) { module.exports = require("node-gyp-build")(root);
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require("../../build/Debug/tree_sitter_spl_binding");
} catch (error2) {
if (error2.code !== 'MODULE_NOT_FOUND') {
throw error2;
}
throw error1
}
}
try { try {
module.exports.nodeTypeInfo = require("../../src/node-types.json"); module.exports.nodeTypeInfo = require("../../src/node-types.json");

View file

@ -7,6 +7,9 @@ fn main() {
.flag_if_supported("-Wno-unused-parameter") .flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-unused-but-set-variable") .flag_if_supported("-Wno-unused-but-set-variable")
.flag_if_supported("-Wno-trigraphs"); .flag_if_supported("-Wno-trigraphs");
#[cfg(target_env = "msvc")]
c_config.flag("-utf-8");
let parser_path = src_dir.join("parser.c"); let parser_path = src_dir.join("parser.c");
c_config.file(&parser_path); c_config.file(&parser_path);

View file

@ -3,7 +3,7 @@ module.exports = grammar({
rules: { rules: {
source_file: $ => repeat($._statement), source_file: $ => repeat($._statement),
_statement: $ => seq( _unspaced_statement: $ => seq(
choice( choice(
$.string, $.string,
$.function_definition, $.function_definition,
@ -17,13 +17,17 @@ module.exports = grammar({
$.include, $.include,
$.def, $.def,
$.number, $.number,
$.expression, $.call_expr,
seq( seq(
repeat('&'), repeat('&'),
$.call $.call
), ),
), ),
optional(';'), optional(';'),
optional($.call_expr),
),
_statement: $ => seq(
$._unspaced_statement,
$._spacing, $._spacing,
), ),
function_definition: $ => choice($.func, $.block), function_definition: $ => choice($.func, $.block),
@ -37,7 +41,7 @@ module.exports = grammar({
repeat($._statement), repeat($._statement),
'}', '}',
), ),
identifier: $ => /[^ \n\r\t:;&{}"']+/, identifier: $ => /[^ \n\r\t<>:;&{}"']+/,
call: $ => seq( call: $ => seq(
choice( choice(
seq( seq(
@ -50,7 +54,7 @@ module.exports = grammar({
), ),
number: $ => /\d+(\.\d+)?/, number: $ => /\d+(\.\d+)?/,
string: $ => seq('"', repeat(choice(/\\./, /./)), '"'), string: $ => seq('"', repeat(choice(/\\./, /./)), '"'),
expression: $ => seq('<{', $._spacing, repeat($._statement), '}'), call_expr: $ => seq('<', optional($._spacing), repeat($._statement), $._unspaced_statement, '>'),
with_expr: $ => seq( with_expr: $ => seq(
'with', $._spacing, 'with', $._spacing,
repeat($.identifier), repeat($.identifier),

View file

@ -3,8 +3,11 @@
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "",
"main": "bindings/node", "main": "bindings/node",
"types": "bindings/node",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1",
"install": "node-gyp-build",
"prebuildify": "prebuildify --napi --strip"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -17,10 +20,20 @@
}, },
"homepage": "https://github.com/tudbut/tree-sitter-spl#readme", "homepage": "https://github.com/tudbut/tree-sitter-spl#readme",
"dependencies": { "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": { "devDependencies": {
"tree-sitter-cli": "^0.20.8" "tree-sitter-cli": "^0.20.8",
"prebuildify": "^6.0.0"
}, },
"tree-sitter": [ "tree-sitter": [
{ {
@ -31,5 +44,13 @@
"sbl" "sbl"
] ]
} }
],
"files": [
"grammar.js",
"binding.gyp",
"prebuilds/**",
"bindings/node/*",
"queries/*",
"src/**"
] ]
} }

View file

@ -8,7 +8,7 @@
"name": "_statement" "name": "_statement"
} }
}, },
"_statement": { "_unspaced_statement": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
@ -64,7 +64,7 @@
}, },
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "expression" "name": "call_expr"
}, },
{ {
"type": "SEQ", "type": "SEQ",
@ -96,6 +96,27 @@
} }
] ]
}, },
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "call_expr"
},
{
"type": "BLANK"
}
]
}
]
},
"_statement": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "_unspaced_statement"
},
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_spacing" "name": "_spacing"
@ -241,7 +262,7 @@
}, },
"identifier": { "identifier": {
"type": "PATTERN", "type": "PATTERN",
"value": "[^ \\n\\r\\t:;&{}\"']+" "value": "[^ \\n\\r\\t<>:;&{}\"']+"
}, },
"call": { "call": {
"type": "SEQ", "type": "SEQ",
@ -315,17 +336,25 @@
} }
] ]
}, },
"expression": { "call_expr": {
"type": "SEQ", "type": "SEQ",
"members": [ "members": [
{ {
"type": "STRING", "type": "STRING",
"value": "<{" "value": "<"
}, },
{
"type": "CHOICE",
"members": [
{ {
"type": "SYMBOL", "type": "SYMBOL",
"name": "_spacing" "name": "_spacing"
}, },
{
"type": "BLANK"
}
]
},
{ {
"type": "REPEAT", "type": "REPEAT",
"content": { "content": {
@ -333,9 +362,13 @@
"name": "_statement" "name": "_statement"
} }
}, },
{
"type": "SYMBOL",
"name": "_unspaced_statement"
},
{ {
"type": "STRING", "type": "STRING",
"value": "}" "value": ">"
} }
] ]
}, },
@ -751,4 +784,3 @@
"inline": [], "inline": [],
"supertypes": [] "supertypes": []
} }

View file

@ -15,6 +15,10 @@
"type": "call", "type": "call",
"named": true "named": true
}, },
{
"type": "call_expr",
"named": true
},
{ {
"type": "catch", "type": "catch",
"named": true "named": true
@ -23,10 +27,6 @@
"type": "def", "type": "def",
"named": true "named": true
}, },
{
"type": "expression",
"named": true
},
{ {
"type": "function_definition", "type": "function_definition",
"named": true "named": true
@ -82,6 +82,10 @@
"type": "call", "type": "call",
"named": true "named": true
}, },
{
"type": "call_expr",
"named": true
},
{ {
"type": "catch", "type": "catch",
"named": true "named": true
@ -90,10 +94,6 @@
"type": "def", "type": "def",
"named": true "named": true
}, },
{
"type": "expression",
"named": true
},
{ {
"type": "function_definition", "type": "function_definition",
"named": true "named": true
@ -167,6 +167,73 @@
] ]
} }
}, },
{
"type": "call_expr",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": true,
"types": [
{
"type": "array",
"named": true
},
{
"type": "call",
"named": true
},
{
"type": "call_expr",
"named": true
},
{
"type": "catch",
"named": true
},
{
"type": "def",
"named": true
},
{
"type": "function_definition",
"named": true
},
{
"type": "if",
"named": true
},
{
"type": "include",
"named": true
},
{
"type": "number",
"named": true
},
{
"type": "string",
"named": true
},
{
"type": "type_definition",
"named": true
},
{
"type": "use",
"named": true
},
{
"type": "while",
"named": true
},
{
"type": "with_expr",
"named": true
}
]
}
},
{ {
"type": "catch", "type": "catch",
"named": true, "named": true,
@ -183,6 +250,10 @@
"type": "call", "type": "call",
"named": true "named": true
}, },
{
"type": "call_expr",
"named": true
},
{ {
"type": "catch", "type": "catch",
"named": true "named": true
@ -191,10 +262,6 @@
"type": "def", "type": "def",
"named": true "named": true
}, },
{
"type": "expression",
"named": true
},
{ {
"type": "function_definition", "type": "function_definition",
"named": true "named": true
@ -249,73 +316,6 @@
] ]
} }
}, },
{
"type": "expression",
"named": true,
"fields": {},
"children": {
"multiple": true,
"required": false,
"types": [
{
"type": "array",
"named": true
},
{
"type": "call",
"named": true
},
{
"type": "catch",
"named": true
},
{
"type": "def",
"named": true
},
{
"type": "expression",
"named": true
},
{
"type": "function_definition",
"named": true
},
{
"type": "if",
"named": true
},
{
"type": "include",
"named": true
},
{
"type": "number",
"named": true
},
{
"type": "string",
"named": true
},
{
"type": "type_definition",
"named": true
},
{
"type": "use",
"named": true
},
{
"type": "while",
"named": true
},
{
"type": "with_expr",
"named": true
}
]
}
},
{ {
"type": "func", "type": "func",
"named": true, "named": true,
@ -374,6 +374,10 @@
"type": "call", "type": "call",
"named": true "named": true
}, },
{
"type": "call_expr",
"named": true
},
{ {
"type": "catch", "type": "catch",
"named": true "named": true
@ -382,10 +386,6 @@
"type": "def", "type": "def",
"named": true "named": true
}, },
{
"type": "expression",
"named": true
},
{ {
"type": "function_definition", "type": "function_definition",
"named": true "named": true
@ -456,6 +456,10 @@
"type": "call", "type": "call",
"named": true "named": true
}, },
{
"type": "call_expr",
"named": true
},
{ {
"type": "catch", "type": "catch",
"named": true "named": true
@ -464,10 +468,6 @@
"type": "def", "type": "def",
"named": true "named": true
}, },
{
"type": "expression",
"named": true
},
{ {
"type": "function_definition", "type": "function_definition",
"named": true "named": true
@ -566,6 +566,10 @@
"type": "call", "type": "call",
"named": true "named": true
}, },
{
"type": "call_expr",
"named": true
},
{ {
"type": "catch", "type": "catch",
"named": true "named": true
@ -574,10 +578,6 @@
"type": "def", "type": "def",
"named": true "named": true
}, },
{
"type": "expression",
"named": true
},
{ {
"type": "function_definition", "type": "function_definition",
"named": true "named": true
@ -653,13 +653,17 @@
"named": false "named": false
}, },
{ {
"type": "<{", "type": "<",
"named": false "named": false
}, },
{ {
"type": "=", "type": "=",
"named": false "named": false
}, },
{
"type": ">",
"named": false
},
{ {
"type": "@", "type": "@",
"named": false "named": false

File diff suppressed because it is too large Load diff

View file

@ -13,9 +13,8 @@ extern "C" {
#define ts_builtin_sym_end 0 #define ts_builtin_sym_end 0
#define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024 #define TREE_SITTER_SERIALIZATION_BUFFER_SIZE 1024
typedef uint16_t TSStateId;
#ifndef TREE_SITTER_API_H_ #ifndef TREE_SITTER_API_H_
typedef uint16_t TSStateId;
typedef uint16_t TSSymbol; typedef uint16_t TSSymbol;
typedef uint16_t TSFieldId; typedef uint16_t TSFieldId;
typedef struct TSLanguage TSLanguage; typedef struct TSLanguage TSLanguage;
@ -87,6 +86,11 @@ typedef union {
} entry; } entry;
} TSParseActionEntry; } TSParseActionEntry;
typedef struct {
int32_t start;
int32_t end;
} TSCharacterRange;
struct TSLanguage { struct TSLanguage {
uint32_t version; uint32_t version;
uint32_t symbol_count; uint32_t symbol_count;
@ -126,13 +130,38 @@ struct TSLanguage {
const TSStateId *primary_state_ids; const TSStateId *primary_state_ids;
}; };
static inline bool set_contains(TSCharacterRange *ranges, uint32_t len, int32_t lookahead) {
uint32_t index = 0;
uint32_t size = len - index;
while (size > 1) {
uint32_t half_size = size / 2;
uint32_t mid_index = index + half_size;
TSCharacterRange *range = &ranges[mid_index];
if (lookahead >= range->start && lookahead <= range->end) {
return true;
} else if (lookahead > range->end) {
index = mid_index;
}
size -= half_size;
}
TSCharacterRange *range = &ranges[index];
return (lookahead >= range->start && lookahead <= range->end);
}
/* /*
* Lexer Macros * Lexer Macros
*/ */
#ifdef _MSC_VER
#define UNUSED __pragma(warning(suppress : 4101))
#else
#define UNUSED __attribute__((unused))
#endif
#define START_LEXER() \ #define START_LEXER() \
bool result = false; \ bool result = false; \
bool skip = false; \ bool skip = false; \
UNUSED \
bool eof = false; \ bool eof = false; \
int32_t lookahead; \ int32_t lookahead; \
goto start; \ goto start; \
@ -148,6 +177,17 @@ struct TSLanguage {
goto next_state; \ goto next_state; \
} }
#define ADVANCE_MAP(...) \
{ \
static const uint16_t map[] = { __VA_ARGS__ }; \
for (uint32_t i = 0; i < sizeof(map) / sizeof(map[0]); i += 2) { \
if (map[i] == lookahead) { \
state = map[i + 1]; \
goto next_state; \
} \
} \
}
#define SKIP(state_value) \ #define SKIP(state_value) \
{ \ { \
skip = true; \ skip = true; \
@ -166,7 +206,7 @@ struct TSLanguage {
* Parse Table Macros * Parse Table Macros
*/ */
#define SMALL_STATE(id) id - LARGE_STATE_COUNT #define SMALL_STATE(id) ((id) - LARGE_STATE_COUNT)
#define STATE(id) id #define STATE(id) id
@ -176,7 +216,7 @@ struct TSLanguage {
{{ \ {{ \
.shift = { \ .shift = { \
.type = TSParseActionTypeShift, \ .type = TSParseActionTypeShift, \
.state = state_value \ .state = (state_value) \
} \ } \
}} }}
@ -184,7 +224,7 @@ struct TSLanguage {
{{ \ {{ \
.shift = { \ .shift = { \
.type = TSParseActionTypeShift, \ .type = TSParseActionTypeShift, \
.state = state_value, \ .state = (state_value), \
.repetition = true \ .repetition = true \
} \ } \
}} }}
@ -197,13 +237,14 @@ struct TSLanguage {
} \ } \
}} }}
#define REDUCE(symbol_val, child_count_val, ...) \ #define REDUCE(symbol_name, children, precedence, prod_id) \
{{ \ {{ \
.reduce = { \ .reduce = { \
.type = TSParseActionTypeReduce, \ .type = TSParseActionTypeReduce, \
.symbol = symbol_val, \ .symbol = symbol_name, \
.child_count = child_count_val, \ .child_count = children, \
__VA_ARGS__ \ .dynamic_precedence = precedence, \
.production_id = prod_id \
}, \ }, \
}} }}