Merge pull request #43 from tree-sitter/no-oom-recovery

Remove logic for recovering from OOM
This commit is contained in:
Max Brunsfeld 2016-11-06 21:35:52 -08:00 committed by GitHub
commit ad910ed4f5
15 changed files with 192 additions and 517 deletions

View file

@ -105,8 +105,8 @@ TSLogger ts_document_logger(const TSDocument *);
void ts_document_set_logger(TSDocument *, TSLogger);
void ts_document_print_debugging_graphs(TSDocument *, bool);
void ts_document_edit(TSDocument *, TSInputEdit);
int ts_document_parse(TSDocument *);
int ts_document_parse_and_get_changed_ranges(TSDocument *, TSRange **, size_t *);
void ts_document_parse(TSDocument *);
void ts_document_parse_and_get_changed_ranges(TSDocument *, TSRange **, size_t *);
void ts_document_invalidate(TSDocument *);
TSNode ts_document_root_node(const TSDocument *);
size_t ts_document_parse_count(const TSDocument *);

View file

@ -24,7 +24,7 @@ OPTIONS
-s set the seed used to control random behavior
-z pipe tests' stderr to `dot(1)`, to render an SVG log
-z pipe tests' stderr to \`dot(1)\` to render an SVG log
EOF
}

View file

@ -6,10 +6,9 @@
using std::map;
using std::set;
bool _enabled = false;
static bool _enabled = false;
static size_t _allocation_count = 0;
static map<void *, size_t> _outstanding_allocations;
static size_t _allocation_failure_index = -1;
namespace record_alloc {
@ -17,17 +16,12 @@ void start() {
_enabled = true;
_allocation_count = 0;
_outstanding_allocations.clear();
_allocation_failure_index = -1;
}
void stop() {
_enabled = false;
}
void fail_at_allocation_index(size_t failure_index) {
_allocation_failure_index = failure_index;
}
set<size_t> outstanding_allocation_indices() {
set<size_t> result;
for (const auto &entry : _outstanding_allocations) {
@ -42,12 +36,7 @@ size_t allocation_count() {
} // namespace record_alloc
static bool can_allocate() {
if (!_enabled)
return true;
return _allocation_count < _allocation_failure_index;
}
extern "C" {
static void *record_allocation(void *result) {
if (!_enabled)
@ -58,10 +47,6 @@ static void *record_allocation(void *result) {
return result;
}
static void record_allocation_failure() {
_allocation_count++;
}
static void record_deallocation(void *pointer) {
if (!_enabled)
return;
@ -72,34 +57,17 @@ static void record_deallocation(void *pointer) {
}
}
extern "C" {
void *ts_record_malloc(size_t size) {
if (can_allocate()) {
return record_allocation(malloc(size));
} else {
record_allocation_failure();
return NULL;
}
return record_allocation(malloc(size));
}
void *ts_record_realloc(void *pointer, size_t size) {
if (can_allocate()) {
record_deallocation(pointer);
return record_allocation(realloc(pointer, size));
} else {
record_allocation_failure();
return NULL;
}
record_deallocation(pointer);
return record_allocation(realloc(pointer, size));
}
void *ts_record_calloc(size_t count, size_t size) {
if (can_allocate()) {
return record_allocation(calloc(count, size));
} else {
record_allocation_failure();
return NULL;
}
return record_allocation(calloc(count, size));
}
void ts_record_free(void *pointer) {

View file

@ -36,7 +36,7 @@ describe("Parser", [&]() {
auto set_text = [&](const char *text) {
input = new SpyInput(text, chunk_size);
ts_document_set_input(doc, input->input());
AssertThat(ts_document_parse(doc), Equals(0));
ts_document_parse(doc);
root = ts_document_root_node(doc);
AssertThat(ts_node_end_byte(root), Equals(strlen(text)));
@ -446,81 +446,6 @@ describe("Parser", [&]() {
AssertThat(ts_node_end_byte(root), Equals(strlen("'\u03A9\u03A9\u03A9 \u2014 \u0394\u0394';")));
});
});
describe("handling allocation failures", [&]() {
it("handles failures when allocating documents", [&]() {
record_alloc::start();
TSDocument *document = ts_document_new();
ts_document_free(document);
AssertThat(record_alloc::outstanding_allocation_indices(), IsEmpty());
size_t allocation_count = record_alloc::allocation_count();
AssertThat(allocation_count, IsGreaterThan<size_t>(1));
for (size_t i = 0; i < allocation_count; i++) {
record_alloc::start();
record_alloc::fail_at_allocation_index(i);
AssertThat(ts_document_new(), Equals<TSDocument *>(nullptr));
AssertThat(record_alloc::outstanding_allocation_indices(), IsEmpty());
}
record_alloc::stop();
});
it("handles allocation failures during parsing", [&]() {
const TSLanguage *language = get_test_language("cpp");
const char *input_string = "int main() { return vector<int *>().size(); }";
string expected_node_string =
"(translation_unit (function_definition "
"(identifier) "
"(function_declarator (identifier)) "
"(compound_statement "
"(return_statement (call_expression (field_expression "
"(call_expression (template_call "
"(identifier) "
"(type_name (identifier) (abstract_pointer_declarator)))) "
"(identifier)))))))";
record_alloc::start();
ts_document_set_language(doc, language);
ts_document_set_input_string(doc, input_string);
AssertThat(ts_document_parse(doc), Equals(0));
size_t allocation_count = record_alloc::allocation_count();
AssertThat(allocation_count, IsGreaterThan<size_t>(1));
assert_root_node(expected_node_string);
ts_document_free(doc);
doc = nullptr;
AssertThat(record_alloc::outstanding_allocation_indices(), IsEmpty());
for (size_t i = 0; i < allocation_count; i++) {
record_alloc::stop();
doc = ts_document_new();
record_alloc::start();
record_alloc::fail_at_allocation_index(i);
ts_document_set_language(doc, language);
ts_document_set_input_string(doc, input_string);
AssertThat(ts_document_parse(doc), Equals(-1));
AssertThat(ts_document_root_node(doc).data, Equals<void *>(nullptr));
ts_document_free(doc);
doc = nullptr;
}
record_alloc::stop();
doc = ts_document_new();
record_alloc::start();
record_alloc::fail_at_allocation_index(allocation_count + 1);
ts_document_set_language(doc, language);
ts_document_set_input_string(doc, input_string);
AssertThat(ts_document_parse(doc), Equals(0));
assert_root_node(expected_node_string);
});
});
});
END_TEST

View file

@ -217,7 +217,7 @@ describe("Stack", [&]() {
// ↑
// └─*
StackPopResult pop = ts_stack_pop_count(stack, 0, 2);
AssertThat(pop.status, Equals(StackPopResult::StackPopSucceeded));
AssertThat(pop.stopped_at_error, Equals(false));
AssertThat(pop.slices.size, Equals<size_t>(1));
AssertThat(ts_stack_version_count(stack), Equals<size_t>(2));
@ -236,7 +236,7 @@ describe("Stack", [&]() {
// ↑
// └─*
StackPopResult pop = ts_stack_pop_count(stack, 0, 2);
AssertThat(pop.status, Equals(StackPopResult::StackPopSucceeded));
AssertThat(pop.stopped_at_error, Equals(false));
AssertThat(pop.slices.size, Equals<size_t>(1));
StackSlice slice = pop.slices.contents[0];
@ -255,7 +255,7 @@ describe("Stack", [&]() {
// ↑
// └─*
StackPopResult pop = ts_stack_pop_count(stack, 0, 3);
AssertThat(pop.status, Equals(StackPopResult::StackPopStoppedAtError));
AssertThat(pop.stopped_at_error, Equals(true));
AssertThat(ts_stack_version_count(stack), Equals<size_t>(2));
AssertThat(ts_stack_top_state(stack, 1), Equals(ERROR_STATE));
@ -471,7 +471,7 @@ describe("Stack", [&]() {
ts_stack_push(stack, 0, trees[1], true, stateB);
StackPopResult pop = ts_stack_pop_pending(stack, 0);
AssertThat(pop.status, Equals(StackPopResult::StackPopSucceeded));
AssertThat(pop.stopped_at_error, Equals(false));
AssertThat(pop.slices.size, Equals<size_t>(1));
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({
@ -492,7 +492,7 @@ describe("Stack", [&]() {
ts_stack_push(stack, 0, trees[3], false, stateB);
StackPopResult pop = ts_stack_pop_pending(stack, 0);
AssertThat(pop.status, Equals(StackPopResult::StackPopSucceeded));
AssertThat(pop.stopped_at_error, Equals(false));
AssertThat(pop.slices.size, Equals<size_t>(1));
AssertThat(pop.slices.contents[0].trees, Equals(vector<TSTree *>({ trees[1], trees[2], trees[3] })));
@ -509,7 +509,7 @@ describe("Stack", [&]() {
ts_stack_push(stack, 0, trees[1], false, stateB);
StackPopResult pop = ts_stack_pop_pending(stack, 0);
AssertThat(pop.status, Equals(StackPopResult::StackPopSucceeded));
AssertThat(pop.stopped_at_error, Equals(false));
AssertThat(pop.slices.size, Equals<size_t>(0));
AssertThat(get_stack_entries(stack, 0), Equals(vector<StackEntry>({

View file

@ -7,6 +7,7 @@ extern "C" {
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#if defined(TREE_SITTER_WRAP_MALLOC)
@ -45,19 +46,34 @@ static inline bool ts_toggle_allocation_recording(bool value) {
}
static inline void *ts_malloc(size_t size) {
return malloc(size);
void *result = malloc(size);
if (size > 0 && !result) {
fprintf(stderr, "tree-sitter failed to allocate %lu bytes", size);
exit(1);
}
return result;
}
static inline void *ts_calloc(size_t count, size_t size) {
return calloc(count, size);
void *result = calloc(count, size);
if (count > 0 && !result) {
fprintf(stderr, "tree-sitter failed to allocate %lu bytes", count * size);
exit(1);
}
return result;
}
static inline void *ts_realloc(void *buffer, size_t size) {
return realloc(buffer, size);
void *result = realloc(buffer, size);
if (size > 0 && !result) {
fprintf(stderr, "tree-sitter failed to reallocate %lu bytes", size);
exit(1);
}
return result;
}
static inline void ts_free(void *buffer) {
return free(buffer);
free(buffer);
}
#endif

View file

@ -41,10 +41,9 @@ extern "C" {
#define array_delete(self) array__delete((VoidArray *)self)
#define array_push(self, element) \
(((self)->size < (self)->capacity || \
array_grow((self), (self)->capacity ? (self)->capacity * 2 : 8)) && \
((self)->contents[(self)->size++] = (element), true))
#define array_push(self, element) \
(array_grow((self), (self)->size + 1), \
(self)->contents[(self)->size++] = (element))
#define array_splice(self, index, old_count, new_count, new_elements) \
array__splice((VoidArray *)(self), array__elem_size(self), index, old_count, \
@ -80,25 +79,22 @@ static inline void array__erase(VoidArray *self, size_t element_size,
self->size--;
}
static inline bool array__grow(VoidArray *self, size_t element_size,
static inline void array__grow(VoidArray *self, size_t element_size,
size_t new_capacity) {
if (new_capacity == 0)
return true;
void *new_contents;
if (self->contents)
new_contents = ts_realloc(self->contents, new_capacity * element_size);
else
new_contents = ts_calloc(new_capacity, element_size);
if (!new_contents)
return false;
self->capacity = new_capacity;
self->contents = new_contents;
return true;
if (new_capacity > self->capacity) {
if (new_capacity < 2 * self->capacity)
new_capacity = 2 * self->capacity;
if (new_capacity < 8)
new_capacity = 8;
if (self->contents)
self->contents = ts_realloc(self->contents, new_capacity * element_size);
else
self->contents = ts_calloc(new_capacity, element_size);
self->capacity = new_capacity;
}
}
static inline bool array__splice(VoidArray *self, size_t element_size,
static inline void array__splice(VoidArray *self, size_t element_size,
size_t index, size_t old_count,
size_t new_count, void *elements) {
size_t new_size = self->size + new_count - old_count;
@ -106,9 +102,7 @@ static inline bool array__splice(VoidArray *self, size_t element_size,
size_t new_end = index + new_count;
assert(old_end <= self->size);
if (new_size >= self->capacity)
if (!array__grow(self, element_size, new_size))
return false;
array__grow(self, element_size, new_size);
char *contents = (char *)self->contents;
if (self->size > old_end)
@ -118,7 +112,6 @@ static inline bool array__splice(VoidArray *self, size_t element_size,
memcpy((contents + index * element_size), elements,
new_count * element_size);
self->size += new_count - old_count;
return true;
}
static inline void array__reverse(VoidArray *self, size_t element_size) {

View file

@ -89,21 +89,19 @@ void ts_document_edit(TSDocument *self, TSInputEdit edit) {
ts_tree_edit(self->tree, &edit);
}
int ts_document_parse_and_get_changed_ranges(TSDocument *self, TSRange **ranges,
void ts_document_parse_and_get_changed_ranges(TSDocument *self, TSRange **ranges,
size_t *range_count) {
if (ranges) *ranges = NULL;
if (range_count) *range_count = 0;
if (!self->input.read || !self->parser.language)
return -1;
return;
TSTree *reusable_tree = self->valid ? self->tree : NULL;
if (reusable_tree && !reusable_tree->has_changes)
return 0;
return;
TSTree *tree = parser_parse(&self->parser, self->input, reusable_tree);
if (!tree)
return -1;
if (self->tree) {
TSTree *old_tree = self->tree;
@ -112,9 +110,8 @@ int ts_document_parse_and_get_changed_ranges(TSDocument *self, TSRange **ranges,
if (ranges && range_count) {
tree_path_init(&self->parser.tree_path1, old_tree);
tree_path_init(&self->parser.tree_path2, tree);
if (!tree_path_get_changes(&self->parser.tree_path1,
&self->parser.tree_path2, ranges, range_count))
return -1;
tree_path_get_changes(&self->parser.tree_path1, &self->parser.tree_path2,
ranges, range_count);
}
ts_tree_release(old_tree);
@ -123,11 +120,10 @@ int ts_document_parse_and_get_changed_ranges(TSDocument *self, TSRange **ranges,
self->tree = tree;
self->parse_count++;
self->valid = true;
return 0;
}
int ts_document_parse(TSDocument *self) {
return ts_document_parse_and_get_changed_ranges(self, NULL, NULL);
void ts_document_parse(TSDocument *self) {
ts_document_parse_and_get_changed_ranges(self, NULL, NULL);
}
void ts_document_invalidate(TSDocument *self) {

View file

@ -42,11 +42,6 @@
#define BOOL_STRING(value) (value ? "true" : "false")
#define CHECK(expr) \
if (!(expr)) { \
goto error; \
}
typedef struct {
Parser *parser;
TSSymbol lookahead_symbol;
@ -59,42 +54,22 @@ typedef struct {
} ErrorRepairSession;
typedef struct {
enum {
ReduceFailed,
ReduceSucceeded,
ReduceStoppedAtError,
} status;
bool stopped_at_error;
StackSlice slice;
} Reduction;
typedef enum {
RepairFailed,
RepairSucceeded,
RepairNoneFound,
} RepairResult;
typedef enum {
BreakdownFailed,
BreakdownPerformed,
BreakdownAborted,
} BreakdownResult;
static bool parser__push(Parser *self, StackVersion version, TSTree *tree,
static void parser__push(Parser *self, StackVersion version, TSTree *tree,
TSStateId state) {
bool result = ts_stack_push(self->stack, version, tree, false, state);
ts_stack_push(self->stack, version, tree, false, state);
ts_tree_release(tree);
return result;
}
static BreakdownResult parser__breakdown_top_of_stack(Parser *self,
StackVersion version) {
static bool parser__breakdown_top_of_stack(Parser *self, StackVersion version) {
bool did_break_down = false;
bool pending = false;
do {
StackPopResult pop = ts_stack_pop_pending(self->stack, version);
CHECK(pop.status);
if (!pop.slices.size)
break;
@ -119,12 +94,12 @@ static BreakdownResult parser__breakdown_top_of_stack(Parser *self,
state = action->params.to_state;
}
CHECK(ts_stack_push(self->stack, slice.version, child, pending, state));
ts_stack_push(self->stack, slice.version, child, pending, state);
}
for (size_t j = 1; j < slice.trees.size; j++) {
TSTree *tree = slice.trees.contents[j];
CHECK(parser__push(self, slice.version, tree, state));
parser__push(self, slice.version, tree, state);
}
LOG("breakdown_top_of_stack tree:%s", SYM_NAME(parent->symbol));
@ -137,10 +112,7 @@ static BreakdownResult parser__breakdown_top_of_stack(Parser *self,
}
} while (pending);
return did_break_down ? BreakdownPerformed : BreakdownAborted;
error:
return BreakdownFailed;
return did_break_down;
}
static void parser__pop_reusable_node(ReusableNode *reusable_node) {
@ -335,7 +307,7 @@ static TSTree *parser__get_lookahead(Parser *self, StackVersion version,
reusable_node->tree->size.bytes);
if (!parser__breakdown_reusable_node(reusable_node)) {
parser__pop_reusable_node(reusable_node);
CHECK(parser__breakdown_top_of_stack(self, version));
parser__breakdown_top_of_stack(self, version);
}
continue;
}
@ -346,7 +318,7 @@ static TSTree *parser__get_lookahead(Parser *self, StackVersion version,
reusable_node->tree->size.bytes);
if (!parser__breakdown_reusable_node(reusable_node)) {
parser__pop_reusable_node(reusable_node);
CHECK(parser__breakdown_top_of_stack(self, version));
parser__breakdown_top_of_stack(self, version);
}
continue;
}
@ -364,9 +336,6 @@ static TSTree *parser__get_lookahead(Parser *self, StackVersion version,
ts_lexer_reset(&self->lexer, position);
TSStateId parse_state = ts_stack_top_state(self->stack, version);
return parser__lex(self, parse_state);
error:
return NULL;
}
static bool parser__select_tree(Parser *self, TSTree *left, TSTree *right) {
@ -427,13 +396,13 @@ static bool parser__better_version_exists(Parser *self, StackVersion version,
return false;
}
static bool parser__shift(Parser *self, StackVersion version, TSStateId state,
static void parser__shift(Parser *self, StackVersion version, TSStateId state,
TSTree *lookahead, bool extra) {
if (extra != lookahead->extra) {
TSSymbolMetadata metadata =
ts_language_symbol_metadata(self->language, lookahead->symbol);
if (metadata.structural && ts_stack_version_count(self->stack) > 1) {
CHECK(lookahead = ts_tree_make_copy(lookahead));
lookahead = ts_tree_make_copy(lookahead);
} else {
ts_tree_retain(lookahead);
}
@ -443,13 +412,8 @@ static bool parser__shift(Parser *self, StackVersion version, TSStateId state,
}
bool is_pending = lookahead->child_count > 0;
CHECK(ts_stack_push(self->stack, version, lookahead, is_pending, state));
ts_stack_push(self->stack, version, lookahead, is_pending, state);
ts_tree_release(lookahead);
return true;
error:
ts_tree_release(lookahead);
return false;
}
static bool parser__switch_children(Parser *self, TSTree *tree,
@ -476,13 +440,8 @@ static Reduction parser__reduce(Parser *self, StackVersion version,
bool allow_skipping) {
size_t initial_version_count = ts_stack_version_count(self->stack);
StackPopResult pop = ts_stack_pop_count(self->stack, version, count);
switch (pop.status) {
case StackPopFailed:
goto error;
case StackPopStoppedAtError:
return (Reduction){ ReduceStoppedAtError, pop.slices.contents[0] };
default:
break;
if (pop.stopped_at_error) {
return (Reduction){ true, pop.slices.contents[0] };
}
const TSLanguage *language = self->language;
@ -497,10 +456,6 @@ static Reduction parser__reduce(Parser *self, StackVersion version,
TSTree *parent =
ts_tree_make_node(symbol, child_count, slice.trees.contents, metadata);
if (!parent) {
ts_tree_array_delete(&slice.trees);
goto error;
}
while (i + 1 < pop.slices.size) {
StackSlice next_slice = pop.slices.contents[i + 1];
@ -540,14 +495,11 @@ static Reduction parser__reduce(Parser *self, StackVersion version,
allow_skipping) {
StackVersion other_version =
ts_stack_duplicate_version(self->stack, slice.version);
CHECK(other_version != STACK_VERSION_NONE);
CHECK(
ts_stack_push(self->stack, other_version, parent, false, ERROR_STATE));
ts_stack_push(self->stack, other_version, parent, false, ERROR_STATE);
for (size_t j = parent->child_count; j < slice.trees.size; j++) {
TSTree *tree = slice.trees.contents[j];
CHECK(
ts_stack_push(self->stack, other_version, tree, false, ERROR_STATE));
ts_stack_push(self->stack, other_version, tree, false, ERROR_STATE);
}
ErrorStatus error_status =
@ -556,10 +508,10 @@ static Reduction parser__reduce(Parser *self, StackVersion version,
ts_stack_remove_version(self->stack, other_version);
}
CHECK(parser__push(self, slice.version, parent, action->params.to_state));
parser__push(self, slice.version, parent, action->params.to_state);
for (size_t j = parent->child_count; j < slice.trees.size; j++) {
TSTree *tree = slice.trees.contents[j];
CHECK(parser__push(self, slice.version, tree, action->params.to_state));
parser__push(self, slice.version, tree, action->params.to_state);
}
}
@ -573,10 +525,7 @@ static Reduction parser__reduce(Parser *self, StackVersion version,
}
}
return (Reduction){ ReduceSucceeded, pop.slices.contents[0] };
error:
return (Reduction){ ReduceFailed, {} };
return (Reduction){ false, pop.slices.contents[0] };
}
static inline const TSParseAction *parser__reductions_after_sequence(
@ -698,7 +647,7 @@ static StackIterateAction parser__error_repair_callback(
return result;
}
static RepairResult parser__repair_error(Parser *self, StackSlice slice,
static bool parser__repair_error(Parser *self, StackSlice slice,
TSSymbol lookahead_symbol,
const TSParseAction *actions,
size_t action_count) {
@ -719,21 +668,18 @@ static RepairResult parser__repair_error(Parser *self, StackSlice slice,
if ((child_count > session.tree_count_above_error) ||
(child_count == session.tree_count_above_error &&
!ts_language_symbol_metadata(self->language, symbol).visible))
CHECK(array_push(
&self->reduce_actions,
((ReduceAction){.symbol = symbol, .count = child_count })));
array_push(&self->reduce_actions, ((ReduceAction){symbol, child_count }));
}
}
StackPopResult pop = ts_stack_iterate(
self->stack, slice.version, parser__error_repair_callback, &session);
CHECK(pop.status);
if (!session.found_repair) {
LOG("no_repair_found");
ts_stack_remove_version(self->stack, slice.version);
ts_tree_array_delete(&slice.trees);
return RepairNoneFound;
return false;
}
ReduceAction repair = session.best_repair;
@ -754,12 +700,11 @@ static RepairResult parser__repair_error(Parser *self, StackSlice slice,
}
TreeArray skipped_children = array_new();
CHECK(array_grow(&skipped_children, skip_count));
array_grow(&skipped_children, skip_count);
for (size_t i = count_below; i < children.size; i++)
array_push(&skipped_children, children.contents[i]);
TSTree *error = ts_tree_make_error_node(&skipped_children);
CHECK(error);
children.size = count_below;
array_push(&children, error);
@ -770,24 +715,19 @@ static RepairResult parser__repair_error(Parser *self, StackSlice slice,
TSTree *parent =
ts_tree_make_node(symbol, children.size, children.contents,
ts_language_symbol_metadata(self->language, symbol));
CHECK(parent);
CHECK(parser__push(self, slice.version, parent, next_state));
parser__push(self, slice.version, parent, next_state);
ts_stack_decrease_push_count(self->stack, slice.version, error->child_count);
ErrorStatus error_status = ts_stack_error_status(self->stack, slice.version);
if (parser__better_version_exists(self, slice.version, error_status)) {
LOG("no_better_repair_found");
ts_stack_halt(self->stack, slice.version);
return RepairNoneFound;
return false;
} else {
LOG("repair_found sym:%s, child_count:%lu, cost:%u", SYM_NAME(symbol),
repair.count, parent->error_cost);
return RepairSucceeded;
return true;
}
error:
ts_tree_array_delete(&slice.trees);
return RepairFailed;
}
static void parser__start(Parser *self, TSInput input, TSTree *previous_tree) {
@ -804,14 +744,12 @@ static void parser__start(Parser *self, TSInput input, TSTree *previous_tree) {
self->finished_tree = NULL;
}
static bool parser__accept(Parser *self, StackVersion version,
static void parser__accept(Parser *self, StackVersion version,
TSTree *lookahead) {
lookahead->extra = true;
assert(lookahead->symbol == ts_builtin_sym_end);
CHECK(ts_stack_push(self->stack, version, lookahead, false, 1));
ts_stack_push(self->stack, version, lookahead, false, 1);
StackPopResult pop = ts_stack_pop_all(self->stack, version);
CHECK(pop.status);
CHECK(pop.slices.size);
for (size_t i = 0; i < pop.slices.size; i++) {
StackSlice slice = pop.slices.contents[i];
@ -825,11 +763,11 @@ static bool parser__accept(Parser *self, StackVersion version,
for (size_t j = trees.size - 1; j + 1 > 0; j--) {
TSTree *child = trees.contents[j];
if (!child->extra) {
CHECK(root = ts_tree_make_copy(child));
root = ts_tree_make_copy(child);
root->child_count = 0;
for (size_t k = 0; k < child->child_count; k++)
ts_tree_retain(child->children[k]);
CHECK(array_splice(&trees, j, 1, child->child_count, child->children));
array_splice(&trees, j, 1, child->child_count, child->children);
ts_tree_set_children(root, trees.size, trees.contents);
ts_tree_release(child);
break;
@ -848,22 +786,9 @@ static bool parser__accept(Parser *self, StackVersion version,
ts_stack_remove_version(self->stack, pop.slices.contents[0].version);
ts_stack_halt(self->stack, version);
return true;
error:
for (size_t i = 0; i < pop.slices.size; i++)
ts_tree_array_delete(&pop.slices.contents[i].trees);
return false;
}
typedef enum {
PotentialReductionsFailed,
PotentialReductionsContinue,
PotentialReductionsDone,
} PotentialReductionStatus;
static PotentialReductionStatus parser__do_potential_reductions(
static bool parser__do_potential_reductions(
Parser *self, StackVersion version) {
bool has_shift_action = false;
TSStateId state = ts_stack_top_state(self->stack, version);
@ -884,11 +809,10 @@ static PotentialReductionStatus parser__do_potential_reductions(
break;
case TSParseActionTypeReduce:
if (action.params.child_count > 0)
CHECK(ts_reduce_action_set_add(
&self->reduce_actions,
(ReduceAction){
.symbol = action.params.symbol, .count = action.params.child_count,
}));
ts_reduce_action_set_add(&self->reduce_actions, (ReduceAction){
.symbol = action.params.symbol,
.count = action.params.child_count,
});
default:
break;
}
@ -900,32 +824,25 @@ static PotentialReductionStatus parser__do_potential_reductions(
ReduceAction action = self->reduce_actions.contents[i];
Reduction reduction =
parser__reduce(self, version, action.symbol, action.count, true, false);
switch (reduction.status) {
case ReduceFailed:
goto error;
case ReduceStoppedAtError:
ts_tree_array_delete(&reduction.slice.trees);
ts_stack_remove_version(self->stack, reduction.slice.version);
continue;
default:
did_reduce = true;
break;
if (reduction.stopped_at_error) {
ts_tree_array_delete(&reduction.slice.trees);
ts_stack_remove_version(self->stack, reduction.slice.version);
continue;
} else {
did_reduce = true;
}
}
if (did_reduce) {
if (has_shift_action) {
return PotentialReductionsDone;
return true;
} else {
ts_stack_renumber_version(self->stack, previous_version_count, version);
return PotentialReductionsContinue;
return false;
}
} else {
return PotentialReductionsDone;
return true;
}
error:
return PotentialReductionsFailed;
}
typedef struct {
@ -949,12 +866,11 @@ static StackIterateAction parser__repair_consumed_error_callback(
return StackIterateNone;
}
static bool parser__repair_consumed_error(Parser *self, StackVersion version,
static void parser__repair_consumed_error(Parser *self, StackVersion version,
TSSymbol lookahead_symbol) {
SkipPrecedingTokensSession session = { self, lookahead_symbol };
StackPopResult pop = ts_stack_iterate(
self->stack, version, parser__repair_consumed_error_callback, &session);
CHECK(pop.status);
StackVersion last_slice_version = STACK_VERSION_NONE;
for (size_t i = 0; i < pop.slices.size; i++) {
@ -966,96 +882,73 @@ static bool parser__repair_consumed_error(Parser *self, StackVersion version,
last_slice_version = slice.version;
TSTree *error = ts_tree_make_error_node(&slice.trees);
CHECK(error);
error->extra = true;
TSStateId state = ts_stack_top_state(self->stack, slice.version);
parser__push(self, slice.version, error, state);
}
return true;
error:
return false;
}
static bool parser__handle_error(Parser *self, StackVersion version,
static void parser__handle_error(Parser *self, StackVersion version,
TSSymbol lookahead_symbol) {
ErrorStatus error_status = ts_stack_error_status(self->stack, version);
error_status.count++;
if (parser__better_version_exists(self, version, error_status)) {
ts_stack_halt(self->stack, version);
LOG("bail_on_error");
return true;
return;
}
LOG("handle_error");
CHECK(parser__repair_consumed_error(self, version, lookahead_symbol));
parser__repair_consumed_error(self, version, lookahead_symbol);
size_t previous_version_count = ts_stack_version_count(self->stack);
for (StackVersion v = version; v < ts_stack_version_count(self->stack);) {
switch (parser__do_potential_reductions(self, v)) {
case PotentialReductionsFailed:
goto error;
case PotentialReductionsContinue:
break;
case PotentialReductionsDone:
if (v == version) {
v = previous_version_count;
} else {
v++;
}
break;
if (parser__do_potential_reductions(self, v)) {
if (v == version) {
v = previous_version_count;
} else {
v++;
}
}
}
CHECK(ts_stack_push(self->stack, version, NULL, false, ERROR_STATE));
ts_stack_push(self->stack, version, NULL, false, ERROR_STATE);
while (ts_stack_version_count(self->stack) > previous_version_count) {
CHECK(ts_stack_push(self->stack, previous_version_count, NULL, false,
ERROR_STATE));
ts_stack_push(self->stack, previous_version_count, NULL, false, ERROR_STATE);
assert(ts_stack_merge(self->stack, version, previous_version_count));
}
return true;
error:
return false;
}
static bool parser__recover(Parser *self, StackVersion version, TSStateId state,
static void parser__recover(Parser *self, StackVersion version, TSStateId state,
TSTree *lookahead) {
if (lookahead->symbol == ts_builtin_sym_end) {
LOG("recover_eof");
TreeArray children = array_new();
TSTree *parent = ts_tree_make_error_node(&children);
CHECK(parser__push(self, version, parent, 1));
return parser__accept(self, version, lookahead);
parser__push(self, version, parent, 1);
parser__accept(self, version, lookahead);
}
LOG("recover state:%u", state);
StackVersion new_version = ts_stack_duplicate_version(self->stack, version);
CHECK(new_version != STACK_VERSION_NONE);
CHECK(parser__shift(
parser__shift(
self, new_version, ERROR_STATE, lookahead,
ts_language_symbol_metadata(self->language, lookahead->symbol).extra));
ts_language_symbol_metadata(self->language, lookahead->symbol).extra);
ErrorStatus error_status = ts_stack_error_status(self->stack, new_version);
if (parser__better_version_exists(self, version, error_status)) {
ts_stack_remove_version(self->stack, new_version);
LOG("bail_on_recovery");
}
CHECK(parser__shift(self, version, state, lookahead, false));
return true;
error:
return false;
parser__shift(self, version, state, lookahead, false);
}
static bool parser__advance(Parser *self, StackVersion version,
static void parser__advance(Parser *self, StackVersion version,
ReusableNode *reusable_node) {
bool validated_lookahead = false;
TSTree *lookahead = parser__get_lookahead(self, version, reusable_node);
CHECK(lookahead);
for (;;) {
TSStateId state = ts_stack_top_state(self->stack, version);
@ -1073,7 +966,6 @@ static bool parser__advance(Parser *self, StackVersion version,
ts_tree_release(lookahead);
lookahead = parser__get_lookahead(self, version, reusable_node);
CHECK(lookahead);
continue;
}
@ -1097,7 +989,6 @@ static bool parser__advance(Parser *self, StackVersion version,
parser__pop_reusable_node(reusable_node);
ts_tree_release(lookahead);
lookahead = parser__get_lookahead(self, version, reusable_node);
CHECK(lookahead);
}
}
@ -1114,14 +1005,13 @@ static bool parser__advance(Parser *self, StackVersion version,
LOG("shift state:%u", next_state);
}
CHECK(
parser__shift(self, version, next_state, lookahead, action.extra));
parser__shift(self, version, next_state, lookahead, action.extra);
if (lookahead == reusable_node->tree)
parser__pop_reusable_node(reusable_node);
ts_tree_release(lookahead);
return true;
return;
}
case TSParseActionTypeReduce: {
@ -1135,28 +1025,17 @@ static bool parser__advance(Parser *self, StackVersion version,
parser__reduce(self, version, action.params.symbol, action.params.child_count,
(i < table_entry.action_count - 1), true);
switch (reduction.status) {
case ReduceFailed:
goto error;
case ReduceSucceeded:
if (reduction.stopped_at_error) {
reduction_stopped_at_error = true;
if (parser__repair_error(
self, reduction.slice, lookahead->first_leaf.symbol,
table_entry.actions, table_entry.action_count)) {
last_reduction_version = reduction.slice.version;
break;
case ReduceStoppedAtError: {
reduction_stopped_at_error = true;
switch (parser__repair_error(
self, reduction.slice, lookahead->first_leaf.symbol,
table_entry.actions, table_entry.action_count)) {
case RepairFailed:
goto error;
case RepairNoneFound:
break;
case RepairSucceeded:
last_reduction_version = reduction.slice.version;
break;
}
break;
}
} else {
last_reduction_version = reduction.slice.version;
}
break;
}
@ -1165,10 +1044,10 @@ static bool parser__advance(Parser *self, StackVersion version,
continue;
LOG("accept");
CHECK(parser__accept(self, version, lookahead));
parser__accept(self, version, lookahead);
ts_tree_release(lookahead);
return true;
return;
}
case TSParseActionTypeRecover: {
@ -1181,13 +1060,13 @@ static bool parser__advance(Parser *self, StackVersion version,
action =
*ts_language_last_action(self->language, state, lookahead->symbol);
CHECK(parser__recover(self, version, action.params.to_state, lookahead));
parser__recover(self, version, action.params.to_state, lookahead);
if (lookahead == reusable_node->tree)
parser__pop_reusable_node(reusable_node);
ts_tree_release(lookahead);
return true;
return;
}
}
}
@ -1198,58 +1077,33 @@ static bool parser__advance(Parser *self, StackVersion version,
continue;
}
switch (parser__breakdown_top_of_stack(self, version)) {
case BreakdownFailed:
goto error;
case BreakdownPerformed:
continue;
case BreakdownAborted:
break;
if (parser__breakdown_top_of_stack(self, version)) {
continue;
}
if (state == ERROR_STATE) {
return parser__push(self, version, lookahead, ERROR_STATE);
parser__push(self, version, lookahead, ERROR_STATE);
return;
}
CHECK(parser__handle_error(self, version, lookahead->symbol));
parser__handle_error(self, version, lookahead->symbol);
if (ts_stack_is_halted(self->stack, version)) {
ts_tree_release(lookahead);
return true;
return;
}
}
error:
if (lookahead)
ts_tree_release(lookahead);
return false;
}
bool parser_init(Parser *self) {
ts_lexer_init(&self->lexer);
self->finished_tree = NULL;
self->stack = NULL;
array_init(&self->reduce_actions);
array_init(&self->tree_path1);
array_init(&self->tree_path2);
array_grow(&self->reduce_actions, 4);
self->stack = ts_stack_new();
if (!self->stack)
goto error;
if (!array_grow(&self->reduce_actions, 4))
goto error;
self->finished_tree = NULL;
return true;
error:
if (self->stack) {
ts_stack_delete(self->stack);
self->stack = NULL;
}
if (self->reduce_actions.contents)
array_delete(&self->reduce_actions);
return false;
}
void parser_destroy(Parser *self) {
@ -1287,7 +1141,7 @@ TSTree *parser_parse(Parser *self, TSInput input, TSTree *old_tree) {
ts_stack_top_position(self->stack, version).extent.row + 1,
ts_stack_top_position(self->stack, version).extent.column + 1);
CHECK(parser__advance(self, version, &reusable_node));
parser__advance(self, version, &reusable_node);
LOG_STACK();
}
}
@ -1306,9 +1160,6 @@ TSTree *parser_parse(Parser *self, TSInput input, TSTree *old_tree) {
LOG_TREE();
ts_stack_clear(self->stack);
parser__clear_cached_token(self);
CHECK(ts_tree_assign_parents(self->finished_tree, &self->tree_path1));
ts_tree_assign_parents(self->finished_tree, &self->tree_path1);
return self->finished_tree;
error:
return NULL;
}

View file

@ -15,14 +15,14 @@ typedef struct {
typedef Array(ReduceAction) ReduceActionSet;
static inline bool ts_reduce_action_set_add(ReduceActionSet *self,
static inline void ts_reduce_action_set_add(ReduceActionSet *self,
ReduceAction new_action) {
for (size_t i = 0; i < self->size; i++) {
ReduceAction action = self->contents[i];
if (action.symbol == new_action.symbol && action.count == new_action.count)
return true;
return;
}
return array_push(self, new_action);
array_push(self, new_action);
}
#ifdef __cplusplus

View file

@ -79,8 +79,11 @@ static void stack_node_release(StackNode *self, StackNodeArray *pool) {
stack_node_release(self->links[i].node, pool);
}
if (pool->size >= MAX_NODE_POOL_SIZE || !array_push(pool, self))
if (pool->size < MAX_NODE_POOL_SIZE) {
array_push(pool, self);
} else {
ts_free(self);
}
}
}
@ -167,29 +170,29 @@ static void stack_node_add_link(StackNode *self, StackLink link) {
static StackVersion ts_stack__add_version(Stack *self, StackNode *node,
unsigned push_count) {
StackHead head = {
.node = node, .is_halted = false, .push_count = push_count,
.node = node,
.is_halted = false,
.push_count = push_count,
};
if (!array_push(&self->heads, head))
return STACK_VERSION_NONE;
array_push(&self->heads, head);
stack_node_retain(node);
return (StackVersion)(self->heads.size - 1);
}
static bool ts_stack__add_slice(Stack *self, StackNode *node, TreeArray *trees,
static void ts_stack__add_slice(Stack *self, StackNode *node, TreeArray *trees,
unsigned push_count) {
for (size_t i = self->slices.size - 1; i + 1 > 0; i--) {
StackVersion version = self->slices.contents[i].version;
if (self->heads.contents[version].node == node) {
StackSlice slice = { *trees, version };
return array_insert(&self->slices, i + 1, slice);
array_insert(&self->slices, i + 1, slice);
return;
}
}
StackVersion version = ts_stack__add_version(self, node, push_count);
if (version == STACK_VERSION_NONE)
return false;
StackSlice slice = { *trees, version };
return array_push(&self->slices, slice);
array_push(&self->slices, slice);
}
INLINE StackPopResult stack__iter(Stack *self, StackVersion version,
@ -206,8 +209,7 @@ INLINE StackPopResult stack__iter(Stack *self, StackVersion version,
.is_pending = true,
.push_count = 0,
};
if (!array_push(&self->iterators, iterator))
goto error;
array_push(&self->iterators, iterator);
while (self->iterators.size > 0) {
for (size_t i = 0, size = self->iterators.size; i < size; i++) {
@ -225,12 +227,9 @@ INLINE StackPopResult stack__iter(Stack *self, StackVersion version,
if (should_pop) {
TreeArray trees = iterator->trees;
if (!should_stop)
if (!ts_tree_array_copy(trees, &trees))
goto error;
ts_tree_array_copy(trees, &trees);
array_reverse(&trees);
if (!ts_stack__add_slice(self, node, &trees,
push_count + iterator->push_count))
goto error;
ts_stack__add_slice(self, node, &trees, push_count + iterator->push_count);
}
if (should_stop) {
@ -249,11 +248,9 @@ INLINE StackPopResult stack__iter(Stack *self, StackVersion version,
next_iterator = &self->iterators.contents[i];
} else {
link = node->links[j];
if (!array_push(&self->iterators, self->iterators.contents[i]))
goto error;
array_push(&self->iterators, self->iterators.contents[i]);
next_iterator = array_back(&self->iterators);
if (!ts_tree_array_copy(next_iterator->trees, &next_iterator->trees))
goto error;
ts_tree_array_copy(next_iterator->trees, &next_iterator->trees);
}
next_iterator->node = link.node;
@ -264,8 +261,7 @@ INLINE StackPopResult stack__iter(Stack *self, StackVersion version,
if (!link.is_pending)
next_iterator->is_pending = false;
}
if (!array_push(&next_iterator->trees, link.tree))
goto error;
array_push(&next_iterator->trees, link.tree);
ts_tree_retain(link.tree);
} else {
next_iterator->is_pending = false;
@ -274,60 +270,27 @@ INLINE StackPopResult stack__iter(Stack *self, StackVersion version,
}
}
return (StackPopResult){ StackPopSucceeded, self->slices };
error:
for (size_t i = 0; i < self->iterators.size; i++)
ts_tree_array_delete(&self->iterators.contents[i].trees);
array_clear(&self->slices);
return (StackPopResult){.status = StackPopFailed };
return (StackPopResult){ false, self->slices };
}
Stack *ts_stack_new() {
Stack *self = ts_calloc(1, sizeof(Stack));
if (!self)
goto error;
array_init(&self->heads);
array_init(&self->slices);
array_init(&self->iterators);
array_init(&self->node_pool);
if (!array_grow(&self->heads, 4))
goto error;
if (!array_grow(&self->slices, 4))
goto error;
if (!array_grow(&self->iterators, 4))
goto error;
if (!array_grow(&self->node_pool, MAX_NODE_POOL_SIZE))
goto error;
array_grow(&self->heads, 4);
array_grow(&self->slices, 4);
array_grow(&self->iterators, 4);
array_grow(&self->node_pool, MAX_NODE_POOL_SIZE);
self->base_node =
stack_node_new(NULL, NULL, false, 1, ts_length_zero(), &self->node_pool);
stack_node_retain(self->base_node);
if (!self->base_node)
goto error;
array_push(&self->heads, ((StackHead){ self->base_node, false, 0 }));
return self;
error:
if (self) {
if (self->heads.contents)
array_delete(&self->heads);
if (self->slices.contents)
array_delete(&self->slices);
if (self->iterators.contents)
array_delete(&self->iterators);
if (self->node_pool.contents)
array_delete(&self->node_pool);
ts_free(self);
}
return NULL;
}
void ts_stack_delete(Stack *self) {
@ -436,7 +399,7 @@ StackPopResult ts_stack_pop_count(Stack *self, StackVersion version,
.goal_tree_count = count, .found_error = false, .found_valid_path = false,
};
StackPopResult pop = stack__iter(self, version, pop_count_callback, &session);
if (pop.status && session.found_error) {
if (session.found_error) {
if (session.found_valid_path) {
StackSlice error_slice = pop.slices.contents[0];
ts_tree_array_delete(&error_slice.trees);
@ -447,7 +410,7 @@ StackPopResult ts_stack_pop_count(Stack *self, StackVersion version,
pop.slices.contents[i].version--;
}
} else {
pop.status = StackPopStoppedAtError;
pop.stopped_at_error = true;
}
}
return pop;
@ -503,8 +466,7 @@ void ts_stack_renumber_version(Stack *self, StackVersion v1, StackVersion v2) {
StackVersion ts_stack_duplicate_version(Stack *self, StackVersion version) {
assert(version < self->heads.size);
if (!array_push(&self->heads, self->heads.contents[version]))
return STACK_VERSION_NONE;
array_push(&self->heads, self->heads.contents[version]);
stack_node_retain(array_back(&self->heads)->node);
return self->heads.size - 1;
}
@ -568,8 +530,7 @@ bool ts_stack_print_dot_graph(Stack *self, const char **symbol_names, FILE *f) {
"node_head_%lu -> node_%p [label=%lu, fontcolor=blue, weight=10000, "
"labeltooltip=\"push_count: %u\"]\n",
i, head->node, i, head->push_count);
if (!array_push(&self->iterators, ((Iterator){.node = head->node })))
goto error;
array_push(&self->iterators, ((Iterator){.node = head->node }));
}
bool all_iterators_done = false;
@ -638,15 +599,13 @@ bool ts_stack_print_dot_graph(Stack *self, const char **symbol_names, FILE *f) {
if (j == 0) {
iterator->node = link.node;
} else {
if (!array_push(&self->iterators, *iterator))
goto error;
array_push(&self->iterators, *iterator);
Iterator *next_iterator = array_back(&self->iterators);
next_iterator->node = link.node;
}
}
if (!array_push(&visited_nodes, node))
goto error;
array_push(&visited_nodes, node);
}
}
@ -655,10 +614,4 @@ bool ts_stack_print_dot_graph(Stack *self, const char **symbol_names, FILE *f) {
array_delete(&visited_nodes);
ts_toggle_allocation_recording(was_recording_allocations);
return true;
error:
ts_toggle_allocation_recording(was_recording_allocations);
if (visited_nodes.contents)
array_delete(&visited_nodes);
return false;
}

View file

@ -24,11 +24,7 @@ typedef struct {
typedef Array(StackSlice) StackSliceArray;
typedef struct {
enum {
StackPopFailed,
StackPopStoppedAtError,
StackPopSucceeded,
} status;
bool stopped_at_error;
StackSliceArray slices;
} StackPopResult;

View file

@ -13,9 +13,6 @@ TSStateId TS_TREE_STATE_NONE = USHRT_MAX;
TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size,
TSSymbolMetadata metadata) {
TSTree *result = ts_malloc(sizeof(TSTree));
if (!result)
return NULL;
*result = (TSTree){
.ref_count = 1,
.symbol = sym,
@ -30,7 +27,6 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size,
.first_leaf.symbol = sym,
.has_changes = false,
};
return result;
}
@ -38,8 +34,6 @@ bool ts_tree_array_copy(TreeArray self, TreeArray *dest) {
TSTree **contents = NULL;
if (self.capacity > 0) {
contents = ts_calloc(self.capacity, sizeof(TSTree *));
if (!contents)
return false;
memcpy(contents, self.contents, self.size * sizeof(TSTree *));
for (size_t i = 0; i < self.size; i++)
ts_tree_retain(contents[i]);
@ -72,9 +66,6 @@ TSTree *ts_tree_make_error(TSLength size, TSLength padding, char lookahead_char)
(TSSymbolMetadata){
.visible = true, .named = true,
});
if (!result)
return NULL;
result->fragile_left = true;
result->fragile_right = true;
result->lookahead_char = lookahead_char;
@ -83,18 +74,14 @@ TSTree *ts_tree_make_error(TSLength size, TSLength padding, char lookahead_char)
TSTree *ts_tree_make_copy(TSTree *self) {
TSTree *result = ts_malloc(sizeof(TSTree));
if (!result)
return NULL;
*result = *self;
result->ref_count = 1;
return result;
}
bool ts_tree_assign_parents(TSTree *self, TreePath *path) {
void ts_tree_assign_parents(TSTree *self, TreePath *path) {
array_clear(path);
if (!array_push(path, ((TreePathEntry){self, ts_length_zero(), 0})))
return false;
array_push(path, ((TreePathEntry){self, ts_length_zero(), 0}));
while (path->size > 0) {
TSTree *tree = array_pop(path).tree;
TSLength offset = ts_length_zero();
@ -104,13 +91,11 @@ bool ts_tree_assign_parents(TSTree *self, TreePath *path) {
child->context.parent = tree;
child->context.index = i;
child->context.offset = offset;
if (!array_push(path, ((TreePathEntry){child, ts_length_zero(), 0})))
return false;
array_push(path, ((TreePathEntry){child, ts_length_zero(), 0}));
}
offset = ts_length_add(offset, ts_tree_total_size(child));
}
}
return true;
}
@ -172,9 +157,6 @@ TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count,
TSTree **children, TSSymbolMetadata metadata) {
TSTree *result =
ts_tree_make_leaf(symbol, ts_length_zero(), ts_length_zero(), metadata);
if (!result)
return NULL;
ts_tree_set_children(result, child_count, children);
return result;
}
@ -183,8 +165,7 @@ TSTree *ts_tree_make_error_node(TreeArray *children) {
for (size_t i = 0; i < children->size; i++) {
TSTree *child = children->contents[i];
if (child->symbol == ts_builtin_sym_error && child->child_count > 0) {
if (!array_splice(children, i, 1, child->child_count, child->children))
return NULL;
array_splice(children, i, 1, child->child_count, child->children);
i += child->child_count - 1;
for (size_t j = 0; j < child->child_count; j++)
ts_tree_retain(child->children[j]);
@ -196,9 +177,6 @@ TSTree *ts_tree_make_error_node(TreeArray *children) {
ts_builtin_sym_error, children->size, children->contents,
(TSSymbolMetadata){.extra = false, .visible = true, .named = true });
if (!result)
return NULL;
result->fragile_left = true;
result->fragile_right = true;
return result;

View file

@ -77,7 +77,7 @@ int ts_tree_compare(const TSTree *tree1, const TSTree *tree2);
size_t ts_tree_start_column(const TSTree *self);
size_t ts_tree_end_column(const TSTree *self);
void ts_tree_set_children(TSTree *, size_t, TSTree **);
bool ts_tree_assign_parents(TSTree *, TreePath *);
void ts_tree_assign_parents(TSTree *, TreePath *);
void ts_tree_edit(TSTree *, const TSInputEdit *edit);
char *ts_tree_string(const TSTree *, const TSLanguage *, bool include_all);
void ts_tree_print_dot_graph(const TSTree *, const TSLanguage *, FILE *);

View file

@ -6,24 +6,23 @@
typedef Array(TSRange) RangeArray;
static bool range_array_add(RangeArray *results, TSPoint start, TSPoint end) {
static void range_array_add(RangeArray *results, TSPoint start, TSPoint end) {
if (results->size > 0) {
TSRange *last_range = array_back(results);
if (ts_point_lte(start, last_range->end)) {
last_range->end = end;
return true;
return;
}
}
if (ts_point_lt(start, end)) {
TSRange range = { start, end };
return array_push(results, range);
array_push(results, range);
}
return true;
}
static bool tree_path_descend(TreePath *path, TSPoint position) {
size_t original_size = path->size;
bool did_descend;
do {
did_descend = false;
@ -47,6 +46,7 @@ static bool tree_path_descend(TreePath *path, TSPoint position) {
child_position = child_right_position;
}
} while (did_descend);
path->size = original_size;
return false;
}
@ -82,7 +82,7 @@ static size_t tree_path_advance(TreePath *path) {
static void tree_path_ascend(TreePath *path, size_t count) {
for (size_t i = 0; i < count; i++) {
do {
array_pop(path);
path->size--;
} while (path->size > 0 && !array_back(path)->tree->visible);
}
}
@ -109,7 +109,7 @@ static bool tree_must_eq(TSTree *old_tree, TSTree *new_tree) {
);
}
static bool tree_path_get_changes(TreePath *old_path, TreePath *new_path,
static void tree_path_get_changes(TreePath *old_path, TreePath *new_path,
TSRange **ranges, size_t *range_count) {
TSPoint position = { 0, 0 };
RangeArray results = array_new();
@ -195,7 +195,6 @@ static bool tree_path_get_changes(TreePath *old_path, TreePath *new_path,
*ranges = results.contents;
*range_count = results.size;
return true;
}
#endif // RUNTIME_TREE_PATH_H_