Tweak query interface

* Rename TSQueryContext -> TSQueryCursor
* Remove the permanent association between the cursor and its query. The 
cursor can now be used again for a different query.
This commit is contained in:
Max Brunsfeld 2019-09-11 14:44:49 -07:00
parent c8c75782e3
commit c71de5bd81
9 changed files with 142 additions and 147 deletions

View file

@ -306,6 +306,7 @@ void ts_tree_cursor_current_node_wasm(const TSTree *tree) {
/******************/
static TSTreeCursor scratch_cursor = {0};
static TSQueryCursor *scratch_query_cursor = NULL;
uint16_t ts_node_symbol_wasm(const TSTree *tree) {
TSNode node = unmarshal_node(tree);
@ -566,24 +567,22 @@ int ts_node_is_missing_wasm(const TSTree *tree) {
/* Section - Query */
/******************/
void ts_query_exec_wasm(
const TSQuery *self,
TSQueryContext *context,
const TSTree *tree
) {
void ts_query_exec_wasm(const TSQuery *self, const TSTree *tree) {
if (!scratch_query_cursor) scratch_query_cursor = ts_query_cursor_new();
TSNode node = unmarshal_node(tree);
Array(const void *) result = array_new();
unsigned index = 0;
unsigned match_count = 0;
ts_query_context_exec(context, node);
while (ts_query_context_next(context)) {
ts_query_cursor_exec(scratch_query_cursor, self, node);
while (ts_query_cursor_next(scratch_query_cursor)) {
match_count++;
uint32_t pattern_index = ts_query_context_matched_pattern_index(context);
uint32_t pattern_index = ts_query_cursor_matched_pattern_index(scratch_query_cursor);
uint32_t capture_count;
const TSQueryCapture *captures = ts_query_context_matched_captures(
context,
const TSQueryCapture *captures = ts_query_cursor_matched_captures(
scratch_query_cursor,
&capture_count
);

View file

@ -677,7 +677,6 @@ class Language {
TRANSFER_BUFFER + SIZE_OF_INT
);
if (address) {
const contextAddress = C._ts_query_context_new(address);
const captureCount = C._ts_query_capture_count(address);
const captureNames = new Array(captureCount);
for (let i = 0; i < captureCount; i++) {
@ -689,7 +688,7 @@ class Language {
const nameLength = getValue(TRANSFER_BUFFER, 'i32');
captureNames[i] = UTF8ToString(nameAddress, nameLength);
}
return new Query(INTERNAL, address, contextAddress, captureNames);
return new Query(INTERNAL, address, captureNames);
} else {
const errorId = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
const utf8ErrorOffset = getValue(TRANSFER_BUFFER, 'i32');
@ -743,22 +742,20 @@ class Language {
}
class Query {
constructor(internal, address, contextAddress, captureNames) {
constructor(internal, address, captureNames) {
assertInternal(internal);
this[0] = address;
this[1] = contextAddress;
this.captureNames = captureNames;
}
delete() {
C._ts_query_delete(this[0]);
C._ts_query_context_delete(this[0]);
}
exec(queryNode) {
marshalNode(queryNode);
C._ts_query_exec_wasm(this[0], this[1], queryNode.tree[0]);
C._ts_query_exec_wasm(this[0], queryNode.tree[0]);
const matchCount = getValue(TRANSFER_BUFFER, 'i32');
const nodesAddress = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');