Rizin
unix-like reverse engineering framework and cli tools
tree.c File Reference
#include "tree_sitter/api.h"
#include "./array.h"
#include "./get_changed_ranges.h"
#include "./subtree.h"
#include "./tree_cursor.h"
#include "./tree.h"

Go to the source code of this file.

Functions

TSTreets_tree_new (Subtree root, const TSLanguage *language, const TSRange *included_ranges, unsigned included_range_count)
 
TSTreets_tree_copy (const TSTree *self)
 
void ts_tree_delete (TSTree *self)
 
TSNode ts_tree_root_node (const TSTree *self)
 
const TSLanguagets_tree_language (const TSTree *self)
 
void ts_tree_edit (TSTree *self, const TSInputEdit *edit)
 
TSRangets_tree_get_changed_ranges (const TSTree *self, const TSTree *other, uint32_t *count)
 
void ts_tree_print_dot_graph (const TSTree *self, FILE *file)
 

Function Documentation

◆ ts_tree_copy()

TSTree* ts_tree_copy ( const TSTree self)

Create a shallow copy of the syntax tree. This is very fast.

You need to copy a syntax tree in order to use it on more than one thread at a time, as syntax trees are not thread safe.

Definition at line 21 of file tree.c.

21  {
22  ts_subtree_retain(self->root);
23  return ts_tree_new(self->root, self->language, self->included_ranges, self->included_range_count);
24 }
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
TSTree * ts_tree_new(Subtree root, const TSLanguage *language, const TSRange *included_ranges, unsigned included_range_count)
Definition: tree.c:8
void ts_subtree_retain(Subtree self)
Definition: subtree.c:577

References ts_subtree_retain(), and ts_tree_new().

◆ ts_tree_delete()

void ts_tree_delete ( TSTree self)

Delete the syntax tree, freeing all of the memory that it used.

Definition at line 26 of file tree.c.

26  {
27  if (!self) return;
28 
30  ts_subtree_release(&pool, self->root);
32  ts_free(self->included_ranges);
33  ts_free(self);
34 }
#define ts_free
Definition: alloc.h:30
void ts_subtree_release(SubtreePool *pool, Subtree self)
Definition: subtree.c:584
SubtreePool ts_subtree_pool_new(uint32_t capacity)
Definition: subtree.c:123
void ts_subtree_pool_delete(SubtreePool *self)
Definition: subtree.c:129

References ts_free, ts_subtree_pool_delete(), ts_subtree_pool_new(), and ts_subtree_release().

Referenced by core_cmd_tsrzcmd(), guess_data_free(), guess_next_autocmplt_token(), rz_type_parse_string_declaration_single(), rz_type_parse_string_single(), substitute_args_do(), substitute_args_fini(), and type_parse_string().

◆ ts_tree_edit()

void ts_tree_edit ( TSTree self,
const TSInputEdit edit 
)

Edit the syntax tree to keep it in sync with source code that has been edited.

You must describe the edit both in terms of byte offsets and in terms of (row, column) coordinates.

Definition at line 44 of file tree.c.

44  {
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 }
lzma_index ** i
Definition: index.h:629
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
#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:60
Subtree ts_subtree_edit(Subtree self, const TSInputEdit *edit, SubtreePool *pool)
Definition: subtree.c:640

References i, TSInputEdit::new_end_byte, TSInputEdit::new_end_point, TSInputEdit::old_end_byte, TSInputEdit::old_end_point, point_add(), POINT_MAX, point_sub(), capstone::range, ts_subtree_edit(), ts_subtree_pool_delete(), ts_subtree_pool_new(), and UINT32_MAX.

Referenced by ts_tree_edit_wasm().

◆ ts_tree_get_changed_ranges()

TSRange* ts_tree_get_changed_ranges ( const TSTree old_tree,
const TSTree new_tree,
uint32_t length 
)

Compare an old edited syntax tree to a new syntax tree representing the same document, returning an array of ranges whose syntactic structure has changed.

For this to work correctly, the old syntax tree must have been edited such that its ranges match up to the new tree. Generally, you'll want to call this function right after calling one of the ts_parser_parse functions. You need to pass the old tree that was passed to parse, as well as the new tree that was returned from that function.

The returned array is allocated using malloc and the caller is responsible for freeing it using free. The length of the array will be written to the given length pointer.

Definition at line 78 of file tree.c.

78  {
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();
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 }
#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
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)
TSNode ts_tree_root_node(const TSTree *self)
Definition: tree.c:36
void ts_tree_cursor_init(TreeCursor *self, TSNode node)
Definition: tree_cursor.c:80

References array_delete, array_new, count, TSTree::included_range_count, TSTree::included_ranges, NULL, TSTree::root, ts_range_array_get_changed_ranges(), ts_subtree_get_changed_ranges(), ts_tree_cursor_init(), and ts_tree_root_node().

Referenced by ts_tree_get_changed_ranges_wasm().

◆ ts_tree_language()

const TSLanguage* ts_tree_language ( const TSTree self)

Get the language that was used to parse the syntax tree.

Definition at line 40 of file tree.c.

40  {
41  return self->language;
42 }

◆ ts_tree_new()

TSTree* ts_tree_new ( Subtree  root,
const TSLanguage language,
const TSRange included_ranges,
unsigned  included_range_count 
)

Definition at line 8 of file tree.c.

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 }
#define ts_malloc
Definition: alloc.h:21
#define ts_calloc
Definition: alloc.h:24
int root
Definition: enough.c:226
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
Definition: tree.h:15

References TSTree::included_range_count, TSTree::included_ranges, TSTree::language, memcpy(), TSTree::root, root, ts_calloc, and ts_malloc.

Referenced by ts_parser_parse(), and ts_tree_copy().

◆ ts_tree_print_dot_graph()

void ts_tree_print_dot_graph ( const TSTree self,
FILE *  file 
)

Write a DOT graph describing the syntax tree to the given file.

Definition at line 103 of file tree.c.

103  {
105 }
Definition: gzappend.c:170
void ts_subtree_print_dot_graph(Subtree self, const TSLanguage *language, FILE *f)
Definition: subtree.c:1020

References ts_subtree_print_dot_graph().

◆ ts_tree_root_node()

TSNode ts_tree_root_node ( const TSTree self)

Get the root node of the syntax tree.

Definition at line 36 of file tree.c.

36  {
37  return ts_node_new(self, &self->root, ts_subtree_padding(self->root), 0);
38 }
TSNode ts_node_new(const TSTree *tree, const Subtree *subtree, Length position, TSSymbol alias)
Definition: node.c:17
static Length ts_subtree_padding(Subtree self)
Definition: subtree.h:256

References ts_node_new(), and ts_subtree_padding().

Referenced by core_cmd_tsrzcmd(), guess_next_autocmplt_token(), rz_type_parse_string_declaration_single(), rz_type_parse_string_single(), substitute_args_do(), ts_node_parent(), ts_tree_get_changed_ranges(), ts_tree_root_node_wasm(), and type_parse_string().