Rizin
unix-like reverse engineering framework and cli tools
tree.c
Go to the documentation of this file.
1 #include "tree_sitter/api.h"
2 #include "./array.h"
3 #include "./get_changed_ranges.h"
4 #include "./subtree.h"
5 #include "./tree_cursor.h"
6 #include "./tree.h"
7 
9  Subtree root, const TSLanguage *language,
10  const TSRange *included_ranges, unsigned included_range_count
11 ) {
12  TSTree *result = ts_malloc(sizeof(TSTree));
13  result->root = root;
14  result->language = language;
15  result->included_ranges = ts_calloc(included_range_count, sizeof(TSRange));
16  memcpy(result->included_ranges, included_ranges, included_range_count * sizeof(TSRange));
17  result->included_range_count = included_range_count;
18  return result;
19 }
20 
21 TSTree *ts_tree_copy(const TSTree *self) {
22  ts_subtree_retain(self->root);
23  return ts_tree_new(self->root, self->language, self->included_ranges, self->included_range_count);
24 }
25 
26 void ts_tree_delete(TSTree *self) {
27  if (!self) return;
28 
30  ts_subtree_release(&pool, self->root);
32  ts_free(self->included_ranges);
33  ts_free(self);
34 }
35 
37  return ts_node_new(self, &self->root, ts_subtree_padding(self->root), 0);
38 }
39 
40 const TSLanguage *ts_tree_language(const TSTree *self) {
41  return self->language;
42 }
43 
44 void ts_tree_edit(TSTree *self, const TSInputEdit *edit) {
45  for (unsigned i = 0; i < self->included_range_count; i++) {
46  TSRange *range = &self->included_ranges[i];
47  if (range->end_byte >= edit->old_end_byte) {
48  if (range->end_byte != UINT32_MAX) {
49  range->end_byte = edit->new_end_byte + (range->end_byte - edit->old_end_byte);
50  range->end_point = point_add(
51  edit->new_end_point,
52  point_sub(range->end_point, edit->old_end_point)
53  );
54  if (range->end_byte < edit->new_end_byte) {
55  range->end_byte = UINT32_MAX;
56  range->end_point = POINT_MAX;
57  }
58  }
59  if (range->start_byte >= edit->old_end_byte) {
60  range->start_byte = edit->new_end_byte + (range->start_byte - edit->old_end_byte);
61  range->start_point = point_add(
62  edit->new_end_point,
63  point_sub(range->start_point, edit->old_end_point)
64  );
65  if (range->start_byte < edit->new_end_byte) {
66  range->start_byte = UINT32_MAX;
67  range->start_point = POINT_MAX;
68  }
69  }
70  }
71  }
72 
74  self->root = ts_subtree_edit(self->root, edit, &pool);
76 }
77 
79  TreeCursor cursor1 = {NULL, array_new()};
80  TreeCursor cursor2 = {NULL, array_new()};
81  ts_tree_cursor_init(&cursor1, ts_tree_root_node(self));
82  ts_tree_cursor_init(&cursor2, ts_tree_root_node(other));
83 
84  TSRangeArray included_range_differences = array_new();
86  self->included_ranges, self->included_range_count,
88  &included_range_differences
89  );
90 
91  TSRange *result;
93  &self->root, &other->root, &cursor1, &cursor2,
94  self->language, &included_range_differences, &result
95  );
96 
97  array_delete(&included_range_differences);
98  array_delete(&cursor1.stack);
99  array_delete(&cursor2.stack);
100  return result;
101 }
102 
104  ts_subtree_print_dot_graph(self->root, self->language, file);
105 }
#define ts_malloc
Definition: alloc.h:21
#define ts_free
Definition: alloc.h:30
#define ts_calloc
Definition: alloc.h:24
lzma_index ** i
Definition: index.h:629
#define array_new()
Definition: array.h:25
#define array_delete(self)
Definition: array.h:41
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
int root
Definition: enough.c:226
void ts_range_array_get_changed_ranges(const TSRange *old_ranges, unsigned old_range_count, const TSRange *new_ranges, unsigned new_range_count, TSRangeArray *differences)
unsigned ts_subtree_get_changed_ranges(const Subtree *old_tree, const Subtree *new_tree, TreeCursor *cursor1, TreeCursor *cursor2, const TSLanguage *language, const TSRangeArray *included_range_differences, TSRange **ranges)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
string FILE
Definition: benchmark.py:21
TSNode ts_node_new(const TSTree *tree, const Subtree *subtree, Length position, TSSymbol alias)
Definition: node.c:17
static TSPoint point_sub(TSPoint a, TSPoint b)
Definition: point.h:21
static TSPoint point_add(TSPoint a, TSPoint b)
Definition: point.h:14
#define POINT_MAX
Definition: point.h:7
unsigned int uint32_t
Definition: sftypes.h:29
#define UINT32_MAX
uint32_t old_end_byte
Definition: api.h:85
uint32_t new_end_byte
Definition: api.h:86
TSPoint old_end_point
Definition: api.h:88
TSPoint new_end_point
Definition: api.h:89
Definition: api.h:92
Definition: api.h:60
Definition: tree.h:15
Subtree root
Definition: tree.h:16
TSRange * included_ranges
Definition: tree.h:18
unsigned included_range_count
Definition: tree.h:19
const TSLanguage * language
Definition: tree.h:17
Definition: gzappend.c:170
TSTree * ts_tree_new(Subtree root, const TSLanguage *language, const TSRange *included_ranges, unsigned included_range_count)
Definition: tree.c:8
void ts_tree_edit(TSTree *self, const TSInputEdit *edit)
Definition: tree.c:44
void ts_tree_print_dot_graph(const TSTree *self, FILE *file)
Definition: tree.c:103
TSTree * ts_tree_copy(const TSTree *self)
Definition: tree.c:21
void ts_tree_delete(TSTree *self)
Definition: tree.c:26
TSRange * ts_tree_get_changed_ranges(const TSTree *self, const TSTree *other, uint32_t *count)
Definition: tree.c:78
TSNode ts_tree_root_node(const TSTree *self)
Definition: tree.c:36
const TSLanguage * ts_tree_language(const TSTree *self)
Definition: tree.c:40
Subtree ts_subtree_edit(Subtree self, const TSInputEdit *edit, SubtreePool *pool)
Definition: subtree.c:640
void ts_subtree_release(SubtreePool *pool, Subtree self)
Definition: subtree.c:584
void ts_subtree_print_dot_graph(Subtree self, const TSLanguage *language, FILE *f)
Definition: subtree.c:1020
void ts_subtree_retain(Subtree self)
Definition: subtree.c:577
SubtreePool ts_subtree_pool_new(uint32_t capacity)
Definition: subtree.c:123
void ts_subtree_pool_delete(SubtreePool *self)
Definition: subtree.c:129
static Length ts_subtree_padding(Subtree self)
Definition: subtree.h:256
void ts_tree_cursor_init(TreeCursor *self, TSNode node)
Definition: tree_cursor.c:80