| 1 |
#include <ctype.h> |
| 2 |
#include <stdint.h> |
| 3 |
#include <stdbool.h> |
| 4 |
#include <string.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include "./alloc.h" |
| 7 |
#include "./array.h" |
| 8 |
#include "./atomic.h" |
| 9 |
#include "./subtree.h" |
| 10 |
#include "./length.h" |
| 11 |
#include "./language.h" |
| 12 |
#include "./error_costs.h" |
| 13 |
#include "./ts_assert.h" |
| 14 |
#include <stddef.h> |
| 15 |
|
| 16 |
typedef struct { |
| 17 |
Length start; |
| 18 |
Length old_end; |
| 19 |
Length new_end; |
| 20 |
} Edit; |
| 21 |
|
| 22 |
#define TS_MAX_INLINE_TREE_LENGTH UINT8_MAX |
| 23 |
#define TS_MAX_TREE_POOL_SIZE 32 |
| 24 |
|
| 25 |
// ExternalScannerState |
| 26 |
|
| 27 |
void ts_external_scanner_state_init(ExternalScannerState *self, const char *data, unsigned length) { |
| 28 |
self->length = length; |
| 29 |
if (length > sizeof(self->short_data)) { |
| 30 |
self->long_data = ts_malloc(length); |
| 31 |
memcpy(self->long_data, data, length); |
| 32 |
} else { |
| 33 |
memcpy(self->short_data, data, length); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
ExternalScannerState ts_external_scanner_state_copy(const ExternalScannerState *self) { |
| 38 |
ExternalScannerState result = *self; |
| 39 |
if (self->length > sizeof(self->short_data)) { |
| 40 |
result.long_data = ts_malloc(self->length); |
| 41 |
memcpy(result.long_data, self->long_data, self->length); |
| 42 |
} |
| 43 |
return result; |
| 44 |
} |
| 45 |
|
| 46 |
void ts_external_scanner_state_delete(ExternalScannerState *self) { |
| 47 |
if (self->length > sizeof(self->short_data)) { |
| 48 |
ts_free(self->long_data); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
const char *ts_external_scanner_state_data(const ExternalScannerState *self) { |
| 53 |
if (self->length > sizeof(self->short_data)) { |
| 54 |
return self->long_data; |
| 55 |
} else { |
| 56 |
return self->short_data; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
bool ts_external_scanner_state_eq(const ExternalScannerState *self, const char *buffer, unsigned length) { |
| 61 |
return |
| 62 |
self->length == length && |
| 63 |
memcmp(ts_external_scanner_state_data(self), buffer, length) == 0; |
| 64 |
} |
| 65 |
|
| 66 |
// SubtreeArray |
| 67 |
|
| 68 |
void ts_subtree_array_copy(SubtreeArray self, SubtreeArray *dest) { |
| 69 |
dest->size = self.size; |
| 70 |
dest->capacity = self.capacity; |
| 71 |
dest->contents = self.contents; |
| 72 |
if (self.capacity > 0) { |
| 73 |
dest->contents = ts_calloc(self.capacity, sizeof(Subtree)); |
| 74 |
memcpy(dest->contents, self.contents, self.size * sizeof(Subtree)); |
| 75 |
for (uint32_t i = 0; i < self.size; i++) { |
| 76 |
ts_subtree_retain(*array_get(dest, i)); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
void ts_subtree_array_clear(SubtreePool *pool, SubtreeArray *self) { |
| 82 |
for (uint32_t i = 0; i < self->size; i++) { |
| 83 |
ts_subtree_release(pool, *array_get(self, i)); |
| 84 |
} |
| 85 |
array_clear(self); |
| 86 |
} |
| 87 |
|
| 88 |
void ts_subtree_array_delete(SubtreePool *pool, SubtreeArray *self) { |
| 89 |
ts_subtree_array_clear(pool, self); |
| 90 |
array_delete(self); |
| 91 |
} |
| 92 |
|
| 93 |
void ts_subtree_array_remove_trailing_extras( |
| 94 |
SubtreeArray *self, |
| 95 |
SubtreeArray *destination |
| 96 |
) { |
| 97 |
array_clear(destination); |
| 98 |
while (self->size > 0) { |
| 99 |
Subtree last = *array_get(self, self->size - 1); |
| 100 |
if (ts_subtree_extra(last)) { |
| 101 |
self->size--; |
| 102 |
array_push(destination, last); |
| 103 |
} else { |
| 104 |
break; |
| 105 |
} |
| 106 |
} |
| 107 |
ts_subtree_array_reverse(destination); |
| 108 |
} |
| 109 |
|
| 110 |
void ts_subtree_array_reverse(SubtreeArray *self) { |
| 111 |
for (uint32_t i = 0, limit = self->size / 2; i < limit; i++) { |
| 112 |
size_t reverse_index = self->size - 1 - i; |
| 113 |
Subtree swap = *array_get(self, i); |
| 114 |
*array_get(self, i) = *array_get(self, reverse_index); |
| 115 |
*array_get(self, reverse_index) = swap; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// SubtreePool |
| 120 |
|
| 121 |
SubtreePool ts_subtree_pool_new(uint32_t capacity) { |
| 122 |
SubtreePool self = {array_new(), array_new()}; |
| 123 |
array_reserve(&self.free_trees, capacity); |
| 124 |
return self; |
| 125 |
} |
| 126 |
|
| 127 |
void ts_subtree_pool_delete(SubtreePool *self) { |
| 128 |
if (self->free_trees.contents) { |
| 129 |
for (unsigned i = 0; i < self->free_trees.size; i++) { |
| 130 |
ts_free(array_get(&self->free_trees, i)->ptr); |
| 131 |
} |
| 132 |
array_delete(&self->free_trees); |
| 133 |
} |
| 134 |
if (self->tree_stack.contents) array_delete(&self->tree_stack); |
| 135 |
} |
| 136 |
|
| 137 |
static SubtreeHeapData *ts_subtree_pool_allocate(SubtreePool *self) { |
| 138 |
if (self->free_trees.size > 0) { |
| 139 |
return array_pop(&self->free_trees).ptr; |
| 140 |
} else { |
| 141 |
return ts_malloc(sizeof(SubtreeHeapData)); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
static void ts_subtree_pool_free(SubtreePool *self, SubtreeHeapData *tree) { |
| 146 |
if (self->free_trees.capacity > 0 && self->free_trees.size + 1 <= TS_MAX_TREE_POOL_SIZE) { |
| 147 |
array_push(&self->free_trees, (MutableSubtree) {.ptr = tree}); |
| 148 |
} else { |
| 149 |
ts_free(tree); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
// Subtree |
| 154 |
|
| 155 |
static inline bool ts_subtree_can_inline(Length padding, Length size, uint32_t lookahead_bytes) { |
| 156 |
return |
| 157 |
padding.bytes < TS_MAX_INLINE_TREE_LENGTH && |
| 158 |
padding.extent.row < 16 && |
| 159 |
padding.extent.column < TS_MAX_INLINE_TREE_LENGTH && |
| 160 |
size.bytes < TS_MAX_INLINE_TREE_LENGTH && |
| 161 |
size.extent.row == 0 && |
| 162 |
size.extent.column < TS_MAX_INLINE_TREE_LENGTH && |
| 163 |
lookahead_bytes < 16; |
| 164 |
} |
| 165 |
|
| 166 |
Subtree ts_subtree_new_leaf( |
| 167 |
SubtreePool *pool, TSSymbol symbol, Length padding, Length size, |
| 168 |
uint32_t lookahead_bytes, TSStateId parse_state, |
| 169 |
bool has_external_tokens, bool depends_on_column, |
| 170 |
bool is_keyword, const TSLanguage *language |
| 171 |
) { |
| 172 |
TSSymbolMetadata metadata = ts_language_symbol_metadata(language, symbol); |
| 173 |
bool extra = symbol == ts_builtin_sym_end; |
| 174 |
|
| 175 |
bool is_inline = ( |
| 176 |
symbol <= UINT8_MAX && |
| 177 |
!has_external_tokens && |
| 178 |
ts_subtree_can_inline(padding, size, lookahead_bytes) |
| 179 |
); |
| 180 |
|
| 181 |
if (is_inline) { |
| 182 |
return (Subtree) {{ |
| 183 |
.parse_state = parse_state, |
| 184 |
.symbol = symbol, |
| 185 |
.padding_bytes = padding.bytes, |
| 186 |
.padding_rows = padding.extent.row, |
| 187 |
.padding_columns = padding.extent.column, |
| 188 |
.size_bytes = size.bytes, |
| 189 |
.lookahead_bytes = lookahead_bytes, |
| 190 |
.visible = metadata.visible, |
| 191 |
.named = metadata.named, |
| 192 |
.extra = extra, |
| 193 |
.has_changes = false, |
| 194 |
.is_missing = false, |
| 195 |
.is_keyword = is_keyword, |
| 196 |
.is_inline = true, |
| 197 |
}}; |
| 198 |
} else { |
| 199 |
SubtreeHeapData *data = ts_subtree_pool_allocate(pool); |
| 200 |
*data = (SubtreeHeapData) { |
| 201 |
.ref_count = 1, |
| 202 |
.padding = padding, |
| 203 |
.size = size, |
| 204 |
.lookahead_bytes = lookahead_bytes, |
| 205 |
.error_cost = 0, |
| 206 |
.child_count = 0, |
| 207 |
.symbol = symbol, |
| 208 |
.parse_state = parse_state, |
| 209 |
.visible = metadata.visible, |
| 210 |
.named = metadata.named, |
| 211 |
.extra = extra, |
| 212 |
.fragile_left = false, |
| 213 |
.fragile_right = false, |
| 214 |
.has_changes = false, |
| 215 |
.has_external_tokens = has_external_tokens, |
| 216 |
.has_external_scanner_state_change = false, |
| 217 |
.depends_on_column = depends_on_column, |
| 218 |
.is_missing = false, |
| 219 |
.is_keyword = is_keyword, |
| 220 |
{{.first_leaf = {.symbol = 0, .parse_state = 0}}} |
| 221 |
}; |
| 222 |
return (Subtree) {.ptr = data}; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
void ts_subtree_set_symbol( |
| 227 |
MutableSubtree *self, |
| 228 |
TSSymbol symbol, |
| 229 |
const TSLanguage *language |
| 230 |
) { |
| 231 |
TSSymbolMetadata metadata = ts_language_symbol_metadata(language, symbol); |
| 232 |
if (self->data.is_inline) { |
| 233 |
ts_assert(symbol < UINT8_MAX); |
| 234 |
self->data.symbol = symbol; |
| 235 |
self->data.named = metadata.named; |
| 236 |
self->data.visible = metadata.visible; |
| 237 |
} else { |
| 238 |
self->ptr->symbol = symbol; |
| 239 |
self->ptr->named = metadata.named; |
| 240 |
self->ptr->visible = metadata.visible; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
Subtree ts_subtree_new_error( |
| 245 |
SubtreePool *pool, int32_t lookahead_char, Length padding, Length size, |
| 246 |
uint32_t bytes_scanned, TSStateId parse_state, const TSLanguage *language |
| 247 |
) { |
| 248 |
Subtree result = ts_subtree_new_leaf( |
| 249 |
pool, ts_builtin_sym_error, padding, size, bytes_scanned, |
| 250 |
parse_state, false, false, false, language |
| 251 |
); |
| 252 |
SubtreeHeapData *data = (SubtreeHeapData *)result.ptr; |
| 253 |
data->fragile_left = true; |
| 254 |
data->fragile_right = true; |
| 255 |
data->lookahead_char = lookahead_char; |
| 256 |
return result; |
| 257 |
} |
| 258 |
|
| 259 |
// Clone a subtree. |
| 260 |
MutableSubtree ts_subtree_clone(Subtree self) { |
| 261 |
size_t alloc_size = ts_subtree_alloc_size(self.ptr->child_count); |
| 262 |
Subtree *new_children = ts_malloc(alloc_size); |
| 263 |
Subtree *old_children = ts_subtree_children(self); |
| 264 |
memcpy(new_children, old_children, alloc_size); |
| 265 |
SubtreeHeapData *result = (SubtreeHeapData *)&new_children[self.ptr->child_count]; |
| 266 |
if (self.ptr->child_count > 0) { |
| 267 |
for (uint32_t i = 0; i < self.ptr->child_count; i++) { |
| 268 |
ts_subtree_retain(new_children[i]); |
| 269 |
} |
| 270 |
} else if (self.ptr->has_external_tokens) { |
| 271 |
result->external_scanner_state = ts_external_scanner_state_copy( |
| 272 |
&self.ptr->external_scanner_state |
| 273 |
); |
| 274 |
} |
| 275 |
result->ref_count = 1; |
| 276 |
return (MutableSubtree) {.ptr = result}; |
| 277 |
} |
| 278 |
|
| 279 |
// Get mutable version of a subtree. |
| 280 |
// |
| 281 |
// This takes ownership of the subtree. If the subtree has only one owner, |
| 282 |
// this will directly convert it into a mutable version. Otherwise, it will |
| 283 |
// perform a copy. |
| 284 |
MutableSubtree ts_subtree_make_mut(SubtreePool *pool, Subtree self) { |
| 285 |
if (self.data.is_inline) return (MutableSubtree) {self.data}; |
| 286 |
if (self.ptr->ref_count == 1) return ts_subtree_to_mut_unsafe(self); |
| 287 |
MutableSubtree result = ts_subtree_clone(self); |
| 288 |
ts_subtree_release(pool, self); |
| 289 |
return result; |
| 290 |
} |
| 291 |
|
| 292 |
void ts_subtree_compress( |
| 293 |
MutableSubtree self, |
| 294 |
unsigned count, |
| 295 |
const TSLanguage *language, |
| 296 |
MutableSubtreeArray *stack |
| 297 |
) { |
| 298 |
unsigned initial_stack_size = stack->size; |
| 299 |
|
| 300 |
MutableSubtree tree = self; |
| 301 |
TSSymbol symbol = tree.ptr->symbol; |
| 302 |
for (unsigned i = 0; i < count; i++) { |
| 303 |
if (tree.ptr->ref_count > 1 || tree.ptr->child_count < 2) break; |
| 304 |
|
| 305 |
MutableSubtree child = ts_subtree_to_mut_unsafe(ts_subtree_children(tree)[0]); |
| 306 |
if ( |
| 307 |
child.data.is_inline || |
| 308 |
child.ptr->child_count < 2 || |
| 309 |
child.ptr->ref_count > 1 || |
| 310 |
child.ptr->symbol != symbol |
| 311 |
) break; |
| 312 |
|
| 313 |
MutableSubtree grandchild = ts_subtree_to_mut_unsafe(ts_subtree_children(child)[0]); |
| 314 |
if ( |
| 315 |
grandchild.data.is_inline || |
| 316 |
grandchild.ptr->child_count < 2 || |
| 317 |
grandchild.ptr->ref_count > 1 || |
| 318 |
grandchild.ptr->symbol != symbol |
| 319 |
) break; |
| 320 |
|
| 321 |
ts_subtree_children(tree)[0] = ts_subtree_from_mut(grandchild); |
| 322 |
ts_subtree_children(child)[0] = ts_subtree_children(grandchild)[grandchild.ptr->child_count - 1]; |
| 323 |
ts_subtree_children(grandchild)[grandchild.ptr->child_count - 1] = ts_subtree_from_mut(child); |
| 324 |
array_push(stack, tree); |
| 325 |
tree = grandchild; |
| 326 |
} |
| 327 |
|
| 328 |
while (stack->size > initial_stack_size) { |
| 329 |
tree = array_pop(stack); |
| 330 |
MutableSubtree child = ts_subtree_to_mut_unsafe(ts_subtree_children(tree)[0]); |
| 331 |
MutableSubtree grandchild = ts_subtree_to_mut_unsafe(ts_subtree_children(child)[child.ptr->child_count - 1]); |
| 332 |
ts_subtree_summarize_children(grandchild, language); |
| 333 |
ts_subtree_summarize_children(child, language); |
| 334 |
ts_subtree_summarize_children(tree, language); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
// Assign all of the node's properties that depend on its children. |
| 339 |
void ts_subtree_summarize_children( |
| 340 |
MutableSubtree self, |
| 341 |
const TSLanguage *language |
| 342 |
) { |
| 343 |
ts_assert(!self.data.is_inline); |
| 344 |
|
| 345 |
self.ptr->named_child_count = 0; |
| 346 |
self.ptr->visible_child_count = 0; |
| 347 |
self.ptr->error_cost = 0; |
| 348 |
self.ptr->repeat_depth = 0; |
| 349 |
self.ptr->visible_descendant_count = 0; |
| 350 |
self.ptr->has_external_tokens = false; |
| 351 |
self.ptr->depends_on_column = false; |
| 352 |
self.ptr->has_external_scanner_state_change = false; |
| 353 |
self.ptr->dynamic_precedence = 0; |
| 354 |
|
| 355 |
uint32_t structural_index = 0; |
| 356 |
const TSSymbol *alias_sequence = ts_language_alias_sequence(language, self.ptr->production_id); |
| 357 |
uint32_t lookahead_end_byte = 0; |
| 358 |
|
| 359 |
const Subtree *children = ts_subtree_children(self); |
| 360 |
for (uint32_t i = 0; i < self.ptr->child_count; i++) { |
| 361 |
Subtree child = children[i]; |
| 362 |
|
| 363 |
if ( |
| 364 |
self.ptr->size.extent.row == 0 && |
| 365 |
ts_subtree_depends_on_column(child) |
| 366 |
) { |
| 367 |
self.ptr->depends_on_column = true; |
| 368 |
} |
| 369 |
|
| 370 |
if (ts_subtree_has_external_scanner_state_change(child)) { |
| 371 |
self.ptr->has_external_scanner_state_change = true; |
| 372 |
} |
| 373 |
|
| 374 |
if (i == 0) { |
| 375 |
self.ptr->padding = ts_subtree_padding(child); |
| 376 |
self.ptr->size = ts_subtree_size(child); |
| 377 |
} else { |
| 378 |
self.ptr->size = length_add(self.ptr->size, ts_subtree_total_size(child)); |
| 379 |
} |
| 380 |
|
| 381 |
uint32_t child_lookahead_end_byte = |
| 382 |
self.ptr->padding.bytes + |
| 383 |
self.ptr->size.bytes + |
| 384 |
ts_subtree_lookahead_bytes(child); |
| 385 |
if (child_lookahead_end_byte > lookahead_end_byte) { |
| 386 |
lookahead_end_byte = child_lookahead_end_byte; |
| 387 |
} |
| 388 |
|
| 389 |
if (ts_subtree_symbol(child) != ts_builtin_sym_error_repeat) { |
| 390 |
self.ptr->error_cost += ts_subtree_error_cost(child); |
| 391 |
} |
| 392 |
|
| 393 |
uint32_t grandchild_count = ts_subtree_child_count(child); |
| 394 |
if ( |
| 395 |
self.ptr->symbol == ts_builtin_sym_error || |
| 396 |
self.ptr->symbol == ts_builtin_sym_error_repeat |
| 397 |
) { |
| 398 |
if (!ts_subtree_extra(child) && !(ts_subtree_is_error(child) && grandchild_count == 0)) { |
| 399 |
if (ts_subtree_visible(child)) { |
| 400 |
self.ptr->error_cost += ERROR_COST_PER_SKIPPED_TREE; |
| 401 |
} else if (grandchild_count > 0) { |
| 402 |
self.ptr->error_cost += ERROR_COST_PER_SKIPPED_TREE * child.ptr->visible_child_count; |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
self.ptr->dynamic_precedence += ts_subtree_dynamic_precedence(child); |
| 408 |
self.ptr->visible_descendant_count += ts_subtree_visible_descendant_count(child); |
| 409 |
|
| 410 |
if ( |
| 411 |
!ts_subtree_extra(child) && |
| 412 |
ts_subtree_symbol(child) != 0 && |
| 413 |
alias_sequence && |
| 414 |
alias_sequence[structural_index] != 0 |
| 415 |
) { |
| 416 |
self.ptr->visible_descendant_count++; |
| 417 |
self.ptr->visible_child_count++; |
| 418 |
if (ts_language_symbol_metadata(language, alias_sequence[structural_index]).named) { |
| 419 |
self.ptr->named_child_count++; |
| 420 |
} |
| 421 |
} else if (ts_subtree_visible(child)) { |
| 422 |
self.ptr->visible_descendant_count++; |
| 423 |
self.ptr->visible_child_count++; |
| 424 |
if (ts_subtree_named(child)) self.ptr->named_child_count++; |
| 425 |
} else if (grandchild_count > 0) { |
| 426 |
self.ptr->visible_child_count += child.ptr->visible_child_count; |
| 427 |
self.ptr->named_child_count += child.ptr->named_child_count; |
| 428 |
} |
| 429 |
|
| 430 |
if (ts_subtree_has_external_tokens(child)) self.ptr->has_external_tokens = true; |
| 431 |
|
| 432 |
if (ts_subtree_is_error(child)) { |
| 433 |
self.ptr->fragile_left = self.ptr->fragile_right = true; |
| 434 |
self.ptr->parse_state = TS_TREE_STATE_NONE; |
| 435 |
} |
| 436 |
|
| 437 |
if (!ts_subtree_extra(child)) structural_index++; |
| 438 |
} |
| 439 |
|
| 440 |
self.ptr->lookahead_bytes = lookahead_end_byte - self.ptr->size.bytes - self.ptr->padding.bytes; |
| 441 |
|
| 442 |
if ( |
| 443 |
self.ptr->symbol == ts_builtin_sym_error || |
| 444 |
self.ptr->symbol == ts_builtin_sym_error_repeat |
| 445 |
) { |
| 446 |
self.ptr->error_cost += |
| 447 |
ERROR_COST_PER_RECOVERY + |
| 448 |
ERROR_COST_PER_SKIPPED_CHAR * self.ptr->size.bytes + |
| 449 |
ERROR_COST_PER_SKIPPED_LINE * self.ptr->size.extent.row; |
| 450 |
} |
| 451 |
|
| 452 |
if (self.ptr->child_count > 0) { |
| 453 |
Subtree first_child = children[0]; |
| 454 |
Subtree last_child = children[self.ptr->child_count - 1]; |
| 455 |
|
| 456 |
self.ptr->first_leaf.symbol = ts_subtree_leaf_symbol(first_child); |
| 457 |
self.ptr->first_leaf.parse_state = ts_subtree_leaf_parse_state(first_child); |
| 458 |
|
| 459 |
if (ts_subtree_fragile_left(first_child)) self.ptr->fragile_left = true; |
| 460 |
if (ts_subtree_fragile_right(last_child)) self.ptr->fragile_right = true; |
| 461 |
|
| 462 |
if ( |
| 463 |
self.ptr->child_count >= 2 && |
| 464 |
!self.ptr->visible && |
| 465 |
!self.ptr->named && |
| 466 |
ts_subtree_symbol(first_child) == self.ptr->symbol |
| 467 |
) { |
| 468 |
if (ts_subtree_repeat_depth(first_child) > ts_subtree_repeat_depth(last_child)) { |
| 469 |
self.ptr->repeat_depth = ts_subtree_repeat_depth(first_child) + 1; |
| 470 |
} else { |
| 471 |
self.ptr->repeat_depth = ts_subtree_repeat_depth(last_child) + 1; |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
// Create a new parent node with the given children. |
| 478 |
// |
| 479 |
// This takes ownership of the children array. |
| 480 |
MutableSubtree ts_subtree_new_node( |
| 481 |
TSSymbol symbol, |
| 482 |
SubtreeArray *children, |
| 483 |
unsigned production_id, |
| 484 |
const TSLanguage *language |
| 485 |
) { |
| 486 |
TSSymbolMetadata metadata = ts_language_symbol_metadata(language, symbol); |
| 487 |
bool fragile = symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat; |
| 488 |
|
| 489 |
// Allocate the node's data at the end of the array of children. |
| 490 |
size_t new_byte_size = ts_subtree_alloc_size(children->size); |
| 491 |
if (children->capacity * sizeof(Subtree) < new_byte_size) { |
| 492 |
children->contents = ts_realloc(children->contents, new_byte_size); |
| 493 |
children->capacity = (uint32_t)(new_byte_size / sizeof(Subtree)); |
| 494 |
} |
| 495 |
SubtreeHeapData *data = (SubtreeHeapData *)&children->contents[children->size]; |
| 496 |
|
| 497 |
*data = (SubtreeHeapData) { |
| 498 |
.ref_count = 1, |
| 499 |
.symbol = symbol, |
| 500 |
.child_count = children->size, |
| 501 |
.visible = metadata.visible, |
| 502 |
.named = metadata.named, |
| 503 |
.has_changes = false, |
| 504 |
.has_external_scanner_state_change = false, |
| 505 |
.fragile_left = fragile, |
| 506 |
.fragile_right = fragile, |
| 507 |
.is_keyword = false, |
| 508 |
{{ |
| 509 |
.visible_descendant_count = 0, |
| 510 |
.production_id = production_id, |
| 511 |
.first_leaf = {.symbol = 0, .parse_state = 0}, |
| 512 |
}} |
| 513 |
}; |
| 514 |
MutableSubtree result = {.ptr = data}; |
| 515 |
ts_subtree_summarize_children(result, language); |
| 516 |
return result; |
| 517 |
} |
| 518 |
|
| 519 |
// Create a new error node containing the given children. |
| 520 |
// |
| 521 |
// This node is treated as 'extra'. Its children are prevented from having |
| 522 |
// having any effect on the parse state. |
| 523 |
Subtree ts_subtree_new_error_node( |
| 524 |
SubtreeArray *children, |
| 525 |
bool extra, |
| 526 |
const TSLanguage *language |
| 527 |
) { |
| 528 |
MutableSubtree result = ts_subtree_new_node( |
| 529 |
ts_builtin_sym_error, children, 0, language |
| 530 |
); |
| 531 |
result.ptr->extra = extra; |
| 532 |
return ts_subtree_from_mut(result); |
| 533 |
} |
| 534 |
|
| 535 |
// Create a new 'missing leaf' node. |
| 536 |
// |
| 537 |
// This node is treated as 'extra'. Its children are prevented from having |
| 538 |
// having any effect on the parse state. |
| 539 |
Subtree ts_subtree_new_missing_leaf( |
| 540 |
SubtreePool *pool, |
| 541 |
TSSymbol symbol, |
| 542 |
Length padding, |
| 543 |
uint32_t lookahead_bytes, |
| 544 |
const TSLanguage *language |
| 545 |
) { |
| 546 |
Subtree result = ts_subtree_new_leaf( |
| 547 |
pool, symbol, padding, length_zero(), lookahead_bytes, |
| 548 |
0, false, false, false, language |
| 549 |
); |
| 550 |
if (result.data.is_inline) { |
| 551 |
result.data.is_missing = true; |
| 552 |
} else { |
| 553 |
((SubtreeHeapData *)result.ptr)->is_missing = true; |
| 554 |
} |
| 555 |
return result; |
| 556 |
} |
| 557 |
|
| 558 |
void ts_subtree_retain(Subtree self) { |
| 559 |
if (self.data.is_inline) return; |
| 560 |
ts_assert(self.ptr->ref_count > 0); |
| 561 |
atomic_inc((volatile uint32_t *)&self.ptr->ref_count); |
| 562 |
ts_assert(self.ptr->ref_count != 0); |
| 563 |
} |
| 564 |
|
| 565 |
void ts_subtree_release(SubtreePool *pool, Subtree self) { |
| 566 |
if (self.data.is_inline) return; |
| 567 |
array_clear(&pool->tree_stack); |
| 568 |
|
| 569 |
ts_assert(self.ptr->ref_count > 0); |
| 570 |
if (atomic_dec((volatile uint32_t *)&self.ptr->ref_count) == 0) { |
| 571 |
array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(self)); |
| 572 |
} |
| 573 |
|
| 574 |
while (pool->tree_stack.size > 0) { |
| 575 |
MutableSubtree tree = array_pop(&pool->tree_stack); |
| 576 |
if (tree.ptr->child_count > 0) { |
| 577 |
Subtree *children = ts_subtree_children(tree); |
| 578 |
for (uint32_t i = 0; i < tree.ptr->child_count; i++) { |
| 579 |
Subtree child = children[i]; |
| 580 |
if (child.data.is_inline) continue; |
| 581 |
ts_assert(child.ptr->ref_count > 0); |
| 582 |
if (atomic_dec((volatile uint32_t *)&child.ptr->ref_count) == 0) { |
| 583 |
array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(child)); |
| 584 |
} |
| 585 |
} |
| 586 |
ts_free(children); |
| 587 |
} else { |
| 588 |
if (tree.ptr->has_external_tokens) { |
| 589 |
ts_external_scanner_state_delete(&tree.ptr->external_scanner_state); |
| 590 |
} |
| 591 |
ts_subtree_pool_free(pool, tree.ptr); |
| 592 |
} |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
int ts_subtree_compare(Subtree left, Subtree right, SubtreePool *pool) { |
| 597 |
array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(left)); |
| 598 |
array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(right)); |
| 599 |
|
| 600 |
while (pool->tree_stack.size > 0) { |
| 601 |
right = ts_subtree_from_mut(array_pop(&pool->tree_stack)); |
| 602 |
left = ts_subtree_from_mut(array_pop(&pool->tree_stack)); |
| 603 |
|
| 604 |
int result = 0; |
| 605 |
if (ts_subtree_symbol(left) < ts_subtree_symbol(right)) result = -1; |
| 606 |
else if (ts_subtree_symbol(right) < ts_subtree_symbol(left)) result = 1; |
| 607 |
else if (ts_subtree_child_count(left) < ts_subtree_child_count(right)) result = -1; |
| 608 |
else if (ts_subtree_child_count(right) < ts_subtree_child_count(left)) result = 1; |
| 609 |
if (result != 0) { |
| 610 |
array_clear(&pool->tree_stack); |
| 611 |
return result; |
| 612 |
} |
| 613 |
|
| 614 |
for (uint32_t i = ts_subtree_child_count(left); i > 0; i--) { |
| 615 |
Subtree left_child = ts_subtree_children(left)[i - 1]; |
| 616 |
Subtree right_child = ts_subtree_children(right)[i - 1]; |
| 617 |
array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(left_child)); |
| 618 |
array_push(&pool->tree_stack, ts_subtree_to_mut_unsafe(right_child)); |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
return 0; |
| 623 |
} |
| 624 |
|
| 625 |
static inline void ts_subtree_set_has_changes(MutableSubtree *self) { |
| 626 |
if (self->data.is_inline) { |
| 627 |
self->data.has_changes = true; |
| 628 |
} else { |
| 629 |
self->ptr->has_changes = true; |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
Subtree ts_subtree_edit(Subtree self, const TSInputEdit *input_edit, SubtreePool *pool) { |
| 634 |
typedef struct { |
| 635 |
Subtree *tree; |
| 636 |
Edit edit; |
| 637 |
} EditEntry; |
| 638 |
|
| 639 |
Array(EditEntry) stack = array_new(); |
| 640 |
array_push(&stack, ((EditEntry) { |
| 641 |
.tree = &self, |
| 642 |
.edit = (Edit) { |
| 643 |
.start = {input_edit->start_byte, input_edit->start_point}, |
| 644 |
.old_end = {input_edit->old_end_byte, input_edit->old_end_point}, |
| 645 |
.new_end = {input_edit->new_end_byte, input_edit->new_end_point}, |
| 646 |
}, |
| 647 |
})); |
| 648 |
|
| 649 |
while (stack.size) { |
| 650 |
EditEntry entry = array_pop(&stack); |
| 651 |
Edit edit = entry.edit; |
| 652 |
bool is_noop = edit.old_end.bytes == edit.start.bytes && edit.new_end.bytes == edit.start.bytes; |
| 653 |
bool is_pure_insertion = edit.old_end.bytes == edit.start.bytes; |
| 654 |
bool parent_depends_on_column = ts_subtree_depends_on_column(*entry.tree); |
| 655 |
bool column_shifted = edit.new_end.extent.column != edit.old_end.extent.column; |
| 656 |
|
| 657 |
Length size = ts_subtree_size(*entry.tree); |
| 658 |
Length padding = ts_subtree_padding(*entry.tree); |
| 659 |
Length total_size = length_add(padding, size); |
| 660 |
uint32_t lookahead_bytes = ts_subtree_lookahead_bytes(*entry.tree); |
| 661 |
uint32_t end_byte = total_size.bytes + lookahead_bytes; |
| 662 |
if (edit.start.bytes > end_byte || (is_noop && edit.start.bytes == end_byte)) continue; |
| 663 |
|
| 664 |
// If the edit is entirely within the space before this subtree, then shift this |
| 665 |
// subtree over according to the edit without changing its size. |
| 666 |
if (edit.old_end.bytes <= padding.bytes) { |
| 667 |
padding = length_add(edit.new_end, length_sub(padding, edit.old_end)); |
| 668 |
} |
| 669 |
|
| 670 |
// If the edit starts in the space before this subtree and extends into this subtree, |
| 671 |
// shrink the subtree's content to compensate for the change in the space before it. |
| 672 |
else if (edit.start.bytes < padding.bytes) { |
| 673 |
size = length_saturating_sub(size, length_sub(edit.old_end, padding)); |
| 674 |
padding = edit.new_end; |
| 675 |
} |
| 676 |
|
| 677 |
// If the edit is within this subtree, resize the subtree to reflect the edit. |
| 678 |
else if ( |
| 679 |
edit.start.bytes < total_size.bytes || |
| 680 |
(edit.start.bytes == total_size.bytes && is_pure_insertion) |
| 681 |
) { |
| 682 |
size = length_add( |
| 683 |
length_sub(edit.new_end, padding), |
| 684 |
length_saturating_sub(total_size, edit.old_end) |
| 685 |
); |
| 686 |
} |
| 687 |
|
| 688 |
MutableSubtree result = ts_subtree_make_mut(pool, *entry.tree); |
| 689 |
|
| 690 |
if (result.data.is_inline) { |
| 691 |
if (ts_subtree_can_inline(padding, size, lookahead_bytes)) { |
| 692 |
result.data.padding_bytes = padding.bytes; |
| 693 |
result.data.padding_rows = padding.extent.row; |
| 694 |
result.data.padding_columns = padding.extent.column; |
| 695 |
result.data.size_bytes = size.bytes; |
| 696 |
} else { |
| 697 |
SubtreeHeapData *data = ts_subtree_pool_allocate(pool); |
| 698 |
data->ref_count = 1; |
| 699 |
data->padding = padding; |
| 700 |
data->size = size; |
| 701 |
data->lookahead_bytes = lookahead_bytes; |
| 702 |
data->error_cost = 0; |
| 703 |
data->child_count = 0; |
| 704 |
data->symbol = result.data.symbol; |
| 705 |
data->parse_state = result.data.parse_state; |
| 706 |
data->visible = result.data.visible; |
| 707 |
data->named = result.data.named; |
| 708 |
data->extra = result.data.extra; |
| 709 |
data->fragile_left = false; |
| 710 |
data->fragile_right = false; |
| 711 |
data->has_changes = false; |
| 712 |
data->has_external_tokens = false; |
| 713 |
data->depends_on_column = false; |
| 714 |
data->is_missing = result.data.is_missing; |
| 715 |
data->is_keyword = result.data.is_keyword; |
| 716 |
result.ptr = data; |
| 717 |
} |
| 718 |
} else { |
| 719 |
result.ptr->padding = padding; |
| 720 |
result.ptr->size = size; |
| 721 |
} |
| 722 |
|
| 723 |
ts_subtree_set_has_changes(&result); |
| 724 |
*entry.tree = ts_subtree_from_mut(result); |
| 725 |
|
| 726 |
Length child_left, child_right = length_zero(); |
| 727 |
for (uint32_t i = 0, n = ts_subtree_child_count(*entry.tree); i < n; i++) { |
| 728 |
Subtree *child = &ts_subtree_children(*entry.tree)[i]; |
| 729 |
Length child_size = ts_subtree_total_size(*child); |
| 730 |
child_left = child_right; |
| 731 |
child_right = length_add(child_left, child_size); |
| 732 |
|
| 733 |
// If this child ends before the edit, it is not affected. |
| 734 |
if (child_right.bytes + ts_subtree_lookahead_bytes(*child) < edit.start.bytes) continue; |
| 735 |
|
| 736 |
// Keep editing child nodes until a node is reached that starts after the edit. |
| 737 |
// Also, if this node's validity depends on its column position, then continue |
| 738 |
// invalidating child nodes until reaching a line break. |
| 739 |
if (( |
| 740 |
(child_left.bytes > edit.old_end.bytes) || |
| 741 |
(child_left.bytes == edit.old_end.bytes && child_size.bytes > 0 && i > 0) |
| 742 |
) && ( |
| 743 |
!parent_depends_on_column || |
| 744 |
child_left.extent.row > padding.extent.row |
| 745 |
) && ( |
| 746 |
!ts_subtree_depends_on_column(*child) || |
| 747 |
!column_shifted || |
| 748 |
child_left.extent.row > edit.old_end.extent.row |
| 749 |
)) { |
| 750 |
break; |
| 751 |
} |
| 752 |
|
| 753 |
// Transform edit into the child's coordinate space. |
| 754 |
Edit child_edit = { |
| 755 |
.start = length_saturating_sub(edit.start, child_left), |
| 756 |
.old_end = length_saturating_sub(edit.old_end, child_left), |
| 757 |
.new_end = length_saturating_sub(edit.new_end, child_left), |
| 758 |
}; |
| 759 |
|
| 760 |
// Interpret all inserted text as applying to the *first* child that touches the edit. |
| 761 |
// Subsequent children are only never have any text inserted into them; they are only |
| 762 |
// shrunk to compensate for the edit. |
| 763 |
if ( |
| 764 |
child_right.bytes > edit.start.bytes || |
| 765 |
(child_right.bytes == edit.start.bytes && is_pure_insertion) |
| 766 |
) { |
| 767 |
edit.new_end = edit.start; |
| 768 |
} |
| 769 |
|
| 770 |
// Children that occur before the edit are not reshaped by the edit. |
| 771 |
else { |
| 772 |
child_edit.old_end = child_edit.start; |
| 773 |
child_edit.new_end = child_edit.start; |
| 774 |
} |
| 775 |
|
| 776 |
// Queue processing of this child's subtree. |
| 777 |
array_push(&stack, ((EditEntry) { |
| 778 |
.tree = child, |
| 779 |
.edit = child_edit, |
| 780 |
})); |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
array_delete(&stack); |
| 785 |
return self; |
| 786 |
} |
| 787 |
|
| 788 |
Subtree ts_subtree_last_external_token(Subtree tree) { |
| 789 |
if (!ts_subtree_has_external_tokens(tree)) return NULL_SUBTREE; |
| 790 |
while (tree.ptr->child_count > 0) { |
| 791 |
for (uint32_t i = tree.ptr->child_count - 1; i + 1 > 0; i--) { |
| 792 |
Subtree child = ts_subtree_children(tree)[i]; |
| 793 |
if (ts_subtree_has_external_tokens(child)) { |
| 794 |
tree = child; |
| 795 |
break; |
| 796 |
} |
| 797 |
} |
| 798 |
} |
| 799 |
return tree; |
| 800 |
} |
| 801 |
|
| 802 |
static size_t ts_subtree__write_char_to_string(char *str, size_t n, int32_t chr) { |
| 803 |
if (chr == -1) |
| 804 |
return snprintf(str, n, "INVALID"); |
| 805 |
else if (chr == '\0') |
| 806 |
return snprintf(str, n, "'\\0'"); |
| 807 |
else if (chr == '\n') |
| 808 |
return snprintf(str, n, "'\\n'"); |
| 809 |
else if (chr == '\t') |
| 810 |
return snprintf(str, n, "'\\t'"); |
| 811 |
else if (chr == '\r') |
| 812 |
return snprintf(str, n, "'\\r'"); |
| 813 |
else if (0 < chr && chr < 128 && isprint(chr)) |
| 814 |
return snprintf(str, n, "'%c'", chr); |
| 815 |
else |
| 816 |
return snprintf(str, n, "%d", chr); |
| 817 |
} |
| 818 |
|
| 819 |
static const char *const ROOT_FIELD = "__ROOT__"; |
| 820 |
|
| 821 |
static size_t ts_subtree__write_to_string( |
| 822 |
Subtree self, char *string, size_t limit, |
| 823 |
const TSLanguage *language, bool include_all, |
| 824 |
TSSymbol alias_symbol, bool alias_is_named, const char *field_name |
| 825 |
) { |
| 826 |
if (!self.ptr) return snprintf(string, limit, "(NULL)"); |
| 827 |
|
| 828 |
char *cursor = string; |
| 829 |
char **writer = (limit > 1) ? &cursor : &string; |
| 830 |
bool is_root = field_name == ROOT_FIELD; |
| 831 |
bool is_visible = |
| 832 |
include_all || |
| 833 |
ts_subtree_missing(self) || |
| 834 |
( |
| 835 |
alias_symbol |
| 836 |
? alias_is_named |
| 837 |
: ts_subtree_visible(self) && ts_subtree_named(self) |
| 838 |
); |
| 839 |
|
| 840 |
if (is_visible) { |
| 841 |
if (!is_root) { |
| 842 |
cursor += snprintf(*writer, limit, " "); |
| 843 |
if (field_name) { |
| 844 |
cursor += snprintf(*writer, limit, "%s: ", field_name); |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
if (ts_subtree_is_error(self) && ts_subtree_child_count(self) == 0 && self.ptr->size.bytes > 0) { |
| 849 |
cursor += snprintf(*writer, limit, "(UNEXPECTED "); |
| 850 |
cursor += ts_subtree__write_char_to_string(*writer, limit, self.ptr->lookahead_char); |
| 851 |
} else { |
| 852 |
TSSymbol symbol = alias_symbol ? alias_symbol : ts_subtree_symbol(self); |
| 853 |
const char *symbol_name = ts_language_symbol_name(language, symbol); |
| 854 |
if (ts_subtree_missing(self)) { |
| 855 |
cursor += snprintf(*writer, limit, "(MISSING "); |
| 856 |
if (alias_is_named || ts_subtree_named(self)) { |
| 857 |
cursor += snprintf(*writer, limit, "%s", symbol_name); |
| 858 |
} else { |
| 859 |
cursor += snprintf(*writer, limit, "\"%s\"", symbol_name); |
| 860 |
} |
| 861 |
} else { |
| 862 |
cursor += snprintf(*writer, limit, "(%s", symbol_name); |
| 863 |
} |
| 864 |
} |
| 865 |
} else if (is_root) { |
| 866 |
TSSymbol symbol = alias_symbol ? alias_symbol : ts_subtree_symbol(self); |
| 867 |
const char *symbol_name = ts_language_symbol_name(language, symbol); |
| 868 |
if (ts_subtree_child_count(self) > 0) { |
| 869 |
cursor += snprintf(*writer, limit, "(%s", symbol_name); |
| 870 |
} else if (ts_subtree_named(self)) { |
| 871 |
cursor += snprintf(*writer, limit, "(%s)", symbol_name); |
| 872 |
} else { |
| 873 |
cursor += snprintf(*writer, limit, "(\"%s\")", symbol_name); |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
if (ts_subtree_child_count(self)) { |
| 878 |
const TSSymbol *alias_sequence = ts_language_alias_sequence(language, self.ptr->production_id); |
| 879 |
const TSFieldMapEntry *field_map, *field_map_end; |
| 880 |
ts_language_field_map( |
| 881 |
language, |
| 882 |
self.ptr->production_id, |
| 883 |
&field_map, |
| 884 |
&field_map_end |
| 885 |
); |
| 886 |
|
| 887 |
uint32_t structural_child_index = 0; |
| 888 |
for (uint32_t i = 0; i < self.ptr->child_count; i++) { |
| 889 |
Subtree child = ts_subtree_children(self)[i]; |
| 890 |
if (ts_subtree_extra(child)) { |
| 891 |
cursor += ts_subtree__write_to_string( |
| 892 |
child, *writer, limit, |
| 893 |
language, include_all, |
| 894 |
0, false, NULL |
| 895 |
); |
| 896 |
} else { |
| 897 |
TSSymbol subtree_alias_symbol = alias_sequence |
| 898 |
? alias_sequence[structural_child_index] |
| 899 |
: 0; |
| 900 |
bool subtree_alias_is_named = subtree_alias_symbol |
| 901 |
? ts_language_symbol_metadata(language, subtree_alias_symbol).named |
| 902 |
: false; |
| 903 |
|
| 904 |
const char *child_field_name = is_visible ? NULL : field_name; |
| 905 |
for (const TSFieldMapEntry *map = field_map; map < field_map_end; map++) { |
| 906 |
if (!map->inherited && map->child_index == structural_child_index) { |
| 907 |
child_field_name = language->field_names[map->field_id]; |
| 908 |
break; |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
cursor += ts_subtree__write_to_string( |
| 913 |
child, *writer, limit, |
| 914 |
language, include_all, |
| 915 |
subtree_alias_symbol, subtree_alias_is_named, child_field_name |
| 916 |
); |
| 917 |
structural_child_index++; |
| 918 |
} |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
if (is_visible) cursor += snprintf(*writer, limit, ")"); |
| 923 |
|
| 924 |
return cursor - string; |
| 925 |
} |
| 926 |
|
| 927 |
char *ts_subtree_string( |
| 928 |
Subtree self, |
| 929 |
TSSymbol alias_symbol, |
| 930 |
bool alias_is_named, |
| 931 |
const TSLanguage *language, |
| 932 |
bool include_all |
| 933 |
) { |
| 934 |
char scratch_string[1]; |
| 935 |
size_t size = ts_subtree__write_to_string( |
| 936 |
self, scratch_string, 1, |
| 937 |
language, include_all, |
| 938 |
alias_symbol, alias_is_named, ROOT_FIELD |
| 939 |
) + 1; |
| 940 |
char *result = ts_malloc(size * sizeof(char)); |
| 941 |
ts_subtree__write_to_string( |
| 942 |
self, result, size, |
| 943 |
language, include_all, |
| 944 |
alias_symbol, alias_is_named, ROOT_FIELD |
| 945 |
); |
| 946 |
return result; |
| 947 |
} |
| 948 |
|
| 949 |
void ts_subtree__print_dot_graph(const Subtree *self, uint32_t start_offset, |
| 950 |
const TSLanguage *language, TSSymbol alias_symbol, |
| 951 |
FILE *f) { |
| 952 |
TSSymbol subtree_symbol = ts_subtree_symbol(*self); |
| 953 |
TSSymbol symbol = alias_symbol ? alias_symbol : subtree_symbol; |
| 954 |
uint32_t end_offset = start_offset + ts_subtree_total_bytes(*self); |
| 955 |
fprintf(f, "tree_%p [label=\"", (void *)self); |
| 956 |
ts_language_write_symbol_as_dot_string(language, f, symbol); |
| 957 |
fprintf(f, "\""); |
| 958 |
|
| 959 |
if (ts_subtree_child_count(*self) == 0) fprintf(f, ", shape=plaintext"); |
| 960 |
if (ts_subtree_extra(*self)) fprintf(f, ", fontcolor=gray"); |
| 961 |
if (ts_subtree_has_changes(*self)) fprintf(f, ", color=green, penwidth=2"); |
| 962 |
|
| 963 |
fprintf(f, ", tooltip=\"" |
| 964 |
"range: %u - %u\n" |
| 965 |
"state: %d\n" |
| 966 |
"error-cost: %u\n" |
| 967 |
"has-changes: %u\n" |
| 968 |
"depends-on-column: %u\n" |
| 969 |
"descendant-count: %u\n" |
| 970 |
"repeat-depth: %u\n" |
| 971 |
"lookahead-bytes: %u", |
| 972 |
start_offset, end_offset, |
| 973 |
ts_subtree_parse_state(*self), |
| 974 |
ts_subtree_error_cost(*self), |
| 975 |
ts_subtree_has_changes(*self), |
| 976 |
ts_subtree_depends_on_column(*self), |
| 977 |
ts_subtree_visible_descendant_count(*self), |
| 978 |
ts_subtree_repeat_depth(*self), |
| 979 |
ts_subtree_lookahead_bytes(*self) |
| 980 |
); |
| 981 |
|
| 982 |
if (ts_subtree_is_error(*self) && ts_subtree_child_count(*self) == 0 && self->ptr->lookahead_char != 0) { |
| 983 |
fprintf(f, "\ncharacter: '%c'", self->ptr->lookahead_char); |
| 984 |
} |
| 985 |
|
| 986 |
fprintf(f, "\"]\n"); |
| 987 |
|
| 988 |
uint32_t child_start_offset = start_offset; |
| 989 |
uint32_t child_info_offset = |
| 990 |
language->max_alias_sequence_length * |
| 991 |
ts_subtree_production_id(*self); |
| 992 |
for (uint32_t i = 0, n = ts_subtree_child_count(*self); i < n; i++) { |
| 993 |
const Subtree *child = &ts_subtree_children(*self)[i]; |
| 994 |
TSSymbol subtree_alias_symbol = 0; |
| 995 |
if (!ts_subtree_extra(*child) && child_info_offset) { |
| 996 |
subtree_alias_symbol = language->alias_sequences[child_info_offset]; |
| 997 |
child_info_offset++; |
| 998 |
} |
| 999 |
ts_subtree__print_dot_graph(child, child_start_offset, language, subtree_alias_symbol, f); |
| 1000 |
fprintf(f, "tree_%p -> tree_%p [tooltip=%u]\n", (void *)self, (void *)child, i); |
| 1001 |
child_start_offset += ts_subtree_total_bytes(*child); |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
void ts_subtree_print_dot_graph(Subtree self, const TSLanguage *language, FILE *f) { |
| 1006 |
fprintf(f, "digraph tree {\n"); |
| 1007 |
fprintf(f, "edge [arrowhead=none]\n"); |
| 1008 |
ts_subtree__print_dot_graph(&self, 0, language, 0, f); |
| 1009 |
fprintf(f, "}\n"); |
| 1010 |
} |
| 1011 |
|
| 1012 |
const ExternalScannerState *ts_subtree_external_scanner_state(Subtree self) { |
| 1013 |
static const ExternalScannerState empty_state = {{.short_data = {0}}, .length = 0}; |
| 1014 |
if ( |
| 1015 |
self.ptr && |
| 1016 |
!self.data.is_inline && |
| 1017 |
self.ptr->has_external_tokens && |
| 1018 |
self.ptr->child_count == 0 |
| 1019 |
) { |
| 1020 |
return &self.ptr->external_scanner_state; |
| 1021 |
} else { |
| 1022 |
return &empty_state; |
| 1023 |
} |
| 1024 |
} |
| 1025 |
|
| 1026 |
bool ts_subtree_external_scanner_state_eq(Subtree self, Subtree other) { |
| 1027 |
const ExternalScannerState *state_self = ts_subtree_external_scanner_state(self); |
| 1028 |
const ExternalScannerState *state_other = ts_subtree_external_scanner_state(other); |
| 1029 |
return ts_external_scanner_state_eq( |
| 1030 |
state_self, |
| 1031 |
ts_external_scanner_state_data(state_other), |
| 1032 |
state_other->length |
| 1033 |
); |
| 1034 |
} |