From 245510cfefeeef201a50653d3efaa3fd76fbb316 Mon Sep 17 00:00:00 2001 From: Amaan Qureshi Date: Sat, 9 Mar 2024 01:27:58 -0500 Subject: [PATCH] refactor!: renaming and aligning with wasm --- index.js | 72 +++++++---- src/lookaheaditerator.cc | 23 +--- src/lookaheaditerator.h | 5 +- src/node.cc | 39 +++--- src/query.cc | 31 +++-- src/tree_cursor.cc | 12 ++ src/tree_cursor.h | 2 + ...tor_test.js => lookahead_iterable_test.js} | 11 +- test/node_test.js | 5 +- tree-sitter.d.ts | 121 ++++++++---------- 10 files changed, 173 insertions(+), 148 deletions(-) rename test/{lookahead_iterator_test.js => lookahead_iterable_test.js} (76%) diff --git a/index.js b/index.js index d6ae4088..e62ade70 100644 --- a/index.js +++ b/index.js @@ -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() { @@ -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) { @@ -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; } } @@ -352,7 +353,7 @@ Parser.prototype.setLanguage = function(language) { return this; }; -Parser.prototype.getLanguage = function(language) { +Parser.prototype.getLanguage = function(_language) { return this[languageSymbol] || null; }; @@ -360,7 +361,7 @@ 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 @@ -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: { @@ -424,13 +425,6 @@ Object.defineProperties(TreeCursor.prototype, { } }); -TreeCursor.prototype.reset = function(node) { - marshalNode(node); - if (this instanceof TreeCursor && reset) { - reset.call(this); - } -} - /* * Query */ @@ -627,7 +621,7 @@ Query.prototype._init = function() { } Query.prototype.matches = function( - rootNode, + node, { startPosition = ZERO_POINT, endPosition = ZERO_POINT, @@ -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 @@ -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 @@ -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 */ @@ -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; @@ -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; } diff --git a/src/lookaheaditerator.cc b/src/lookaheaditerator.cc index 902c0273..fe041e7e 100644 --- a/src/lookaheaditerator.cc +++ b/src/lookaheaditerator.cc @@ -13,13 +13,12 @@ void LookaheadIterator::Init(Napi::Env env, Napi::Object exports) { auto *data = env.GetInstanceData(); 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; @@ -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_)); } @@ -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 diff --git a/src/lookaheaditerator.h b/src/lookaheaditerator.h index b996a9aa..49a6b4a7 100644 --- a/src/lookaheaditerator.h +++ b/src/lookaheaditerator.h @@ -23,10 +23,9 @@ class LookaheadIterator final : public Napi::ObjectWrap { 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 diff --git a/src/node.cc b/src/node.cc index 2d20116c..7dba9d80 100644 --- a/src/node.cc +++ b/src/node.cc @@ -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); @@ -635,24 +635,21 @@ Napi::Value ChildrenForFieldName(const Napi::CallbackInfo &info) { } std::string field_name = info[1].As().Utf8Value(); - if (!info[2].IsObject()) { - throw TypeError::New(env, "Second argument must be a cursor"); - } - TSTreeCursor *cursor = &TreeCursor::Unwrap(info[2].As())->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 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; } @@ -660,8 +657,8 @@ Napi::Value ChildrenForFieldName(const Napi::CallbackInfo &info) { 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); @@ -683,21 +680,19 @@ Napi::Value ChildrenForFieldId(const Napi::CallbackInfo &info) { } uint32_t field_id = info[1].As().Uint32Value(); - if (!info[2].IsObject()) { - throw TypeError::New(env, "Second argument must be a cursor"); - } - TSTreeCursor *cursor = &TreeCursor::Unwrap(info[2].As())->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 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; } @@ -705,8 +700,8 @@ Napi::Value ChildrenForFieldId(const Napi::CallbackInfo &info) { 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); @@ -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}, diff --git a/src/query.cc b/src/query.cc index d67a5a77..1c807397 100644 --- a/src/query.cc +++ b/src/query.cc @@ -207,14 +207,14 @@ Napi::Value Query::Matches(const Napi::CallbackInfo &info) { } TSQuery *ts_query = query->query_; - TSNode rootNode = node_methods::UnmarshalNode(env, tree); + TSNode root_node = node_methods::UnmarshalNode(env, tree); TSPoint start_point = {start_row, start_column}; TSPoint end_point = {end_row, end_column}; ts_query_cursor_set_point_range(data->ts_query_cursor, start_point, end_point); ts_query_cursor_set_byte_range(data->ts_query_cursor, start_index, end_index); ts_query_cursor_set_match_limit(data->ts_query_cursor, match_limit); ts_query_cursor_set_max_start_depth(data->ts_query_cursor, max_start_depth); - ts_query_cursor_exec(data->ts_query_cursor, ts_query, rootNode); + ts_query_cursor_exec(data->ts_query_cursor, ts_query, root_node); Array js_matches = Array::New(env); unsigned index = 0; @@ -252,22 +252,34 @@ Napi::Value Query::Captures(const Napi::CallbackInfo &info) { auto *data = env.GetInstanceData(); Query *query = Query::UnwrapQuery(info.This()); const Tree *tree = Tree::UnwrapTree(info[0]); - uint32_t start_row = 0; + + uint32_t start_row = 0, start_column = 0, end_row = 0, end_column = 0, start_index = 0, end_index = 0, + match_limit = UINT32_MAX, max_start_depth = UINT32_MAX; + if (info.Length() > 1 && info[1].IsNumber()) { start_row = info[1].As().Uint32Value(); } - uint32_t start_column = 0; if (info.Length() > 2 && info[2].IsNumber()) { start_column = info[2].As().Uint32Value() << 1; } - uint32_t end_row = 0; if (info.Length() > 3 && info[3].IsNumber()) { end_row = info[3].As().Uint32Value(); } - uint32_t end_column = 0; if (info.Length() > 4 && info[4].IsNumber()) { end_column = info[4].As().Uint32Value() << 1; } + if (info.Length() > 5 && info[5].IsNumber()) { + start_index = info[5].As().Uint32Value(); + } + if (info.Length() > 6 && info[6].IsNumber()) { + end_index = info[6].As().Uint32Value() << 1; + } + if (info.Length() > 7 && info[7].IsNumber()) { + match_limit = info[7].As().Uint32Value(); + } + if (info.Length() > 8 && info[8].IsNumber()) { + max_start_depth = info[8].As().Uint32Value(); + } if (query == nullptr) { throw Error::New(env, "Missing argument query"); @@ -278,11 +290,14 @@ Napi::Value Query::Captures(const Napi::CallbackInfo &info) { } TSQuery *ts_query = query->query_; - TSNode rootNode = node_methods::UnmarshalNode(env, tree); + TSNode root_node = node_methods::UnmarshalNode(env, tree); TSPoint start_point = {start_row, start_column}; TSPoint end_point = {end_row, end_column}; ts_query_cursor_set_point_range(data->ts_query_cursor, start_point, end_point); - ts_query_cursor_exec(data->ts_query_cursor, ts_query, rootNode); + ts_query_cursor_set_byte_range(data->ts_query_cursor, start_index, end_index); + ts_query_cursor_set_match_limit(data->ts_query_cursor, match_limit); + ts_query_cursor_set_max_start_depth(data->ts_query_cursor, max_start_depth); + ts_query_cursor_exec(data->ts_query_cursor, ts_query, root_node); Array js_matches = Array::New(env); unsigned index = 0; diff --git a/src/tree_cursor.cc b/src/tree_cursor.cc index de286391..c0f8bbc8 100644 --- a/src/tree_cursor.cc +++ b/src/tree_cursor.cc @@ -17,6 +17,8 @@ void TreeCursor::Init(Napi::Env env, Napi::Object exports) { InstanceAccessor("startIndex", &TreeCursor::StartIndex, nullptr, napi_default_method), InstanceAccessor("endIndex", &TreeCursor::EndIndex, nullptr, napi_default_method), InstanceAccessor("nodeType", &TreeCursor::NodeType, nullptr, napi_default_method), + InstanceAccessor("nodeTypeId", &TreeCursor::NodeTypeId, nullptr, napi_default_method), + InstanceAccessor("nodeStateId", &TreeCursor::NodeStateId, nullptr, napi_default_method), InstanceAccessor("nodeIsNamed", &TreeCursor::NodeIsNamed, nullptr, napi_default_method), InstanceAccessor("nodeIsMissing", &TreeCursor::NodeIsMissing, nullptr, napi_default_method), InstanceAccessor("currentFieldId", &TreeCursor::CurrentFieldId, nullptr, napi_default_method), @@ -154,6 +156,16 @@ Napi::Value TreeCursor::NodeType(const Napi::CallbackInfo &info) { return String::New(info.Env(), ts_node_type(node));; } +Napi::Value TreeCursor::NodeTypeId(const Napi::CallbackInfo &info) { + TSNode node = ts_tree_cursor_current_node(&cursor_); + return Number::New(info.Env(), static_cast(ts_node_symbol(node))); +} + +Napi::Value TreeCursor::NodeStateId(const Napi::CallbackInfo &info) { + TSNode node = ts_tree_cursor_current_node(&cursor_); + return Number::New(info.Env(), static_cast(ts_node_parse_state(node))); +} + Napi::Value TreeCursor::NodeIsNamed(const Napi::CallbackInfo &info) { TSNode node = ts_tree_cursor_current_node(&cursor_); return Boolean::New(info.Env(), ts_node_is_named(node)); diff --git a/src/tree_cursor.h b/src/tree_cursor.h index e20a20ab..8c6d6342 100644 --- a/src/tree_cursor.h +++ b/src/tree_cursor.h @@ -35,6 +35,8 @@ class TreeCursor final : public Napi::ObjectWrap { Napi::Value ResetTo(const Napi::CallbackInfo &); Napi::Value NodeType(const Napi::CallbackInfo &); + Napi::Value NodeTypeId(const Napi::CallbackInfo &); + Napi::Value NodeStateId(const Napi::CallbackInfo &); Napi::Value NodeIsNamed(const Napi::CallbackInfo &); Napi::Value NodeIsMissing(const Napi::CallbackInfo &); Napi::Value CurrentFieldId(const Napi::CallbackInfo &); diff --git a/test/lookahead_iterator_test.js b/test/lookahead_iterable_test.js similarity index 76% rename from test/lookahead_iterator_test.js rename to test/lookahead_iterable_test.js index 62428196..5836183b 100644 --- a/test/lookahead_iterator_test.js +++ b/test/lookahead_iterable_test.js @@ -21,18 +21,21 @@ describe("LookaheadIterator", () => { assert.notEqual(nextState, 0); assert(cursor.gotoNextSibling()); // type_identifier assert.equal(nextState, cursor.currentNode.parseState); - assert.equal(cursor.currentNode.grammarName, "identifier"); + assert.equal(cursor.currentNode.grammarType, "identifier"); assert.notEqual(cursor.currentNode.grammarId, cursor.currentNode.typeId); const expectedSymbols = ["//", "/*", "identifier", "line_comment", "block_comment"] const lookahead = new LookaheadIterator(Rust, nextState); - assert.deepEqual(lookahead.iterNames(), expectedSymbols); + let symbols = Array.from(lookahead); + assert.deepEqual(symbols, expectedSymbols); assert(lookahead.resetState(nextState)); - assert.deepEqual(lookahead.iterNames(), expectedSymbols); + symbols = Array.from(lookahead); + assert.deepEqual(symbols, expectedSymbols); assert(lookahead.reset(Rust, nextState)); - assert.deepEqual(lookahead.iterNames(), expectedSymbols); + symbols = Array.from(lookahead); + assert.deepEqual(symbols, expectedSymbols); }); }); }); diff --git a/test/node_test.js b/test/node_test.js index b1b62954..fd3904f8 100644 --- a/test/node_test.js +++ b/test/node_test.js @@ -278,8 +278,7 @@ describe("Node", () => { const tree = parser.parse(source); const node = tree.rootNode.firstChild; assert.equal(node.type, 'if_statement'); - const cursor = tree.walk(); - const alternatives = node.childrenForFieldName('alternative', cursor); + const alternatives = node.childrenForFieldName('alternative'); const alternative_texts = alternatives.map(n => { const condition = n.childForFieldName('condition'); return source.slice(condition.startIndex, condition.endIndex); @@ -638,7 +637,7 @@ describe("Node", () => { }); }); - describe(".isMissing)", () => { + describe(".isMissing", () => { it("returns true if the node is missing from the source and was inserted via error recovery", () => { const tree = parser.parse("(2 ||)"); const node = tree.rootNode; diff --git a/tree-sitter.d.ts b/tree-sitter.d.ts index 2dda0e26..8aeca141 100644 --- a/tree-sitter.d.ts +++ b/tree-sitter.d.ts @@ -1,18 +1,22 @@ declare module "tree-sitter" { class Parser { - parse(input: string | Parser.Input | Parser.InputReader, oldTree?: Parser.Tree, options?: { bufferSize?: number, includedRanges?: Parser.Range[] }): Parser.Tree; + parse(input: string | Parser.Input, oldTree?: Parser.Tree, options?: Parser.Options): Parser.Tree; getIncludedRanges(): Parser.Range[]; getTimeoutMicros(): number; setTimeoutMicros(timeout: number): void; + reset(): void; getLanguage(): any; - setLanguage(language: any): void; + setLanguage(language?: any): void; getLogger(): Parser.Logger; - setLogger(logFunc: Parser.Logger): void; + setLogger(logFunc?: Parser.Logger | false | null): void; printDotGraphs(enabled?: boolean, fd?: number): void; - reset(): void; } namespace Parser { + export type Options = { + bufferSize?: number, includedRanges?: Range[]; + }; + export type Point = { row: number; column: number; @@ -36,17 +40,12 @@ declare module "tree-sitter" { export type Logger = ( message: string, - params: {[param: string]: string}, + params: { [param: string]: string }, type: "parse" | "lex" ) => void; - export interface InputReader { - (index: any, position: Point): string; - } - export interface Input { - seek(index: number): void; - read(): any; + (index: number, position?: Point): string | null; } export interface SyntaxNode { @@ -55,9 +54,16 @@ declare module "tree-sitter" { typeId: number; grammarId: number; type: string; - grammarName: string; + grammarType: string; isNamed: boolean; + isMissing: boolean; + isExtra: boolean; + hasChanges: boolean; + hasError: boolean; + isError: boolean; text: string; + parseState: number; + nextParseState: number; startPosition: Point; endPosition: Point; startIndex: number; @@ -75,15 +81,8 @@ declare module "tree-sitter" { nextNamedSibling: SyntaxNode | null; previousSibling: SyntaxNode | null; previousNamedSibling: SyntaxNode | null; - parseState: number; - nextParseState: number; descendantCount: number; - hasChanges(): boolean; - hasError(): boolean; - isMissing(): boolean; - isExtra(): boolean; - isError(): boolean; toString(): string; child(index: number): SyntaxNode | null; namedChild(index: number): SyntaxNode | null; @@ -111,6 +110,8 @@ declare module "tree-sitter" { export interface TreeCursor { nodeType: string; + nodeTypeId: number; + nodeStateId: number; nodeText: string; nodeIsNamed: boolean; nodeIsMissing: boolean; @@ -124,8 +125,8 @@ declare module "tree-sitter" { readonly currentDepth: number; readonly currentDescendantIndex: number; - reset(node: SyntaxNode): void - resetTo(other: TreeCursor): void; + reset(node: SyntaxNode): void; + resetTo(cursor: TreeCursor): void; gotoParent(): boolean; gotoFirstChild(): boolean; gotoLastChild(): boolean; @@ -133,7 +134,7 @@ declare module "tree-sitter" { gotoFirstChildForPosition(goalPosition: Point): boolean; gotoNextSibling(): boolean; gotoPreviousSibling(): boolean; - gotoDescendant(goalDescendantIndex: number): boolean; + gotoDescendant(goalDescendantIndex: number): void; } export interface Tree { @@ -148,18 +149,32 @@ declare module "tree-sitter" { printDotGraph(fd?: number): void; } + export interface QueryCapture { + name: string; + text?: string; + node: SyntaxNode; + setProperties?: { [prop: string]: string | null }; + assertedProperties?: { [prop: string]: string | null }; + refutedProperties?: { [prop: string]: string | null }; + } + export interface QueryMatch { - pattern: number, - captures: QueryCapture[], + pattern: number; + captures: QueryCapture[]; } - export interface QueryCapture { - name: string, - text?: string, - node: SyntaxNode, - setProperties?: {[prop: string]: string | null}, - assertedProperties?: {[prop: string]: string | null}, - refutedProperties?: {[prop: string]: string | null}, + export type QueryOptions = { + startPosition?: Point; + endPosition?: Point; + startIndex?: number; + endIndex?: number; + matchLimit?: number; + maxStartDepth?: number; + }; + + export interface PredicateResult { + operator: string; + operands: { name: string; type: string }[]; } export class Query { @@ -169,33 +184,10 @@ declare module "tree-sitter" { readonly refutedProperties: any[]; readonly matchLimit: number; - constructor( - language: any, - source: string | Buffer, - ); - - matches( - rootNode: SyntaxNode, - options?: { - startPosition?: Point; - endPosition?: Point; - startIndex?: number; - endIndex?: number; - matchLimit?: number; - maxStartDepth?: number; - } - ): QueryMatch[]; - captures( - rootNode: SyntaxNode, - options?: { - startPosition?: Point; - endPosition?: Point; - startIndex?: number; - endIndex?: number; - matchLimit?: number; - maxStartDepth?: number; - } - ): QueryCapture[]; + constructor(language: any, source: string | Buffer); + + captures(node: SyntaxNode, options?: QueryOptions): QueryCapture[]; + matches(node: SyntaxNode, options?: QueryOptions): QueryMatch[]; disableCapture(captureName: string): void; disablePattern(patternIndex: number): void; isPatternGuaranteedAtStep(byteOffset: number): boolean; @@ -205,14 +197,13 @@ declare module "tree-sitter" { didExceedMatchLimit(): boolean; } - export class LookaheadIterator { - readonly currentSymbol: number; - readonly currentSymbolName: string; + export class LookaheadIterable { + readonly currentTypeId: number; + readonly currentType: string; - reset(language: any, state: number): boolean; - resetState(state: number): boolean; - next(): number; - iterNames(): string[]; + reset(language: any, stateId: number): boolean; + resetState(stateId: number): boolean; + [Symbol.iterator](): Iterator; } }