Add wasm binding for running tree queries in a limited range

This commit is contained in:
Max Brunsfeld 2019-09-11 14:44:49 -07:00
parent 0528ad5f58
commit 49ce2fddb9
6 changed files with 143 additions and 24 deletions

View file

@ -688,24 +688,30 @@ class Language {
const nameLength = getValue(TRANSFER_BUFFER, 'i32');
captureNames[i] = UTF8ToString(nameAddress, nameLength);
}
C._free(sourceAddress);
return new Query(INTERNAL, address, captureNames);
} else {
const errorId = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');
const utf8ErrorOffset = getValue(TRANSFER_BUFFER, 'i32');
const errorOffset = UTF8ToString(sourceAddress, utf8ErrorOffset).length;
C._free(sourceAddress);
const suffix = source.slice(errorOffset, 100);
const errorByte = getValue(TRANSFER_BUFFER, 'i32');
const errorIndex = UTF8ToString(sourceAddress, errorByte).length;
const suffix = source.slice(errorIndex, 100);
const word = suffix.match(QUERY_WORD_REGEX)[0];
let error;
switch (errorId) {
case 2: throw new RangeError(
`Bad node name '${suffix.match(QUERY_WORD_REGEX)[0]}'`
);
case 3: throw new RangeError(
`Bad field name '${suffix.match(QUERY_WORD_REGEX)[0]}'`
);
default: throw new SyntaxError(
`Bad syntax at offset ${errorOffset}: '${suffix}'...`
);
case 2:
error = new RangeError(`Bad node name '${word}'`);
break;
case 3:
error = new RangeError(`Bad field name '${word}'`);
break;
default:
error = new SyntaxError(`Bad syntax at offset ${errorIndex}: '${suffix}'...`);
break;
}
error.index = errorIndex;
error.length = word.length;
C._free(sourceAddress);
throw error;
}
}
@ -752,10 +758,20 @@ class Query {
C._ts_query_delete(this[0]);
}
exec(queryNode) {
exec(queryNode, startPosition, endPosition) {
if (!startPosition) startPosition = ZERO_POINT;
if (!endPosition) endPosition = ZERO_POINT;
marshalNode(queryNode);
C._ts_query_exec_wasm(this[0], queryNode.tree[0]);
C._ts_query_exec_wasm(
this[0],
queryNode.tree[0],
startPosition.row,
startPosition.column,
endPosition.row,
endPosition.column
);
const matchCount = getValue(TRANSFER_BUFFER, 'i32');
const nodesAddress = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32');