API extensions

This commit is contained in:
Daumantas Kavolis 2023-05-17 10:39:37 +03:00
parent 08f4e82bb2
commit c47e217e73
14 changed files with 741 additions and 29 deletions

View file

@ -1,6 +1,7 @@
const C = Module;
const INTERNAL = {};
const SIZE_OF_INT = 4;
const SIZE_OF_CURSOR = 3 * SIZE_OF_INT;
const SIZE_OF_NODE = 5 * SIZE_OF_INT;
const SIZE_OF_POINT = 2 * SIZE_OF_INT;
const SIZE_OF_RANGE = 2 * SIZE_OF_INT + 2 * SIZE_OF_POINT;
@ -227,6 +228,11 @@ class Node {
return getText(this.tree, this.startIndex, this.endIndex);
}
get parseState() {
marshalNode(this);
return C._ts_node_parse_state_wasm(this.tree[0]);
}
isNamed() {
marshalNode(this);
return C._ts_node_is_named_wasm(this.tree[0]) === 1;
@ -242,6 +248,11 @@ class Node {
return C._ts_node_has_changes_wasm(this.tree[0]) === 1;
}
isError() {
marshalNode(this);
return C._ts_node_is_error_wasm(this.tree[0]) === 1;
}
isMissing() {
marshalNode(this);
return C._ts_node_is_missing_wasm(this.tree[0]) === 1;
@ -505,6 +516,13 @@ class TreeCursor {
unmarshalTreeCursor(this);
}
resetTo(cursor) {
marshalTreeCursor(this, TRANSFER_BUFFER);
marshalTreeCursor(cursor, TRANSFER_BUFFER + SIZE_OF_CURSOR);
C._ts_tree_cursor_reset_to_wasm(this.tree[0], cursor.tree[0]);
unmarshalTreeCursor(this);
}
get nodeType() {
return this.tree.language.types[this.nodeTypeId] || 'ERROR';
}
@ -514,6 +532,11 @@ class TreeCursor {
return C._ts_tree_cursor_current_node_type_id_wasm(this.tree[0]);
}
get nodeStateId() {
marshalTreeCursor(this);
return C._ts_tree_cursor_current_node_state_id_wasm(this.tree[0]);
}
get nodeId() {
marshalTreeCursor(this);
return C._ts_tree_cursor_current_node_id_wasm(this.tree[0]);
@ -580,6 +603,13 @@ class TreeCursor {
return result === 1;
}
gotoLastChild() {
marshalTreeCursor(this);
const result = C._ts_tree_cursor_goto_last_child_wasm(this.tree[0]);
unmarshalTreeCursor(this);
return result === 1;
}
gotoNextSibling() {
marshalTreeCursor(this);
const result = C._ts_tree_cursor_goto_next_sibling_wasm(this.tree[0]);
@ -587,6 +617,13 @@ class TreeCursor {
return result === 1;
}
gotoPreviousSibling() {
marshalTreeCursor(this);
const result = C._ts_tree_cursor_goto_previous_sibling_wasm(this.tree[0]);
unmarshalTreeCursor(this);
return result === 1;
}
gotoParent() {
marshalTreeCursor(this);
const result = C._ts_tree_cursor_goto_parent_wasm(this.tree[0]);
@ -624,6 +661,10 @@ class Language {
return this.fields.length - 1;
}
get stateCount() {
return C._ts_language_state_count(this[0]);
}
fieldIdForName(fieldName) {
const result = this.fields.indexOf(fieldName);
if (result !== -1) {
@ -663,6 +704,15 @@ class Language {
return C._ts_language_type_is_visible_wasm(this[0], typeId) ? true : false;
}
nextState(stateId, typeId) {
return C._ts_language_next_state(this[0], stateId, typeId);
}
lookaheadIterator(stateId) {
const address = C._ts_lookahead_iterator_new(this[0], stateId);
if (address) return new LookaheadIterable(INTERNAL, address, this);
}
query(source) {
const sourceLength = lengthBytesUTF8(source);
const sourceAddress = C._malloc(sourceLength + 1);
@ -924,6 +974,53 @@ class Language {
}
}
class LookaheadIterable {
constructor(internal, address, language) {
assertInternal(internal);
this[0] = address;
this.language = language;
}
get currentTypeId() {
return C._ts_lookahead_iterator_current_symbol(this[0]);
}
get currentType() {
return this.language.types[this.currentTypeId] || 'ERROR'
}
delete() {
C._ts_lookahead_iterator_delete(this[0]);
this[0] = 0;
}
resetState(stateId) {
return C._ts_lookahead_iterator_reset_state(this[0], stateId);
}
reset(language, stateId) {
if (C._ts_lookahead_iterator_reset(this[0], language, stateId)) {
this.language = language;
return true;
}
return false;
}
[Symbol.iterator]() {
const self = this;
return {
next() {
if (C._ts_lookahead_iterator_advance(self[0])) {
return { done: false, value: self.currentType };
}
return { done: true, value: "" };
}
};
}
}
class Query {
constructor(
internal, address, captureNames, textPredicates, predicates,