| 1 |
#include "tree_sitter/api.h" |
| 2 |
#include "./tree_cursor.h" |
| 3 |
#include "./language.h" |
| 4 |
#include "./tree.h" |
| 5 |
|
| 6 |
typedef struct { |
| 7 |
Subtree parent; |
| 8 |
const TSTree *tree; |
| 9 |
Length position; |
| 10 |
uint32_t child_index; |
| 11 |
uint32_t structural_child_index; |
| 12 |
uint32_t descendant_index; |
| 13 |
const TSSymbol *alias_sequence; |
| 14 |
} CursorChildIterator; |
| 15 |
|
| 16 |
// CursorChildIterator |
| 17 |
|
| 18 |
static inline bool ts_tree_cursor_is_entry_visible(const TreeCursor *self, uint32_t index) { |
| 19 |
TreeCursorEntry *entry = array_get(&self->stack, index); |
| 20 |
if (index == 0 || ts_subtree_visible(*entry->subtree)) { |
| 21 |
return true; |
| 22 |
} else if (!ts_subtree_extra(*entry->subtree)) { |
| 23 |
TreeCursorEntry *parent_entry = array_get(&self->stack, index - 1); |
| 24 |
return ts_language_alias_at( |
| 25 |
self->tree->language, |
| 26 |
parent_entry->subtree->ptr->production_id, |
| 27 |
entry->structural_child_index |
| 28 |
); |
| 29 |
} else { |
| 30 |
return false; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
static inline CursorChildIterator ts_tree_cursor_iterate_children(const TreeCursor *self) { |
| 35 |
TreeCursorEntry *last_entry = array_back(&self->stack); |
| 36 |
if (ts_subtree_child_count(*last_entry->subtree) == 0) { |
| 37 |
return (CursorChildIterator) {NULL_SUBTREE, self->tree, length_zero(), 0, 0, 0, NULL}; |
| 38 |
} |
| 39 |
const TSSymbol *alias_sequence = ts_language_alias_sequence( |
| 40 |
self->tree->language, |
| 41 |
last_entry->subtree->ptr->production_id |
| 42 |
); |
| 43 |
|
| 44 |
uint32_t descendant_index = last_entry->descendant_index; |
| 45 |
if (ts_tree_cursor_is_entry_visible(self, self->stack.size - 1)) { |
| 46 |
descendant_index += 1; |
| 47 |
} |
| 48 |
|
| 49 |
return (CursorChildIterator) { |
| 50 |
.tree = self->tree, |
| 51 |
.parent = *last_entry->subtree, |
| 52 |
.position = last_entry->position, |
| 53 |
.child_index = 0, |
| 54 |
.structural_child_index = 0, |
| 55 |
.descendant_index = descendant_index, |
| 56 |
.alias_sequence = alias_sequence, |
| 57 |
}; |
| 58 |
} |
| 59 |
|
| 60 |
static inline bool ts_tree_cursor_child_iterator_next( |
| 61 |
CursorChildIterator *self, |
| 62 |
TreeCursorEntry *result, |
| 63 |
bool *visible |
| 64 |
) { |
| 65 |
if (!self->parent.ptr || self->child_index == self->parent.ptr->child_count) return false; |
| 66 |
const Subtree *child = &ts_subtree_children(self->parent)[self->child_index]; |
| 67 |
*result = (TreeCursorEntry) { |
| 68 |
.subtree = child, |
| 69 |
.position = self->position, |
| 70 |
.child_index = self->child_index, |
| 71 |
.structural_child_index = self->structural_child_index, |
| 72 |
.descendant_index = self->descendant_index, |
| 73 |
}; |
| 74 |
*visible = ts_subtree_visible(*child); |
| 75 |
bool extra = ts_subtree_extra(*child); |
| 76 |
if (!extra) { |
| 77 |
if (self->alias_sequence) { |
| 78 |
*visible |= self->alias_sequence[self->structural_child_index]; |
| 79 |
} |
| 80 |
self->structural_child_index++; |
| 81 |
} |
| 82 |
|
| 83 |
self->descendant_index += ts_subtree_visible_descendant_count(*child); |
| 84 |
if (*visible) { |
| 85 |
self->descendant_index += 1; |
| 86 |
} |
| 87 |
|
| 88 |
self->position = length_add(self->position, ts_subtree_size(*child)); |
| 89 |
self->child_index++; |
| 90 |
|
| 91 |
if (self->child_index < self->parent.ptr->child_count) { |
| 92 |
Subtree next_child = ts_subtree_children(self->parent)[self->child_index]; |
| 93 |
self->position = length_add(self->position, ts_subtree_padding(next_child)); |
| 94 |
} |
| 95 |
|
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
// Return a position that, when `b` is added to it, yields `a`. This |
| 100 |
// can only be computed if `b` has zero rows. Otherwise, this function |
| 101 |
// returns `LENGTH_UNDEFINED`, and the caller needs to recompute |
| 102 |
// the position some other way. |
| 103 |
static inline Length length_backtrack(Length a, Length b) { |
| 104 |
if (length_is_undefined(a) || b.extent.row != 0) { |
| 105 |
return LENGTH_UNDEFINED; |
| 106 |
} |
| 107 |
|
| 108 |
Length result; |
| 109 |
result.bytes = a.bytes - b.bytes; |
| 110 |
result.extent.row = a.extent.row; |
| 111 |
result.extent.column = a.extent.column - b.extent.column; |
| 112 |
return result; |
| 113 |
} |
| 114 |
|
| 115 |
static inline bool ts_tree_cursor_child_iterator_previous( |
| 116 |
CursorChildIterator *self, |
| 117 |
TreeCursorEntry *result, |
| 118 |
bool *visible |
| 119 |
) { |
| 120 |
// this is mostly a reverse `ts_tree_cursor_child_iterator_next` taking into |
| 121 |
// account unsigned underflow |
| 122 |
if (!self->parent.ptr || (int8_t)self->child_index == -1) return false; |
| 123 |
const Subtree *child = &ts_subtree_children(self->parent)[self->child_index]; |
| 124 |
*result = (TreeCursorEntry) { |
| 125 |
.subtree = child, |
| 126 |
.position = self->position, |
| 127 |
.child_index = self->child_index, |
| 128 |
.structural_child_index = self->structural_child_index, |
| 129 |
}; |
| 130 |
*visible = ts_subtree_visible(*child); |
| 131 |
bool extra = ts_subtree_extra(*child); |
| 132 |
|
| 133 |
self->position = length_backtrack(self->position, ts_subtree_padding(*child)); |
| 134 |
self->child_index--; |
| 135 |
|
| 136 |
if (!extra && self->alias_sequence) { |
| 137 |
*visible |= self->alias_sequence[self->structural_child_index]; |
| 138 |
if (self->structural_child_index > 0) { |
| 139 |
self->structural_child_index--; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// unsigned can underflow so compare it to child_count |
| 144 |
if (self->child_index < self->parent.ptr->child_count) { |
| 145 |
Subtree previous_child = ts_subtree_children(self->parent)[self->child_index]; |
| 146 |
Length size = ts_subtree_size(previous_child); |
| 147 |
self->position = length_backtrack(self->position, size); |
| 148 |
} |
| 149 |
|
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
// TSTreeCursor - lifecycle |
| 154 |
|
| 155 |
TSTreeCursor ts_tree_cursor_new(TSNode node) { |
| 156 |
TSTreeCursor self = {NULL, NULL, {0, 0, 0}}; |
| 157 |
ts_tree_cursor_init((TreeCursor *)&self, node); |
| 158 |
return self; |
| 159 |
} |
| 160 |
|
| 161 |
void ts_tree_cursor_reset(TSTreeCursor *_self, TSNode node) { |
| 162 |
ts_tree_cursor_init((TreeCursor *)_self, node); |
| 163 |
} |
| 164 |
|
| 165 |
void ts_tree_cursor_init(TreeCursor *self, TSNode node) { |
| 166 |
self->tree = node.tree; |
| 167 |
self->root_alias_symbol = node.context[3]; |
| 168 |
array_clear(&self->stack); |
| 169 |
array_push(&self->stack, ((TreeCursorEntry) { |
| 170 |
.subtree = (const Subtree *)node.id, |
| 171 |
.position = { |
| 172 |
ts_node_start_byte(node), |
| 173 |
ts_node_start_point(node) |
| 174 |
}, |
| 175 |
.child_index = 0, |
| 176 |
.structural_child_index = 0, |
| 177 |
.descendant_index = 0, |
| 178 |
})); |
| 179 |
} |
| 180 |
|
| 181 |
void ts_tree_cursor_delete(TSTreeCursor *_self) { |
| 182 |
TreeCursor *self = (TreeCursor *)_self; |
| 183 |
array_delete(&self->stack); |
| 184 |
} |
| 185 |
|
| 186 |
// TSTreeCursor - walking the tree |
| 187 |
|
| 188 |
TreeCursorStep ts_tree_cursor_goto_first_child_internal(TSTreeCursor *_self) { |
| 189 |
TreeCursor *self = (TreeCursor *)_self; |
| 190 |
bool visible; |
| 191 |
TreeCursorEntry entry; |
| 192 |
CursorChildIterator iterator = ts_tree_cursor_iterate_children(self); |
| 193 |
while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) { |
| 194 |
if (visible) { |
| 195 |
array_push(&self->stack, entry); |
| 196 |
return TreeCursorStepVisible; |
| 197 |
} |
| 198 |
if (ts_subtree_visible_child_count(*entry.subtree) > 0) { |
| 199 |
array_push(&self->stack, entry); |
| 200 |
return TreeCursorStepHidden; |
| 201 |
} |
| 202 |
} |
| 203 |
return TreeCursorStepNone; |
| 204 |
} |
| 205 |
|
| 206 |
bool ts_tree_cursor_goto_first_child(TSTreeCursor *self) { |
| 207 |
for (;;) { |
| 208 |
switch (ts_tree_cursor_goto_first_child_internal(self)) { |
| 209 |
case TreeCursorStepHidden: |
| 210 |
continue; |
| 211 |
case TreeCursorStepVisible: |
| 212 |
return true; |
| 213 |
default: |
| 214 |
return false; |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
TreeCursorStep ts_tree_cursor_goto_last_child_internal(TSTreeCursor *_self) { |
| 220 |
TreeCursor *self = (TreeCursor *)_self; |
| 221 |
bool visible; |
| 222 |
TreeCursorEntry entry; |
| 223 |
CursorChildIterator iterator = ts_tree_cursor_iterate_children(self); |
| 224 |
if (!iterator.parent.ptr || iterator.parent.ptr->child_count == 0) return TreeCursorStepNone; |
| 225 |
|
| 226 |
TreeCursorEntry last_entry = {0}; |
| 227 |
TreeCursorStep last_step = TreeCursorStepNone; |
| 228 |
while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) { |
| 229 |
if (visible) { |
| 230 |
last_entry = entry; |
| 231 |
last_step = TreeCursorStepVisible; |
| 232 |
} |
| 233 |
else if (ts_subtree_visible_child_count(*entry.subtree) > 0) { |
| 234 |
last_entry = entry; |
| 235 |
last_step = TreeCursorStepHidden; |
| 236 |
} |
| 237 |
} |
| 238 |
if (last_entry.subtree) { |
| 239 |
array_push(&self->stack, last_entry); |
| 240 |
return last_step; |
| 241 |
} |
| 242 |
|
| 243 |
return TreeCursorStepNone; |
| 244 |
} |
| 245 |
|
| 246 |
bool ts_tree_cursor_goto_last_child(TSTreeCursor *self) { |
| 247 |
for (;;) { |
| 248 |
switch (ts_tree_cursor_goto_last_child_internal(self)) { |
| 249 |
case TreeCursorStepHidden: |
| 250 |
continue; |
| 251 |
case TreeCursorStepVisible: |
| 252 |
return true; |
| 253 |
default: |
| 254 |
return false; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
static inline int64_t ts_tree_cursor_goto_first_child_for_byte_and_point( |
| 260 |
TSTreeCursor *_self, |
| 261 |
uint32_t goal_byte, |
| 262 |
TSPoint goal_point |
| 263 |
) { |
| 264 |
TreeCursor *self = (TreeCursor *)_self; |
| 265 |
uint32_t initial_size = self->stack.size; |
| 266 |
uint32_t visible_child_index = 0; |
| 267 |
|
| 268 |
bool did_descend; |
| 269 |
do { |
| 270 |
did_descend = false; |
| 271 |
|
| 272 |
bool visible; |
| 273 |
TreeCursorEntry entry; |
| 274 |
CursorChildIterator iterator = ts_tree_cursor_iterate_children(self); |
| 275 |
while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) { |
| 276 |
Length entry_end = length_add(entry.position, ts_subtree_size(*entry.subtree)); |
| 277 |
bool at_goal = entry_end.bytes > goal_byte && point_gt(entry_end.extent, goal_point); |
| 278 |
uint32_t visible_child_count = ts_subtree_visible_child_count(*entry.subtree); |
| 279 |
if (at_goal) { |
| 280 |
if (visible) { |
| 281 |
array_push(&self->stack, entry); |
| 282 |
return visible_child_index; |
| 283 |
} |
| 284 |
if (visible_child_count > 0) { |
| 285 |
array_push(&self->stack, entry); |
| 286 |
did_descend = true; |
| 287 |
break; |
| 288 |
} |
| 289 |
} else if (visible) { |
| 290 |
visible_child_index++; |
| 291 |
} else { |
| 292 |
visible_child_index += visible_child_count; |
| 293 |
} |
| 294 |
} |
| 295 |
} while (did_descend); |
| 296 |
|
| 297 |
self->stack.size = initial_size; |
| 298 |
return -1; |
| 299 |
} |
| 300 |
|
| 301 |
int64_t ts_tree_cursor_goto_first_child_for_byte(TSTreeCursor *self, uint32_t goal_byte) { |
| 302 |
return ts_tree_cursor_goto_first_child_for_byte_and_point(self, goal_byte, POINT_ZERO); |
| 303 |
} |
| 304 |
|
| 305 |
int64_t ts_tree_cursor_goto_first_child_for_point(TSTreeCursor *self, TSPoint goal_point) { |
| 306 |
return ts_tree_cursor_goto_first_child_for_byte_and_point(self, 0, goal_point); |
| 307 |
} |
| 308 |
|
| 309 |
TreeCursorStep ts_tree_cursor_goto_sibling_internal( |
| 310 |
TSTreeCursor *_self, |
| 311 |
bool (*advance)(CursorChildIterator *, TreeCursorEntry *, bool *) |
| 312 |
) { |
| 313 |
TreeCursor *self = (TreeCursor *)_self; |
| 314 |
uint32_t initial_size = self->stack.size; |
| 315 |
|
| 316 |
while (self->stack.size > 1) { |
| 317 |
TreeCursorEntry entry = array_pop(&self->stack); |
| 318 |
CursorChildIterator iterator = ts_tree_cursor_iterate_children(self); |
| 319 |
iterator.child_index = entry.child_index; |
| 320 |
iterator.structural_child_index = entry.structural_child_index; |
| 321 |
iterator.position = entry.position; |
| 322 |
iterator.descendant_index = entry.descendant_index; |
| 323 |
|
| 324 |
bool visible = false; |
| 325 |
advance(&iterator, &entry, &visible); |
| 326 |
if (visible && self->stack.size + 1 < initial_size) break; |
| 327 |
|
| 328 |
while (advance(&iterator, &entry, &visible)) { |
| 329 |
if (visible) { |
| 330 |
array_push(&self->stack, entry); |
| 331 |
return TreeCursorStepVisible; |
| 332 |
} |
| 333 |
|
| 334 |
if (ts_subtree_visible_child_count(*entry.subtree)) { |
| 335 |
array_push(&self->stack, entry); |
| 336 |
return TreeCursorStepHidden; |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
self->stack.size = initial_size; |
| 342 |
return TreeCursorStepNone; |
| 343 |
} |
| 344 |
|
| 345 |
TreeCursorStep ts_tree_cursor_goto_next_sibling_internal(TSTreeCursor *_self) { |
| 346 |
return ts_tree_cursor_goto_sibling_internal(_self, ts_tree_cursor_child_iterator_next); |
| 347 |
} |
| 348 |
|
| 349 |
bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *self) { |
| 350 |
switch (ts_tree_cursor_goto_next_sibling_internal(self)) { |
| 351 |
case TreeCursorStepHidden: |
| 352 |
ts_tree_cursor_goto_first_child(self); |
| 353 |
return true; |
| 354 |
case TreeCursorStepVisible: |
| 355 |
return true; |
| 356 |
default: |
| 357 |
return false; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
TreeCursorStep ts_tree_cursor_goto_previous_sibling_internal(TSTreeCursor *_self) { |
| 362 |
// since subtracting across row loses column information, we may have to |
| 363 |
// restore it |
| 364 |
TreeCursor *self = (TreeCursor *)_self; |
| 365 |
|
| 366 |
// for that, save current position before traversing |
| 367 |
TreeCursorStep step = ts_tree_cursor_goto_sibling_internal( |
| 368 |
_self, ts_tree_cursor_child_iterator_previous); |
| 369 |
if (step == TreeCursorStepNone) |
| 370 |
return step; |
| 371 |
|
| 372 |
// if length is already valid, there's no need to recompute it |
| 373 |
if (!length_is_undefined(array_back(&self->stack)->position)) |
| 374 |
return step; |
| 375 |
|
| 376 |
// restore position from the parent node |
| 377 |
const TreeCursorEntry *parent = array_get(&self->stack, self->stack.size - 2); |
| 378 |
Length position = parent->position; |
| 379 |
uint32_t child_index = array_back(&self->stack)->child_index; |
| 380 |
const Subtree *children = ts_subtree_children((*(parent->subtree))); |
| 381 |
|
| 382 |
if (child_index > 0) { |
| 383 |
// skip first child padding since its position should match the position of the parent |
| 384 |
position = length_add(position, ts_subtree_size(children[0])); |
| 385 |
for (uint32_t i = 1; i < child_index; ++i) { |
| 386 |
position = length_add(position, ts_subtree_total_size(children[i])); |
| 387 |
} |
| 388 |
position = length_add(position, ts_subtree_padding(children[child_index])); |
| 389 |
} |
| 390 |
|
| 391 |
array_back(&self->stack)->position = position; |
| 392 |
|
| 393 |
return step; |
| 394 |
} |
| 395 |
|
| 396 |
bool ts_tree_cursor_goto_previous_sibling(TSTreeCursor *self) { |
| 397 |
switch (ts_tree_cursor_goto_previous_sibling_internal(self)) { |
| 398 |
case TreeCursorStepHidden: |
| 399 |
ts_tree_cursor_goto_last_child(self); |
| 400 |
return true; |
| 401 |
case TreeCursorStepVisible: |
| 402 |
return true; |
| 403 |
default: |
| 404 |
return false; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
bool ts_tree_cursor_goto_parent(TSTreeCursor *_self) { |
| 409 |
TreeCursor *self = (TreeCursor *)_self; |
| 410 |
for (unsigned i = self->stack.size - 2; i + 1 > 0; i--) { |
| 411 |
if (ts_tree_cursor_is_entry_visible(self, i)) { |
| 412 |
self->stack.size = i + 1; |
| 413 |
return true; |
| 414 |
} |
| 415 |
} |
| 416 |
return false; |
| 417 |
} |
| 418 |
|
| 419 |
void ts_tree_cursor_goto_descendant( |
| 420 |
TSTreeCursor *_self, |
| 421 |
uint32_t goal_descendant_index |
| 422 |
) { |
| 423 |
TreeCursor *self = (TreeCursor *)_self; |
| 424 |
|
| 425 |
// Ascend to the lowest ancestor that contains the goal node. |
| 426 |
for (;;) { |
| 427 |
uint32_t i = self->stack.size - 1; |
| 428 |
TreeCursorEntry *entry = array_get(&self->stack, i); |
| 429 |
uint32_t next_descendant_index = |
| 430 |
entry->descendant_index + |
| 431 |
(ts_tree_cursor_is_entry_visible(self, i) ? 1 : 0) + |
| 432 |
ts_subtree_visible_descendant_count(*entry->subtree); |
| 433 |
if ( |
| 434 |
(entry->descendant_index <= goal_descendant_index) && |
| 435 |
(next_descendant_index > goal_descendant_index) |
| 436 |
) { |
| 437 |
break; |
| 438 |
} else if (self->stack.size <= 1) { |
| 439 |
return; |
| 440 |
} else { |
| 441 |
self->stack.size--; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
// Descend to the goal node. |
| 446 |
bool did_descend = true; |
| 447 |
do { |
| 448 |
did_descend = false; |
| 449 |
bool visible; |
| 450 |
TreeCursorEntry entry; |
| 451 |
CursorChildIterator iterator = ts_tree_cursor_iterate_children(self); |
| 452 |
if (iterator.descendant_index > goal_descendant_index) { |
| 453 |
return; |
| 454 |
} |
| 455 |
|
| 456 |
while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) { |
| 457 |
if (iterator.descendant_index > goal_descendant_index) { |
| 458 |
array_push(&self->stack, entry); |
| 459 |
if (visible && entry.descendant_index == goal_descendant_index) { |
| 460 |
return; |
| 461 |
} else { |
| 462 |
did_descend = true; |
| 463 |
break; |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
} while (did_descend); |
| 468 |
} |
| 469 |
|
| 470 |
uint32_t ts_tree_cursor_current_descendant_index(const TSTreeCursor *_self) { |
| 471 |
const TreeCursor *self = (const TreeCursor *)_self; |
| 472 |
TreeCursorEntry *last_entry = array_back(&self->stack); |
| 473 |
return last_entry->descendant_index; |
| 474 |
} |
| 475 |
|
| 476 |
TSNode ts_tree_cursor_current_node(const TSTreeCursor *_self) { |
| 477 |
const TreeCursor *self = (const TreeCursor *)_self; |
| 478 |
TreeCursorEntry *last_entry = array_back(&self->stack); |
| 479 |
bool is_extra = ts_subtree_extra(*last_entry->subtree); |
| 480 |
TSSymbol alias_symbol = is_extra ? 0 : self->root_alias_symbol; |
| 481 |
if (self->stack.size > 1 && !is_extra) { |
| 482 |
TreeCursorEntry *parent_entry = array_get(&self->stack, self->stack.size - 2); |
| 483 |
alias_symbol = ts_language_alias_at( |
| 484 |
self->tree->language, |
| 485 |
parent_entry->subtree->ptr->production_id, |
| 486 |
last_entry->structural_child_index |
| 487 |
); |
| 488 |
} |
| 489 |
return ts_node_new( |
| 490 |
self->tree, |
| 491 |
last_entry->subtree, |
| 492 |
last_entry->position, |
| 493 |
alias_symbol |
| 494 |
); |
| 495 |
} |
| 496 |
|
| 497 |
// Private - Get various facts about the current node that are needed |
| 498 |
// when executing tree queries. |
| 499 |
void ts_tree_cursor_current_status( |
| 500 |
const TSTreeCursor *_self, |
| 501 |
TSFieldId *field_id, |
| 502 |
bool *has_later_siblings, |
| 503 |
bool *has_later_named_siblings, |
| 504 |
bool *can_have_later_siblings_with_this_field, |
| 505 |
TSSymbol *supertypes, |
| 506 |
unsigned *supertype_count |
| 507 |
) { |
| 508 |
const TreeCursor *self = (const TreeCursor *)_self; |
| 509 |
unsigned max_supertypes = *supertype_count; |
| 510 |
*field_id = 0; |
| 511 |
*supertype_count = 0; |
| 512 |
*has_later_siblings = false; |
| 513 |
*has_later_named_siblings = false; |
| 514 |
*can_have_later_siblings_with_this_field = false; |
| 515 |
|
| 516 |
// Walk up the tree, visiting the current node and its invisible ancestors, |
| 517 |
// because fields can refer to nodes through invisible *wrapper* nodes, |
| 518 |
for (unsigned i = self->stack.size - 1; i > 0; i--) { |
| 519 |
TreeCursorEntry *entry = array_get(&self->stack, i); |
| 520 |
TreeCursorEntry *parent_entry = array_get(&self->stack, i - 1); |
| 521 |
|
| 522 |
const TSSymbol *alias_sequence = ts_language_alias_sequence( |
| 523 |
self->tree->language, |
| 524 |
parent_entry->subtree->ptr->production_id |
| 525 |
); |
| 526 |
|
| 527 |
#define subtree_symbol(subtree, structural_child_index) \ |
| 528 |
(( \ |
| 529 |
!ts_subtree_extra(subtree) && \ |
| 530 |
alias_sequence && \ |
| 531 |
alias_sequence[structural_child_index] \ |
| 532 |
) ? \ |
| 533 |
alias_sequence[structural_child_index] : \ |
| 534 |
ts_subtree_symbol(subtree)) |
| 535 |
|
| 536 |
// Stop walking up when a visible ancestor is found. |
| 537 |
TSSymbol entry_symbol = subtree_symbol( |
| 538 |
*entry->subtree, |
| 539 |
entry->structural_child_index |
| 540 |
); |
| 541 |
TSSymbolMetadata entry_metadata = ts_language_symbol_metadata( |
| 542 |
self->tree->language, |
| 543 |
entry_symbol |
| 544 |
); |
| 545 |
if (i != self->stack.size - 1 && entry_metadata.visible) break; |
| 546 |
|
| 547 |
// Record any supertypes |
| 548 |
if (entry_metadata.supertype && *supertype_count < max_supertypes) { |
| 549 |
supertypes[*supertype_count] = entry_symbol; |
| 550 |
(*supertype_count)++; |
| 551 |
} |
| 552 |
|
| 553 |
// Determine if the current node has later siblings. |
| 554 |
if (!*has_later_siblings) { |
| 555 |
unsigned sibling_count = parent_entry->subtree->ptr->child_count; |
| 556 |
unsigned structural_child_index = entry->structural_child_index; |
| 557 |
if (!ts_subtree_extra(*entry->subtree)) structural_child_index++; |
| 558 |
for (unsigned j = entry->child_index + 1; j < sibling_count; j++) { |
| 559 |
Subtree sibling = ts_subtree_children(*parent_entry->subtree)[j]; |
| 560 |
TSSymbolMetadata sibling_metadata = ts_language_symbol_metadata( |
| 561 |
self->tree->language, |
| 562 |
subtree_symbol(sibling, structural_child_index) |
| 563 |
); |
| 564 |
if (sibling_metadata.visible) { |
| 565 |
*has_later_siblings = true; |
| 566 |
if (*has_later_named_siblings) break; |
| 567 |
if (sibling_metadata.named) { |
| 568 |
*has_later_named_siblings = true; |
| 569 |
break; |
| 570 |
} |
| 571 |
} else if (ts_subtree_visible_child_count(sibling) > 0) { |
| 572 |
*has_later_siblings = true; |
| 573 |
if (*has_later_named_siblings) break; |
| 574 |
if (sibling.ptr->named_child_count > 0) { |
| 575 |
*has_later_named_siblings = true; |
| 576 |
break; |
| 577 |
} |
| 578 |
} |
| 579 |
if (!ts_subtree_extra(sibling)) structural_child_index++; |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
#undef subtree_symbol |
| 584 |
|
| 585 |
if (!ts_subtree_extra(*entry->subtree)) { |
| 586 |
const TSFieldMapEntry *field_map, *field_map_end; |
| 587 |
ts_language_field_map( |
| 588 |
self->tree->language, |
| 589 |
parent_entry->subtree->ptr->production_id, |
| 590 |
&field_map, &field_map_end |
| 591 |
); |
| 592 |
|
| 593 |
// Look for a field name associated with the current node. |
| 594 |
if (!*field_id) { |
| 595 |
for (const TSFieldMapEntry *map = field_map; map < field_map_end; map++) { |
| 596 |
if (!map->inherited && map->child_index == entry->structural_child_index) { |
| 597 |
*field_id = map->field_id; |
| 598 |
break; |
| 599 |
} |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
// Determine if the current node can have later siblings with the same field name. |
| 604 |
if (*field_id) { |
| 605 |
for (const TSFieldMapEntry *map = field_map; map < field_map_end; map++) { |
| 606 |
if ( |
| 607 |
map->field_id == *field_id && |
| 608 |
map->child_index > entry->structural_child_index |
| 609 |
) { |
| 610 |
*can_have_later_siblings_with_this_field = true; |
| 611 |
break; |
| 612 |
} |
| 613 |
} |
| 614 |
} |
| 615 |
} |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
uint32_t ts_tree_cursor_current_depth(const TSTreeCursor *_self) { |
| 620 |
const TreeCursor *self = (const TreeCursor *)_self; |
| 621 |
uint32_t depth = 0; |
| 622 |
for (unsigned i = 1; i < self->stack.size; i++) { |
| 623 |
if (ts_tree_cursor_is_entry_visible(self, i)) { |
| 624 |
depth++; |
| 625 |
} |
| 626 |
} |
| 627 |
return depth; |
| 628 |
} |
| 629 |
|
| 630 |
TSNode ts_tree_cursor_parent_node(const TSTreeCursor *_self) { |
| 631 |
const TreeCursor *self = (const TreeCursor *)_self; |
| 632 |
for (int i = (int)self->stack.size - 2; i >= 0; i--) { |
| 633 |
TreeCursorEntry *entry = array_get(&self->stack, i); |
| 634 |
bool is_visible = true; |
| 635 |
TSSymbol alias_symbol = 0; |
| 636 |
if (i > 0) { |
| 637 |
TreeCursorEntry *parent_entry = array_get(&self->stack, i - 1); |
| 638 |
alias_symbol = ts_language_alias_at( |
| 639 |
self->tree->language, |
| 640 |
parent_entry->subtree->ptr->production_id, |
| 641 |
entry->structural_child_index |
| 642 |
); |
| 643 |
is_visible = (alias_symbol != 0) || ts_subtree_visible(*entry->subtree); |
| 644 |
} |
| 645 |
if (is_visible) { |
| 646 |
return ts_node_new( |
| 647 |
self->tree, |
| 648 |
entry->subtree, |
| 649 |
entry->position, |
| 650 |
alias_symbol |
| 651 |
); |
| 652 |
} |
| 653 |
} |
| 654 |
return ts_node_new(NULL, NULL, length_zero(), 0); |
| 655 |
} |
| 656 |
|
| 657 |
TSFieldId ts_tree_cursor_current_field_id(const TSTreeCursor *_self) { |
| 658 |
const TreeCursor *self = (const TreeCursor *)_self; |
| 659 |
|
| 660 |
// Walk up the tree, visiting the current node and its invisible ancestors. |
| 661 |
for (unsigned i = self->stack.size - 1; i > 0; i--) { |
| 662 |
TreeCursorEntry *entry = array_get(&self->stack, i); |
| 663 |
TreeCursorEntry *parent_entry = array_get(&self->stack, i - 1); |
| 664 |
|
| 665 |
// Stop walking up when another visible node is found. |
| 666 |
if ( |
| 667 |
i != self->stack.size - 1 && |
| 668 |
ts_tree_cursor_is_entry_visible(self, i) |
| 669 |
) break; |
| 670 |
|
| 671 |
if (ts_subtree_extra(*entry->subtree)) break; |
| 672 |
|
| 673 |
const TSFieldMapEntry *field_map, *field_map_end; |
| 674 |
ts_language_field_map( |
| 675 |
self->tree->language, |
| 676 |
parent_entry->subtree->ptr->production_id, |
| 677 |
&field_map, &field_map_end |
| 678 |
); |
| 679 |
for (const TSFieldMapEntry *map = field_map; map < field_map_end; map++) { |
| 680 |
if (!map->inherited && map->child_index == entry->structural_child_index) { |
| 681 |
return map->field_id; |
| 682 |
} |
| 683 |
} |
| 684 |
} |
| 685 |
return 0; |
| 686 |
} |
| 687 |
|
| 688 |
const char *ts_tree_cursor_current_field_name(const TSTreeCursor *_self) { |
| 689 |
TSFieldId id = ts_tree_cursor_current_field_id(_self); |
| 690 |
if (id) { |
| 691 |
const TreeCursor *self = (const TreeCursor *)_self; |
| 692 |
return self->tree->language->field_names[id]; |
| 693 |
} else { |
| 694 |
return NULL; |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
TSTreeCursor ts_tree_cursor_copy(const TSTreeCursor *_cursor) { |
| 699 |
const TreeCursor *cursor = (const TreeCursor *)_cursor; |
| 700 |
TSTreeCursor res = {NULL, NULL, {0, 0}}; |
| 701 |
TreeCursor *copy = (TreeCursor *)&res; |
| 702 |
copy->tree = cursor->tree; |
| 703 |
copy->root_alias_symbol = cursor->root_alias_symbol; |
| 704 |
array_init(©->stack); |
| 705 |
array_push_all(©->stack, &cursor->stack); |
| 706 |
return res; |
| 707 |
} |
| 708 |
|
| 709 |
void ts_tree_cursor_reset_to(TSTreeCursor *_dst, const TSTreeCursor *_src) { |
| 710 |
const TreeCursor *cursor = (const TreeCursor *)_src; |
| 711 |
TreeCursor *copy = (TreeCursor *)_dst; |
| 712 |
copy->tree = cursor->tree; |
| 713 |
copy->root_alias_symbol = cursor->root_alias_symbol; |
| 714 |
array_clear(©->stack); |
| 715 |
array_push_all(©->stack, &cursor->stack); |
| 716 |
} |