aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/tree_sitter.c85
1 files changed, 30 insertions, 55 deletions
diff --git a/app/tree_sitter.c b/app/tree_sitter.c
index d0f9fa8..4a5615f 100644
--- a/app/tree_sitter.c
+++ b/app/tree_sitter.c
@@ -18,68 +18,43 @@ void extract_comments(
ts_parser_set_language(parser, language);
TSTree* tree = ts_parser_parse_string(parser, NULL, input, strlen(input));
TSNode root_node = ts_tree_root_node(tree);
+ TSTreeCursor tree_cursor = ts_tree_cursor_new(root_node);
- char* pattern = "(comment) @comment";
- uint32_t error_offset;
- TSQueryError error_type;
- TSQuery* query = ts_query_new(language, pattern, strlen(pattern), &error_offset, &error_type);
- TSQueryCursor* query_cursor = ts_query_cursor_new();
- ts_query_cursor_exec(query_cursor, query, root_node);
-
- TSQueryMatch query_match;
uint32_t n_max = 1024;
- *out = malloc(sizeof(Node) * n_max);
- Node* node = *out;
uint32_t n = 0;
- while (ts_query_cursor_next_match(query_cursor, &query_match)) {
- if (n >= n_max) {
- n_max *= 2;
- *out = realloc(*out, sizeof(Node) * n_max);
- node = *out + n;
+ *out = malloc(sizeof(Node) * n_max);
+
+ Node* current_out = *out;
+ TSNode current_node;
+ bool process = true;
+ while (true) {
+ current_node = ts_tree_cursor_current_node(&tree_cursor);
+ if (process && (0 == strcmp("comment", ts_node_type(current_node)))) {
+ if (n >= n_max) {
+ n_max *= 2;
+ *out = realloc(*out, sizeof(Node) * n_max);
+ current_out = *out + n;
+ }
+ current_out->start_byte = ts_node_start_byte(current_node);
+ current_out->end_byte = ts_node_end_byte(current_node);
+ current_out->start_point = ts_node_start_point(current_node);
+ current_out->end_point = ts_node_end_point(current_node);
+ n++;
+ current_out++;
+ }
+ if (process && ts_tree_cursor_goto_first_child(&tree_cursor)) {
+ process = true;
+ } else if (ts_tree_cursor_goto_next_sibling(&tree_cursor)) {
+ process = true;
+ } else {
+ if (!ts_tree_cursor_goto_parent(&tree_cursor)) break;
+ process = false;
}
- TSNode captured_node = query_match.captures[0].node;
- node->start_byte = ts_node_start_byte(captured_node);
- node->end_byte = ts_node_end_byte(captured_node);
- node->start_point = ts_node_start_point(captured_node);
- node->end_point = ts_node_end_point(captured_node);
- node++; n++;
}
+
*out_len = n;
- ts_query_cursor_delete(query_cursor);
- ts_query_delete(query);
+ ts_tree_cursor_delete(&tree_cursor);
ts_tree_delete(tree);
ts_parser_delete(parser);
}
-
-void ts_tree_root_node_p(TSTree *tree, TSNode *node) {
- (*node) = ts_tree_root_node(tree);
-}
-
-uint32_t ts_node_named_child_count_p(TSNode *node) {
- return ts_node_named_child_count(*node);
-}
-
-uint32_t ts_node_start_byte_p(TSNode *node) {
- return ts_node_start_byte(*node);
-}
-
-uint32_t ts_node_end_byte_p(TSNode *node) {
- return ts_node_end_byte(*node);
-}
-
-uint32_t ts_node_start_point_p(TSNode *node, TSPoint *point) {
- (*point) = ts_node_start_point(*node);
-}
-
-uint32_t ts_node_end_point_p(TSNode *node, TSPoint *point) {
- (*point) = ts_node_end_point(*node);
-}
-
-const char* ts_node_type_p(TSNode *node) {
- return ts_node_type(*node);
-}
-
-void ts_node_named_child_p(TSNode* self, uint32_t child_index, TSNode* node) {
- (*node) = ts_node_named_child(*self, child_index);
-}