feat(web)!: rewrite the library in TypeScript

This commit is contained in:
Amaan Qureshi 2025-01-13 01:48:42 -05:00
parent 07a86b1729
commit 2cae67892e
39 changed files with 7856 additions and 3629 deletions

View file

@ -0,0 +1,120 @@
"ts_init",
"ts_language_field_count",
"ts_language_field_name_for_id",
"ts_language_type_is_named_wasm",
"ts_language_type_is_visible_wasm",
"ts_language_symbol_count",
"ts_language_state_count",
"ts_language_supertypes_wasm",
"ts_language_subtypes_wasm",
"ts_language_symbol_for_name",
"ts_language_symbol_name",
"ts_language_symbol_type",
"ts_language_name",
"ts_language_version",
"ts_language_next_state",
"ts_node_field_name_for_child_wasm",
"ts_node_field_name_for_named_child_wasm",
"ts_node_children_by_field_id_wasm",
"ts_node_first_child_for_byte_wasm",
"ts_node_first_named_child_for_byte_wasm",
"ts_node_child_by_field_id_wasm",
"ts_node_child_count_wasm",
"ts_node_child_wasm",
"ts_node_children_wasm",
"ts_node_descendant_for_index_wasm",
"ts_node_descendant_for_position_wasm",
"ts_node_descendants_of_type_wasm",
"ts_node_end_index_wasm",
"ts_node_end_point_wasm",
"ts_node_has_changes_wasm",
"ts_node_has_error_wasm",
"ts_node_is_error_wasm",
"ts_node_is_missing_wasm",
"ts_node_is_extra_wasm",
"ts_node_is_named_wasm",
"ts_node_parse_state_wasm",
"ts_node_next_parse_state_wasm",
"ts_node_named_child_count_wasm",
"ts_node_named_child_wasm",
"ts_node_named_children_wasm",
"ts_node_named_descendant_for_index_wasm",
"ts_node_named_descendant_for_position_wasm",
"ts_node_next_named_sibling_wasm",
"ts_node_next_sibling_wasm",
"ts_node_parent_wasm",
"ts_node_prev_named_sibling_wasm",
"ts_node_prev_sibling_wasm",
"ts_node_descendant_count_wasm",
"ts_node_start_index_wasm",
"ts_node_start_point_wasm",
"ts_node_symbol_wasm",
"ts_node_grammar_symbol_wasm",
"ts_node_to_string_wasm",
"ts_parser_delete",
"ts_parser_enable_logger_wasm",
"ts_parser_new_wasm",
"ts_parser_parse_wasm",
"ts_parser_reset",
"ts_parser_set_language",
"ts_parser_set_included_ranges",
"ts_parser_included_ranges_wasm",
"ts_parser_set_timeout_micros",
"ts_parser_timeout_micros",
"ts_query_capture_count",
"ts_query_capture_name_for_id",
"ts_query_captures_wasm",
"ts_query_delete",
"ts_query_matches_wasm",
"ts_query_new",
"ts_query_pattern_count",
"ts_query_predicates_for_pattern",
"ts_query_disable_capture",
"ts_query_start_byte_for_pattern",
"ts_query_end_byte_for_pattern",
"ts_query_string_count",
"ts_query_string_value_for_id",
"ts_query_disable_pattern",
"ts_query_capture_quantifier_for_id",
"ts_query_is_pattern_non_local",
"ts_query_is_pattern_rooted",
"ts_query_is_pattern_guaranteed_at_step",
"ts_tree_copy",
"ts_tree_cursor_current_field_id_wasm",
"ts_tree_cursor_current_depth_wasm",
"ts_tree_cursor_current_descendant_index_wasm",
"ts_tree_cursor_current_node_id_wasm",
"ts_tree_cursor_current_node_is_missing_wasm",
"ts_tree_cursor_current_node_is_named_wasm",
"ts_tree_cursor_current_node_type_id_wasm",
"ts_tree_cursor_current_node_state_id_wasm",
"ts_tree_cursor_current_node_wasm",
"ts_tree_cursor_delete_wasm",
"ts_tree_cursor_end_index_wasm",
"ts_tree_cursor_end_position_wasm",
"ts_tree_cursor_goto_first_child_wasm",
"ts_tree_cursor_goto_last_child_wasm",
"ts_tree_cursor_goto_first_child_for_index_wasm",
"ts_tree_cursor_goto_first_child_for_position_wasm",
"ts_tree_cursor_goto_next_sibling_wasm",
"ts_tree_cursor_goto_previous_sibling_wasm",
"ts_tree_cursor_goto_descendant_wasm",
"ts_tree_cursor_goto_parent_wasm",
"ts_tree_cursor_new_wasm",
"ts_tree_cursor_reset_wasm",
"ts_tree_cursor_reset_to_wasm",
"ts_tree_cursor_start_index_wasm",
"ts_tree_cursor_start_position_wasm",
"ts_tree_cursor_copy_wasm",
"ts_tree_delete",
"ts_tree_included_ranges_wasm",
"ts_tree_edit_wasm",
"ts_tree_get_changed_ranges_wasm",
"ts_tree_root_node_wasm",
"ts_tree_root_node_with_offset_wasm",
"ts_lookahead_iterator_new",
"ts_lookahead_iterator_delete",
"ts_lookahead_iterator_reset_state",
"ts_lookahead_iterator_reset",
"ts_lookahead_iterator_next",
"ts_lookahead_iterator_current_symbol",

View file

@ -0,0 +1,39 @@
mergeInto(LibraryManager.library, {
tree_sitter_parse_callback(
inputBufferAddress,
index,
row,
column,
lengthAddress,
) {
const INPUT_BUFFER_SIZE = 10 * 1024;
const string = currentParseCallback(index, {row, column});
if (typeof string === 'string') {
setValue(lengthAddress, string.length, 'i32');
stringToUTF16(string, inputBufferAddress, INPUT_BUFFER_SIZE);
} else {
setValue(lengthAddress, 0, 'i32');
}
},
tree_sitter_log_callback(isLexMessage, messageAddress) {
if (currentLogCallback) {
const message = UTF8ToString(messageAddress);
currentLogCallback(message, isLexMessage !== 0);
}
},
tree_sitter_progress_callback(currentOffset) {
if (currentProgressCallback) {
return currentProgressCallback({currentOffset});
}
return false;
},
tree_sitter_query_progress_callback(currentOffset) {
if (currentQueryProgressCallback) {
return currentQueryProgressCallback({currentOffset});
}
return false;
},
});

View file

@ -0,0 +1,19 @@
var TreeSitter = function() {
var initPromise;
var document = typeof window == 'object'
? {currentScript: window.document.currentScript}
: null;
class Parser {
constructor() {
this.initialize();
}
initialize() {
throw new Error("cannot construct a Parser before calling `init()`");
}
static init(moduleOptions) {
if (initPromise) return initPromise;
Module = Object.assign({}, Module, moduleOptions);
return initPromise = new Promise((resolveInitPromise) => {

View file

@ -0,0 +1,23 @@
for (const name of Object.getOwnPropertyNames(ParserImpl.prototype)) {
Object.defineProperty(Parser.prototype, name, {
value: ParserImpl.prototype[name],
enumerable: false,
writable: false,
})
}
Parser.Language = Language;
Module.onRuntimeInitialized = () => {
ParserImpl.init();
resolveInitPromise();
};
});
}
}
return Parser;
}();
if (typeof exports === 'object') {
module.exports = TreeSitter;
}