Merge branch 'master' into wasm-language
This commit is contained in:
commit
f4e2f68f14
161 changed files with 10293 additions and 4253 deletions
|
|
@ -5,7 +5,7 @@ static void *ts_malloc_default(size_t size) {
|
|||
void *result = malloc(size);
|
||||
if (size > 0 && !result) {
|
||||
fprintf(stderr, "tree-sitter failed to allocate %zu bytes", size);
|
||||
exit(1);
|
||||
abort();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
@ -14,7 +14,7 @@ static void *ts_calloc_default(size_t count, size_t size) {
|
|||
void *result = calloc(count, size);
|
||||
if (count > 0 && !result) {
|
||||
fprintf(stderr, "tree-sitter failed to allocate %zu bytes", count * size);
|
||||
exit(1);
|
||||
abort();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
@ -23,7 +23,7 @@ static void *ts_realloc_default(void *buffer, size_t size) {
|
|||
void *result = realloc(buffer, size);
|
||||
if (size > 0 && !result) {
|
||||
fprintf(stderr, "tree-sitter failed to reallocate %zu bytes", size);
|
||||
exit(1);
|
||||
abort();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
@ -35,10 +35,10 @@ void *(*ts_current_realloc)(void *, size_t) = ts_realloc_default;
|
|||
void (*ts_current_free)(void *) = free;
|
||||
|
||||
void ts_set_allocator(
|
||||
void *(*new_malloc)(size_t),
|
||||
void *(*new_calloc)(size_t, size_t),
|
||||
void *(*new_realloc)(void *, size_t),
|
||||
void (*new_free)(void *)
|
||||
void *(*new_malloc)(size_t size),
|
||||
void *(*new_calloc)(size_t count, size_t size),
|
||||
void *(*new_realloc)(void *ptr, size_t size),
|
||||
void (*new_free)(void *ptr)
|
||||
) {
|
||||
ts_current_malloc = new_malloc ? new_malloc : ts_malloc_default;
|
||||
ts_current_calloc = new_calloc ? new_calloc : ts_calloc_default;
|
||||
|
|
|
|||
|
|
@ -25,8 +25,8 @@ extern "C" {
|
|||
#define array_new() \
|
||||
{ NULL, 0, 0 }
|
||||
|
||||
#define array_get(self, index) \
|
||||
(assert((uint32_t)index < (self)->size), &(self)->contents[index])
|
||||
#define array_get(self, _index) \
|
||||
(assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])
|
||||
|
||||
#define array_front(self) array_get(self, 0)
|
||||
|
||||
|
|
@ -38,7 +38,7 @@ extern "C" {
|
|||
array__reserve((VoidArray *)(self), array__elem_size(self), new_capacity)
|
||||
|
||||
// Free any memory allocated for this array.
|
||||
#define array_delete(self) array__delete((VoidArray *)self)
|
||||
#define array_delete(self) array__delete((VoidArray *)(self))
|
||||
|
||||
#define array_push(self, element) \
|
||||
(array__grow((VoidArray *)(self), 1, array__elem_size(self)), \
|
||||
|
|
@ -65,19 +65,19 @@ extern "C" {
|
|||
// Remove `old_count` elements from the array starting at the given `index`. At
|
||||
// the same index, insert `new_count` new elements, reading their values from the
|
||||
// `new_contents` pointer.
|
||||
#define array_splice(self, index, old_count, new_count, new_contents) \
|
||||
#define array_splice(self, _index, old_count, new_count, new_contents) \
|
||||
array__splice( \
|
||||
(VoidArray *)(self), array__elem_size(self), index, \
|
||||
(VoidArray *)(self), array__elem_size(self), _index, \
|
||||
old_count, new_count, new_contents \
|
||||
)
|
||||
|
||||
// Insert one `element` into the array at the given `index`.
|
||||
#define array_insert(self, index, element) \
|
||||
array__splice((VoidArray *)(self), array__elem_size(self), index, 0, 1, &element)
|
||||
#define array_insert(self, _index, element) \
|
||||
array__splice((VoidArray *)(self), array__elem_size(self), _index, 0, 1, &(element))
|
||||
|
||||
// Remove one `element` from the array at the given `index`.
|
||||
#define array_erase(self, index) \
|
||||
array__erase((VoidArray *)(self), array__elem_size(self), index)
|
||||
#define array_erase(self, _index) \
|
||||
array__erase((VoidArray *)(self), array__elem_size(self), _index)
|
||||
|
||||
#define array_pop(self) ((self)->contents[--(self)->size])
|
||||
|
||||
|
|
@ -95,23 +95,23 @@ extern "C" {
|
|||
// out-parameter is set to true. Otherwise, `index` is set to an index where
|
||||
// `needle` should be inserted in order to preserve the sorting, and `exists`
|
||||
// is set to false.
|
||||
#define array_search_sorted_with(self, compare, needle, index, exists) \
|
||||
array__search_sorted(self, 0, compare, , needle, index, exists)
|
||||
#define array_search_sorted_with(self, compare, needle, _index, _exists) \
|
||||
array__search_sorted(self, 0, compare, , needle, _index, _exists)
|
||||
|
||||
// Search a sorted array for a given `needle` value, using integer comparisons
|
||||
// of a given struct field (specified with a leading dot) to determine the order.
|
||||
//
|
||||
// See also `array_search_sorted_with`.
|
||||
#define array_search_sorted_by(self, field, needle, index, exists) \
|
||||
array__search_sorted(self, 0, _compare_int, field, needle, index, exists)
|
||||
#define array_search_sorted_by(self, field, needle, _index, _exists) \
|
||||
array__search_sorted(self, 0, compare_int, field, needle, _index, _exists)
|
||||
|
||||
// Insert a given `value` into a sorted array, using the given `compare`
|
||||
// callback to determine the order.
|
||||
#define array_insert_sorted_with(self, compare, value) \
|
||||
do { \
|
||||
unsigned index, exists; \
|
||||
array_search_sorted_with(self, compare, &(value), &index, &exists); \
|
||||
if (!exists) array_insert(self, index, value); \
|
||||
unsigned _index, _exists; \
|
||||
array_search_sorted_with(self, compare, &(value), &_index, &_exists); \
|
||||
if (!_exists) array_insert(self, _index, value); \
|
||||
} while (0)
|
||||
|
||||
// Insert a given `value` into a sorted array, using integer comparisons of
|
||||
|
|
@ -120,9 +120,9 @@ extern "C" {
|
|||
// See also `array_search_sorted_by`.
|
||||
#define array_insert_sorted_by(self, field, value) \
|
||||
do { \
|
||||
unsigned index, exists; \
|
||||
array_search_sorted_by(self, field, (value) field, &index, &exists); \
|
||||
if (!exists) array_insert(self, index, value); \
|
||||
unsigned _index, _exists; \
|
||||
array_search_sorted_by(self, field, (value) field, &_index, &_exists); \
|
||||
if (!_exists) array_insert(self, _index, value); \
|
||||
} while (0)
|
||||
|
||||
// Private
|
||||
|
|
@ -132,10 +132,12 @@ typedef Array(void) VoidArray;
|
|||
#define array__elem_size(self) sizeof(*(self)->contents)
|
||||
|
||||
static inline void array__delete(VoidArray *self) {
|
||||
ts_free(self->contents);
|
||||
self->contents = NULL;
|
||||
self->size = 0;
|
||||
self->capacity = 0;
|
||||
if (self->contents) {
|
||||
ts_free(self->contents);
|
||||
self->contents = NULL;
|
||||
self->size = 0;
|
||||
self->capacity = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void array__erase(VoidArray *self, size_t element_size,
|
||||
|
|
@ -217,28 +219,28 @@ static inline void array__splice(VoidArray *self, size_t element_size,
|
|||
}
|
||||
|
||||
// A binary search routine, based on Rust's `std::slice::binary_search_by`.
|
||||
#define array__search_sorted(self, start, compare, suffix, needle, index, exists) \
|
||||
#define array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \
|
||||
do { \
|
||||
*(index) = start; \
|
||||
*(exists) = false; \
|
||||
uint32_t size = (self)->size - *(index); \
|
||||
*(_index) = start; \
|
||||
*(_exists) = false; \
|
||||
uint32_t size = (self)->size - *(_index); \
|
||||
if (size == 0) break; \
|
||||
int comparison; \
|
||||
while (size > 1) { \
|
||||
uint32_t half_size = size / 2; \
|
||||
uint32_t mid_index = *(index) + half_size; \
|
||||
uint32_t mid_index = *(_index) + half_size; \
|
||||
comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
|
||||
if (comparison <= 0) *(index) = mid_index; \
|
||||
if (comparison <= 0) *(_index) = mid_index; \
|
||||
size -= half_size; \
|
||||
} \
|
||||
comparison = compare(&((self)->contents[*(index)] suffix), (needle)); \
|
||||
if (comparison == 0) *(exists) = true; \
|
||||
else if (comparison < 0) *(index) += 1; \
|
||||
comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
|
||||
if (comparison == 0) *(_exists) = true; \
|
||||
else if (comparison < 0) *(_index) += 1; \
|
||||
} while (0)
|
||||
|
||||
// Helper macro for the `_sorted_by` routines below. This takes the left (existing)
|
||||
// parameter by reference in order to work with the generic sorting function above.
|
||||
#define _compare_int(a, b) ((int)*(a) - (int)(b))
|
||||
#define compare_int(a, b) ((int)*(a) - (int)(b))
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef TREE_SITTER_ATOMIC_H_
|
||||
#define TREE_SITTER_ATOMIC_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
|
@ -47,11 +48,19 @@ static inline size_t atomic_load(const volatile size_t *p) {
|
|||
}
|
||||
|
||||
static inline uint32_t atomic_inc(volatile uint32_t *p) {
|
||||
return __sync_add_and_fetch(p, 1u);
|
||||
#ifdef __ATOMIC_RELAXED
|
||||
return __atomic_add_fetch(p, 1U, __ATOMIC_SEQ_CST);
|
||||
#else
|
||||
return __sync_add_and_fetch(p, 1U);
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline uint32_t atomic_dec(volatile uint32_t *p) {
|
||||
return __sync_sub_and_fetch(p, 1u);
|
||||
#ifdef __ATOMIC_RELAXED
|
||||
return __atomic_sub_fetch(p, 1U, __ATOMIC_SEQ_CST);
|
||||
#else
|
||||
return __sync_sub_and_fetch(p, 1U);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef TREE_SITTER_CLOCK_H_
|
||||
#define TREE_SITTER_CLOCK_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef uint64_t TSDuration;
|
||||
|
|
@ -82,6 +83,10 @@ static inline TSClock clock_after(TSClock base, TSDuration duration) {
|
|||
TSClock result = base;
|
||||
result.tv_sec += duration / 1000000;
|
||||
result.tv_nsec += (duration % 1000000) * 1000;
|
||||
if (result.tv_nsec >= 1000000000) {
|
||||
result.tv_nsec -= 1000000000;
|
||||
++(result.tv_sec);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -210,7 +210,7 @@ static void iterator_ascend(Iterator *self) {
|
|||
static bool iterator_descend(Iterator *self, uint32_t goal_position) {
|
||||
if (self->in_padding) return false;
|
||||
|
||||
bool did_descend;
|
||||
bool did_descend = false;
|
||||
do {
|
||||
did_descend = false;
|
||||
TreeCursorEntry entry = *array_back(&self->cursor.stack);
|
||||
|
|
|
|||
|
|
@ -7,6 +7,10 @@ uint32_t ts_language_symbol_count(const TSLanguage *self) {
|
|||
return self->symbol_count + self->alias_count;
|
||||
}
|
||||
|
||||
uint32_t ts_language_state_count(const TSLanguage *self) {
|
||||
return self->state_count;
|
||||
}
|
||||
|
||||
uint32_t ts_language_version(const TSLanguage *self) {
|
||||
return self->version;
|
||||
}
|
||||
|
|
@ -56,6 +60,28 @@ TSSymbol ts_language_public_symbol(
|
|||
return self->public_symbol_map[symbol];
|
||||
}
|
||||
|
||||
TSStateId ts_language_next_state(
|
||||
const TSLanguage *self,
|
||||
TSStateId state,
|
||||
TSSymbol symbol
|
||||
) {
|
||||
if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) {
|
||||
return 0;
|
||||
} else if (symbol < self->token_count) {
|
||||
uint32_t count;
|
||||
const TSParseAction *actions = ts_language_actions(self, state, symbol, &count);
|
||||
if (count > 0) {
|
||||
TSParseAction action = actions[count - 1];
|
||||
if (action.type == TSParseActionTypeShift) {
|
||||
return action.shift.extra ? state : action.shift.state;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
} else {
|
||||
return ts_language_lookup(self, state, symbol);
|
||||
}
|
||||
}
|
||||
|
||||
const char *ts_language_symbol_name(
|
||||
const TSLanguage *self,
|
||||
TSSymbol symbol
|
||||
|
|
@ -78,7 +104,7 @@ TSSymbol ts_language_symbol_for_name(
|
|||
bool is_named
|
||||
) {
|
||||
if (!strncmp(string, "ERROR", length)) return ts_builtin_sym_error;
|
||||
uint32_t count = ts_language_symbol_count(self);
|
||||
uint16_t count = (uint16_t)ts_language_symbol_count(self);
|
||||
for (TSSymbol i = 0; i < count; i++) {
|
||||
TSSymbolMetadata metadata = ts_language_symbol_metadata(self, i);
|
||||
if ((!metadata.visible && !metadata.supertype) || metadata.named != is_named) continue;
|
||||
|
|
@ -121,7 +147,7 @@ TSFieldId ts_language_field_id_for_name(
|
|||
const char *name,
|
||||
uint32_t name_length
|
||||
) {
|
||||
uint32_t count = ts_language_field_count(self);
|
||||
uint16_t count = (uint16_t)ts_language_field_count(self);
|
||||
for (TSSymbol i = 1; i < count + 1; i++) {
|
||||
switch (strncmp(name, self->field_names[i], name_length)) {
|
||||
case 0:
|
||||
|
|
@ -135,3 +161,48 @@ TSFieldId ts_language_field_id_for_name(
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
TSLookaheadIterator *ts_lookahead_iterator_new(const TSLanguage *self, TSStateId state) {
|
||||
if (state >= self->state_count) return NULL;
|
||||
LookaheadIterator *iterator = ts_malloc(sizeof(LookaheadIterator));
|
||||
*iterator = ts_language_lookaheads(self, state);
|
||||
return (TSLookaheadIterator *)iterator;
|
||||
}
|
||||
|
||||
void ts_lookahead_iterator_delete(TSLookaheadIterator *self) {
|
||||
ts_free(self);
|
||||
}
|
||||
|
||||
bool ts_lookahead_iterator_reset_state(TSLookaheadIterator * self, TSStateId state) {
|
||||
LookaheadIterator *iterator = (LookaheadIterator *)self;
|
||||
if (state >= iterator->language->state_count) return false;
|
||||
*iterator = ts_language_lookaheads(iterator->language, state);
|
||||
return true;
|
||||
}
|
||||
|
||||
const TSLanguage *ts_lookahead_iterator_language(const TSLookaheadIterator *self) {
|
||||
const LookaheadIterator *iterator = (const LookaheadIterator *)self;
|
||||
return iterator->language;
|
||||
}
|
||||
|
||||
bool ts_lookahead_iterator_reset(TSLookaheadIterator *self, const TSLanguage *language, TSStateId state) {
|
||||
if (state >= language->state_count) return false;
|
||||
LookaheadIterator *iterator = (LookaheadIterator *)self;
|
||||
*iterator = ts_language_lookaheads(language, state);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ts_lookahead_iterator_next(TSLookaheadIterator *self) {
|
||||
LookaheadIterator *iterator = (LookaheadIterator *)self;
|
||||
return ts_lookahead_iterator__next(iterator);
|
||||
}
|
||||
|
||||
TSSymbol ts_lookahead_iterator_current_symbol(const TSLookaheadIterator *self) {
|
||||
const LookaheadIterator *iterator = (const LookaheadIterator *)self;
|
||||
return iterator->symbol;
|
||||
}
|
||||
|
||||
const char *ts_lookahead_iterator_current_symbol_name(const TSLookaheadIterator *self) {
|
||||
const LookaheadIterator *iterator = (const LookaheadIterator *)self;
|
||||
return ts_language_symbol_name(iterator->language, iterator->symbol);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,6 +38,8 @@ TSSymbolMetadata ts_language_symbol_metadata(const TSLanguage *, TSSymbol);
|
|||
|
||||
TSSymbol ts_language_public_symbol(const TSLanguage *, TSSymbol);
|
||||
|
||||
TSStateId ts_language_next_state(const TSLanguage *self, TSStateId state, TSSymbol symbol);
|
||||
|
||||
static inline bool ts_language_is_symbol_external(const TSLanguage *self, TSSymbol symbol) {
|
||||
return 0 < symbol && symbol < self->external_token_count + 1;
|
||||
}
|
||||
|
|
@ -83,7 +85,7 @@ static inline uint16_t ts_language_lookup(
|
|||
for (unsigned i = 0; i < group_count; i++) {
|
||||
uint16_t section_value = *(data++);
|
||||
uint16_t symbol_count = *(data++);
|
||||
for (unsigned i = 0; i < symbol_count; i++) {
|
||||
for (unsigned j = 0; j < symbol_count; j++) {
|
||||
if (*(data++) == symbol) return section_value;
|
||||
}
|
||||
}
|
||||
|
|
@ -134,7 +136,7 @@ static inline LookaheadIterator ts_language_lookaheads(
|
|||
};
|
||||
}
|
||||
|
||||
static inline bool ts_lookahead_iterator_next(LookaheadIterator *self) {
|
||||
static inline bool ts_lookahead_iterator__next(LookaheadIterator *self) {
|
||||
// For small parse states, valid symbols are listed explicitly,
|
||||
// grouped by their value. There's no need to look up the actions
|
||||
// again until moving to the next group.
|
||||
|
|
@ -178,28 +180,6 @@ static inline bool ts_lookahead_iterator_next(LookaheadIterator *self) {
|
|||
return true;
|
||||
}
|
||||
|
||||
static inline TSStateId ts_language_next_state(
|
||||
const TSLanguage *self,
|
||||
TSStateId state,
|
||||
TSSymbol symbol
|
||||
) {
|
||||
if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) {
|
||||
return 0;
|
||||
} else if (symbol < self->token_count) {
|
||||
uint32_t count;
|
||||
const TSParseAction *actions = ts_language_actions(self, state, symbol, &count);
|
||||
if (count > 0) {
|
||||
TSParseAction action = actions[count - 1];
|
||||
if (action.type == TSParseActionTypeShift) {
|
||||
return action.shift.extra ? state : action.shift.state;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
} else {
|
||||
return ts_language_lookup(self, state, symbol);
|
||||
}
|
||||
}
|
||||
|
||||
// Whether the state is a "primary state". If this returns false, it indicates that there exists
|
||||
// another state that behaves identically to this one with respect to query analysis.
|
||||
static inline bool ts_language_state_is_primary(
|
||||
|
|
@ -269,17 +249,17 @@ static inline void ts_language_aliases_for_symbol(
|
|||
*start = &self->public_symbol_map[original_symbol];
|
||||
*end = *start + 1;
|
||||
|
||||
unsigned i = 0;
|
||||
unsigned idx = 0;
|
||||
for (;;) {
|
||||
TSSymbol symbol = self->alias_map[i++];
|
||||
TSSymbol symbol = self->alias_map[idx++];
|
||||
if (symbol == 0 || symbol > original_symbol) break;
|
||||
uint16_t count = self->alias_map[i++];
|
||||
uint16_t count = self->alias_map[idx++];
|
||||
if (symbol == original_symbol) {
|
||||
*start = &self->alias_map[i];
|
||||
*end = &self->alias_map[i + count];
|
||||
*start = &self->alias_map[idx];
|
||||
*end = &self->alias_map[idx + count];
|
||||
break;
|
||||
}
|
||||
i += count;
|
||||
idx += count;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -289,21 +269,21 @@ static inline void ts_language_write_symbol_as_dot_string(
|
|||
TSSymbol symbol
|
||||
) {
|
||||
const char *name = ts_language_symbol_name(self, symbol);
|
||||
for (const char *c = name; *c; c++) {
|
||||
switch (*c) {
|
||||
for (const char *chr = name; *chr; chr++) {
|
||||
switch (*chr) {
|
||||
case '"':
|
||||
case '\\':
|
||||
fputc('\\', f);
|
||||
fputc(*c, f);
|
||||
fputc(*chr, f);
|
||||
break;
|
||||
case '\n':
|
||||
fputs("\\n", f);
|
||||
break;
|
||||
case '\t':
|
||||
fputs("\\n", f);
|
||||
fputs("\\t", f);
|
||||
break;
|
||||
default:
|
||||
fputc(*c, f);
|
||||
fputc(*chr, f);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -172,7 +172,9 @@ static void ts_lexer__do_advance(Lexer *self, bool skip) {
|
|||
self->current_position.bytes >= current_range->end_byte ||
|
||||
current_range->end_byte == current_range->start_byte
|
||||
) {
|
||||
self->current_included_range_index++;
|
||||
if (self->current_included_range_index < self->included_range_count) {
|
||||
self->current_included_range_index++;
|
||||
}
|
||||
if (self->current_included_range_index < self->included_range_count) {
|
||||
current_range++;
|
||||
self->current_position = (Length) {
|
||||
|
|
@ -209,11 +211,11 @@ static void ts_lexer__advance(TSLexer *_self, bool skip) {
|
|||
if (!self->chunk) return;
|
||||
|
||||
if (skip) {
|
||||
LOG("skip", self->data.lookahead);
|
||||
LOG("skip", self->data.lookahead)
|
||||
} else {
|
||||
LOG("consume", self->data.lookahead);
|
||||
LOG("consume", self->data.lookahead)
|
||||
}
|
||||
|
||||
|
||||
ts_lexer__do_advance(self, skip);
|
||||
}
|
||||
|
||||
|
|
@ -245,9 +247,9 @@ static void ts_lexer__mark_end(TSLexer *_self) {
|
|||
|
||||
static uint32_t ts_lexer__get_column(TSLexer *_self) {
|
||||
Lexer *self = (Lexer *)_self;
|
||||
|
||||
|
||||
uint32_t goal_byte = self->current_position.bytes;
|
||||
|
||||
|
||||
self->did_get_column = true;
|
||||
self->current_position.bytes -= self->current_position.extent.column;
|
||||
self->current_position.extent.column = 0;
|
||||
|
|
@ -257,10 +259,13 @@ static uint32_t ts_lexer__get_column(TSLexer *_self) {
|
|||
}
|
||||
|
||||
uint32_t result = 0;
|
||||
ts_lexer__get_lookahead(self);
|
||||
while (self->current_position.bytes < goal_byte && !ts_lexer__eof(_self) && self->chunk) {
|
||||
ts_lexer__do_advance(self, false);
|
||||
result++;
|
||||
if (!ts_lexer__eof(_self)) {
|
||||
ts_lexer__get_lookahead(self);
|
||||
while (self->current_position.bytes < goal_byte && self->chunk) {
|
||||
result++;
|
||||
ts_lexer__do_advance(self, false);
|
||||
if (ts_lexer__eof(_self)) break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
|
|||
|
|
@ -237,6 +237,8 @@ static inline TSNode ts_node__prev_sibling(TSNode self, bool include_anonymous)
|
|||
return earlier_node;
|
||||
} else {
|
||||
node = earlier_node;
|
||||
earlier_node = ts_node__null();
|
||||
earlier_node_is_relevant = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -423,6 +425,19 @@ const char *ts_node_type(TSNode self) {
|
|||
return ts_language_symbol_name(self.tree->language, symbol);
|
||||
}
|
||||
|
||||
const TSLanguage *ts_node_language(TSNode self) {
|
||||
return self.tree->language;
|
||||
}
|
||||
|
||||
TSSymbol ts_node_grammar_symbol(TSNode self) {
|
||||
return ts_subtree_symbol(ts_node__subtree(self));
|
||||
}
|
||||
|
||||
const char *ts_node_grammar_type(TSNode self) {
|
||||
TSSymbol symbol = ts_subtree_symbol(ts_node__subtree(self));
|
||||
return ts_language_symbol_name(self.tree->language, symbol);
|
||||
}
|
||||
|
||||
char *ts_node_string(TSNode self) {
|
||||
return ts_subtree_string(ts_node__subtree(self), self.tree->language, false);
|
||||
}
|
||||
|
|
@ -458,6 +473,29 @@ bool ts_node_has_error(TSNode self) {
|
|||
return ts_subtree_error_cost(ts_node__subtree(self)) > 0;
|
||||
}
|
||||
|
||||
bool ts_node_is_error(TSNode self) {
|
||||
TSSymbol symbol = ts_node_symbol(self);
|
||||
return symbol == ts_builtin_sym_error;
|
||||
}
|
||||
|
||||
uint32_t ts_node_descendant_count(TSNode self) {
|
||||
return ts_subtree_visible_descendant_count(ts_node__subtree(self)) + 1;
|
||||
}
|
||||
|
||||
TSStateId ts_node_parse_state(TSNode self) {
|
||||
return ts_subtree_parse_state(ts_node__subtree(self));
|
||||
}
|
||||
|
||||
TSStateId ts_node_next_parse_state(TSNode self) {
|
||||
const TSLanguage *language = self.tree->language;
|
||||
uint16_t state = ts_node_parse_state(self);
|
||||
if (state == TS_TREE_STATE_NONE) {
|
||||
return TS_TREE_STATE_NONE;
|
||||
}
|
||||
uint16_t symbol = ts_node_grammar_symbol(self);
|
||||
return ts_language_next_state(language, state, symbol);
|
||||
}
|
||||
|
||||
TSNode ts_node_parent(TSNode self) {
|
||||
TSNode node = ts_tree_root_node(self.tree);
|
||||
uint32_t end_byte = ts_node_end_byte(self);
|
||||
|
|
@ -569,24 +607,58 @@ recur:
|
|||
return ts_node__null();
|
||||
}
|
||||
|
||||
const char *ts_node_field_name_for_child(TSNode self, uint32_t child_index) {
|
||||
const TSFieldMapEntry *field_map_start = NULL, *field_map_end = NULL;
|
||||
if (!ts_node_child_count(self)) {
|
||||
static inline const char *ts_node__field_name_from_language(TSNode self, uint32_t structural_child_index) {
|
||||
const TSFieldMapEntry *field_map, *field_map_end;
|
||||
ts_language_field_map(
|
||||
self.tree->language,
|
||||
ts_node__subtree(self).ptr->production_id,
|
||||
&field_map,
|
||||
&field_map_end
|
||||
);
|
||||
for (; field_map != field_map_end; field_map++) {
|
||||
if (!field_map->inherited && field_map->child_index == structural_child_index) {
|
||||
return self.tree->language->field_names[field_map->field_id];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
ts_language_field_map(
|
||||
self.tree->language,
|
||||
ts_node__subtree(self).ptr->production_id,
|
||||
&field_map_start,
|
||||
&field_map_end
|
||||
);
|
||||
const char *ts_node_field_name_for_child(TSNode self, uint32_t child_index) {
|
||||
TSNode result = self;
|
||||
bool did_descend = true;
|
||||
const char *inherited_field_name = NULL;
|
||||
|
||||
for (const TSFieldMapEntry *i = field_map_start; i < field_map_end; i++) {
|
||||
if (i->child_index == child_index) {
|
||||
return self.tree->language->field_names[i->field_id];
|
||||
while (did_descend) {
|
||||
did_descend = false;
|
||||
|
||||
TSNode child;
|
||||
uint32_t index = 0;
|
||||
NodeChildIterator iterator = ts_node_iterate_children(&result);
|
||||
while (ts_node_child_iterator_next(&iterator, &child)) {
|
||||
if (ts_node__is_relevant(child, true)) {
|
||||
if (index == child_index) {
|
||||
const char *field_name = ts_node__field_name_from_language(result, iterator.structural_child_index - 1);
|
||||
if (field_name) return field_name;
|
||||
return inherited_field_name;
|
||||
}
|
||||
index++;
|
||||
} else {
|
||||
uint32_t grandchild_index = child_index - index;
|
||||
uint32_t grandchild_count = ts_node__relevant_child_count(child, true);
|
||||
if (grandchild_index < grandchild_count) {
|
||||
const char *field_name = ts_node__field_name_from_language(result, iterator.structural_child_index - 1);
|
||||
if (field_name) inherited_field_name = field_name;
|
||||
|
||||
did_descend = true;
|
||||
result = child;
|
||||
child_index = grandchild_index;
|
||||
break;
|
||||
}
|
||||
index += grandchild_count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -134,10 +134,10 @@ typedef struct {
|
|||
static const char *ts_string_input_read(
|
||||
void *_self,
|
||||
uint32_t byte,
|
||||
TSPoint pt,
|
||||
TSPoint point,
|
||||
uint32_t *length
|
||||
) {
|
||||
(void)pt;
|
||||
(void)point;
|
||||
TSStringInput *self = (TSStringInput *)_self;
|
||||
if (byte >= self->length) {
|
||||
*length = 0;
|
||||
|
|
@ -161,9 +161,9 @@ static void ts_parser__log(TSParser *self) {
|
|||
|
||||
if (self->dot_graph_file) {
|
||||
fprintf(self->dot_graph_file, "graph {\nlabel=\"");
|
||||
for (char *c = &self->lexer.debug_buffer[0]; *c != 0; c++) {
|
||||
if (*c == '"' || *c == '\\') fputc('\\', self->dot_graph_file);
|
||||
fputc(*c, self->dot_graph_file);
|
||||
for (char *chr = &self->lexer.debug_buffer[0]; *chr != 0; chr++) {
|
||||
if (*chr == '"' || *chr == '\\') fputc('\\', self->dot_graph_file);
|
||||
fputc(*chr, self->dot_graph_file);
|
||||
}
|
||||
fprintf(self->dot_graph_file, "\"\n}\n\n");
|
||||
}
|
||||
|
|
@ -957,19 +957,19 @@ static StackVersion ts_parser__reduce(
|
|||
if (next_slice.version != slice.version) break;
|
||||
i++;
|
||||
|
||||
SubtreeArray children = next_slice.subtrees;
|
||||
ts_subtree_array_remove_trailing_extras(&children, &self->trailing_extras2);
|
||||
SubtreeArray next_slice_children = next_slice.subtrees;
|
||||
ts_subtree_array_remove_trailing_extras(&next_slice_children, &self->trailing_extras2);
|
||||
|
||||
if (ts_parser__select_children(
|
||||
self,
|
||||
ts_subtree_from_mut(parent),
|
||||
&children
|
||||
&next_slice_children
|
||||
)) {
|
||||
ts_subtree_array_clear(&self->tree_pool, &self->trailing_extras);
|
||||
ts_subtree_release(&self->tree_pool, ts_subtree_from_mut(parent));
|
||||
array_swap(&self->trailing_extras, &self->trailing_extras2);
|
||||
parent = ts_subtree_new_node(
|
||||
symbol, &children, production_id, self->language
|
||||
symbol, &next_slice_children, production_id, self->language
|
||||
);
|
||||
} else {
|
||||
array_clear(&self->trailing_extras2);
|
||||
|
|
@ -1080,8 +1080,8 @@ static bool ts_parser__do_all_potential_reductions(
|
|||
if (version >= version_count) break;
|
||||
|
||||
bool merged = false;
|
||||
for (StackVersion i = initial_version_count; i < version; i++) {
|
||||
if (ts_stack_merge(self->stack, i, version)) {
|
||||
for (StackVersion j = initial_version_count; j < version; j++) {
|
||||
if (ts_stack_merge(self->stack, j, version)) {
|
||||
merged = true;
|
||||
break;
|
||||
}
|
||||
|
|
@ -1104,8 +1104,8 @@ static bool ts_parser__do_all_potential_reductions(
|
|||
for (TSSymbol symbol = first_symbol; symbol < end_symbol; symbol++) {
|
||||
TableEntry entry;
|
||||
ts_language_table_entry(self->language, state, symbol, &entry);
|
||||
for (uint32_t i = 0; i < entry.action_count; i++) {
|
||||
TSParseAction action = entry.actions[i];
|
||||
for (uint32_t j = 0; j < entry.action_count; j++) {
|
||||
TSParseAction action = entry.actions[j];
|
||||
switch (action.type) {
|
||||
case TSParseActionTypeShift:
|
||||
case TSParseActionTypeRecover:
|
||||
|
|
@ -1127,8 +1127,8 @@ static bool ts_parser__do_all_potential_reductions(
|
|||
}
|
||||
|
||||
StackVersion reduction_version = STACK_VERSION_NONE;
|
||||
for (uint32_t i = 0; i < self->reduce_actions.size; i++) {
|
||||
ReduceAction action = self->reduce_actions.contents[i];
|
||||
for (uint32_t j = 0; j < self->reduce_actions.size; j++) {
|
||||
ReduceAction action = self->reduce_actions.contents[j];
|
||||
|
||||
reduction_version = ts_parser__reduce(
|
||||
self, version, action.symbol, action.count,
|
||||
|
|
@ -1414,7 +1414,7 @@ static void ts_parser__handle_error(
|
|||
TSStateId state = ts_stack_state(self->stack, v);
|
||||
for (
|
||||
TSSymbol missing_symbol = 1;
|
||||
missing_symbol < self->language->token_count;
|
||||
missing_symbol < (uint16_t)self->language->token_count;
|
||||
missing_symbol++
|
||||
) {
|
||||
TSStateId state_after_missing_symbol = ts_language_next_state(
|
||||
|
|
@ -1905,7 +1905,11 @@ void ts_parser_print_dot_graphs(TSParser *self, int fd) {
|
|||
}
|
||||
|
||||
if (fd >= 0) {
|
||||
#ifdef _WIN32
|
||||
self->dot_graph_file = _fdopen(fd, "a");
|
||||
#else
|
||||
self->dot_graph_file = fdopen(fd, "a");
|
||||
#endif
|
||||
} else {
|
||||
self->dot_graph_file = NULL;
|
||||
}
|
||||
|
|
@ -2039,8 +2043,16 @@ TSTree *ts_parser_parse(
|
|||
}
|
||||
}
|
||||
|
||||
// After advancing each version of the stack, re-sort the versions by their cost,
|
||||
// removing any versions that are no longer worth pursuing.
|
||||
unsigned min_error_cost = ts_parser__condense_stack(self);
|
||||
|
||||
// If there's already a finished parse tree that's better than any in-progress version,
|
||||
// then terminate parsing. Clear the parse stack to remove any extra references to subtrees
|
||||
// within the finished tree, ensuring that these subtrees can be safely mutated in-place
|
||||
// for rebalancing.
|
||||
if (self->finished_tree.ptr && ts_subtree_error_cost(self->finished_tree) < min_error_cost) {
|
||||
ts_stack_clear(self->stack);
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
|||
1890
lib/src/query.c
1890
lib/src/query.c
File diff suppressed because it is too large
Load diff
|
|
@ -120,6 +120,20 @@ recur:
|
|||
}
|
||||
}
|
||||
|
||||
/// Get the number of nodes in the subtree, for the purpose of measuring
|
||||
/// how much progress has been made by a given version of the stack.
|
||||
static uint32_t stack__subtree_node_count(Subtree subtree) {
|
||||
uint32_t count = ts_subtree_visible_descendant_count(subtree);
|
||||
if (ts_subtree_visible(subtree)) count++;
|
||||
|
||||
// Count intermediate error nodes even though they are not visible,
|
||||
// because a stack version's node count is used to check whether it
|
||||
// has made any progress since the last time it encountered an error.
|
||||
if (ts_subtree_symbol(subtree) == ts_builtin_sym_error_repeat) count++;
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
static StackNode *stack_node_new(
|
||||
StackNode *previous_node,
|
||||
Subtree subtree,
|
||||
|
|
@ -152,7 +166,7 @@ static StackNode *stack_node_new(
|
|||
if (subtree.ptr) {
|
||||
node->error_cost += ts_subtree_error_cost(subtree);
|
||||
node->position = length_add(node->position, ts_subtree_total_size(subtree));
|
||||
node->node_count += ts_subtree_node_count(subtree);
|
||||
node->node_count += stack__subtree_node_count(subtree);
|
||||
node->dynamic_precedence += ts_subtree_dynamic_precedence(subtree);
|
||||
}
|
||||
} else {
|
||||
|
|
@ -239,7 +253,7 @@ static void stack_node_add_link(
|
|||
|
||||
if (link.subtree.ptr) {
|
||||
ts_subtree_retain(link.subtree);
|
||||
node_count += ts_subtree_node_count(link.subtree);
|
||||
node_count += stack__subtree_node_count(link.subtree);
|
||||
dynamic_precedence += ts_subtree_dynamic_precedence(link.subtree);
|
||||
}
|
||||
|
||||
|
|
@ -305,7 +319,7 @@ static void ts_stack__add_slice(
|
|||
array_push(&self->slices, slice);
|
||||
}
|
||||
|
||||
inline StackSliceArray stack__iter(
|
||||
static StackSliceArray stack__iter(
|
||||
Stack *self,
|
||||
StackVersion version,
|
||||
StackCallback callback,
|
||||
|
|
@ -316,7 +330,7 @@ inline StackSliceArray stack__iter(
|
|||
array_clear(&self->iterators);
|
||||
|
||||
StackHead *head = array_get(&self->heads, version);
|
||||
StackIterator iterator = {
|
||||
StackIterator new_iterator = {
|
||||
.node = head->node,
|
||||
.subtrees = array_new(),
|
||||
.subtree_count = 0,
|
||||
|
|
@ -326,10 +340,10 @@ inline StackSliceArray stack__iter(
|
|||
bool include_subtrees = false;
|
||||
if (goal_subtree_count >= 0) {
|
||||
include_subtrees = true;
|
||||
array_reserve(&iterator.subtrees, (uint32_t)ts_subtree_alloc_size(goal_subtree_count) / sizeof(Subtree));
|
||||
array_reserve(&new_iterator.subtrees, (uint32_t)ts_subtree_alloc_size(goal_subtree_count) / sizeof(Subtree));
|
||||
}
|
||||
|
||||
array_push(&self->iterators, iterator);
|
||||
array_push(&self->iterators, new_iterator);
|
||||
|
||||
while (self->iterators.size > 0) {
|
||||
for (uint32_t i = 0, size = self->iterators.size; i < size; i++) {
|
||||
|
|
@ -505,7 +519,7 @@ inline StackAction pop_count_callback(void *payload, const StackIterator *iterat
|
|||
}
|
||||
|
||||
StackSliceArray ts_stack_pop_count(Stack *self, StackVersion version, uint32_t count) {
|
||||
return stack__iter(self, version, pop_count_callback, &count, count);
|
||||
return stack__iter(self, version, pop_count_callback, &count, (int)count);
|
||||
}
|
||||
|
||||
inline StackAction pop_pending_callback(void *payload, const StackIterator *iterator) {
|
||||
|
|
|
|||
|
|
@ -56,10 +56,10 @@ const char *ts_external_scanner_state_data(const ExternalScannerState *self) {
|
|||
}
|
||||
}
|
||||
|
||||
bool ts_external_scanner_state_eq(const ExternalScannerState *a, const char *buffer, unsigned length) {
|
||||
bool ts_external_scanner_state_eq(const ExternalScannerState *self, const char *buffer, unsigned length) {
|
||||
return
|
||||
a->length == length &&
|
||||
memcmp(ts_external_scanner_state_data(a), buffer, length) == 0;
|
||||
self->length == length &&
|
||||
memcmp(ts_external_scanner_state_data(self), buffer, length) == 0;
|
||||
}
|
||||
|
||||
// SubtreeArray
|
||||
|
|
@ -348,7 +348,7 @@ void ts_subtree_balance(Subtree self, SubtreePool *pool, const TSLanguage *langu
|
|||
Subtree child2 = ts_subtree_children(tree)[tree.ptr->child_count - 1];
|
||||
long repeat_delta = (long)ts_subtree_repeat_depth(child1) - (long)ts_subtree_repeat_depth(child2);
|
||||
if (repeat_delta > 0) {
|
||||
unsigned n = repeat_delta;
|
||||
unsigned n = (unsigned)repeat_delta;
|
||||
for (unsigned i = n / 2; i > 0; i /= 2) {
|
||||
ts_subtree__compress(tree, i, language, &pool->tree_stack);
|
||||
n -= i;
|
||||
|
|
@ -376,7 +376,7 @@ void ts_subtree_summarize_children(
|
|||
self.ptr->visible_child_count = 0;
|
||||
self.ptr->error_cost = 0;
|
||||
self.ptr->repeat_depth = 0;
|
||||
self.ptr->node_count = 1;
|
||||
self.ptr->visible_descendant_count = 0;
|
||||
self.ptr->has_external_tokens = false;
|
||||
self.ptr->depends_on_column = false;
|
||||
self.ptr->has_external_scanner_state_change = false;
|
||||
|
|
@ -435,14 +435,16 @@ void ts_subtree_summarize_children(
|
|||
}
|
||||
|
||||
self.ptr->dynamic_precedence += ts_subtree_dynamic_precedence(child);
|
||||
self.ptr->node_count += ts_subtree_node_count(child);
|
||||
self.ptr->visible_descendant_count += ts_subtree_visible_descendant_count(child);
|
||||
|
||||
if (alias_sequence && alias_sequence[structural_index] != 0 && !ts_subtree_extra(child)) {
|
||||
self.ptr->visible_descendant_count++;
|
||||
self.ptr->visible_child_count++;
|
||||
if (ts_language_symbol_metadata(language, alias_sequence[structural_index]).named) {
|
||||
self.ptr->named_child_count++;
|
||||
}
|
||||
} else if (ts_subtree_visible(child)) {
|
||||
self.ptr->visible_descendant_count++;
|
||||
self.ptr->visible_child_count++;
|
||||
if (ts_subtree_named(child)) self.ptr->named_child_count++;
|
||||
} else if (grandchild_count > 0) {
|
||||
|
|
@ -513,7 +515,7 @@ MutableSubtree ts_subtree_new_node(
|
|||
size_t new_byte_size = ts_subtree_alloc_size(children->size);
|
||||
if (children->capacity * sizeof(Subtree) < new_byte_size) {
|
||||
children->contents = ts_realloc(children->contents, new_byte_size);
|
||||
children->capacity = new_byte_size / sizeof(Subtree);
|
||||
children->capacity = (uint32_t)(new_byte_size / sizeof(Subtree));
|
||||
}
|
||||
SubtreeHeapData *data = (SubtreeHeapData *)&children->contents[children->size];
|
||||
|
||||
|
|
@ -529,7 +531,7 @@ MutableSubtree ts_subtree_new_node(
|
|||
.fragile_right = fragile,
|
||||
.is_keyword = false,
|
||||
{{
|
||||
.node_count = 0,
|
||||
.visible_descendant_count = 0,
|
||||
.production_id = production_id,
|
||||
.first_leaf = {.symbol = 0, .parse_state = 0},
|
||||
}}
|
||||
|
|
@ -641,24 +643,24 @@ static inline void ts_subtree_set_has_changes(MutableSubtree *self) {
|
|||
}
|
||||
}
|
||||
|
||||
Subtree ts_subtree_edit(Subtree self, const TSInputEdit *edit, SubtreePool *pool) {
|
||||
Subtree ts_subtree_edit(Subtree self, const TSInputEdit *input_edit, SubtreePool *pool) {
|
||||
typedef struct {
|
||||
Subtree *tree;
|
||||
Edit edit;
|
||||
} StackEntry;
|
||||
} EditEntry;
|
||||
|
||||
Array(StackEntry) stack = array_new();
|
||||
array_push(&stack, ((StackEntry) {
|
||||
Array(EditEntry) stack = array_new();
|
||||
array_push(&stack, ((EditEntry) {
|
||||
.tree = &self,
|
||||
.edit = (Edit) {
|
||||
.start = {edit->start_byte, edit->start_point},
|
||||
.old_end = {edit->old_end_byte, edit->old_end_point},
|
||||
.new_end = {edit->new_end_byte, edit->new_end_point},
|
||||
.start = {input_edit->start_byte, input_edit->start_point},
|
||||
.old_end = {input_edit->old_end_byte, input_edit->old_end_point},
|
||||
.new_end = {input_edit->new_end_byte, input_edit->new_end_point},
|
||||
},
|
||||
}));
|
||||
|
||||
while (stack.size) {
|
||||
StackEntry entry = array_pop(&stack);
|
||||
EditEntry entry = array_pop(&stack);
|
||||
Edit edit = entry.edit;
|
||||
bool is_noop = edit.old_end.bytes == edit.start.bytes && edit.new_end.bytes == edit.start.bytes;
|
||||
bool is_pure_insertion = edit.old_end.bytes == edit.start.bytes;
|
||||
|
|
@ -786,7 +788,7 @@ Subtree ts_subtree_edit(Subtree self, const TSInputEdit *edit, SubtreePool *pool
|
|||
}
|
||||
|
||||
// Queue processing of this child's subtree.
|
||||
array_push(&stack, ((StackEntry) {
|
||||
array_push(&stack, ((EditEntry) {
|
||||
.tree = child,
|
||||
.edit = child_edit,
|
||||
}));
|
||||
|
|
@ -811,24 +813,24 @@ Subtree ts_subtree_last_external_token(Subtree tree) {
|
|||
return tree;
|
||||
}
|
||||
|
||||
static size_t ts_subtree__write_char_to_string(char *s, size_t n, int32_t c) {
|
||||
if (c == -1)
|
||||
return snprintf(s, n, "INVALID");
|
||||
else if (c == '\0')
|
||||
return snprintf(s, n, "'\\0'");
|
||||
else if (c == '\n')
|
||||
return snprintf(s, n, "'\\n'");
|
||||
else if (c == '\t')
|
||||
return snprintf(s, n, "'\\t'");
|
||||
else if (c == '\r')
|
||||
return snprintf(s, n, "'\\r'");
|
||||
else if (0 < c && c < 128 && isprint(c))
|
||||
return snprintf(s, n, "'%c'", c);
|
||||
static size_t ts_subtree__write_char_to_string(char *str, size_t n, int32_t chr) {
|
||||
if (chr == -1)
|
||||
return snprintf(str, n, "INVALID");
|
||||
else if (chr == '\0')
|
||||
return snprintf(str, n, "'\\0'");
|
||||
else if (chr == '\n')
|
||||
return snprintf(str, n, "'\\n'");
|
||||
else if (chr == '\t')
|
||||
return snprintf(str, n, "'\\t'");
|
||||
else if (chr == '\r')
|
||||
return snprintf(str, n, "'\\r'");
|
||||
else if (0 < chr && chr < 128 && isprint(chr))
|
||||
return snprintf(str, n, "'%c'", chr);
|
||||
else
|
||||
return snprintf(s, n, "%d", c);
|
||||
return snprintf(str, n, "%d", chr);
|
||||
}
|
||||
|
||||
static const char *ROOT_FIELD = "__ROOT__";
|
||||
static const char *const ROOT_FIELD = "__ROOT__";
|
||||
|
||||
static size_t ts_subtree__write_to_string(
|
||||
Subtree self, char *string, size_t limit,
|
||||
|
|
@ -900,17 +902,17 @@ static size_t ts_subtree__write_to_string(
|
|||
0, false, NULL
|
||||
);
|
||||
} else {
|
||||
TSSymbol alias_symbol = alias_sequence
|
||||
TSSymbol subtree_alias_symbol = alias_sequence
|
||||
? alias_sequence[structural_child_index]
|
||||
: 0;
|
||||
bool alias_is_named = alias_symbol
|
||||
? ts_language_symbol_metadata(language, alias_symbol).named
|
||||
bool subtree_alias_is_named = subtree_alias_symbol
|
||||
? ts_language_symbol_metadata(language, subtree_alias_symbol).named
|
||||
: false;
|
||||
|
||||
const char *child_field_name = is_visible ? NULL : field_name;
|
||||
for (const TSFieldMapEntry *i = field_map; i < field_map_end; i++) {
|
||||
if (!i->inherited && i->child_index == structural_child_index) {
|
||||
child_field_name = language->field_names[i->field_id];
|
||||
for (const TSFieldMapEntry *map = field_map; map < field_map_end; map++) {
|
||||
if (!map->inherited && map->child_index == structural_child_index) {
|
||||
child_field_name = language->field_names[map->field_id];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -918,7 +920,7 @@ static size_t ts_subtree__write_to_string(
|
|||
cursor += ts_subtree__write_to_string(
|
||||
child, *writer, limit,
|
||||
language, include_all,
|
||||
alias_symbol, alias_is_named, child_field_name
|
||||
subtree_alias_symbol, subtree_alias_is_named, child_field_name
|
||||
);
|
||||
structural_child_index++;
|
||||
}
|
||||
|
|
@ -969,6 +971,7 @@ void ts_subtree__print_dot_graph(const Subtree *self, uint32_t start_offset,
|
|||
"error-cost: %u\n"
|
||||
"has-changes: %u\n"
|
||||
"depends-on-column: %u\n"
|
||||
"descendant-count: %u\n"
|
||||
"repeat-depth: %u\n"
|
||||
"lookahead-bytes: %u",
|
||||
start_offset, end_offset,
|
||||
|
|
@ -976,6 +979,7 @@ void ts_subtree__print_dot_graph(const Subtree *self, uint32_t start_offset,
|
|||
ts_subtree_error_cost(*self),
|
||||
ts_subtree_has_changes(*self),
|
||||
ts_subtree_depends_on_column(*self),
|
||||
ts_subtree_visible_descendant_count(*self),
|
||||
ts_subtree_repeat_depth(*self),
|
||||
ts_subtree_lookahead_bytes(*self)
|
||||
);
|
||||
|
|
@ -992,12 +996,12 @@ void ts_subtree__print_dot_graph(const Subtree *self, uint32_t start_offset,
|
|||
ts_subtree_production_id(*self);
|
||||
for (uint32_t i = 0, n = ts_subtree_child_count(*self); i < n; i++) {
|
||||
const Subtree *child = &ts_subtree_children(*self)[i];
|
||||
TSSymbol alias_symbol = 0;
|
||||
TSSymbol subtree_alias_symbol = 0;
|
||||
if (!ts_subtree_extra(*child) && child_info_offset) {
|
||||
alias_symbol = language->alias_sequences[child_info_offset];
|
||||
subtree_alias_symbol = language->alias_sequences[child_info_offset];
|
||||
child_info_offset++;
|
||||
}
|
||||
ts_subtree__print_dot_graph(child, child_start_offset, language, alias_symbol, f);
|
||||
ts_subtree__print_dot_graph(child, child_start_offset, language, subtree_alias_symbol, f);
|
||||
fprintf(f, "tree_%p -> tree_%p [tooltip=%u]\n", (void *)self, (void *)child, i);
|
||||
child_start_offset += ts_subtree_total_bytes(*child);
|
||||
}
|
||||
|
|
@ -1024,12 +1028,12 @@ const ExternalScannerState *ts_subtree_external_scanner_state(Subtree self) {
|
|||
}
|
||||
}
|
||||
|
||||
bool ts_subtree_external_scanner_state_eq(Subtree a, Subtree b) {
|
||||
const ExternalScannerState *state_a = ts_subtree_external_scanner_state(a);
|
||||
const ExternalScannerState *state_b = ts_subtree_external_scanner_state(b);
|
||||
bool ts_subtree_external_scanner_state_eq(Subtree self, Subtree other) {
|
||||
const ExternalScannerState *state_self = ts_subtree_external_scanner_state(self);
|
||||
const ExternalScannerState *state_other = ts_subtree_external_scanner_state(other);
|
||||
return ts_external_scanner_state_eq(
|
||||
state_a,
|
||||
ts_external_scanner_state_data(state_b),
|
||||
state_b->length
|
||||
state_self,
|
||||
ts_external_scanner_state_data(state_other),
|
||||
state_other->length
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -135,7 +135,7 @@ typedef struct {
|
|||
struct {
|
||||
uint32_t visible_child_count;
|
||||
uint32_t named_child_count;
|
||||
uint32_t node_count;
|
||||
uint32_t visible_descendant_count;
|
||||
int32_t dynamic_precedence;
|
||||
uint16_t repeat_depth;
|
||||
uint16_t production_id;
|
||||
|
|
@ -175,7 +175,7 @@ typedef struct {
|
|||
|
||||
void ts_external_scanner_state_init(ExternalScannerState *, const char *, unsigned);
|
||||
const char *ts_external_scanner_state_data(const ExternalScannerState *);
|
||||
bool ts_external_scanner_state_eq(const ExternalScannerState *a, const char *, unsigned);
|
||||
bool ts_external_scanner_state_eq(const ExternalScannerState *self, const char *, unsigned);
|
||||
void ts_external_scanner_state_delete(ExternalScannerState *self);
|
||||
|
||||
void ts_subtree_array_copy(SubtreeArray, SubtreeArray *);
|
||||
|
|
@ -212,7 +212,7 @@ Subtree ts_subtree_last_external_token(Subtree);
|
|||
const ExternalScannerState *ts_subtree_external_scanner_state(Subtree self);
|
||||
bool ts_subtree_external_scanner_state_eq(Subtree, Subtree);
|
||||
|
||||
#define SUBTREE_GET(self, name) (self.data.is_inline ? self.data.name : self.ptr->name)
|
||||
#define SUBTREE_GET(self, name) ((self).data.is_inline ? (self).data.name : (self).ptr->name)
|
||||
|
||||
static inline TSSymbol ts_subtree_symbol(Subtree self) { return SUBTREE_GET(self, symbol); }
|
||||
static inline bool ts_subtree_visible(Subtree self) { return SUBTREE_GET(self, visible); }
|
||||
|
|
@ -291,8 +291,16 @@ static inline uint32_t ts_subtree_repeat_depth(Subtree self) {
|
|||
return self.data.is_inline ? 0 : self.ptr->repeat_depth;
|
||||
}
|
||||
|
||||
static inline uint32_t ts_subtree_node_count(Subtree self) {
|
||||
return (self.data.is_inline || self.ptr->child_count == 0) ? 1 : self.ptr->node_count;
|
||||
static inline uint32_t ts_subtree_is_repetition(Subtree self) {
|
||||
return self.data.is_inline
|
||||
? 0
|
||||
: !self.ptr->named && !self.ptr->visible && self.ptr->child_count != 0;
|
||||
}
|
||||
|
||||
static inline uint32_t ts_subtree_visible_descendant_count(Subtree self) {
|
||||
return (self.data.is_inline || self.ptr->child_count == 0)
|
||||
? 0
|
||||
: self.ptr->visible_descendant_count;
|
||||
}
|
||||
|
||||
static inline uint32_t ts_subtree_visible_child_count(Subtree self) {
|
||||
|
|
|
|||
|
|
@ -98,23 +98,23 @@ TSRange *ts_tree_included_ranges(const TSTree *self, uint32_t *length) {
|
|||
return ranges;
|
||||
}
|
||||
|
||||
TSRange *ts_tree_get_changed_ranges(const TSTree *self, const TSTree *other, uint32_t *count) {
|
||||
TSRange *ts_tree_get_changed_ranges(const TSTree *old_tree, const TSTree *new_tree, uint32_t *length) {
|
||||
TreeCursor cursor1 = {NULL, array_new()};
|
||||
TreeCursor cursor2 = {NULL, array_new()};
|
||||
ts_tree_cursor_init(&cursor1, ts_tree_root_node(self));
|
||||
ts_tree_cursor_init(&cursor2, ts_tree_root_node(other));
|
||||
ts_tree_cursor_init(&cursor1, ts_tree_root_node(old_tree));
|
||||
ts_tree_cursor_init(&cursor2, ts_tree_root_node(new_tree));
|
||||
|
||||
TSRangeArray included_range_differences = array_new();
|
||||
ts_range_array_get_changed_ranges(
|
||||
self->included_ranges, self->included_range_count,
|
||||
other->included_ranges, other->included_range_count,
|
||||
old_tree->included_ranges, old_tree->included_range_count,
|
||||
new_tree->included_ranges, new_tree->included_range_count,
|
||||
&included_range_differences
|
||||
);
|
||||
|
||||
TSRange *result;
|
||||
*count = ts_subtree_get_changed_ranges(
|
||||
&self->root, &other->root, &cursor1, &cursor2,
|
||||
self->language, &included_range_differences, &result
|
||||
*length = ts_subtree_get_changed_ranges(
|
||||
&old_tree->root, &new_tree->root, &cursor1, &cursor2,
|
||||
old_tree->language, &included_range_differences, &result
|
||||
);
|
||||
|
||||
array_delete(&included_range_differences);
|
||||
|
|
@ -123,6 +123,21 @@ TSRange *ts_tree_get_changed_ranges(const TSTree *self, const TSTree *other, uin
|
|||
return result;
|
||||
}
|
||||
|
||||
void ts_tree_print_dot_graph(const TSTree *self, FILE *file) {
|
||||
ts_subtree_print_dot_graph(self->root, self->language, file);
|
||||
#ifdef _WIN32
|
||||
|
||||
void ts_tree_print_dot_graph(const TSTree *self, int fd) {
|
||||
(void)self;
|
||||
(void)fd;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
void ts_tree_print_dot_graph(const TSTree *self, int file_descriptor) {
|
||||
FILE *file = fdopen(dup(file_descriptor), "a");
|
||||
ts_subtree_print_dot_graph(self->root, self->language, file);
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
#ifndef TREE_SITTER_TREE_H_
|
||||
#define TREE_SITTER_TREE_H_
|
||||
|
||||
#include "./subtree.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -10,26 +10,50 @@ typedef struct {
|
|||
Length position;
|
||||
uint32_t child_index;
|
||||
uint32_t structural_child_index;
|
||||
uint32_t descendant_index;
|
||||
const TSSymbol *alias_sequence;
|
||||
} CursorChildIterator;
|
||||
|
||||
// CursorChildIterator
|
||||
|
||||
static inline bool ts_tree_cursor_is_entry_visible(const TreeCursor *self, uint32_t index) {
|
||||
TreeCursorEntry *entry = &self->stack.contents[index];
|
||||
if (index == 0 || ts_subtree_visible(*entry->subtree)) {
|
||||
return true;
|
||||
} else if (!ts_subtree_extra(*entry->subtree)) {
|
||||
TreeCursorEntry *parent_entry = &self->stack.contents[index - 1];
|
||||
return ts_language_alias_at(
|
||||
self->tree->language,
|
||||
parent_entry->subtree->ptr->production_id,
|
||||
entry->structural_child_index
|
||||
);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static inline CursorChildIterator ts_tree_cursor_iterate_children(const TreeCursor *self) {
|
||||
TreeCursorEntry *last_entry = array_back(&self->stack);
|
||||
if (ts_subtree_child_count(*last_entry->subtree) == 0) {
|
||||
return (CursorChildIterator) {NULL_SUBTREE, self->tree, length_zero(), 0, 0, NULL};
|
||||
return (CursorChildIterator) {NULL_SUBTREE, self->tree, length_zero(), 0, 0, 0, NULL};
|
||||
}
|
||||
const TSSymbol *alias_sequence = ts_language_alias_sequence(
|
||||
self->tree->language,
|
||||
last_entry->subtree->ptr->production_id
|
||||
);
|
||||
|
||||
uint32_t descendant_index = last_entry->descendant_index;
|
||||
if (ts_tree_cursor_is_entry_visible(self, self->stack.size - 1)) {
|
||||
descendant_index += 1;
|
||||
}
|
||||
|
||||
return (CursorChildIterator) {
|
||||
.tree = self->tree,
|
||||
.parent = *last_entry->subtree,
|
||||
.position = last_entry->position,
|
||||
.child_index = 0,
|
||||
.structural_child_index = 0,
|
||||
.descendant_index = descendant_index,
|
||||
.alias_sequence = alias_sequence,
|
||||
};
|
||||
}
|
||||
|
|
@ -46,14 +70,22 @@ static inline bool ts_tree_cursor_child_iterator_next(
|
|||
.position = self->position,
|
||||
.child_index = self->child_index,
|
||||
.structural_child_index = self->structural_child_index,
|
||||
.descendant_index = self->descendant_index,
|
||||
};
|
||||
*visible = ts_subtree_visible(*child);
|
||||
bool extra = ts_subtree_extra(*child);
|
||||
if (!extra && self->alias_sequence) {
|
||||
*visible |= self->alias_sequence[self->structural_child_index];
|
||||
if (!extra) {
|
||||
if (self->alias_sequence) {
|
||||
*visible |= self->alias_sequence[self->structural_child_index];
|
||||
}
|
||||
self->structural_child_index++;
|
||||
}
|
||||
|
||||
self->descendant_index += ts_subtree_visible_descendant_count(*child);
|
||||
if (*visible) {
|
||||
self->descendant_index += 1;
|
||||
}
|
||||
|
||||
self->position = length_add(self->position, ts_subtree_size(*child));
|
||||
self->child_index++;
|
||||
|
||||
|
|
@ -65,6 +97,57 @@ static inline bool ts_tree_cursor_child_iterator_next(
|
|||
return true;
|
||||
}
|
||||
|
||||
// Return a position that, when `b` is added to it, yields `a`. This
|
||||
// can only be computed if `b` has zero rows. Otherwise, this function
|
||||
// returns `LENGTH_UNDEFINED`, and the caller needs to recompute
|
||||
// the position some other way.
|
||||
static inline Length length_backtrack(Length a, Length b) {
|
||||
if (length_is_undefined(a) || b.extent.row != 0) {
|
||||
return LENGTH_UNDEFINED;
|
||||
}
|
||||
|
||||
Length result;
|
||||
result.bytes = a.bytes - b.bytes;
|
||||
result.extent.row = a.extent.row;
|
||||
result.extent.column = a.extent.column - b.extent.column;
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline bool ts_tree_cursor_child_iterator_previous(
|
||||
CursorChildIterator *self,
|
||||
TreeCursorEntry *result,
|
||||
bool *visible
|
||||
) {
|
||||
// this is mostly a reverse `ts_tree_cursor_child_iterator_next` taking into
|
||||
// account unsigned underflow
|
||||
if (!self->parent.ptr || (int8_t)self->child_index == -1) return false;
|
||||
const Subtree *child = &ts_subtree_children(self->parent)[self->child_index];
|
||||
*result = (TreeCursorEntry) {
|
||||
.subtree = child,
|
||||
.position = self->position,
|
||||
.child_index = self->child_index,
|
||||
.structural_child_index = self->structural_child_index,
|
||||
};
|
||||
*visible = ts_subtree_visible(*child);
|
||||
bool extra = ts_subtree_extra(*child);
|
||||
if (!extra && self->alias_sequence) {
|
||||
*visible |= self->alias_sequence[self->structural_child_index];
|
||||
self->structural_child_index--;
|
||||
}
|
||||
|
||||
self->position = length_backtrack(self->position, ts_subtree_padding(*child));
|
||||
self->child_index--;
|
||||
|
||||
// unsigned can underflow so compare it to child_count
|
||||
if (self->child_index < self->parent.ptr->child_count) {
|
||||
Subtree previous_child = ts_subtree_children(self->parent)[self->child_index];
|
||||
Length size = ts_subtree_size(previous_child);
|
||||
self->position = length_backtrack(self->position, size);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// TSTreeCursor - lifecycle
|
||||
|
||||
TSTreeCursor ts_tree_cursor_new(TSNode node) {
|
||||
|
|
@ -88,6 +171,7 @@ void ts_tree_cursor_init(TreeCursor *self, TSNode node) {
|
|||
},
|
||||
.child_index = 0,
|
||||
.structural_child_index = 0,
|
||||
.descendant_index = 0,
|
||||
}));
|
||||
}
|
||||
|
||||
|
|
@ -98,74 +182,84 @@ void ts_tree_cursor_delete(TSTreeCursor *_self) {
|
|||
|
||||
// TSTreeCursor - walking the tree
|
||||
|
||||
bool ts_tree_cursor_goto_first_child(TSTreeCursor *_self) {
|
||||
TreeCursorStep ts_tree_cursor_goto_first_child_internal(TSTreeCursor *_self) {
|
||||
TreeCursor *self = (TreeCursor *)_self;
|
||||
|
||||
bool did_descend;
|
||||
do {
|
||||
did_descend = false;
|
||||
|
||||
bool visible;
|
||||
TreeCursorEntry entry;
|
||||
CursorChildIterator iterator = ts_tree_cursor_iterate_children(self);
|
||||
while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) {
|
||||
if (visible) {
|
||||
array_push(&self->stack, entry);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (ts_subtree_visible_child_count(*entry.subtree) > 0) {
|
||||
array_push(&self->stack, entry);
|
||||
did_descend = true;
|
||||
break;
|
||||
}
|
||||
bool visible;
|
||||
TreeCursorEntry entry;
|
||||
CursorChildIterator iterator = ts_tree_cursor_iterate_children(self);
|
||||
while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) {
|
||||
if (visible) {
|
||||
array_push(&self->stack, entry);
|
||||
return TreeCursorStepVisible;
|
||||
}
|
||||
} while (did_descend);
|
||||
if (ts_subtree_visible_child_count(*entry.subtree) > 0) {
|
||||
array_push(&self->stack, entry);
|
||||
return TreeCursorStepHidden;
|
||||
}
|
||||
}
|
||||
return TreeCursorStepNone;
|
||||
}
|
||||
|
||||
bool ts_tree_cursor_goto_first_child(TSTreeCursor *self) {
|
||||
for (;;) {
|
||||
switch (ts_tree_cursor_goto_first_child_internal(self)) {
|
||||
case TreeCursorStepHidden:
|
||||
continue;
|
||||
case TreeCursorStepVisible:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int64_t ts_tree_cursor_goto_first_child_for_byte(TSTreeCursor *_self, uint32_t goal_byte) {
|
||||
TreeCursorStep ts_tree_cursor_goto_last_child_internal(TSTreeCursor *_self) {
|
||||
TreeCursor *self = (TreeCursor *)_self;
|
||||
uint32_t initial_size = self->stack.size;
|
||||
uint32_t visible_child_index = 0;
|
||||
bool visible;
|
||||
TreeCursorEntry entry;
|
||||
CursorChildIterator iterator = ts_tree_cursor_iterate_children(self);
|
||||
if (!iterator.parent.ptr || iterator.parent.ptr->child_count == 0) return TreeCursorStepNone;
|
||||
|
||||
bool did_descend;
|
||||
do {
|
||||
did_descend = false;
|
||||
|
||||
bool visible;
|
||||
TreeCursorEntry entry;
|
||||
CursorChildIterator iterator = ts_tree_cursor_iterate_children(self);
|
||||
while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) {
|
||||
uint32_t end_byte = entry.position.bytes + ts_subtree_size(*entry.subtree).bytes;
|
||||
bool at_goal = end_byte >= goal_byte;
|
||||
uint32_t visible_child_count = ts_subtree_visible_child_count(*entry.subtree);
|
||||
|
||||
if (at_goal) {
|
||||
if (visible) {
|
||||
array_push(&self->stack, entry);
|
||||
return visible_child_index;
|
||||
}
|
||||
|
||||
if (visible_child_count > 0) {
|
||||
array_push(&self->stack, entry);
|
||||
did_descend = true;
|
||||
break;
|
||||
}
|
||||
} else if (visible) {
|
||||
visible_child_index++;
|
||||
} else {
|
||||
visible_child_index += visible_child_count;
|
||||
}
|
||||
TreeCursorEntry last_entry;
|
||||
TreeCursorStep last_step = TreeCursorStepNone;
|
||||
while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) {
|
||||
if (visible) {
|
||||
last_entry = entry;
|
||||
last_step = TreeCursorStepVisible;
|
||||
}
|
||||
} while (did_descend);
|
||||
else if (ts_subtree_visible_child_count(*entry.subtree) > 0) {
|
||||
last_entry = entry;
|
||||
last_step = TreeCursorStepHidden;
|
||||
}
|
||||
}
|
||||
if (last_entry.subtree) {
|
||||
array_push(&self->stack, last_entry);
|
||||
return last_step;
|
||||
}
|
||||
|
||||
self->stack.size = initial_size;
|
||||
return -1;
|
||||
return TreeCursorStepNone;
|
||||
}
|
||||
|
||||
int64_t ts_tree_cursor_goto_first_child_for_point(TSTreeCursor *_self, TSPoint goal_point) {
|
||||
bool ts_tree_cursor_goto_last_child(TSTreeCursor *self) {
|
||||
for (;;) {
|
||||
switch (ts_tree_cursor_goto_last_child_internal(self)) {
|
||||
case TreeCursorStepHidden:
|
||||
continue;
|
||||
case TreeCursorStepVisible:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline int64_t ts_tree_cursor_goto_first_child_for_byte_and_point(
|
||||
TSTreeCursor *_self,
|
||||
uint32_t goal_byte,
|
||||
TSPoint goal_point
|
||||
) {
|
||||
TreeCursor *self = (TreeCursor *)_self;
|
||||
uint32_t initial_size = self->stack.size;
|
||||
uint32_t visible_child_index = 0;
|
||||
|
|
@ -178,8 +272,8 @@ int64_t ts_tree_cursor_goto_first_child_for_point(TSTreeCursor *_self, TSPoint g
|
|||
TreeCursorEntry entry;
|
||||
CursorChildIterator iterator = ts_tree_cursor_iterate_children(self);
|
||||
while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) {
|
||||
TSPoint end_point = point_add(entry.position.extent, ts_subtree_size(*entry.subtree).extent);
|
||||
bool at_goal = point_gte(end_point, goal_point);
|
||||
Length entry_end = length_add(entry.position, ts_subtree_size(*entry.subtree));
|
||||
bool at_goal = entry_end.bytes >= goal_byte && point_gte(entry_end.extent, goal_point);
|
||||
uint32_t visible_child_count = ts_subtree_visible_child_count(*entry.subtree);
|
||||
if (at_goal) {
|
||||
if (visible) {
|
||||
|
|
@ -203,7 +297,17 @@ int64_t ts_tree_cursor_goto_first_child_for_point(TSTreeCursor *_self, TSPoint g
|
|||
return -1;
|
||||
}
|
||||
|
||||
bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *_self) {
|
||||
int64_t ts_tree_cursor_goto_first_child_for_byte(TSTreeCursor *self, uint32_t goal_byte) {
|
||||
return ts_tree_cursor_goto_first_child_for_byte_and_point(self, goal_byte, POINT_ZERO);
|
||||
}
|
||||
|
||||
int64_t ts_tree_cursor_goto_first_child_for_point(TSTreeCursor *self, TSPoint goal_point) {
|
||||
return ts_tree_cursor_goto_first_child_for_byte_and_point(self, 0, goal_point);
|
||||
}
|
||||
|
||||
TreeCursorStep ts_tree_cursor_goto_sibling_internal(
|
||||
TSTreeCursor *_self,
|
||||
bool (*advance)(CursorChildIterator *, TreeCursorEntry *, bool *)) {
|
||||
TreeCursor *self = (TreeCursor *)_self;
|
||||
uint32_t initial_size = self->stack.size;
|
||||
|
||||
|
|
@ -213,52 +317,161 @@ bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *_self) {
|
|||
iterator.child_index = entry.child_index;
|
||||
iterator.structural_child_index = entry.structural_child_index;
|
||||
iterator.position = entry.position;
|
||||
iterator.descendant_index = entry.descendant_index;
|
||||
|
||||
bool visible = false;
|
||||
ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible);
|
||||
advance(&iterator, &entry, &visible);
|
||||
if (visible && self->stack.size + 1 < initial_size) break;
|
||||
|
||||
while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) {
|
||||
while (advance(&iterator, &entry, &visible)) {
|
||||
if (visible) {
|
||||
array_push(&self->stack, entry);
|
||||
return true;
|
||||
return TreeCursorStepVisible;
|
||||
}
|
||||
|
||||
if (ts_subtree_visible_child_count(*entry.subtree)) {
|
||||
array_push(&self->stack, entry);
|
||||
ts_tree_cursor_goto_first_child(_self);
|
||||
return true;
|
||||
return TreeCursorStepHidden;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self->stack.size = initial_size;
|
||||
return false;
|
||||
return TreeCursorStepNone;
|
||||
}
|
||||
|
||||
TreeCursorStep ts_tree_cursor_goto_next_sibling_internal(TSTreeCursor *_self) {
|
||||
return ts_tree_cursor_goto_sibling_internal(_self, ts_tree_cursor_child_iterator_next);
|
||||
}
|
||||
|
||||
bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *self) {
|
||||
switch (ts_tree_cursor_goto_next_sibling_internal(self)) {
|
||||
case TreeCursorStepHidden:
|
||||
ts_tree_cursor_goto_first_child(self);
|
||||
return true;
|
||||
case TreeCursorStepVisible:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
TreeCursorStep ts_tree_cursor_goto_previous_sibling_internal(TSTreeCursor *_self) {
|
||||
// since subtracting across row loses column information, we may have to
|
||||
// restore it
|
||||
TreeCursor *self = (TreeCursor *)_self;
|
||||
|
||||
// for that, save current position before traversing
|
||||
Length position = array_back(&self->stack)->position;
|
||||
TreeCursorStep step = ts_tree_cursor_goto_sibling_internal(
|
||||
_self, ts_tree_cursor_child_iterator_previous);
|
||||
if (step == TreeCursorStepNone)
|
||||
return step;
|
||||
|
||||
// if length is already valid, there's no need to recompute it
|
||||
if (!length_is_undefined(array_back(&self->stack)->position))
|
||||
return step;
|
||||
|
||||
// restore position from the parent node
|
||||
const TreeCursorEntry *parent = &self->stack.contents[self->stack.size - 2];
|
||||
position = parent->position;
|
||||
uint32_t child_index = array_back(&self->stack)->child_index;
|
||||
const Subtree *children = ts_subtree_children((*(parent->subtree)));
|
||||
|
||||
if (child_index > 0) {
|
||||
// skip first child padding since its position should match the position of the parent
|
||||
position = length_add(position, ts_subtree_size(children[0]));
|
||||
for (uint32_t i = 1; i < child_index; ++i) {
|
||||
position = length_add(position, ts_subtree_total_size(children[i]));
|
||||
}
|
||||
position = length_add(position, ts_subtree_padding(children[child_index]));
|
||||
}
|
||||
|
||||
array_back(&self->stack)->position = position;
|
||||
|
||||
return step;
|
||||
}
|
||||
|
||||
bool ts_tree_cursor_goto_previous_sibling(TSTreeCursor *self) {
|
||||
switch (ts_tree_cursor_goto_previous_sibling_internal(self)) {
|
||||
case TreeCursorStepHidden:
|
||||
ts_tree_cursor_goto_last_child(self);
|
||||
return true;
|
||||
case TreeCursorStepVisible:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ts_tree_cursor_goto_parent(TSTreeCursor *_self) {
|
||||
TreeCursor *self = (TreeCursor *)_self;
|
||||
for (unsigned i = self->stack.size - 2; i + 1 > 0; i--) {
|
||||
TreeCursorEntry *entry = &self->stack.contents[i];
|
||||
if (ts_subtree_visible(*entry->subtree)) {
|
||||
if (ts_tree_cursor_is_entry_visible(self, i)) {
|
||||
self->stack.size = i + 1;
|
||||
return true;
|
||||
}
|
||||
if (i > 0 && !ts_subtree_extra(*entry->subtree)) {
|
||||
TreeCursorEntry *parent_entry = &self->stack.contents[i - 1];
|
||||
if (ts_language_alias_at(
|
||||
self->tree->language,
|
||||
parent_entry->subtree->ptr->production_id,
|
||||
entry->structural_child_index
|
||||
)) {
|
||||
self->stack.size = i + 1;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void ts_tree_cursor_goto_descendant(
|
||||
TSTreeCursor *_self,
|
||||
uint32_t goal_descendant_index
|
||||
) {
|
||||
TreeCursor *self = (TreeCursor *)_self;
|
||||
|
||||
// Ascend to the lowest ancestor that contains the goal node.
|
||||
for (;;) {
|
||||
uint32_t i = self->stack.size - 1;
|
||||
TreeCursorEntry *entry = &self->stack.contents[i];
|
||||
uint32_t next_descendant_index =
|
||||
entry->descendant_index +
|
||||
(ts_tree_cursor_is_entry_visible(self, i) ? 1 : 0) +
|
||||
ts_subtree_visible_descendant_count(*entry->subtree);
|
||||
if (
|
||||
(entry->descendant_index <= goal_descendant_index) &&
|
||||
(next_descendant_index > goal_descendant_index)
|
||||
) {
|
||||
break;
|
||||
} else if (self->stack.size <= 1) {
|
||||
return;
|
||||
} else {
|
||||
self->stack.size--;
|
||||
}
|
||||
}
|
||||
|
||||
// Descend to the goal node.
|
||||
bool did_descend = true;
|
||||
do {
|
||||
did_descend = false;
|
||||
bool visible;
|
||||
TreeCursorEntry entry;
|
||||
CursorChildIterator iterator = ts_tree_cursor_iterate_children(self);
|
||||
if (iterator.descendant_index > goal_descendant_index) {
|
||||
return;
|
||||
}
|
||||
|
||||
while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) {
|
||||
if (iterator.descendant_index > goal_descendant_index) {
|
||||
array_push(&self->stack, entry);
|
||||
if (visible && entry.descendant_index == goal_descendant_index) {
|
||||
return;
|
||||
} else {
|
||||
did_descend = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (did_descend);
|
||||
}
|
||||
|
||||
uint32_t ts_tree_cursor_current_descendant_index(const TSTreeCursor *_self) {
|
||||
const TreeCursor *self = (const TreeCursor *)_self;
|
||||
TreeCursorEntry *last_entry = array_back(&self->stack);
|
||||
return last_entry->descendant_index;
|
||||
}
|
||||
|
||||
TSNode ts_tree_cursor_current_node(const TSTreeCursor *_self) {
|
||||
const TreeCursor *self = (const TreeCursor *)_self;
|
||||
TreeCursorEntry *last_entry = array_back(&self->stack);
|
||||
|
|
@ -377,9 +590,9 @@ void ts_tree_cursor_current_status(
|
|||
|
||||
// Look for a field name associated with the current node.
|
||||
if (!*field_id) {
|
||||
for (const TSFieldMapEntry *i = field_map; i < field_map_end; i++) {
|
||||
if (!i->inherited && i->child_index == entry->structural_child_index) {
|
||||
*field_id = i->field_id;
|
||||
for (const TSFieldMapEntry *map = field_map; map < field_map_end; map++) {
|
||||
if (!map->inherited && map->child_index == entry->structural_child_index) {
|
||||
*field_id = map->field_id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -387,10 +600,10 @@ void ts_tree_cursor_current_status(
|
|||
|
||||
// Determine if the current node can have later siblings with the same field name.
|
||||
if (*field_id) {
|
||||
for (const TSFieldMapEntry *i = field_map; i < field_map_end; i++) {
|
||||
for (const TSFieldMapEntry *map = field_map; map < field_map_end; map++) {
|
||||
if (
|
||||
i->field_id == *field_id &&
|
||||
i->child_index > entry->structural_child_index
|
||||
map->field_id == *field_id &&
|
||||
map->child_index > entry->structural_child_index
|
||||
) {
|
||||
*can_have_later_siblings_with_this_field = true;
|
||||
break;
|
||||
|
|
@ -401,6 +614,17 @@ void ts_tree_cursor_current_status(
|
|||
}
|
||||
}
|
||||
|
||||
uint32_t ts_tree_cursor_current_depth(const TSTreeCursor *_self) {
|
||||
const TreeCursor *self = (const TreeCursor *)_self;
|
||||
uint32_t depth = 0;
|
||||
for (unsigned i = 1; i < self->stack.size; i++) {
|
||||
if (ts_tree_cursor_is_entry_visible(self, i)) {
|
||||
depth++;
|
||||
}
|
||||
}
|
||||
return depth;
|
||||
}
|
||||
|
||||
TSNode ts_tree_cursor_parent_node(const TSTreeCursor *_self) {
|
||||
const TreeCursor *self = (const TreeCursor *)_self;
|
||||
for (int i = (int)self->stack.size - 2; i >= 0; i--) {
|
||||
|
|
@ -437,17 +661,10 @@ TSFieldId ts_tree_cursor_current_field_id(const TSTreeCursor *_self) {
|
|||
TreeCursorEntry *parent_entry = &self->stack.contents[i - 1];
|
||||
|
||||
// Stop walking up when another visible node is found.
|
||||
if (i != self->stack.size - 1) {
|
||||
if (ts_subtree_visible(*entry->subtree)) break;
|
||||
if (
|
||||
!ts_subtree_extra(*entry->subtree) &&
|
||||
ts_language_alias_at(
|
||||
self->tree->language,
|
||||
parent_entry->subtree->ptr->production_id,
|
||||
entry->structural_child_index
|
||||
)
|
||||
) break;
|
||||
}
|
||||
if (
|
||||
i != self->stack.size - 1 &&
|
||||
ts_tree_cursor_is_entry_visible(self, i)
|
||||
) break;
|
||||
|
||||
if (ts_subtree_extra(*entry->subtree)) break;
|
||||
|
||||
|
|
@ -457,9 +674,9 @@ TSFieldId ts_tree_cursor_current_field_id(const TSTreeCursor *_self) {
|
|||
parent_entry->subtree->ptr->production_id,
|
||||
&field_map, &field_map_end
|
||||
);
|
||||
for (const TSFieldMapEntry *i = field_map; i < field_map_end; i++) {
|
||||
if (!i->inherited && i->child_index == entry->structural_child_index) {
|
||||
return i->field_id;
|
||||
for (const TSFieldMapEntry *map = field_map; map < field_map_end; map++) {
|
||||
if (!map->inherited && map->child_index == entry->structural_child_index) {
|
||||
return map->field_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -485,3 +702,11 @@ TSTreeCursor ts_tree_cursor_copy(const TSTreeCursor *_cursor) {
|
|||
array_push_all(©->stack, &cursor->stack);
|
||||
return res;
|
||||
}
|
||||
|
||||
void ts_tree_cursor_reset_to(TSTreeCursor *_dst, const TSTreeCursor *_src) {
|
||||
const TreeCursor *cursor = (const TreeCursor *)_src;
|
||||
TreeCursor *copy = (TreeCursor *)_dst;
|
||||
copy->tree = cursor->tree;
|
||||
array_clear(©->stack);
|
||||
array_push_all(©->stack, &cursor->stack);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ typedef struct {
|
|||
Length position;
|
||||
uint32_t child_index;
|
||||
uint32_t structural_child_index;
|
||||
uint32_t descendant_index;
|
||||
} TreeCursorEntry;
|
||||
|
||||
typedef struct {
|
||||
|
|
@ -15,6 +16,12 @@ typedef struct {
|
|||
Array(TreeCursorEntry) stack;
|
||||
} TreeCursor;
|
||||
|
||||
typedef enum {
|
||||
TreeCursorStepNone,
|
||||
TreeCursorStepHidden,
|
||||
TreeCursorStepVisible,
|
||||
} TreeCursorStep;
|
||||
|
||||
void ts_tree_cursor_init(TreeCursor *, TSNode);
|
||||
void ts_tree_cursor_current_status(
|
||||
const TSTreeCursor *,
|
||||
|
|
@ -26,6 +33,15 @@ void ts_tree_cursor_current_status(
|
|||
unsigned *
|
||||
);
|
||||
|
||||
TreeCursorStep ts_tree_cursor_goto_first_child_internal(TSTreeCursor *);
|
||||
TreeCursorStep ts_tree_cursor_goto_next_sibling_internal(TSTreeCursor *);
|
||||
|
||||
static inline Subtree ts_tree_cursor_current_subtree(const TSTreeCursor *_self) {
|
||||
const TreeCursor *self = (const TreeCursor *)_self;
|
||||
TreeCursorEntry *last_entry = array_back(&self->stack);
|
||||
return *last_entry->subtree;
|
||||
}
|
||||
|
||||
TSNode ts_tree_cursor_parent_node(const TSTreeCursor *);
|
||||
|
||||
#endif // TREE_SITTER_TREE_CURSOR_H_
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue