Skip to content

Commit

Permalink
refactor!: renaming and aligning with wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq committed Mar 10, 2024
1 parent 9d8c5fd commit 245510c
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 148 deletions.
72 changes: 47 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ class SyntaxNode {
return NodeMethods.type(this.tree);
}

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

get isExtra() {
Expand Down Expand Up @@ -270,9 +270,9 @@ class SyntaxNode {
return NodeMethods.fieldNameForChild(this.tree, childIndex);
}

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

childrenForFieldId(fieldId) {
Expand Down Expand Up @@ -330,6 +330,7 @@ class SyntaxNode {
marshalNode(this);
const cursor = NodeMethods.walk(this.tree);
cursor.tree = this.tree;
unmarshalNode(cursor.currentNode, this.tree);
return cursor;
}
}
Expand All @@ -352,15 +353,15 @@ Parser.prototype.setLanguage = function(language) {
return this;
};

Parser.prototype.getLanguage = function(language) {
Parser.prototype.getLanguage = function(_language) {
return this[languageSymbol] || null;
};

Parser.prototype.parse = function(input, oldTree, {bufferSize, includedRanges}={}) {
let getText, treeInput = input
if (typeof input === 'string') {
const inputString = input;
input = (offset, position) => inputString.slice(offset)
input = (offset, _position) => inputString.slice(offset)
getText = getTextFromString
} else {
getText = getTextFromFunction
Expand All @@ -387,7 +388,7 @@ Parser.prototype.parse = function(input, oldTree, {bufferSize, includedRanges}={
* TreeCursor
*/

const {startPosition, endPosition, currentNode, reset} = TreeCursor.prototype;
const {startPosition, endPosition, currentNode} = TreeCursor.prototype;

Object.defineProperties(TreeCursor.prototype, {
currentNode: {
Expand Down Expand Up @@ -424,13 +425,6 @@ Object.defineProperties(TreeCursor.prototype, {
}
});

TreeCursor.prototype.reset = function(node) {
marshalNode(node);
if (this instanceof TreeCursor && reset) {
reset.call(this);
}
}

/*
* Query
*/
Expand Down Expand Up @@ -627,7 +621,7 @@ Query.prototype._init = function() {
}

Query.prototype.matches = function(
rootNode,
node,
{
startPosition = ZERO_POINT,
endPosition = ZERO_POINT,
Expand All @@ -637,13 +631,13 @@ Query.prototype.matches = function(
maxStartDepth = 0xFFFFFFFF
} = {}
) {
marshalNode(rootNode);
const [returnedMatches, returnedNodes] = _matches.call(this, rootNode.tree,
marshalNode(node);
const [returnedMatches, returnedNodes] = _matches.call(this, node.tree,
startPosition.row, startPosition.column,
endPosition.row, endPosition.column,
startIndex, endIndex, matchLimit, maxStartDepth
);
const nodes = unmarshalNodes(returnedNodes, rootNode.tree);
const nodes = unmarshalNodes(returnedNodes, node.tree);
const results = [];

let i = 0
Expand Down Expand Up @@ -675,13 +669,24 @@ Query.prototype.matches = function(
return results;
}

Query.prototype.captures = function(rootNode, startPosition = ZERO_POINT, endPosition = ZERO_POINT) {
marshalNode(rootNode);
const [returnedMatches, returnedNodes] = _captures.call(this, rootNode.tree,
Query.prototype.captures = function(
node,
{
startPosition = ZERO_POINT,
endPosition = ZERO_POINT,
startIndex = 0,
endIndex = 0,
matchLimit = 0xFFFFFFFF,
maxStartDepth = 0xFFFFFFFF
} = {}
) {
marshalNode(node);
const [returnedMatches, returnedNodes] = _captures.call(this, node.tree,
startPosition.row, startPosition.column,
endPosition.row, endPosition.column
endPosition.row, endPosition.column,
startIndex, endIndex, matchLimit, maxStartDepth
);
const nodes = unmarshalNodes(returnedNodes, rootNode.tree);
const nodes = unmarshalNodes(returnedNodes, node.tree);
const results = [];

let i = 0
Expand Down Expand Up @@ -714,6 +719,23 @@ Query.prototype.captures = function(rootNode, startPosition = ZERO_POINT, endPos
return results;
}

/*
* LookaheadIterator
*/

LookaheadIterator.prototype[Symbol.iterator] = function() {
const self = this;
return {
next() {
if (self._next()) {
return {done: false, value: self.currentType};
}

return {done: true, value: ''};
},
};
}

/*
* Other functions
*/
Expand All @@ -730,7 +752,7 @@ function getTextFromFunction ({startIndex, endIndex}) {
const text = input(startIndex + result.length);
result += text;
}
return result.substr(0, goalLength);
return result.slice(0, goalLength);
}

const {pointTransferArray} = binding;
Expand Down Expand Up @@ -869,7 +891,7 @@ function initializeLanguageNodeClasses(language) {
}

function camelCase(name, upperCase) {
name = name.replace(/_(\w)/g, (match, letter) => letter.toUpperCase());
name = name.replace(/_(\w)/g, (_match, letter) => letter.toUpperCase());
if (upperCase) name = name[0].toUpperCase() + name.slice(1);
return name;
}
Expand Down
23 changes: 5 additions & 18 deletions src/lookaheaditerator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ void LookaheadIterator::Init(Napi::Env env, Napi::Object exports) {
auto *data = env.GetInstanceData<AddonData>();

Function ctor = DefineClass(env, "LookaheadIterator", {
InstanceAccessor("currentSymbol", &LookaheadIterator::CurrentSymbol, nullptr, napi_default_method),
InstanceAccessor("currentSymbolName", &LookaheadIterator::CurrentSymbolName, nullptr, napi_default_method),
InstanceAccessor("currentTypeId", &LookaheadIterator::CurrentTypeId, nullptr, napi_default_method),
InstanceAccessor("currentType", &LookaheadIterator::CurrentType, nullptr, napi_default_method),

InstanceMethod("reset", &LookaheadIterator::Reset, napi_default_method),
InstanceMethod("resetState", &LookaheadIterator::ResetState, napi_default_method),
InstanceMethod("next", &LookaheadIterator::Next, napi_default_method),
InstanceMethod("iterNames", &LookaheadIterator::IterNames, napi_default_method),
InstanceMethod("_next", &LookaheadIterator::Next, napi_default_method),
});

exports["LookaheadIterator"] = ctor;
Expand Down Expand Up @@ -75,12 +74,12 @@ LookaheadIterator *LookaheadIterator::UnwrapLookaheadIterator(const Napi::Value
return LookaheadIterator::Unwrap(js_iterator);
}

Napi::Value LookaheadIterator::CurrentSymbolName(const Napi::CallbackInfo &info) {
Napi::Value LookaheadIterator::CurrentType(const Napi::CallbackInfo &info) {
LookaheadIterator *iterator = UnwrapLookaheadIterator(info.This());
return Napi::String::New(info.Env(), ts_lookahead_iterator_current_symbol_name(iterator->iterator_));
}

Napi::Value LookaheadIterator::CurrentSymbol(const Napi::CallbackInfo &info) {
Napi::Value LookaheadIterator::CurrentTypeId(const Napi::CallbackInfo &info) {
LookaheadIterator *iterator = UnwrapLookaheadIterator(info.This());
return Napi::Number::New(info.Env(), ts_lookahead_iterator_current_symbol(iterator->iterator_));
}
Expand Down Expand Up @@ -120,16 +119,4 @@ Napi::Value LookaheadIterator::Next(const Napi::CallbackInfo &info) {
return Napi::Boolean::New(info.Env(), ts_lookahead_iterator_next(iterator->iterator_));
}

Napi::Value LookaheadIterator::IterNames(const Napi::CallbackInfo &info) {
LookaheadIterator *iterator = UnwrapLookaheadIterator(info.This());
auto result = Napi::Array::New(info.Env());

while (ts_lookahead_iterator_next(iterator->iterator_)) {
const char *name = ts_lookahead_iterator_current_symbol_name(iterator->iterator_);
result.Set(result.Length(), Napi::String::New(info.Env(), name));
}

return result;
}

} // namespace node_tree_sitter
5 changes: 2 additions & 3 deletions src/lookaheaditerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ class LookaheadIterator final : public Napi::ObjectWrap<LookaheadIterator> {
Napi::Value Reset(const Napi::CallbackInfo &);
Napi::Value ResetState(const Napi::CallbackInfo &);
Napi::Value Next(const Napi::CallbackInfo &);
Napi::Value IterNames(const Napi::CallbackInfo &);

Napi::Value CurrentSymbol(const Napi::CallbackInfo &);
Napi::Value CurrentSymbolName(const Napi::CallbackInfo &);
Napi::Value CurrentTypeId(const Napi::CallbackInfo &);
Napi::Value CurrentType(const Napi::CallbackInfo &);
};

} // namespace node_tree_sitter
Expand Down
39 changes: 17 additions & 22 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ Napi::Value Type(const Napi::CallbackInfo &info) {
return env.Undefined();
}

Napi::Value GrammarName(const Napi::CallbackInfo &info) {
Napi::Value GrammarType(const Napi::CallbackInfo &info) {
Env env = info.Env();
const Tree *tree = Tree::UnwrapTree(info[0]);
TSNode node = UnmarshalNode(env, tree);
Expand Down Expand Up @@ -635,33 +635,30 @@ Napi::Value ChildrenForFieldName(const Napi::CallbackInfo &info) {
}
std::string field_name = info[1].As<String>().Utf8Value();

if (!info[2].IsObject()) {
throw TypeError::New(env, "Second argument must be a cursor");
}
TSTreeCursor *cursor = &TreeCursor::Unwrap(info[2].As<Object>())->cursor_;
TSTreeCursor cursor = ts_tree_cursor_new(node);

const TSLanguage *language = ts_tree_language(node.tree);
TSFieldId field_id = ts_language_field_id_for_name(language, field_name.c_str(), field_name.length());

bool done = field_id == 0;
if (!done) {
ts_tree_cursor_reset(cursor, node);
ts_tree_cursor_goto_first_child(cursor);
ts_tree_cursor_reset(&cursor, node);
ts_tree_cursor_goto_first_child(&cursor);
}

vector<TSNode> result;
while (!done) {
while (ts_tree_cursor_current_field_id(cursor) != field_id) {
if (!ts_tree_cursor_goto_next_sibling(cursor)) {
while (ts_tree_cursor_current_field_id(&cursor) != field_id) {
if (!ts_tree_cursor_goto_next_sibling(&cursor)) {
done = true;
break;
}
}
if (done) {
break;
}
TSNode result_node = ts_tree_cursor_current_node(cursor);
if (!ts_tree_cursor_goto_next_sibling(cursor)) {
TSNode result_node = ts_tree_cursor_current_node(&cursor);
if (!ts_tree_cursor_goto_next_sibling(&cursor)) {
done = true;
}
result.push_back(result_node);
Expand All @@ -683,30 +680,28 @@ Napi::Value ChildrenForFieldId(const Napi::CallbackInfo &info) {
}
uint32_t field_id = info[1].As<Number>().Uint32Value();

if (!info[2].IsObject()) {
throw TypeError::New(env, "Second argument must be a cursor");
}
TSTreeCursor *cursor = &TreeCursor::Unwrap(info[2].As<Object>())->cursor_;

TSTreeCursor cursor = ts_tree_cursor_new(node);

bool done = field_id == 0;
if (!done) {
ts_tree_cursor_reset(cursor, node);
ts_tree_cursor_goto_first_child(cursor);
ts_tree_cursor_reset(&cursor, node);
ts_tree_cursor_goto_first_child(&cursor);
}

vector<TSNode> result;
while (!done) {
while (ts_tree_cursor_current_field_id(cursor) != field_id) {
if (!ts_tree_cursor_goto_next_sibling(cursor)) {
while (ts_tree_cursor_current_field_id(&cursor) != field_id) {
if (!ts_tree_cursor_goto_next_sibling(&cursor)) {
done = true;
break;
}
}
if (done) {
break;
}
TSNode result_node = ts_tree_cursor_current_node(cursor);
if (!ts_tree_cursor_goto_next_sibling(cursor)) {
TSNode result_node = ts_tree_cursor_current_node(&cursor);
if (!ts_tree_cursor_goto_next_sibling(&cursor)) {
done = true;
}
result.push_back(result_node);
Expand Down Expand Up @@ -998,7 +993,7 @@ void Init(Napi::Env env, Napi::Object exports) {
{"typeId", TypeId},
{"grammarId", GrammarId},
{"type", Type},
{"grammarName", GrammarName},
{"grammarType", GrammarType},
{"isNamed", IsNamed},
{"isExtra", IsExtra},
{"hasChanges", HasChanges},
Expand Down
Loading

0 comments on commit 245510c

Please sign in to comment.