queries c.h c.scm cpp.h cpp.scm cuda.h cuda.scm glsl.h glsl.scm go.h go.scm javascript.h javascript.scm kotlin.h kotlin.scm lua.h lua.scm odin.h odin.scm php.h php.scm python.h python.scm rust.h rust.scm tcl.h tcl.scm zig.h zig.scm
tests
depth_test
sub level1.c
level0.c
test.c test.cpp test.cu test.cuh test.glsl test.go test.js test.kt test.lua test.odin test.php test.py test.rs test.tcl test.zig
vendor
tree-sitter
lib
include
tree_sitter api.h parser.h
src
unicode ICU_SHA LICENSE README.md ptypes.h umachine.h urename.h utf.h utf16.h utf8.h
alloc.c alloc.h array.h atomic.h clock.h error_costs.h get_changed_ranges.c get_changed_ranges.h host.h language.c language.h length.h lexer.c lexer.h lib.c node.c parser.c point.h query.c reduce_action.h reusable_node.h stack.c stack.h subtree.c subtree.h tree.c tree.h tree_cursor.c tree_cursor.h unicode.h
LICENSE Makefile
tree-sitter-c
src
tree_sitter parser.h
grammar.json node-types.json parser.c
LICENSE Makefile
tree-sitter-cpp
src
tree_sitter alloc.h array.h parser.h
grammar.json node-types.json parser.c scanner.c
LICENSE Makefile
tree-sitter-cuda
src
tree_sitter alloc.h array.h parser.h runtime.h
grammar.json node-types.json parser.c scanner.c
LICENSE Makefile
tree-sitter-glsl
src
tree_sitter alloc.h array.h parser.h runtime.h
grammar.json node-types.json parser.c
LICENSE Makefile
tree-sitter-go
src
tree_sitter alloc.h array.h parser.h
grammar.json node-types.json parser.c
LICENSE Makefile
tree-sitter-javascript
src
tree_sitter alloc.h array.h parser.h
grammar.json node-types.json parser.c scanner.c
LICENSE Makefile
tree-sitter-kotlin
src
tree_sitter alloc.h array.h parser.h
grammar.json node-types.json parser.c scanner.c
LICENSE Makefile
tree-sitter-lua
src
tree_sitter alloc.h array.h parser.h
grammar.json node-types.json parser.c scanner.c
LICENSE.md Makefile
tree-sitter-odin
src
tree_sitter alloc.h array.h parser.h
grammar.json node-types.json parser.c scanner.c
LICENSE Makefile
tree-sitter-php
src
common common.mak define-grammar.js scanner.h
tree_sitter alloc.h array.h parser.h
grammar.json node-types.json parser.c scanner.c
LICENSE Makefile
tree-sitter-python
src
tree_sitter parser.h
grammar.json node-types.json parser.c scanner.c
LICENSE Makefile
tree-sitter-rust
src
tree_sitter alloc.h array.h parser.h
grammar.json node-types.json parser.c scanner.c
LICENSE Makefile
tree-sitter-tcl
src
tree_sitter parser.h
grammar.json node-types.json parser.c scanner.c
LICENSE Makefile
tree-sitter-template Makefile
tree-sitter-zig
src
tree_sitter alloc.h array.h parser.h
grammar.json node-types.json parser.c
LICENSE Makefile
.clang-format .gitattributes .gitignore LICENSE Makefile README.md abicheck.c compile_flags.txt file.c file.h list.c list.h main.c tests.sh tpool.c tpool.h
vendor/tree-sitter/lib/src/tree.c raw
  1#include "tree_sitter/api.h"
  2#include "./array.h"
  3#include "./get_changed_ranges.h"
  4#include "./length.h"
  5#include "./subtree.h"
  6#include "./tree_cursor.h"
  7#include "./tree.h"
  8
  9TSTree *ts_tree_new(
 10  Subtree root, const TSLanguage *language,
 11  const TSRange *included_ranges, unsigned included_range_count
 12) {
 13  TSTree *result = ts_malloc(sizeof(TSTree));
 14  result->root = root;
 15  result->language = language;
 16  result->included_ranges = ts_calloc(included_range_count, sizeof(TSRange));
 17  memcpy(result->included_ranges, included_ranges, included_range_count * sizeof(TSRange));
 18  result->included_range_count = included_range_count;
 19  return result;
 20}
 21
 22TSTree *ts_tree_copy(const TSTree *self) {
 23  ts_subtree_retain(self->root);
 24  return ts_tree_new(self->root, self->language, self->included_ranges, self->included_range_count);
 25}
 26
 27void ts_tree_delete(TSTree *self) {
 28  if (!self) return;
 29
 30  SubtreePool pool = ts_subtree_pool_new(0);
 31  ts_subtree_release(&pool, self->root);
 32  ts_subtree_pool_delete(&pool);
 33  ts_free(self->included_ranges);
 34  ts_free(self);
 35}
 36
 37TSNode ts_tree_root_node(const TSTree *self) {
 38  return ts_node_new(self, &self->root, ts_subtree_padding(self->root), 0);
 39}
 40
 41TSNode ts_tree_root_node_with_offset(
 42  const TSTree *self,
 43  uint32_t offset_bytes,
 44  TSPoint offset_extent
 45) {
 46  Length offset = {offset_bytes, offset_extent};
 47  return ts_node_new(self, &self->root, length_add(offset, ts_subtree_padding(self->root)), 0);
 48}
 49
 50const TSLanguage *ts_tree_language(const TSTree *self) {
 51  return self->language;
 52}
 53
 54void ts_tree_edit(TSTree *self, const TSInputEdit *edit) {
 55  for (unsigned i = 0; i < self->included_range_count; i++) {
 56    TSRange *range = &self->included_ranges[i];
 57    if (range->end_byte >= edit->old_end_byte) {
 58      if (range->end_byte != UINT32_MAX) {
 59        range->end_byte = edit->new_end_byte + (range->end_byte - edit->old_end_byte);
 60        range->end_point = point_add(
 61          edit->new_end_point,
 62          point_sub(range->end_point, edit->old_end_point)
 63        );
 64        if (range->end_byte < edit->new_end_byte) {
 65          range->end_byte = UINT32_MAX;
 66          range->end_point = POINT_MAX;
 67        }
 68      }
 69    } else if (range->end_byte > edit->start_byte) {
 70      range->end_byte = edit->start_byte;
 71      range->end_point = edit->start_point;
 72    }
 73    if (range->start_byte >= edit->old_end_byte) {
 74      range->start_byte = edit->new_end_byte + (range->start_byte - edit->old_end_byte);
 75      range->start_point = point_add(
 76        edit->new_end_point,
 77        point_sub(range->start_point, edit->old_end_point)
 78      );
 79      if (range->start_byte < edit->new_end_byte) {
 80        range->start_byte = UINT32_MAX;
 81        range->start_point = POINT_MAX;
 82      }
 83    } else if (range->start_byte > edit->start_byte) {
 84      range->start_byte = edit->start_byte;
 85      range->start_point = edit->start_point;
 86    }
 87  }
 88
 89  SubtreePool pool = ts_subtree_pool_new(0);
 90  self->root = ts_subtree_edit(self->root, edit, &pool);
 91  ts_subtree_pool_delete(&pool);
 92}
 93
 94TSRange *ts_tree_included_ranges(const TSTree *self, uint32_t *length) {
 95  *length = self->included_range_count;
 96  TSRange *ranges = ts_calloc(self->included_range_count, sizeof(TSRange));
 97  memcpy(ranges, self->included_ranges, self->included_range_count * sizeof(TSRange));
 98  return ranges;
 99}
100
101TSRange *ts_tree_get_changed_ranges(const TSTree *old_tree, const TSTree *new_tree, uint32_t *length) {
102  TreeCursor cursor1 = {NULL, array_new()};
103  TreeCursor cursor2 = {NULL, array_new()};
104  ts_tree_cursor_init(&cursor1, ts_tree_root_node(old_tree));
105  ts_tree_cursor_init(&cursor2, ts_tree_root_node(new_tree));
106
107  TSRangeArray included_range_differences = array_new();
108  ts_range_array_get_changed_ranges(
109    old_tree->included_ranges, old_tree->included_range_count,
110    new_tree->included_ranges, new_tree->included_range_count,
111    &included_range_differences
112  );
113
114  TSRange *result;
115  *length = ts_subtree_get_changed_ranges(
116    &old_tree->root, &new_tree->root, &cursor1, &cursor2,
117    old_tree->language, &included_range_differences, &result
118  );
119
120  array_delete(&included_range_differences);
121  array_delete(&cursor1.stack);
122  array_delete(&cursor2.stack);
123  return result;
124}
125
126#ifdef _WIN32
127
128void ts_tree_print_dot_graph(const TSTree *self, int fd) {
129  (void)self;
130  (void)fd;
131}
132
133#else
134
135#include <unistd.h>
136
137void ts_tree_print_dot_graph(const TSTree *self, int file_descriptor) {
138  FILE *file = fdopen(dup(file_descriptor), "a");
139  ts_subtree_print_dot_graph(self->root, self->language, file);
140  fclose(file);
141}
142
143#endif