Allow predicates in queries, to match on nodes' text
This commit is contained in:
parent
307a1a6c11
commit
096126d039
8 changed files with 781 additions and 186 deletions
|
|
@ -587,20 +587,14 @@ void ts_query_matches_wasm(
|
|||
uint32_t match_count = 0;
|
||||
Array(const void *) result = array_new();
|
||||
|
||||
uint32_t pattern_index, capture_count;
|
||||
const TSQueryCapture *captures;
|
||||
while (ts_query_cursor_next_match(
|
||||
scratch_query_cursor,
|
||||
&pattern_index,
|
||||
&capture_count,
|
||||
&captures
|
||||
)) {
|
||||
TSQueryMatch match;
|
||||
while (ts_query_cursor_next_match(scratch_query_cursor, &match)) {
|
||||
match_count++;
|
||||
array_grow_by(&result, 2 + 6 * capture_count);
|
||||
result.contents[index++] = (const void *)pattern_index;
|
||||
result.contents[index++] = (const void *)capture_count;
|
||||
for (unsigned i = 0; i < capture_count; i++) {
|
||||
const TSQueryCapture *capture = &captures[i];
|
||||
array_grow_by(&result, 2 + 6 * match.capture_count);
|
||||
result.contents[index++] = (const void *)(uint32_t)match.pattern_index;
|
||||
result.contents[index++] = (const void *)(uint32_t)match.capture_count;
|
||||
for (unsigned i = 0; i < match.capture_count; i++) {
|
||||
const TSQueryCapture *capture = &match.captures[i];
|
||||
result.contents[index++] = (const void *)capture->index;
|
||||
marshal_node(result.contents + index, capture->node);
|
||||
index += 5;
|
||||
|
|
@ -631,14 +625,25 @@ void ts_query_captures_wasm(
|
|||
unsigned capture_count = 0;
|
||||
Array(const void *) result = array_new();
|
||||
|
||||
TSQueryCapture capture;
|
||||
while (ts_query_cursor_next_capture(scratch_query_cursor, &capture)) {
|
||||
TSQueryMatch match;
|
||||
uint32_t capture_index;
|
||||
while (ts_query_cursor_next_capture(
|
||||
scratch_query_cursor,
|
||||
&match,
|
||||
&capture_index
|
||||
)) {
|
||||
capture_count++;
|
||||
|
||||
array_grow_by(&result, 6);
|
||||
result.contents[index++] = (const void *)capture.index;
|
||||
marshal_node(result.contents + index, capture.node);
|
||||
index += 5;
|
||||
array_grow_by(&result, 3 + 6 * match.capture_count);
|
||||
result.contents[index++] = (const void *)(uint32_t)match.pattern_index;
|
||||
result.contents[index++] = (const void *)(uint32_t)match.capture_count;
|
||||
result.contents[index++] = (const void *)(uint32_t)capture_index;
|
||||
for (unsigned i = 0; i < match.capture_count; i++) {
|
||||
const TSQueryCapture *capture = &match.captures[i];
|
||||
result.contents[index++] = (const void *)capture->index;
|
||||
marshal_node(result.contents + index, capture->node);
|
||||
index += 5;
|
||||
}
|
||||
}
|
||||
|
||||
TRANSFER_BUFFER[0] = (const void *)(capture_count);
|
||||
|
|
|
|||
|
|
@ -795,14 +795,34 @@ class Query {
|
|||
|
||||
const count = getValue(TRANSFER_BUFFER, 'i32');
|
||||
const startAddress = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
|
||||
const result = new Array(count);
|
||||
unmarshalCaptures(this, node.tree, startAddress, result);
|
||||
const result = [];
|
||||
|
||||
let address = startAddress;
|
||||
for (let i = 0; i < count; i++) {
|
||||
const pattern = getValue(address, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
const captureCount = getValue(address, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
const captureIndex = getValue(address, 'i32');
|
||||
address += SIZE_OF_INT;
|
||||
|
||||
const captures = new Array(captureCount);
|
||||
address = unmarshalCaptures(this, node.tree, address, captures);
|
||||
|
||||
if (capturesMatchConditions(this, node.tree, pattern, captures)) {
|
||||
result.push(captures[captureIndex]);
|
||||
}
|
||||
}
|
||||
|
||||
C._free(startAddress);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
function capturesMatchConditions(query, tree, pattern, captures) {
|
||||
return true;
|
||||
}
|
||||
|
||||
function unmarshalCaptures(query, tree, address, result) {
|
||||
for (let i = 0, n = result.length; i < n; i++) {
|
||||
const captureIndex = getValue(address, 'i32');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue