21 #define TS_MAX_INLINE_TREE_LENGTH UINT8_MAX
22 #define TS_MAX_TREE_POOL_SIZE 32
30 if (
length >
sizeof(self->short_data)) {
40 if (self->length >
sizeof(self->short_data)) {
48 if (self->length >
sizeof(self->short_data)) {
54 if (self->length >
sizeof(self->short_data)) {
55 return self->long_data;
57 return self->short_data;
63 a->length ==
b->length &&
71 dest->size =
self.size;
72 dest->capacity =
self.capacity;
73 dest->contents =
self.contents;
74 if (
self.capacity > 0) {
97 SubtreeArray *destination
100 while (self->size > 0) {
101 Subtree last =
self->contents[
self->size - 1];
114 size_t reverse_index =
self->size - 1 -
i;
116 self->contents[
i] =
self->contents[reverse_index];
117 self->contents[reverse_index] =
swap;
130 if (self->free_trees.contents) {
131 for (
unsigned i = 0;
i <
self->free_trees.size;
i++) {
132 ts_free(self->free_trees.contents[
i].ptr);
136 if (self->tree_stack.contents)
array_delete(&self->tree_stack);
140 if (self->free_trees.size > 0) {
162 size.extent.row == 0 &&
164 lookahead_bytes < 16;
170 bool has_external_tokens,
bool depends_on_column,
178 !has_external_tokens &&
184 .parse_state = parse_state,
186 .padding_bytes = padding.
bytes,
189 .size_bytes =
size.bytes,
190 .lookahead_bytes = lookahead_bytes,
192 .named = metadata.
named,
194 .has_changes =
false,
196 .is_keyword = is_keyword,
205 .lookahead_bytes = lookahead_bytes,
209 .parse_state = parse_state,
211 .named = metadata.
named,
213 .fragile_left =
false,
214 .fragile_right =
false,
215 .has_changes =
false,
216 .has_external_tokens = has_external_tokens,
217 .depends_on_column = depends_on_column,
219 .is_keyword = is_keyword,
220 {{.first_leaf = {.symbol = 0, .parse_state = 0}}}
222 return (
Subtree) {.ptr = data};
232 if (self->data.is_inline) {
234 self->data.symbol = symbol;
235 self->data.named = metadata.
named;
236 self->data.visible = metadata.
visible;
238 self->ptr->symbol = symbol;
239 self->ptr->named = metadata.
named;
240 self->ptr->visible = metadata.
visible;
250 parse_state,
false,
false,
false, language
264 memcpy(new_children, old_children, alloc_size);
266 if (
self.ptr->child_count > 0) {
267 for (
uint32_t i = 0;
i <
self.ptr->child_count;
i++) {
270 }
else if (
self.ptr->has_external_tokens) {
272 &
self.ptr->external_scanner_state
296 MutableSubtreeArray *
stack
298 unsigned initial_stack_size =
stack->size;
302 for (
unsigned i = 0;
i <
count;
i++) {
328 while (
stack->size > initial_stack_size) {
352 if (repeat_delta > 0) {
353 unsigned n = repeat_delta;
354 for (
unsigned i =
n / 2;
i > 0;
i /= 2) {
375 assert(!
self.data.is_inline);
377 self.ptr->named_child_count = 0;
378 self.ptr->visible_child_count = 0;
379 self.ptr->error_cost = 0;
380 self.ptr->repeat_depth = 0;
381 self.ptr->node_count = 1;
382 self.ptr->has_external_tokens =
false;
383 self.ptr->depends_on_column =
false;
384 self.ptr->dynamic_precedence = 0;
391 for (
uint32_t i = 0;
i <
self.ptr->child_count;
i++) {
395 self.ptr->size.extent.row == 0 &&
409 self.ptr->padding.bytes +
410 self.ptr->size.bytes +
412 if (child_lookahead_end_byte > lookahead_end_byte) {
413 lookahead_end_byte = child_lookahead_end_byte;
428 }
else if (grandchild_count > 0) {
437 if (alias_sequence && alias_sequence[structural_index] != 0 && !
ts_subtree_extra(child)) {
438 self.ptr->visible_child_count++;
440 self.ptr->named_child_count++;
443 self.ptr->visible_child_count++;
445 }
else if (grandchild_count > 0) {
453 self.ptr->fragile_left =
self.ptr->fragile_right =
true;
460 self.ptr->lookahead_bytes = lookahead_end_byte -
self.ptr->size.bytes -
self.ptr->padding.bytes;
466 self.ptr->error_cost +=
472 if (
self.ptr->child_count > 0) {
473 Subtree first_child = children[0];
474 Subtree last_child = children[
self.ptr->child_count - 1];
483 self.ptr->child_count >= 2 &&
484 !
self.ptr->visible &&
502 SubtreeArray *children,
503 unsigned production_id,
511 if (children->capacity *
sizeof(
Subtree) < new_byte_size) {
512 children->contents =
ts_realloc(children->contents, new_byte_size);
513 children->capacity = new_byte_size /
sizeof(
Subtree);
520 .child_count = children->size,
522 .named = metadata.
named,
523 .has_changes =
false,
524 .fragile_left = fragile,
525 .fragile_right = fragile,
529 .production_id = production_id,
530 .first_leaf = {.symbol = 0, .parse_state = 0},
543 SubtreeArray *children,
566 pool, symbol, padding,
length_zero(), lookahead_bytes,
567 0,
false,
false,
false, language
570 result.
data.is_missing =
true;
578 if (
self.data.is_inline)
return;
579 assert(
self.ptr->ref_count > 0);
581 assert(
self.ptr->ref_count != 0);
585 if (
self.data.is_inline)
return;
588 assert(
self.ptr->ref_count > 0);
633 if (self->data.is_inline) {
634 self->data.has_changes =
true;
636 self->ptr->has_changes =
true;
692 (edit.
start.
bytes == total_bytes && is_pure_insertion)) {
704 result.
data.padding_bytes = padding.
bytes;
707 result.
data.size_bytes =
size.bytes;
742 child_left = child_right;
743 child_right =
length_add(child_left, child_size);
755 !invalidate_first_row ||
834 for (
const char *
c =
string; *
c;
c++) {
837 }
else if (*
c ==
'\n') {
854 char *cursor = string;
891 }
else if (is_root) {
902 self.ptr->production_id,
907 uint32_t structural_child_index = 0;
908 for (
uint32_t i = 0;
i <
self.ptr->child_count;
i++) {
913 language, include_all,
917 TSSymbol alias_symbol = alias_sequence
918 ? alias_sequence[structural_child_index]
920 bool alias_is_named = alias_symbol
926 if (!
i->inherited &&
i->child_index == structural_child_index) {
934 language, include_all,
935 alias_symbol, alias_is_named, child_field_name
937 structural_child_index++;
944 return cursor - string;
952 char scratch_string[1];
954 self, scratch_string, 1,
955 language, include_all,
961 language, include_all,
973 fprintf(
f,
"tree_%p [label=\"", (
void *)
self);
980 fprintf(
f,
", tooltip=\""
985 "depends-on-column: %u\n"
987 "lookahead-bytes: %u",
988 start_offset, end_offset,
998 fprintf(
f,
"\ncharacter: '%c'", self->ptr->lookahead_char);
1001 fprintf(
f,
"\"]\n");
1003 uint32_t child_start_offset = start_offset;
1012 child_info_offset++;
1015 fprintf(
f,
"tree_%p -> tree_%p [tooltip=%u]\n", (
void *)
self, (
void *)child,
i);
1021 fprintf(
f,
"digraph tree {\n");
1022 fprintf(
f,
"edge [arrowhead=none]\n");
1031 state1 = &
self.ptr->external_scanner_state;
const char * ts_language_symbol_name(const TSLanguage *, TSSymbol)
#define array_push(self, element)
#define array_reserve(self, new_capacity)
#define array_delete(self)
#define array_clear(self)
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
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 long
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 static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
#define ERROR_COST_PER_SKIPPED_TREE
#define ERROR_COST_PER_SKIPPED_LINE
#define ERROR_COST_PER_RECOVERY
#define ERROR_COST_PER_SKIPPED_CHAR
TSSymbolMetadata ts_language_symbol_metadata(const TSLanguage *self, TSSymbol symbol)
static void ts_language_field_map(const TSLanguage *self, uint32_t production_id, const TSFieldMapEntry **start, const TSFieldMapEntry **end)
#define ts_builtin_sym_error_repeat
static const TSSymbol * ts_language_alias_sequence(const TSLanguage *self, uint32_t production_id)
static Length length_zero(void)
static Length length_add(Length len1, Length len2)
static Length length_sub(Length len1, Length len2)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
assert(limit<=UINT32_MAX/2)
static uint32_t const uint8_t uint32_t uint32_t limit
#define ts_builtin_sym_end
#define ts_builtin_sym_error
uint32_t visible_child_count
uint32_t named_child_count
volatile uint32_t ref_count
ExternalScannerState external_scanner_state
struct SubtreeHeapData::@621::@623::@625 first_leaf
SUBTREE_BITS uint8_t symbol
MutableSubtreeArray tree_stack
const char *const * field_names
const TSSymbol * alias_sequences
uint16_t max_alias_sequence_length
static void ts_subtree_set_has_changes(MutableSubtree *self)
void ts_subtree_array_reverse(SubtreeArray *self)
bool ts_subtree_external_scanner_state_eq(Subtree self, Subtree other)
Subtree ts_subtree_edit(Subtree self, const TSInputEdit *edit, SubtreePool *pool)
static void ts_subtree_pool_free(SubtreePool *self, SubtreeHeapData *tree)
MutableSubtree ts_subtree_new_node(TSSymbol symbol, SubtreeArray *children, unsigned production_id, const TSLanguage *language)
static const ExternalScannerState empty_state
void ts_subtree_summarize_children(MutableSubtree self, const TSLanguage *language)
static void ts_subtree__compress(MutableSubtree self, unsigned count, const TSLanguage *language, MutableSubtreeArray *stack)
void ts_subtree_release(SubtreePool *pool, Subtree self)
Subtree ts_subtree_new_leaf(SubtreePool *pool, TSSymbol symbol, Length padding, Length size, uint32_t lookahead_bytes, TSStateId parse_state, bool has_external_tokens, bool depends_on_column, bool is_keyword, const TSLanguage *language)
void ts_subtree_print_dot_graph(Subtree self, const TSLanguage *language, FILE *f)
static SubtreeHeapData * ts_subtree_pool_allocate(SubtreePool *self)
#define TS_MAX_INLINE_TREE_LENGTH
Subtree ts_subtree_new_error_node(SubtreeArray *children, bool extra, const TSLanguage *language)
#define TS_MAX_TREE_POOL_SIZE
static size_t ts_subtree__write_char_to_string(char *s, size_t n, int32_t c)
bool ts_external_scanner_state_eq(const ExternalScannerState *a, const ExternalScannerState *b)
static size_t ts_subtree__write_to_string(Subtree self, char *string, size_t limit, const TSLanguage *language, bool include_all, TSSymbol alias_symbol, bool alias_is_named, const char *field_name)
MutableSubtree ts_subtree_make_mut(SubtreePool *pool, Subtree self)
void ts_subtree_retain(Subtree self)
static void ts_subtree__write_dot_string(FILE *f, const char *string)
void ts_subtree_array_delete(SubtreePool *pool, SubtreeArray *self)
void ts_subtree_array_remove_trailing_extras(SubtreeArray *self, SubtreeArray *destination)
const char * ts_external_scanner_state_data(const ExternalScannerState *self)
void ts_subtree_set_symbol(MutableSubtree *self, TSSymbol symbol, const TSLanguage *language)
void ts_subtree_balance(Subtree self, SubtreePool *pool, const TSLanguage *language)
Subtree ts_subtree_last_external_token(Subtree tree)
void ts_subtree__print_dot_graph(const Subtree *self, uint32_t start_offset, const TSLanguage *language, TSSymbol alias_symbol, FILE *f)
Subtree ts_subtree_new_error(SubtreePool *pool, int32_t lookahead_char, Length padding, Length size, uint32_t bytes_scanned, TSStateId parse_state, const TSLanguage *language)
ExternalScannerState ts_external_scanner_state_copy(const ExternalScannerState *self)
SubtreePool ts_subtree_pool_new(uint32_t capacity)
void ts_subtree_array_copy(SubtreeArray self, SubtreeArray *dest)
void ts_external_scanner_state_init(ExternalScannerState *self, const char *data, unsigned length)
int ts_subtree_compare(Subtree left, Subtree right)
Subtree ts_subtree_new_missing_leaf(SubtreePool *pool, TSSymbol symbol, Length padding, uint32_t lookahead_bytes, const TSLanguage *language)
static bool ts_subtree_can_inline(Length padding, Length size, uint32_t lookahead_bytes)
char * ts_subtree_string(Subtree self, const TSLanguage *language, bool include_all)
void ts_subtree_array_clear(SubtreePool *pool, SubtreeArray *self)
MutableSubtree ts_subtree_clone(Subtree self)
void ts_subtree_pool_delete(SubtreePool *self)
void ts_external_scanner_state_delete(ExternalScannerState *self)
static const char * ROOT_FIELD
static size_t ts_subtree_alloc_size(uint32_t child_count)
static bool ts_subtree_fragile_left(Subtree self)
#define ts_subtree_children(self)
static bool ts_subtree_visible(Subtree self)
static Subtree ts_subtree_from_mut(MutableSubtree self)
static bool ts_subtree_has_external_tokens(Subtree self)
static Length ts_subtree_total_size(Subtree self)
static MutableSubtree ts_subtree_to_mut_unsafe(Subtree self)
static uint32_t ts_subtree_total_bytes(Subtree self)
static uint32_t ts_subtree_error_cost(Subtree self)
#define TS_TREE_STATE_NONE
static int32_t ts_subtree_dynamic_precedence(Subtree self)
static bool ts_subtree_extra(Subtree self)
static bool ts_subtree_is_error(Subtree self)
static bool ts_subtree_has_changes(Subtree self)
static bool ts_subtree_missing(Subtree self)
static uint16_t ts_subtree_production_id(Subtree self)
static Length ts_subtree_size(Subtree self)
static uint32_t ts_subtree_repeat_depth(Subtree self)
static TSSymbol ts_subtree_leaf_symbol(Subtree self)
static bool ts_subtree_depends_on_column(Subtree self)
static bool ts_subtree_named(Subtree self)
static TSStateId ts_subtree_parse_state(Subtree self)
static uint32_t ts_subtree_lookahead_bytes(Subtree self)
static uint32_t ts_subtree_child_count(Subtree self)
static TSStateId ts_subtree_leaf_parse_state(Subtree self)
static Length ts_subtree_padding(Subtree self)
static uint32_t ts_subtree_node_count(Subtree self)
static bool ts_subtree_fragile_right(Subtree self)
static TSSymbol ts_subtree_symbol(Subtree self)
static uint32_t atomic_dec(volatile uint32_t *p)
static uint32_t atomic_inc(volatile uint32_t *p)
#define subtree_symbol(subtree, structural_child_index)
const SubtreeHeapData * ptr
if(dbg->bits==RZ_SYS_BITS_64)