Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ env:
ELECTRON_PREBUILD_CMD: npx prebuild -r electron -t 3.0.0 -t 4.0.0 -t 5.0.0 -t 6.0.0 -t 7.0.0 -t 8.0.0 -t 9.0.0 -t 10.0.0 -t 11.0.0 -t 12.0.0 -t 13.0.0 -t 14.0.0 -t 15.0.0 -t 16.0.0 -t 17.0.0 -t 18.0.0 -t 19.0.0 -t 20.0.0 -t 21.0.0 -t 22.0.0 -t 23.0.0 -t 24.0.0 -t 25.0.0 --strip -u ${{ secrets.GH_TOKEN }}

jobs:

test:
strategy:
matrix:
Expand Down
1 change: 1 addition & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"src/conversions.cc",
"src/language.cc",
"src/logger.cc",
"src/lookaheaditerator.cc",
"src/node.cc",
"src/parser.cc",
"src/query.cc",
Expand Down
110 changes: 101 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ try {
}

const util = require('util')
const {Query, Parser, NodeMethods, Tree, TreeCursor} = binding;
const {Query, Parser, NodeMethods, Tree, TreeCursor, LookaheadIterator} = binding;

/*
* Tree
*/

const {rootNode, edit} = Tree.prototype;
const {rootNode, rootNodeWithOffset, edit} = Tree.prototype;

Object.defineProperty(Tree.prototype, 'rootNode', {
get() {
return unmarshalNode(rootNode.call(this), this);
}
});

Tree.prototype.rootNodeWithOffset = function(offset_bytes, offset_extent) {
return unmarshalNode(rootNodeWithOffset.call(this, offset_bytes, offset_extent.row, offset_extent.column), this);
}

Tree.prototype.edit = function(arg) {
edit.call(
this,
Expand Down Expand Up @@ -58,16 +62,31 @@ class SyntaxNode {
'}'
}

get type() {
get id() {
marshalNode(this);
return NodeMethods.type(this.tree);
return NodeMethods.id(this.tree);
}

get typeId() {
marshalNode(this);
return NodeMethods.typeId(this.tree);
}

get grammarId() {
marshalNode(this);
return NodeMethods.grammarId(this.tree);
}

get type() {
marshalNode(this);
return NodeMethods.type(this.tree);
}

get grammarName() {
marshalNode(this);
return NodeMethods.grammarName(this.tree);
}

get isNamed() {
marshalNode(this);
return NodeMethods.isNamed(this.tree);
Expand Down Expand Up @@ -164,6 +183,21 @@ class SyntaxNode {
return unmarshalNode(NodeMethods.previousNamedSibling(this.tree), this.tree);
}

get parseState() {
marshalNode(this);
return NodeMethods.parseState(this.tree);
}

get nextParseState() {
marshalNode(this);
return NodeMethods.nextParseState(this.tree);
}

get descendantCount() {
marshalNode(this);
return NodeMethods.descendantCount(this.tree);
}

hasChanges() {
marshalNode(this);
return NodeMethods.hasChanges(this.tree);
Expand All @@ -179,6 +213,16 @@ class SyntaxNode {
return NodeMethods.isMissing(this.tree);
}

isExtra() {
marshalNode(this);
return NodeMethods.isExtra(this.tree);
}

isError() {
marshalNode(this);
return NodeMethods.isError(this.tree);
}

toString() {
marshalNode(this);
return NodeMethods.toString(this.tree);
Expand All @@ -194,6 +238,31 @@ class SyntaxNode {
return unmarshalNode(NodeMethods.namedChild(this.tree, index), this.tree);
}

childForFieldName(fieldName) {
marshalNode(this);
return unmarshalNode(NodeMethods.childForFieldName(this.tree, fieldName), this.tree);
}

childForFieldId(fieldId) {
marshalNode(this);
return unmarshalNode(NodeMethods.childForFieldId(this.tree, fieldId), this.tree);
}

fieldNameForChild(childIndex) {
marshalNode(this);
return NodeMethods.fieldNameForChild(this.tree, childIndex);
}

childrenForFieldName(fieldName, cursor) {
marshalNode(this);
return unmarshalNodes(NodeMethods.childrenForFieldName(this.tree, fieldName, cursor), this.tree);
}

childrenForFieldId(fieldId) {
marshalNode(this);
return unmarshalNodes(NodeMethods.childrenForFieldId(this.tree, fieldId), this.tree);
}

firstChildForIndex(index) {
marshalNode(this);
return unmarshalNode(NodeMethods.firstChildForIndex(this.tree, index), this.tree);
Expand Down Expand Up @@ -282,7 +351,7 @@ Parser.prototype.parse = function(input, oldTree, {bufferSize, includedRanges}={
input,
oldTree,
bufferSize,
includedRanges
includedRanges,
);
if (tree) {
tree.input = treeInput
Expand Down Expand Up @@ -467,11 +536,22 @@ Query.prototype._init = function() {
this.refutedProperties = Object.freeze(refutedProperties);
}

Query.prototype.matches = function(rootNode, startPosition = ZERO_POINT, endPosition = ZERO_POINT) {
Query.prototype.matches = function(
rootNode,
{
startPosition = ZERO_POINT,
endPosition = ZERO_POINT,
startIndex = 0,
endIndex = 0,
matchLimit = 0xFFFFFFFF,
maxStartDepth = 0xFFFFFFFF
} = {}
) {
marshalNode(rootNode);
const [returnedMatches, returnedNodes] = _matches.call(this, rootNode.tree,
startPosition.row, startPosition.column,
endPosition.row, endPosition.column
endPosition.row, endPosition.column,
startIndex, endIndex, matchLimit, maxStartDepth
);
const nodes = unmarshalNodes(returnedNodes, rootNode.tree);
const results = [];
Expand Down Expand Up @@ -505,11 +585,22 @@ Query.prototype.matches = function(rootNode, startPosition = ZERO_POINT, endPosi
return results;
}

Query.prototype.captures = function(rootNode, startPosition = ZERO_POINT, endPosition = ZERO_POINT) {
Query.prototype.captures = function(
rootNode,
{
startPosition = ZERO_POINT,
endPosition = ZERO_POINT,
startIndex = 0,
endIndex = 0,
matchLimit = 0xFFFFFFFF,
maxStartDepth = 0xFFFFFFFF
} = {}
) {
marshalNode(rootNode);
const [returnedMatches, returnedNodes] = _captures.call(this, rootNode.tree,
startPosition.row, startPosition.column,
endPosition.row, endPosition.column
endPosition.row, endPosition.column,
startIndex, endIndex, matchLimit, maxStartDepth
);
const nodes = unmarshalNodes(returnedNodes, rootNode.tree);
const results = [];
Expand Down Expand Up @@ -709,3 +800,4 @@ module.exports.Query = Query;
module.exports.Tree = Tree;
module.exports.SyntaxNode = SyntaxNode;
module.exports.TreeCursor = TreeCursor;
module.exports.LookaheadIterator = LookaheadIterator;
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@
"prebuild-install": "^7.1.1"
},
"devDependencies": {
"@types/node": "^20.5.9",
"@types/node": "^20.6.0",
"chai": "^4.3.8",
"mocha": "^8.4.0",
"mocha": "^10.2.0",
"node-gyp": "^9.4.0",
"prebuild": "^12.0.0",
"tree-sitter-javascript": "https://github.com/tree-sitter/tree-sitter-javascript.git#master"
"tmp": "^0.2.1",
"tree-sitter-c": "https://github.com/tree-sitter/tree-sitter-c.git#master",
"tree-sitter-embedded-template": "https://github.com/tree-sitter/tree-sitter-embedded-template.git#master",
"tree-sitter-html": "https://github.com/tree-sitter/tree-sitter-html.git#master",
"tree-sitter-java": "https://github.com/tree-sitter/tree-sitter-java.git#master",
"tree-sitter-javascript": "https://github.com/tree-sitter/tree-sitter-javascript.git#master",
"tree-sitter-json": "https://github.com/tree-sitter/tree-sitter-json.git#master",
"tree-sitter-python": "https://github.com/tree-sitter/tree-sitter-python.git#master",
"tree-sitter-ruby": "https://github.com/tree-sitter/tree-sitter-ruby.git#master",
"tree-sitter-rust": "https://github.com/tree-sitter/tree-sitter-rust.git#master"
},
"scripts": {
"install": "prebuild-install || node-gyp rebuild",
Expand Down
9 changes: 6 additions & 3 deletions src/binding.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include <node.h>
#include <v8.h>
#include "./language.h"
#include "./conversions.h"
#include "./lookaheaditerator.h"
#include "./node.h"
#include "./parser.h"
#include "./query.h"
#include "./tree.h"
#include "./tree_cursor.h"
#include "./conversions.h"

#include <node.h>
#include <v8.h>

namespace node_tree_sitter {

Expand All @@ -16,6 +18,7 @@ void InitAll(Local<Object> exports, Local<Value> m_, void* v_) {
InitConversions(exports);
node_methods::Init(exports);
language_methods::Init(exports);
LookaheadIterator::Init(exports);
Parser::Init(exports);
Query::Init(exports);
Tree::Init(exports);
Expand Down
21 changes: 12 additions & 9 deletions src/conversions.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "./node.h"
#include <nan.h>
#include <tree_sitter/api.h>
#include <v8.h>
#include "./conversions.h"
#include "tree_sitter/api.h"

#include <cmath>
#include <nan.h>
#include <v8.h>

namespace node_tree_sitter {

Expand All @@ -16,8 +17,10 @@ Nan::Persistent<String> start_position_key;
Nan::Persistent<String> end_index_key;
Nan::Persistent<String> end_position_key;

static unsigned BYTES_PER_CHARACTER = 2;
static uint32_t *point_transfer_buffer;
namespace {
const unsigned BYTES_PER_CHARACTER = 2;
uint32_t *point_transfer_buffer;
} // namespace

void InitConversions(Local<Object> exports) {
row_key.Reset(Nan::Persistent<String>(Nan::New("row").ToLocalChecked()));
Expand All @@ -27,7 +30,7 @@ void InitConversions(Local<Object> exports) {
end_index_key.Reset(Nan::Persistent<String>(Nan::New("endIndex").ToLocalChecked()));
end_position_key.Reset(Nan::Persistent<String>(Nan::New("endPosition").ToLocalChecked()));

point_transfer_buffer = static_cast<uint32_t *>(malloc(2 * sizeof(uint32_t)));
point_transfer_buffer = new uint32_t[2];

#if defined(_MSC_VER) && NODE_RUNTIME_ELECTRON && NODE_MODULE_VERSION >= 89
auto nodeBuffer = node::Buffer::New(Isolate::GetCurrent(), (char *)point_transfer_buffer, 2 * sizeof(uint32_t), [](char *data, void *hint) {}, nullptr)
Expand Down Expand Up @@ -74,9 +77,9 @@ Nan::Maybe<TSRange> RangeFromJS(const Local<Value> &arg) {
Nan::ThrowTypeError("Range must be a {startPosition, endPosition, startIndex, endIndex} object"); \
return Nan::Nothing<TSRange>(); \
} \
auto field = Convert(value.ToLocalChecked()); \
if (field.IsJust()) { \
result.field = field.FromJust(); \
auto (field) = Convert(value.ToLocalChecked()); \
if ((field).IsJust()) { \
result.field = (field).FromJust(); \
} else { \
return Nan::Nothing<TSRange>(); \
} \
Expand Down
3 changes: 2 additions & 1 deletion src/conversions.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#ifndef NODE_TREE_SITTER_CONVERSIONS_H_
#define NODE_TREE_SITTER_CONVERSIONS_H_

#include "tree_sitter/api.h"

#include <nan.h>
#include <v8.h>
#include <tree_sitter/api.h>

namespace node_tree_sitter {

Expand Down
Loading