2015-07-08 17:34:21 -07:00
|
|
|
#include <assert.h>
|
2017-06-23 12:08:50 -07:00
|
|
|
#include <ctype.h>
|
2015-12-22 14:37:29 -08:00
|
|
|
#include <limits.h>
|
2014-09-01 14:08:07 -07:00
|
|
|
#include <stdbool.h>
|
2016-04-22 14:55:56 -07:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
2016-01-15 15:08:42 -08:00
|
|
|
#include "runtime/alloc.h"
|
2018-05-09 17:30:13 -07:00
|
|
|
#include "runtime/atomic.h"
|
2018-05-10 15:11:14 -07:00
|
|
|
#include "runtime/subtree.h"
|
2014-09-26 16:15:07 -07:00
|
|
|
#include "runtime/length.h"
|
2017-07-14 10:19:58 -07:00
|
|
|
#include "runtime/language.h"
|
2016-08-31 10:51:59 -07:00
|
|
|
#include "runtime/error_costs.h"
|
2018-09-13 16:13:49 -07:00
|
|
|
#include <stddef.h>
|
2014-03-24 00:34:13 -07:00
|
|
|
|
2018-05-10 12:04:18 -07:00
|
|
|
typedef struct {
|
|
|
|
|
Length start;
|
2018-05-17 11:14:51 -07:00
|
|
|
Length old_end;
|
|
|
|
|
Length new_end;
|
2018-05-10 12:04:18 -07:00
|
|
|
} Edit;
|
|
|
|
|
|
2016-06-27 14:39:12 -07:00
|
|
|
TSStateId TS_TREE_STATE_NONE = USHRT_MAX;
|
2015-12-22 14:37:29 -08:00
|
|
|
|
2018-09-13 16:13:49 -07:00
|
|
|
static const uint32_t MAX_TREE_POOL_SIZE = 0;
|
|
|
|
|
static const uint32_t SMALL_TREE_SIZE = offsetof(Subtree, children);
|
|
|
|
|
static const uint32_t LARGE_TREE_SIZE = sizeof(Subtree);
|
2018-05-10 22:22:37 -07:00
|
|
|
|
2018-05-11 12:57:41 -07:00
|
|
|
static const ExternalScannerState empty_state = {.length = 0, .short_data = {0}};
|
2018-05-10 22:22:37 -07:00
|
|
|
|
2018-05-11 12:57:41 -07:00
|
|
|
// ExternalScannerState
|
2017-10-05 17:32:21 -07:00
|
|
|
|
2018-05-11 12:57:41 -07:00
|
|
|
void ts_external_scanner_state_init(ExternalScannerState *self, const char *data, unsigned length) {
|
2017-07-17 17:12:36 -07:00
|
|
|
self->length = length;
|
|
|
|
|
if (length > sizeof(self->short_data)) {
|
|
|
|
|
self->long_data = ts_malloc(length);
|
2018-05-11 12:57:41 -07:00
|
|
|
memcpy(self->long_data, data, length);
|
2017-07-17 17:12:36 -07:00
|
|
|
} else {
|
2018-05-11 12:57:41 -07:00
|
|
|
memcpy(self->short_data, data, length);
|
2017-07-17 17:12:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-15 16:18:08 -07:00
|
|
|
ExternalScannerState ts_external_scanner_state_copy(const ExternalScannerState *self) {
|
|
|
|
|
ExternalScannerState result = *self;
|
|
|
|
|
if (self->length > sizeof(self->short_data)) {
|
|
|
|
|
result.long_data = ts_malloc(self->length);
|
|
|
|
|
memcpy(result.long_data, self->long_data, self->length);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 12:57:41 -07:00
|
|
|
void ts_external_scanner_state_delete(ExternalScannerState *self) {
|
2017-07-17 17:12:36 -07:00
|
|
|
if (self->length > sizeof(self->short_data)) {
|
|
|
|
|
ts_free(self->long_data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 12:57:41 -07:00
|
|
|
const char *ts_external_scanner_state_data(const ExternalScannerState *self) {
|
2017-07-17 17:12:36 -07:00
|
|
|
if (self->length > sizeof(self->short_data)) {
|
|
|
|
|
return self->long_data;
|
|
|
|
|
} else {
|
|
|
|
|
return self->short_data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 12:57:41 -07:00
|
|
|
bool ts_external_scanner_state_eq(const ExternalScannerState *a, const ExternalScannerState *b) {
|
|
|
|
|
return a == b || (
|
|
|
|
|
a->length == b->length &&
|
|
|
|
|
!memcmp(ts_external_scanner_state_data(a), ts_external_scanner_state_data(b), a->length)
|
|
|
|
|
);
|
2017-07-17 17:12:36 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
// SubtreeArray
|
2014-03-01 00:25:05 -08:00
|
|
|
|
2018-06-15 17:07:35 -07:00
|
|
|
void ts_subtree_array_copy(SubtreeArray self, SubtreeArray *dest) {
|
|
|
|
|
dest->size = self.size;
|
|
|
|
|
dest->capacity = self.capacity;
|
|
|
|
|
dest->contents = self.contents;
|
2016-06-14 14:46:49 -07:00
|
|
|
if (self.capacity > 0) {
|
2018-06-15 17:07:35 -07:00
|
|
|
dest->contents = ts_calloc(self.capacity, sizeof(Subtree *));
|
|
|
|
|
memcpy(dest->contents, self.contents, self.size * sizeof(Subtree *));
|
2018-05-10 12:23:05 -07:00
|
|
|
for (uint32_t i = 0; i < self.size; i++) {
|
2018-06-15 17:07:35 -07:00
|
|
|
ts_subtree_retain(dest->contents[i]);
|
2018-05-10 12:23:05 -07:00
|
|
|
}
|
2016-06-14 14:46:49 -07:00
|
|
|
}
|
2016-03-02 21:03:55 -08:00
|
|
|
}
|
|
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
void ts_subtree_array_delete(SubtreePool *pool, SubtreeArray *self) {
|
2017-06-27 14:30:46 -07:00
|
|
|
for (uint32_t i = 0; i < self->size; i++) {
|
2018-05-10 15:11:14 -07:00
|
|
|
ts_subtree_release(pool, self->contents[i]);
|
2017-06-27 14:30:46 -07:00
|
|
|
}
|
2016-03-07 16:03:23 -08:00
|
|
|
array_delete(self);
|
2016-03-02 21:03:55 -08:00
|
|
|
}
|
|
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
SubtreeArray ts_subtree_array_remove_trailing_extras(SubtreeArray *self) {
|
|
|
|
|
SubtreeArray result = array_new();
|
2017-02-19 13:53:28 -08:00
|
|
|
|
|
|
|
|
uint32_t i = self->size - 1;
|
|
|
|
|
for (; i + 1 > 0; i--) {
|
2018-05-11 15:06:13 -07:00
|
|
|
const Subtree *child = self->contents[i];
|
2017-02-19 13:53:28 -08:00
|
|
|
if (!child->extra) break;
|
|
|
|
|
array_push(&result, child);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self->size = i + 1;
|
2018-05-10 15:11:14 -07:00
|
|
|
ts_subtree_array_reverse(&result);
|
2017-02-19 13:53:28 -08:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
void ts_subtree_array_reverse(SubtreeArray *self) {
|
2017-07-24 21:02:26 -07:00
|
|
|
for (uint32_t i = 0, limit = self->size / 2; i < limit; i++) {
|
|
|
|
|
size_t reverse_index = self->size - 1 - i;
|
2018-05-11 15:06:13 -07:00
|
|
|
const Subtree *swap = self->contents[i];
|
2017-07-24 21:02:26 -07:00
|
|
|
self->contents[i] = self->contents[reverse_index];
|
|
|
|
|
self->contents[reverse_index] = swap;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
// SubtreePool
|
2017-10-05 17:32:21 -07:00
|
|
|
|
2018-05-10 22:22:37 -07:00
|
|
|
SubtreePool ts_subtree_pool_new(uint32_t capacity) {
|
2018-09-13 16:13:49 -07:00
|
|
|
SubtreePool self = {array_new(), array_new(), array_new()};
|
2018-05-10 22:22:37 -07:00
|
|
|
array_reserve(&self.free_trees, capacity);
|
2018-09-13 16:13:49 -07:00
|
|
|
array_reserve(&self.free_small_trees, capacity);
|
2018-05-10 22:22:37 -07:00
|
|
|
return self;
|
2017-10-05 17:32:21 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
void ts_subtree_pool_delete(SubtreePool *self) {
|
2017-10-05 17:32:21 -07:00
|
|
|
if (self->free_trees.contents) {
|
|
|
|
|
for (unsigned i = 0; i < self->free_trees.size; i++) {
|
|
|
|
|
ts_free(self->free_trees.contents[i]);
|
|
|
|
|
}
|
|
|
|
|
array_delete(&self->free_trees);
|
|
|
|
|
}
|
2018-09-13 16:13:49 -07:00
|
|
|
if (self->free_small_trees.contents) {
|
|
|
|
|
for (unsigned i = 0; i < self->free_small_trees.size; i++) {
|
|
|
|
|
ts_free(self->free_small_trees.contents[i]);
|
|
|
|
|
}
|
|
|
|
|
array_delete(&self->free_small_trees);
|
|
|
|
|
}
|
2017-10-05 17:32:21 -07:00
|
|
|
if (self->tree_stack.contents) array_delete(&self->tree_stack);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 16:13:49 -07:00
|
|
|
Subtree *ts_subtree_pool_allocate(SubtreePool *self, bool is_small) {
|
|
|
|
|
MutableSubtreeArray *array = is_small ? &self->free_small_trees : &self->free_trees;
|
|
|
|
|
Subtree *result;
|
|
|
|
|
if (array->size > 0) {
|
|
|
|
|
result = array_pop(array);
|
2017-10-05 17:32:21 -07:00
|
|
|
} else {
|
2018-09-13 16:13:49 -07:00
|
|
|
result = ts_malloc(is_small ? SMALL_TREE_SIZE : LARGE_TREE_SIZE);
|
2017-10-05 17:32:21 -07:00
|
|
|
}
|
2018-09-13 16:13:49 -07:00
|
|
|
result->is_small = is_small;
|
|
|
|
|
return result;
|
2017-10-05 17:32:21 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
void ts_subtree_pool_free(SubtreePool *self, Subtree *tree) {
|
2018-09-13 16:13:49 -07:00
|
|
|
MutableSubtreeArray *array = tree->is_small ? &self->free_small_trees : &self->free_trees;
|
|
|
|
|
if (array->capacity > 0 && array->size < MAX_TREE_POOL_SIZE) {
|
|
|
|
|
array_push(array, tree);
|
2017-10-05 17:32:21 -07:00
|
|
|
} else {
|
|
|
|
|
ts_free(tree);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
// Subtree
|
2017-10-05 17:32:21 -07:00
|
|
|
|
2018-09-15 00:08:47 -07:00
|
|
|
Subtree *ts_subtree_new_leaf(
|
|
|
|
|
SubtreePool *pool, TSSymbol symbol, Length padding, Length size, uint32_t bytes_scanned,
|
|
|
|
|
TSStateId parse_state, bool has_external_tokens, bool is_keyword, const TSLanguage *language
|
|
|
|
|
) {
|
|
|
|
|
TSSymbolMetadata metadata = ts_language_symbol_metadata(language, symbol);
|
|
|
|
|
bool is_small = !has_external_tokens;
|
|
|
|
|
Subtree *result = ts_subtree_pool_allocate(pool, is_small);
|
|
|
|
|
result->ref_count = 1;
|
|
|
|
|
result->padding = padding;
|
|
|
|
|
result->size = size;
|
|
|
|
|
result->bytes_scanned = bytes_scanned;
|
|
|
|
|
result->error_cost = 0;
|
|
|
|
|
result->child_count = 0;
|
|
|
|
|
result->symbol = symbol;
|
|
|
|
|
result->parse_state = parse_state;
|
|
|
|
|
result->is_small = is_small;
|
|
|
|
|
result->visible = metadata.visible;
|
|
|
|
|
result->named = metadata.named;
|
|
|
|
|
result->extra = symbol == ts_builtin_sym_end;
|
|
|
|
|
result->fragile_left = false;
|
|
|
|
|
result->fragile_right = false;
|
|
|
|
|
result->has_changes = false;
|
|
|
|
|
result->has_external_tokens = has_external_tokens;
|
|
|
|
|
result->is_missing = false;
|
|
|
|
|
result->is_keyword = is_keyword;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Subtree *ts_subtree__new(SubtreePool *pool, TSSymbol symbol, Length padding, Length size,
|
2018-09-13 16:13:49 -07:00
|
|
|
bool is_small, const TSLanguage *language) {
|
2017-10-05 17:32:21 -07:00
|
|
|
TSSymbolMetadata metadata = ts_language_symbol_metadata(language, symbol);
|
2018-09-13 16:13:49 -07:00
|
|
|
Subtree *result = ts_subtree_pool_allocate(pool, is_small);
|
|
|
|
|
result->padding = padding;
|
|
|
|
|
result->size = size;
|
|
|
|
|
result->ref_count = 1;
|
|
|
|
|
result->bytes_scanned = 0;
|
|
|
|
|
result->error_cost = 0;
|
|
|
|
|
result->child_count = 0;
|
|
|
|
|
result->is_small = is_small;
|
|
|
|
|
result->visible = metadata.visible;
|
|
|
|
|
result->named = metadata.named;
|
|
|
|
|
result->extra = symbol == ts_builtin_sym_end;
|
|
|
|
|
result->fragile_left = false;
|
|
|
|
|
result->fragile_right = false;
|
|
|
|
|
result->has_changes = false;
|
|
|
|
|
result->has_external_tokens = false;
|
|
|
|
|
result->is_missing = false;
|
|
|
|
|
result->is_keyword = false;
|
|
|
|
|
result->symbol = symbol;
|
|
|
|
|
result->parse_state = 0;
|
2017-10-05 17:32:21 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 13:02:12 -07:00
|
|
|
Subtree *ts_subtree_new_error(SubtreePool *pool, Length size, Length padding,
|
2018-05-11 15:06:13 -07:00
|
|
|
int32_t lookahead_char, const TSLanguage *language) {
|
2018-09-15 00:08:47 -07:00
|
|
|
Subtree *result = ts_subtree__new(pool, ts_builtin_sym_error, padding, size, false, language);
|
2016-06-21 07:28:04 -07:00
|
|
|
result->fragile_left = true;
|
|
|
|
|
result->fragile_right = true;
|
2014-08-28 13:22:06 -07:00
|
|
|
result->lookahead_char = lookahead_char;
|
2014-07-20 20:27:33 -07:00
|
|
|
return result;
|
2014-02-24 18:42:54 -08:00
|
|
|
}
|
|
|
|
|
|
2018-05-11 15:06:13 -07:00
|
|
|
Subtree *ts_subtree_new_copy(SubtreePool *pool, const Subtree *self) {
|
2018-09-13 16:13:49 -07:00
|
|
|
Subtree *result = ts_subtree_pool_allocate(pool, self->is_small);
|
|
|
|
|
memcpy(result, self, self->is_small ? SMALL_TREE_SIZE : LARGE_TREE_SIZE);
|
2018-08-31 10:39:14 -07:00
|
|
|
if (result->child_count > 0) {
|
|
|
|
|
result->children = ts_calloc(self->child_count, sizeof(const Subtree *));
|
|
|
|
|
memcpy(result->children, self->children, self->child_count * sizeof(const Subtree *));
|
|
|
|
|
for (uint32_t i = 0; i < result->child_count; i++) {
|
|
|
|
|
ts_subtree_retain(result->children[i]);
|
|
|
|
|
}
|
2018-06-15 16:18:08 -07:00
|
|
|
} else if (result->has_external_tokens) {
|
|
|
|
|
result->external_scanner_state = ts_external_scanner_state_copy(&self->external_scanner_state);
|
2018-05-10 12:23:05 -07:00
|
|
|
}
|
2016-04-15 23:01:36 -07:00
|
|
|
result->ref_count = 1;
|
2015-12-15 22:28:50 -08:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 15:06:13 -07:00
|
|
|
Subtree *ts_subtree_make_mut(SubtreePool *pool, const Subtree *self) {
|
2018-05-10 12:23:05 -07:00
|
|
|
if (self->ref_count == 1) {
|
2018-05-11 15:06:13 -07:00
|
|
|
return (Subtree *)self;
|
2018-05-10 12:23:05 -07:00
|
|
|
} else {
|
2018-05-11 13:02:12 -07:00
|
|
|
Subtree *result = ts_subtree_new_copy(pool, self);
|
2018-05-10 15:11:14 -07:00
|
|
|
ts_subtree_release(pool, self);
|
2018-05-10 12:23:05 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
static void ts_subtree__compress(Subtree *self, unsigned count, const TSLanguage *language,
|
2018-05-11 15:06:13 -07:00
|
|
|
MutableSubtreeArray *stack) {
|
2018-05-09 10:16:10 -07:00
|
|
|
unsigned initial_stack_size = stack->size;
|
|
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
Subtree *tree = self;
|
2018-01-29 10:41:07 -08:00
|
|
|
for (unsigned i = 0; i < count; i++) {
|
2018-08-31 10:39:14 -07:00
|
|
|
if (tree->ref_count > 1 || tree->child_count != 2) break;
|
2018-02-16 12:44:30 -08:00
|
|
|
|
2018-08-31 10:39:14 -07:00
|
|
|
Subtree *child = (Subtree *)tree->children[0];
|
2018-02-16 12:44:30 -08:00
|
|
|
if (
|
|
|
|
|
child->ref_count > 1 ||
|
2018-08-31 10:39:14 -07:00
|
|
|
child->child_count != 2 ||
|
2018-02-16 12:44:30 -08:00
|
|
|
child->symbol != tree->symbol
|
|
|
|
|
) break;
|
2018-01-29 10:41:07 -08:00
|
|
|
|
2018-08-31 10:39:14 -07:00
|
|
|
Subtree *grandchild = (Subtree *)child->children[0];
|
2018-02-16 12:44:30 -08:00
|
|
|
if (
|
|
|
|
|
grandchild->ref_count > 1 ||
|
2018-08-31 10:39:14 -07:00
|
|
|
grandchild->child_count != 2 ||
|
2018-02-16 12:44:30 -08:00
|
|
|
grandchild->symbol != tree->symbol
|
|
|
|
|
) break;
|
2018-01-29 10:41:07 -08:00
|
|
|
|
2018-08-31 10:39:14 -07:00
|
|
|
tree->children[0] = grandchild;
|
|
|
|
|
child->children[0] = grandchild->children[1];
|
|
|
|
|
grandchild->children[1] = child;
|
2018-05-09 10:16:10 -07:00
|
|
|
array_push(stack, tree);
|
2018-01-29 10:41:07 -08:00
|
|
|
tree = grandchild;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 10:16:10 -07:00
|
|
|
while (stack->size > initial_stack_size) {
|
|
|
|
|
tree = array_pop(stack);
|
2018-05-10 09:48:50 -07:00
|
|
|
assert(tree);
|
2018-08-31 10:39:14 -07:00
|
|
|
Subtree *child = (Subtree *)tree->children[0];
|
|
|
|
|
Subtree *grandchild = (Subtree *)child->children[1];
|
|
|
|
|
ts_subtree_set_children(grandchild, grandchild->children, grandchild->child_count, language);
|
|
|
|
|
ts_subtree_set_children(child, child->children, child->child_count, language);
|
|
|
|
|
ts_subtree_set_children(tree, tree->children, tree->child_count, language);
|
2018-01-29 10:41:07 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 15:06:13 -07:00
|
|
|
void ts_subtree_balance(const Subtree *self, SubtreePool *pool, const TSLanguage *language) {
|
2017-10-05 17:32:21 -07:00
|
|
|
array_clear(&pool->tree_stack);
|
2018-05-11 15:06:13 -07:00
|
|
|
|
|
|
|
|
if (self->ref_count == 1) {
|
|
|
|
|
array_push(&pool->tree_stack, (Subtree *)self);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-05 17:32:21 -07:00
|
|
|
while (pool->tree_stack.size > 0) {
|
2018-05-10 15:11:14 -07:00
|
|
|
Subtree *tree = array_pop(&pool->tree_stack);
|
2018-05-10 09:13:46 -07:00
|
|
|
assert(tree);
|
2018-01-29 10:41:07 -08:00
|
|
|
|
2018-09-13 16:13:49 -07:00
|
|
|
if (
|
|
|
|
|
tree->child_count > 0 &&
|
|
|
|
|
tree->repeat_depth > 0 &&
|
|
|
|
|
tree->children[0]->child_count > 0 &&
|
|
|
|
|
tree->children[1]->child_count > 0 &&
|
|
|
|
|
tree->children[0]->repeat_depth > tree->children[1]->repeat_depth
|
|
|
|
|
) {
|
|
|
|
|
unsigned n = tree->children[0]->repeat_depth - tree->children[1]->repeat_depth;
|
2018-05-11 15:06:13 -07:00
|
|
|
for (unsigned i = n / 2; i > 0; i /= 2) {
|
|
|
|
|
ts_subtree__compress(tree, i, language, &pool->tree_stack);
|
|
|
|
|
n -= i;
|
2018-05-09 10:16:10 -07:00
|
|
|
}
|
2018-01-29 10:41:07 -08:00
|
|
|
}
|
|
|
|
|
|
2018-08-31 10:39:14 -07:00
|
|
|
for (uint32_t i = 0; i < tree->child_count; i++) {
|
|
|
|
|
const Subtree *child = tree->children[i];
|
2018-05-09 10:16:10 -07:00
|
|
|
if (child->ref_count == 1) {
|
2018-05-11 15:06:13 -07:00
|
|
|
array_push(&pool->tree_stack, (Subtree *)child);
|
2016-02-04 12:59:44 -08:00
|
|
|
}
|
2015-11-15 12:21:16 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-31 10:39:14 -07:00
|
|
|
void ts_subtree_set_children(Subtree *self, const Subtree **children, uint32_t child_count, const TSLanguage *language) {
|
|
|
|
|
if (self->child_count > 0 && children != self->children) {
|
|
|
|
|
ts_free(self->children);
|
2018-04-02 18:04:26 -07:00
|
|
|
}
|
2016-04-24 00:54:20 -07:00
|
|
|
|
2018-09-13 16:13:49 -07:00
|
|
|
assert(!self->is_small);
|
|
|
|
|
|
2018-08-31 10:39:14 -07:00
|
|
|
self->child_count = child_count;
|
|
|
|
|
self->children = children;
|
2015-12-04 15:51:04 -08:00
|
|
|
self->named_child_count = 0;
|
2015-10-24 13:45:42 -07:00
|
|
|
self->visible_child_count = 0;
|
2016-08-30 10:58:25 -07:00
|
|
|
self->error_cost = 0;
|
2018-01-29 10:41:07 -08:00
|
|
|
self->repeat_depth = 0;
|
2018-04-02 10:57:44 -07:00
|
|
|
self->node_count = 1;
|
2016-12-20 17:06:20 -08:00
|
|
|
self->has_external_tokens = false;
|
2017-07-06 15:20:11 -07:00
|
|
|
self->dynamic_precedence = 0;
|
2016-04-24 00:54:20 -07:00
|
|
|
|
2017-07-18 21:24:34 -07:00
|
|
|
uint32_t non_extra_index = 0;
|
2017-07-31 11:45:24 -07:00
|
|
|
const TSSymbol *alias_sequence = ts_language_alias_sequence(language, self->alias_sequence_id);
|
2017-07-18 21:24:34 -07:00
|
|
|
|
2018-08-31 10:39:14 -07:00
|
|
|
for (uint32_t i = 0; i < self->child_count; i++) {
|
|
|
|
|
const Subtree *child = self->children[i];
|
2014-09-02 07:41:29 -07:00
|
|
|
|
|
|
|
|
if (i == 0) {
|
2015-10-14 21:52:13 -07:00
|
|
|
self->padding = child->padding;
|
|
|
|
|
self->size = child->size;
|
2017-03-13 17:03:47 -07:00
|
|
|
self->bytes_scanned = child->bytes_scanned;
|
2014-09-02 07:41:29 -07:00
|
|
|
} else {
|
2018-05-10 15:11:14 -07:00
|
|
|
uint32_t bytes_scanned = ts_subtree_total_bytes(self) + child->bytes_scanned;
|
2017-03-13 17:03:47 -07:00
|
|
|
if (bytes_scanned > self->bytes_scanned) self->bytes_scanned = bytes_scanned;
|
2018-05-10 15:11:14 -07:00
|
|
|
self->size = length_add(self->size, ts_subtree_total_size(child));
|
2014-09-02 07:41:29 -07:00
|
|
|
}
|
2014-07-16 18:38:06 -07:00
|
|
|
|
2018-04-06 09:35:17 -07:00
|
|
|
if (child->symbol != ts_builtin_sym_error_repeat) {
|
|
|
|
|
self->error_cost += child->error_cost;
|
|
|
|
|
}
|
2018-09-14 11:02:11 -07:00
|
|
|
|
|
|
|
|
if (child->child_count > 0) {
|
|
|
|
|
self->dynamic_precedence += child->dynamic_precedence;
|
|
|
|
|
self->node_count += child->node_count;
|
|
|
|
|
} else {
|
|
|
|
|
self->node_count++;
|
|
|
|
|
}
|
2016-05-09 14:31:44 -07:00
|
|
|
|
2017-07-31 11:45:24 -07:00
|
|
|
if (alias_sequence && alias_sequence[non_extra_index] != 0 && !child->extra) {
|
2015-11-22 13:32:20 -08:00
|
|
|
self->visible_child_count++;
|
2017-07-31 11:45:24 -07:00
|
|
|
if (ts_language_symbol_metadata(language, alias_sequence[non_extra_index]).named) {
|
|
|
|
|
self->named_child_count++;
|
|
|
|
|
}
|
2017-07-21 15:57:17 -07:00
|
|
|
} else if (child->visible) {
|
|
|
|
|
self->visible_child_count++;
|
|
|
|
|
if (child->named) self->named_child_count++;
|
2018-08-31 10:39:14 -07:00
|
|
|
} else if (child->child_count > 0) {
|
2015-11-22 13:32:20 -08:00
|
|
|
self->visible_child_count += child->visible_child_count;
|
|
|
|
|
self->named_child_count += child->named_child_count;
|
2015-09-07 21:11:37 -07:00
|
|
|
}
|
2015-12-06 20:31:10 -08:00
|
|
|
|
2016-12-20 17:06:20 -08:00
|
|
|
if (child->has_external_tokens) self->has_external_tokens = true;
|
|
|
|
|
|
2015-12-06 20:31:10 -08:00
|
|
|
if (child->symbol == ts_builtin_sym_error) {
|
2015-12-22 14:20:58 -08:00
|
|
|
self->fragile_left = self->fragile_right = true;
|
2016-06-27 14:39:12 -07:00
|
|
|
self->parse_state = TS_TREE_STATE_NONE;
|
2015-12-06 20:31:10 -08:00
|
|
|
}
|
2017-07-18 21:24:34 -07:00
|
|
|
|
|
|
|
|
if (!child->extra) non_extra_index++;
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-06 09:35:17 -07:00
|
|
|
if (self->symbol == ts_builtin_sym_error || self->symbol == ts_builtin_sym_error_repeat) {
|
2018-04-02 11:52:34 -07:00
|
|
|
self->error_cost += ERROR_COST_PER_RECOVERY +
|
|
|
|
|
ERROR_COST_PER_SKIPPED_CHAR * self->size.bytes +
|
2016-09-09 21:11:02 -07:00
|
|
|
ERROR_COST_PER_SKIPPED_LINE * self->size.extent.row;
|
2018-08-31 10:39:14 -07:00
|
|
|
for (uint32_t i = 0; i < self->child_count; i++) {
|
|
|
|
|
const Subtree *child = self->children[i];
|
2018-04-06 09:35:17 -07:00
|
|
|
if (child->extra) continue;
|
2018-08-31 10:39:14 -07:00
|
|
|
if (child->symbol == ts_builtin_sym_error && child->child_count == 0) continue;
|
2018-04-06 09:35:17 -07:00
|
|
|
if (child->visible) {
|
2016-08-31 10:51:59 -07:00
|
|
|
self->error_cost += ERROR_COST_PER_SKIPPED_TREE;
|
2018-04-06 09:35:17 -07:00
|
|
|
} else {
|
|
|
|
|
self->error_cost += ERROR_COST_PER_SKIPPED_TREE * child->visible_child_count;
|
2018-04-02 11:52:34 -07:00
|
|
|
}
|
|
|
|
|
}
|
2016-05-09 14:31:44 -07:00
|
|
|
}
|
2016-04-24 00:54:20 -07:00
|
|
|
|
2018-08-31 10:39:14 -07:00
|
|
|
if (self->child_count > 0) {
|
|
|
|
|
const Subtree *first_child = self->children[0];
|
|
|
|
|
const Subtree *last_child = self->children[self->child_count - 1];
|
2018-09-14 23:08:15 -07:00
|
|
|
if (first_child->child_count > 0) {
|
|
|
|
|
self->first_leaf = first_child->first_leaf;
|
|
|
|
|
} else {
|
|
|
|
|
self->first_leaf.symbol = first_child->symbol;
|
|
|
|
|
self->first_leaf.parse_state = first_child->parse_state;
|
|
|
|
|
}
|
2018-04-02 18:04:26 -07:00
|
|
|
if (first_child->fragile_left) self->fragile_left = true;
|
|
|
|
|
if (last_child->fragile_right) self->fragile_right = true;
|
2018-01-29 10:41:07 -08:00
|
|
|
if (
|
2018-08-31 10:39:14 -07:00
|
|
|
self->child_count == 2 &&
|
2018-01-29 10:41:07 -08:00
|
|
|
!self->visible && !self->named &&
|
2018-04-02 18:04:26 -07:00
|
|
|
first_child->symbol == self->symbol &&
|
|
|
|
|
last_child->symbol == self->symbol
|
2018-01-29 10:41:07 -08:00
|
|
|
) {
|
2018-04-02 18:04:26 -07:00
|
|
|
if (first_child->repeat_depth > last_child->repeat_depth) {
|
|
|
|
|
self->repeat_depth = first_child->repeat_depth + 1;
|
2018-01-29 10:41:07 -08:00
|
|
|
} else {
|
2018-04-02 18:04:26 -07:00
|
|
|
self->repeat_depth = last_child->repeat_depth + 1;
|
2018-01-29 10:41:07 -08:00
|
|
|
}
|
|
|
|
|
}
|
2015-06-15 15:24:15 -07:00
|
|
|
}
|
2015-09-08 21:43:37 -07:00
|
|
|
}
|
2014-07-20 20:27:33 -07:00
|
|
|
|
2018-05-11 13:02:12 -07:00
|
|
|
Subtree *ts_subtree_new_node(SubtreePool *pool, TSSymbol symbol, SubtreeArray *children,
|
2018-05-11 15:06:13 -07:00
|
|
|
unsigned alias_sequence_id, const TSLanguage *language) {
|
2018-09-13 16:13:49 -07:00
|
|
|
TSSymbolMetadata metadata = ts_language_symbol_metadata(language, symbol);
|
|
|
|
|
Subtree *result = ts_subtree_pool_allocate(pool, false);
|
|
|
|
|
*result = (Subtree){
|
|
|
|
|
.ref_count = 1,
|
|
|
|
|
.symbol = symbol,
|
|
|
|
|
.alias_sequence_id = alias_sequence_id,
|
|
|
|
|
.visible = metadata.visible,
|
|
|
|
|
.named = metadata.named,
|
|
|
|
|
.has_changes = false,
|
|
|
|
|
.is_keyword = false,
|
|
|
|
|
.node_count = 0,
|
|
|
|
|
};
|
2018-04-06 09:35:17 -07:00
|
|
|
if (symbol == ts_builtin_sym_error || symbol == ts_builtin_sym_error_repeat) {
|
|
|
|
|
result->fragile_left = true;
|
|
|
|
|
result->fragile_right = true;
|
|
|
|
|
}
|
2018-08-31 10:39:14 -07:00
|
|
|
ts_subtree_set_children(result, children->contents, children->size, language);
|
2014-07-20 20:27:33 -07:00
|
|
|
return result;
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
|
|
|
|
|
2018-05-11 13:02:12 -07:00
|
|
|
Subtree *ts_subtree_new_error_node(SubtreePool *pool, SubtreeArray *children,
|
2018-05-11 15:06:13 -07:00
|
|
|
const TSLanguage *language) {
|
2018-05-11 13:02:12 -07:00
|
|
|
return ts_subtree_new_node(pool, ts_builtin_sym_error, children, 0, language);
|
2016-04-03 23:46:43 -07:00
|
|
|
}
|
|
|
|
|
|
2018-09-11 17:25:28 -07:00
|
|
|
Subtree *ts_subtree_new_missing_leaf(SubtreePool *pool, TSSymbol symbol, Length padding,
|
2018-05-11 15:06:13 -07:00
|
|
|
const TSLanguage *language) {
|
2018-09-15 00:08:47 -07:00
|
|
|
Subtree *result = ts_subtree__new(pool, symbol, padding, length_zero(), true, language);
|
2017-12-28 15:48:35 -08:00
|
|
|
result->is_missing = true;
|
2018-04-02 11:52:34 -07:00
|
|
|
result->error_cost = ERROR_COST_PER_MISSING_TREE + ERROR_COST_PER_RECOVERY;
|
2017-12-28 15:48:35 -08:00
|
|
|
return result;
|
|
|
|
|
}
|
2018-01-29 10:41:07 -08:00
|
|
|
|
2018-05-11 15:06:13 -07:00
|
|
|
void ts_subtree_retain(const Subtree *self) {
|
2015-10-14 21:52:13 -07:00
|
|
|
assert(self->ref_count > 0);
|
2018-05-11 15:06:13 -07:00
|
|
|
atomic_inc((volatile uint32_t *)&self->ref_count);
|
2017-06-29 06:35:26 -07:00
|
|
|
assert(self->ref_count != 0);
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
2014-01-07 21:50:32 -08:00
|
|
|
|
2018-05-11 15:06:13 -07:00
|
|
|
void ts_subtree_release(SubtreePool *pool, const Subtree *self) {
|
2017-10-05 17:32:21 -07:00
|
|
|
array_clear(&pool->tree_stack);
|
2018-05-11 15:06:13 -07:00
|
|
|
|
|
|
|
|
assert(self->ref_count > 0);
|
|
|
|
|
if (atomic_dec((volatile uint32_t *)&self->ref_count) == 0) {
|
|
|
|
|
array_push(&pool->tree_stack, (Subtree *)self);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-05 17:32:21 -07:00
|
|
|
while (pool->tree_stack.size > 0) {
|
2018-05-10 15:11:14 -07:00
|
|
|
Subtree *tree = array_pop(&pool->tree_stack);
|
2018-08-31 10:39:14 -07:00
|
|
|
if (tree->child_count > 0) {
|
|
|
|
|
for (uint32_t i = 0; i < tree->child_count; i++) {
|
|
|
|
|
const Subtree *child = tree->children[i];
|
2018-05-11 15:06:13 -07:00
|
|
|
if (atomic_dec((volatile uint32_t *)&child->ref_count) == 0) {
|
|
|
|
|
array_push(&pool->tree_stack, (Subtree *)child);
|
2017-10-05 17:32:21 -07:00
|
|
|
}
|
2017-06-27 14:30:46 -07:00
|
|
|
}
|
2018-08-31 10:39:14 -07:00
|
|
|
ts_free(tree->children);
|
2018-05-11 15:06:13 -07:00
|
|
|
} else if (tree->has_external_tokens) {
|
|
|
|
|
ts_external_scanner_state_delete(&tree->external_scanner_state);
|
2016-02-04 12:59:44 -08:00
|
|
|
}
|
2018-05-11 15:06:13 -07:00
|
|
|
ts_subtree_pool_free(pool, tree);
|
2014-07-20 20:27:33 -07:00
|
|
|
}
|
2014-03-24 00:34:13 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
bool ts_subtree_eq(const Subtree *self, const Subtree *other) {
|
2015-10-14 21:52:13 -07:00
|
|
|
if (self) {
|
2017-07-31 11:45:24 -07:00
|
|
|
if (!other) return false;
|
2015-06-03 09:44:13 -07:00
|
|
|
} else {
|
2015-10-14 21:52:13 -07:00
|
|
|
return !other;
|
2015-06-03 09:44:13 -07:00
|
|
|
}
|
|
|
|
|
|
2017-07-31 11:45:24 -07:00
|
|
|
if (self->symbol != other->symbol) return false;
|
|
|
|
|
if (self->visible != other->visible) return false;
|
|
|
|
|
if (self->named != other->named) return false;
|
|
|
|
|
if (self->padding.bytes != other->padding.bytes) return false;
|
|
|
|
|
if (self->size.bytes != other->size.bytes) return false;
|
|
|
|
|
if (self->symbol == ts_builtin_sym_error) return self->lookahead_char == other->lookahead_char;
|
2018-08-31 10:39:14 -07:00
|
|
|
if (self->child_count != other->child_count) return false;
|
2018-09-15 00:08:47 -07:00
|
|
|
if (self->child_count > 0) {
|
|
|
|
|
if (self->visible_child_count != other->visible_child_count) return false;
|
|
|
|
|
if (self->named_child_count != other->named_child_count) return false;
|
2017-07-31 11:45:24 -07:00
|
|
|
|
2018-09-15 00:08:47 -07:00
|
|
|
for (uint32_t i = 0; i < self->child_count; i++) {
|
|
|
|
|
if (!ts_subtree_eq(self->children[i], other->children[i])) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-07-31 11:45:24 -07:00
|
|
|
}
|
|
|
|
|
}
|
2014-10-03 16:06:08 -07:00
|
|
|
return true;
|
2014-01-07 21:50:32 -08:00
|
|
|
}
|
|
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
int ts_subtree_compare(const Subtree *left, const Subtree *right) {
|
2015-11-20 11:53:03 -08:00
|
|
|
if (left->symbol < right->symbol)
|
|
|
|
|
return -1;
|
|
|
|
|
if (right->symbol < left->symbol)
|
|
|
|
|
return 1;
|
2018-08-31 10:39:14 -07:00
|
|
|
if (left->child_count < right->child_count)
|
2015-11-20 11:53:03 -08:00
|
|
|
return -1;
|
2018-08-31 10:39:14 -07:00
|
|
|
if (right->child_count < left->child_count)
|
2015-11-20 11:53:03 -08:00
|
|
|
return 1;
|
2018-08-31 10:39:14 -07:00
|
|
|
for (uint32_t i = 0; i < left->child_count; i++) {
|
|
|
|
|
const Subtree *left_child = left->children[i];
|
|
|
|
|
const Subtree *right_child = right->children[i];
|
2018-05-10 15:11:14 -07:00
|
|
|
switch (ts_subtree_compare(left_child, right_child)) {
|
2015-11-20 11:53:03 -08:00
|
|
|
case -1:
|
|
|
|
|
return -1;
|
|
|
|
|
case 1:
|
|
|
|
|
return 1;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-12 13:53:31 -07:00
|
|
|
const Subtree *ts_subtree_edit(const Subtree *self, const TSInputEdit *edit, SubtreePool *pool) {
|
|
|
|
|
typedef struct {
|
|
|
|
|
const Subtree **tree;
|
|
|
|
|
Edit edit;
|
|
|
|
|
} StackEntry;
|
|
|
|
|
|
|
|
|
|
Array(StackEntry) stack = array_new();
|
|
|
|
|
array_push(&stack, ((StackEntry) {
|
|
|
|
|
.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},
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
while (stack.size) {
|
|
|
|
|
StackEntry entry = array_pop(&stack);
|
|
|
|
|
Edit edit = entry.edit;
|
2018-07-13 16:03:01 -07:00
|
|
|
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;
|
|
|
|
|
if (is_noop && edit.start.bytes >= (*entry.tree)->bytes_scanned) continue;
|
2017-03-13 17:03:47 -07:00
|
|
|
|
2018-07-12 13:53:31 -07:00
|
|
|
Subtree *result = ts_subtree_make_mut(pool, *entry.tree);
|
|
|
|
|
*entry.tree = result;
|
|
|
|
|
|
2018-07-13 16:03:01 -07:00
|
|
|
// If the edit is entirely within the space before this subtree, then shift this
|
|
|
|
|
// subtree over according to the edit without changing its size.
|
2018-07-12 13:53:31 -07:00
|
|
|
if (edit.old_end.bytes <= result->padding.bytes) {
|
|
|
|
|
result->padding = length_add(edit.new_end, length_sub(result->padding, edit.old_end));
|
2018-07-13 16:03:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the edit starts in the space before this subtree and extends into this subtree,
|
|
|
|
|
// shrink the subtree's content to compensate for the change in the space before it.
|
|
|
|
|
else if (edit.start.bytes < result->padding.bytes) {
|
2018-07-12 13:53:31 -07:00
|
|
|
result->size = length_sub(result->size, length_sub(edit.old_end, result->padding));
|
|
|
|
|
result->padding = edit.new_end;
|
2018-07-13 16:03:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the edit is a pure insertion right at the start of the subtree,
|
|
|
|
|
// shift the subtree over according to the insertion.
|
|
|
|
|
else if (edit.start.bytes == result->padding.bytes && is_pure_insertion) {
|
2018-07-12 13:53:31 -07:00
|
|
|
result->padding = edit.new_end;
|
|
|
|
|
}
|
2015-09-15 16:00:16 -07:00
|
|
|
|
2018-07-13 16:03:01 -07:00
|
|
|
// If the edit is within this subtree, resize the subtree to reflect the edit.
|
|
|
|
|
else {
|
|
|
|
|
uint32_t total_bytes = ts_subtree_total_bytes(*entry.tree);
|
|
|
|
|
if (edit.start.bytes < total_bytes ||
|
|
|
|
|
(edit.start.bytes == total_bytes && is_pure_insertion)) {
|
|
|
|
|
result->size = length_add(
|
|
|
|
|
length_sub(edit.new_end, result->padding),
|
|
|
|
|
length_sub(result->size, length_sub(edit.old_end, result->padding))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result->has_changes = true;
|
|
|
|
|
|
2018-07-12 13:53:31 -07:00
|
|
|
Length child_left, child_right = length_zero();
|
2018-08-31 10:39:14 -07:00
|
|
|
for (uint32_t i = 0; i < result->child_count; i++) {
|
|
|
|
|
const Subtree **child = &result->children[i];
|
2018-07-12 13:53:31 -07:00
|
|
|
Length child_size = ts_subtree_total_size(*child);
|
|
|
|
|
child_left = child_right;
|
|
|
|
|
child_right = length_add(child_left, child_size);
|
|
|
|
|
|
|
|
|
|
// If this child starts after the edit, then we're done processing children.
|
|
|
|
|
if (child_left.bytes > edit.old_end.bytes ||
|
|
|
|
|
(child_left.bytes == edit.old_end.bytes && child_size.bytes > 0 && i > 0)) break;
|
|
|
|
|
|
2018-07-13 16:03:01 -07:00
|
|
|
// Transform edit into the child's coordinate space.
|
|
|
|
|
Edit child_edit = {
|
|
|
|
|
.start = length_sub(edit.start, child_left),
|
|
|
|
|
.old_end = length_sub(edit.old_end, child_left),
|
|
|
|
|
.new_end = length_sub(edit.new_end, child_left),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Clamp child_edit to the child's bounds.
|
|
|
|
|
if (edit.start.bytes < child_left.bytes) child_edit.start = length_zero();
|
|
|
|
|
if (edit.old_end.bytes < child_left.bytes) child_edit.old_end = length_zero();
|
|
|
|
|
if (edit.new_end.bytes < child_left.bytes) child_edit.new_end = length_zero();
|
|
|
|
|
if (edit.old_end.bytes > child_right.bytes) child_edit.old_end = child_size;
|
|
|
|
|
|
|
|
|
|
// Interpret all inserted text as applying to the *first* child that touches the edit.
|
|
|
|
|
// Subsequent children are only never have any text inserted into them; they are only
|
|
|
|
|
// shrunk to compensate for the edit.
|
2018-07-12 13:53:31 -07:00
|
|
|
if (child_right.bytes > edit.start.bytes ||
|
2018-07-13 16:03:01 -07:00
|
|
|
(child_right.bytes == edit.start.bytes && is_pure_insertion)) {
|
2018-07-12 13:53:31 -07:00
|
|
|
edit.new_end = edit.start;
|
|
|
|
|
}
|
2018-07-13 16:03:01 -07:00
|
|
|
|
|
|
|
|
// Children that occur before the edit are not reshaped by the edit.
|
|
|
|
|
else {
|
|
|
|
|
child_edit.old_end = child_edit.start;
|
|
|
|
|
child_edit.new_end = child_edit.start;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Queue processing of this child's subtree.
|
|
|
|
|
array_push(&stack, ((StackEntry) {
|
|
|
|
|
.tree = child,
|
|
|
|
|
.edit = child_edit,
|
|
|
|
|
}));
|
2018-05-10 12:04:18 -07:00
|
|
|
}
|
2015-09-15 16:00:16 -07:00
|
|
|
}
|
2018-05-10 12:23:05 -07:00
|
|
|
|
2018-07-12 13:53:31 -07:00
|
|
|
array_delete(&stack);
|
|
|
|
|
return self;
|
2018-05-10 12:04:18 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-11 15:06:13 -07:00
|
|
|
const Subtree *ts_subtree_last_external_token(const Subtree *tree) {
|
2017-06-27 14:30:46 -07:00
|
|
|
if (!tree->has_external_tokens) return NULL;
|
2018-08-31 10:39:14 -07:00
|
|
|
while (tree->child_count > 0) {
|
|
|
|
|
for (uint32_t i = tree->child_count - 1; i + 1 > 0; i--) {
|
|
|
|
|
const Subtree *child = tree->children[i];
|
2017-06-27 14:30:46 -07:00
|
|
|
if (child->has_external_tokens) {
|
2016-12-21 13:59:56 -08:00
|
|
|
tree = child;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-27 14:30:46 -07:00
|
|
|
return tree;
|
2016-12-21 13:59:56 -08:00
|
|
|
}
|
|
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
static size_t ts_subtree__write_char_to_string(char *s, size_t n, int32_t c) {
|
2016-09-03 22:45:02 -07:00
|
|
|
if (c == 0)
|
|
|
|
|
return snprintf(s, n, "EOF");
|
2017-06-23 12:08:50 -07:00
|
|
|
if (c == -1)
|
|
|
|
|
return snprintf(s, n, "INVALID");
|
2016-09-19 13:34:24 -07:00
|
|
|
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'");
|
2017-06-23 12:08:50 -07:00
|
|
|
else if (0 < c && c < 128 && isprint(c))
|
2016-09-03 22:45:02 -07:00
|
|
|
return snprintf(s, n, "'%c'", c);
|
|
|
|
|
else
|
|
|
|
|
return snprintf(s, n, "%d", c);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
static size_t ts_subtree__write_to_string(const Subtree *self, char *string, size_t limit,
|
2018-05-09 10:16:10 -07:00
|
|
|
const TSLanguage *language, bool is_root,
|
|
|
|
|
bool include_all, TSSymbol alias_symbol,
|
|
|
|
|
bool alias_is_named) {
|
2017-07-13 17:17:22 -07:00
|
|
|
if (!self) return snprintf(string, limit, "(NULL)");
|
2016-04-22 14:55:56 -07:00
|
|
|
|
|
|
|
|
char *cursor = string;
|
|
|
|
|
char **writer = (limit > 0) ? &cursor : &string;
|
2017-07-14 10:19:58 -07:00
|
|
|
bool visible =
|
|
|
|
|
include_all ||
|
|
|
|
|
is_root ||
|
2017-12-28 15:48:35 -08:00
|
|
|
self->is_missing ||
|
2017-07-14 10:19:58 -07:00
|
|
|
(self->visible && self->named) ||
|
2018-05-09 10:16:10 -07:00
|
|
|
alias_is_named;
|
2016-04-22 14:55:56 -07:00
|
|
|
|
2017-07-13 17:17:22 -07:00
|
|
|
if (visible && !is_root) {
|
2016-04-22 14:55:56 -07:00
|
|
|
cursor += snprintf(*writer, limit, " ");
|
2017-07-13 17:17:22 -07:00
|
|
|
}
|
2016-04-22 14:55:56 -07:00
|
|
|
|
|
|
|
|
if (visible) {
|
2018-08-31 10:39:14 -07:00
|
|
|
if (self->symbol == ts_builtin_sym_error && self->child_count == 0 && self->size.bytes > 0) {
|
2016-09-03 22:45:02 -07:00
|
|
|
cursor += snprintf(*writer, limit, "(UNEXPECTED ");
|
2018-05-10 15:11:14 -07:00
|
|
|
cursor += ts_subtree__write_char_to_string(*writer, limit, self->lookahead_char);
|
2017-12-28 15:48:35 -08:00
|
|
|
} else if (self->is_missing) {
|
|
|
|
|
cursor += snprintf(*writer, limit, "(MISSING");
|
2016-04-22 14:55:56 -07:00
|
|
|
} else {
|
2018-05-09 10:16:10 -07:00
|
|
|
TSSymbol symbol = alias_symbol ? alias_symbol : self->symbol;
|
2017-12-28 15:48:35 -08:00
|
|
|
const char *symbol_name = ts_language_symbol_name(language, symbol);
|
|
|
|
|
cursor += snprintf(*writer, limit, "(%s", symbol_name);
|
2016-04-22 14:55:56 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 16:13:49 -07:00
|
|
|
if (self->child_count) {
|
|
|
|
|
const TSSymbol *alias_sequence = ts_language_alias_sequence(language, self->alias_sequence_id);
|
|
|
|
|
uint32_t structural_child_index = 0;
|
|
|
|
|
for (uint32_t i = 0; i < self->child_count; i++) {
|
|
|
|
|
const Subtree *child = self->children[i];
|
|
|
|
|
if (child->extra) {
|
|
|
|
|
cursor += ts_subtree__write_to_string(
|
|
|
|
|
child, *writer, limit,
|
|
|
|
|
language, false, include_all,
|
|
|
|
|
0, false
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
TSSymbol alias_symbol = alias_sequence ? alias_sequence[structural_child_index] : 0;
|
|
|
|
|
cursor += ts_subtree__write_to_string(
|
|
|
|
|
child, *writer, limit,
|
|
|
|
|
language, false, include_all,
|
|
|
|
|
alias_symbol,
|
|
|
|
|
alias_symbol ? ts_language_symbol_metadata(language, alias_symbol).named : false
|
|
|
|
|
);
|
|
|
|
|
structural_child_index++;
|
|
|
|
|
}
|
2018-05-09 10:16:10 -07:00
|
|
|
}
|
2016-04-22 14:55:56 -07:00
|
|
|
}
|
|
|
|
|
|
2017-07-13 17:17:22 -07:00
|
|
|
if (visible) cursor += snprintf(*writer, limit, ")");
|
2016-04-22 14:55:56 -07:00
|
|
|
|
|
|
|
|
return cursor - string;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
char *ts_subtree_string(const Subtree *self, const TSLanguage *language, bool include_all) {
|
2017-12-28 15:48:35 -08:00
|
|
|
char scratch_string[1];
|
2018-05-10 15:11:14 -07:00
|
|
|
size_t size = ts_subtree__write_to_string(
|
2018-05-09 10:16:10 -07:00
|
|
|
self, scratch_string, 0,
|
|
|
|
|
language, true,
|
|
|
|
|
include_all, 0, false
|
|
|
|
|
) + 1;
|
2016-04-22 14:55:56 -07:00
|
|
|
char *result = ts_malloc(size * sizeof(char));
|
2018-05-10 15:11:14 -07:00
|
|
|
ts_subtree__write_to_string(self, result, size, language, true, include_all, 0, false);
|
2016-04-22 14:55:56 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
2016-06-22 21:04:35 -07:00
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
void ts_subtree__print_dot_graph(const Subtree *self, uint32_t byte_offset,
|
2018-05-09 10:16:10 -07:00
|
|
|
const TSLanguage *language, TSSymbol alias_symbol, FILE *f) {
|
|
|
|
|
TSSymbol symbol = alias_symbol ? alias_symbol : self->symbol;
|
2017-07-13 17:17:22 -07:00
|
|
|
fprintf(f, "tree_%p [label=\"%s\"", self, ts_language_symbol_name(language, symbol));
|
2016-06-22 21:04:35 -07:00
|
|
|
|
2018-08-31 10:39:14 -07:00
|
|
|
if (self->child_count == 0)
|
2016-06-22 21:04:35 -07:00
|
|
|
fprintf(f, ", shape=plaintext");
|
|
|
|
|
if (self->extra)
|
|
|
|
|
fprintf(f, ", fontcolor=gray");
|
|
|
|
|
|
2018-05-10 12:04:18 -07:00
|
|
|
fprintf(f, ", tooltip=\""
|
|
|
|
|
"address:%p\n"
|
|
|
|
|
"range:%u - %u\n"
|
|
|
|
|
"state:%d\n"
|
|
|
|
|
"error-cost:%u\n"
|
|
|
|
|
"repeat-depth:%u\n"
|
|
|
|
|
"bytes-scanned:%u\"]\n",
|
|
|
|
|
self,
|
2018-05-10 15:11:14 -07:00
|
|
|
byte_offset, byte_offset + ts_subtree_total_bytes(self),
|
2018-05-10 12:04:18 -07:00
|
|
|
self->parse_state,
|
|
|
|
|
self->error_cost,
|
2018-09-13 16:13:49 -07:00
|
|
|
self->child_count > 0 ? self->repeat_depth : 0,
|
2018-05-10 12:04:18 -07:00
|
|
|
self->bytes_scanned
|
|
|
|
|
);
|
2018-05-09 10:16:10 -07:00
|
|
|
|
|
|
|
|
const TSSymbol *alias_sequence = ts_language_alias_sequence(language, self->alias_sequence_id);
|
|
|
|
|
uint32_t structural_child_index = 0;
|
2018-08-31 10:39:14 -07:00
|
|
|
for (uint32_t i = 0; i < self->child_count; i++) {
|
|
|
|
|
const Subtree *child = self->children[i];
|
2018-05-09 10:16:10 -07:00
|
|
|
if (child->extra) {
|
2018-05-10 15:11:14 -07:00
|
|
|
ts_subtree__print_dot_graph(child, byte_offset, language, 0, f);
|
2018-05-09 10:16:10 -07:00
|
|
|
} else {
|
|
|
|
|
TSSymbol alias_symbol = alias_sequence ? alias_sequence[structural_child_index] : 0;
|
2018-05-10 15:11:14 -07:00
|
|
|
ts_subtree__print_dot_graph(child, byte_offset, language, alias_symbol, f);
|
2018-05-09 10:16:10 -07:00
|
|
|
structural_child_index++;
|
|
|
|
|
}
|
2016-11-14 12:15:24 -08:00
|
|
|
fprintf(f, "tree_%p -> tree_%p [tooltip=%u]\n", self, child, i);
|
2018-05-10 15:11:14 -07:00
|
|
|
byte_offset += ts_subtree_total_bytes(child);
|
2016-06-22 21:04:35 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 15:11:14 -07:00
|
|
|
void ts_subtree_print_dot_graph(const Subtree *self, const TSLanguage *language, FILE *f) {
|
2016-06-22 21:04:35 -07:00
|
|
|
fprintf(f, "digraph tree {\n");
|
|
|
|
|
fprintf(f, "edge [arrowhead=none]\n");
|
2018-05-10 15:11:14 -07:00
|
|
|
ts_subtree__print_dot_graph(self, 0, language, 0, f);
|
2016-06-22 21:04:35 -07:00
|
|
|
fprintf(f, "}\n");
|
|
|
|
|
}
|
2017-02-20 14:34:10 -08:00
|
|
|
|
2018-05-11 12:57:41 -07:00
|
|
|
bool ts_subtree_external_scanner_state_eq(const Subtree *self, const Subtree *other) {
|
|
|
|
|
const ExternalScannerState *state1 = &empty_state;
|
|
|
|
|
const ExternalScannerState *state2 = &empty_state;
|
2018-08-31 10:39:14 -07:00
|
|
|
if (self && !self->child_count && self->has_external_tokens) {
|
|
|
|
|
state1 = &self->external_scanner_state;
|
|
|
|
|
}
|
|
|
|
|
if (other && !other->child_count && other->has_external_tokens) {
|
|
|
|
|
state2 = &other->external_scanner_state;
|
|
|
|
|
}
|
2018-05-11 12:57:41 -07:00
|
|
|
return ts_external_scanner_state_eq(state1, state2);
|
2017-02-20 14:34:10 -08:00
|
|
|
}
|