2015-05-25 20:21:13 -07:00
|
|
|
#include "tree_sitter/parser.h"
|
2016-01-15 15:08:42 -08:00
|
|
|
#include "runtime/alloc.h"
|
2015-05-25 20:21:13 -07:00
|
|
|
#include "runtime/tree.h"
|
2016-02-17 20:41:29 -08:00
|
|
|
#include "runtime/array.h"
|
2015-09-18 18:04:52 -07:00
|
|
|
#include "runtime/stack.h"
|
2015-05-28 15:06:39 -07:00
|
|
|
#include "runtime/length.h"
|
2015-05-25 20:21:13 -07:00
|
|
|
#include <assert.h>
|
2016-01-28 21:18:57 -08:00
|
|
|
#include <stdio.h>
|
2015-05-25 20:21:13 -07:00
|
|
|
|
2016-04-11 22:41:06 -07:00
|
|
|
#define MAX_LINK_COUNT 8
|
2016-02-04 11:15:46 -08:00
|
|
|
#define MAX_NODE_POOL_SIZE 50
|
2015-06-03 09:44:13 -07:00
|
|
|
|
2016-04-15 21:28:00 -07:00
|
|
|
#define INLINE static inline __attribute__((always_inline))
|
2016-03-07 16:03:23 -08:00
|
|
|
|
2016-02-23 17:35:50 -08:00
|
|
|
typedef struct StackNode StackNode;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
StackNode *node;
|
|
|
|
|
TSTree *tree;
|
2016-03-31 12:03:07 -07:00
|
|
|
bool is_pending;
|
2016-02-23 17:35:50 -08:00
|
|
|
} StackLink;
|
|
|
|
|
|
|
|
|
|
struct StackNode {
|
2016-03-07 16:03:23 -08:00
|
|
|
TSStateId state;
|
|
|
|
|
TSLength position;
|
2016-04-11 22:41:06 -07:00
|
|
|
StackLink links[MAX_LINK_COUNT];
|
|
|
|
|
short unsigned int link_count;
|
2015-06-03 09:44:13 -07:00
|
|
|
short unsigned int ref_count;
|
2016-02-23 17:35:50 -08:00
|
|
|
};
|
2015-05-25 20:21:13 -07:00
|
|
|
|
2015-11-20 00:01:53 -08:00
|
|
|
typedef struct {
|
2016-02-17 20:41:29 -08:00
|
|
|
TreeArray trees;
|
2016-04-15 21:28:00 -07:00
|
|
|
size_t tree_count;
|
2016-03-07 16:03:23 -08:00
|
|
|
StackNode *node;
|
2016-03-31 12:03:07 -07:00
|
|
|
bool is_pending;
|
2015-11-20 00:01:53 -08:00
|
|
|
} PopPath;
|
|
|
|
|
|
2016-03-07 16:03:23 -08:00
|
|
|
typedef struct {
|
2016-04-04 11:59:10 -07:00
|
|
|
size_t goal_tree_count;
|
2016-03-07 16:03:23 -08:00
|
|
|
bool found_error;
|
|
|
|
|
} StackPopSession;
|
|
|
|
|
|
2016-02-25 21:46:13 -08:00
|
|
|
typedef Array(StackNode *) StackNodeArray;
|
|
|
|
|
|
2016-02-17 14:45:00 -08:00
|
|
|
struct Stack {
|
2016-02-17 20:41:29 -08:00
|
|
|
Array(StackNode *) heads;
|
2016-03-03 10:16:10 -08:00
|
|
|
StackSliceArray slices;
|
2016-02-17 20:41:29 -08:00
|
|
|
Array(PopPath) pop_paths;
|
2016-02-25 21:46:13 -08:00
|
|
|
StackNodeArray node_pool;
|
2016-02-17 14:45:00 -08:00
|
|
|
void *tree_selection_payload;
|
|
|
|
|
TreeSelectionFunction tree_selection_function;
|
2016-03-07 16:03:23 -08:00
|
|
|
StackNode *base_node;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const char *COLORS[] = {
|
|
|
|
|
"red", "blue", "orange", "green", "purple",
|
2016-02-17 14:45:00 -08:00
|
|
|
};
|
2016-02-08 12:08:15 -08:00
|
|
|
|
2016-03-07 16:03:23 -08:00
|
|
|
static void stack_node_retain(StackNode *self) {
|
|
|
|
|
if (!self)
|
|
|
|
|
return;
|
|
|
|
|
assert(self->ref_count != 0);
|
|
|
|
|
self->ref_count++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void stack_node_release(StackNode *self, StackNodeArray *pool) {
|
|
|
|
|
if (!self)
|
|
|
|
|
return;
|
|
|
|
|
assert(self->ref_count != 0);
|
|
|
|
|
self->ref_count--;
|
|
|
|
|
if (self->ref_count == 0) {
|
2016-04-11 22:41:06 -07:00
|
|
|
for (int i = 0; i < self->link_count; i++) {
|
|
|
|
|
ts_tree_release(self->links[i].tree);
|
|
|
|
|
stack_node_release(self->links[i].node, pool);
|
2016-03-07 16:03:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pool->size >= MAX_NODE_POOL_SIZE)
|
|
|
|
|
ts_free(self);
|
|
|
|
|
else
|
|
|
|
|
array_push(pool, self);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 12:03:07 -07:00
|
|
|
static StackNode *stack_node_new(StackNode *next, TSTree *tree, bool is_pending,
|
|
|
|
|
TSStateId state, TSLength position,
|
|
|
|
|
StackNodeArray *pool) {
|
2016-03-07 16:03:23 -08:00
|
|
|
StackNode *node;
|
|
|
|
|
if (pool->size > 0)
|
|
|
|
|
node = array_pop(pool);
|
|
|
|
|
else if (!(node = ts_malloc(sizeof(StackNode))))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
*node = (StackNode){
|
|
|
|
|
.ref_count = 1,
|
2016-04-11 22:41:06 -07:00
|
|
|
.link_count = 0,
|
|
|
|
|
.links = {},
|
2016-03-07 16:03:23 -08:00
|
|
|
.state = state,
|
|
|
|
|
.position = position,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (next) {
|
|
|
|
|
ts_tree_retain(tree);
|
|
|
|
|
stack_node_retain(next);
|
2016-04-11 22:41:06 -07:00
|
|
|
node->link_count = 1;
|
|
|
|
|
node->links[0] = (StackLink){ next, tree, is_pending };
|
2016-03-07 16:03:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-10 14:12:24 -07:00
|
|
|
static void stack_node_add_link(StackNode *self, StackLink link) {
|
2016-04-11 22:41:06 -07:00
|
|
|
for (int i = 0; i < self->link_count; i++) {
|
|
|
|
|
StackLink existing_link = self->links[i];
|
2016-04-10 14:12:24 -07:00
|
|
|
if (existing_link.tree == link.tree) {
|
|
|
|
|
if (existing_link.node == link.node)
|
2016-03-07 16:03:23 -08:00
|
|
|
return;
|
2016-04-10 14:12:24 -07:00
|
|
|
if (existing_link.node->state == link.node->state) {
|
2016-04-11 22:41:06 -07:00
|
|
|
for (int j = 0; j < link.node->link_count; j++)
|
|
|
|
|
stack_node_add_link(existing_link.node, link.node->links[j]);
|
2016-03-07 16:03:23 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-11 22:41:06 -07:00
|
|
|
if (self->link_count < MAX_LINK_COUNT) {
|
2016-04-10 14:12:24 -07:00
|
|
|
stack_node_retain(link.node);
|
|
|
|
|
ts_tree_retain(link.tree);
|
2016-04-11 22:41:06 -07:00
|
|
|
self->links[self->link_count++] = (StackLink){
|
2016-04-10 14:12:24 -07:00
|
|
|
link.node, link.tree, link.is_pending,
|
2016-03-10 11:43:13 -08:00
|
|
|
};
|
|
|
|
|
}
|
2016-03-07 16:03:23 -08:00
|
|
|
}
|
|
|
|
|
|
2016-02-08 12:08:15 -08:00
|
|
|
static int ts_stack__default_tree_selection(void *p, TSTree *t1, TSTree *t2) {
|
|
|
|
|
return 0;
|
2015-12-08 12:20:50 -08:00
|
|
|
}
|
|
|
|
|
|
2016-04-15 21:28:00 -07:00
|
|
|
static StackVersion ts_stack__add_version(Stack *self, StackNode *node) {
|
|
|
|
|
if (!array_push(&self->heads, node))
|
|
|
|
|
return STACK_VERSION_NONE;
|
|
|
|
|
stack_node_retain(node);
|
|
|
|
|
return (StackVersion)(self->heads.size - 1);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-15 21:33:31 -07:00
|
|
|
static void ts_stack__update_slice(Stack *self, StackSlice *slice,
|
|
|
|
|
TreeArray *trees) {
|
2016-02-08 12:08:15 -08:00
|
|
|
bool should_update = false;
|
2016-04-15 21:28:00 -07:00
|
|
|
if (slice->trees.size < trees->size) {
|
2016-02-08 12:08:15 -08:00
|
|
|
should_update = true;
|
2016-04-15 21:28:00 -07:00
|
|
|
} else if (slice->trees.size == trees->size) {
|
2016-03-03 10:16:10 -08:00
|
|
|
for (size_t i = 0; i < slice->trees.size; i++) {
|
|
|
|
|
TSTree *tree = slice->trees.contents[i];
|
2016-04-15 21:28:00 -07:00
|
|
|
TSTree *new_tree = trees->contents[i];
|
2016-02-17 14:45:00 -08:00
|
|
|
int comparison = self->tree_selection_function(
|
|
|
|
|
self->tree_selection_payload, tree, new_tree);
|
2016-02-08 12:08:15 -08:00
|
|
|
if (comparison < 0) {
|
|
|
|
|
break;
|
|
|
|
|
} else if (comparison > 0) {
|
|
|
|
|
should_update = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (should_update) {
|
2016-03-07 16:03:23 -08:00
|
|
|
ts_tree_array_delete(&slice->trees);
|
2016-04-15 21:28:00 -07:00
|
|
|
slice->trees = *trees;
|
2016-02-08 12:08:15 -08:00
|
|
|
} else {
|
2016-04-15 21:28:00 -07:00
|
|
|
ts_tree_array_delete(trees);
|
2015-07-15 09:21:53 -07:00
|
|
|
}
|
2015-06-03 09:44:13 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-15 21:28:00 -07:00
|
|
|
static bool ts_stack__add_slice(Stack *self, size_t previous_version_count,
|
|
|
|
|
StackNode *node, TreeArray *trees) {
|
|
|
|
|
for (size_t i = 0; i < self->slices.size; i++) {
|
|
|
|
|
StackSlice *previous_slice = &self->slices.contents[i];
|
|
|
|
|
size_t version_index = previous_version_count + i;
|
|
|
|
|
if (self->heads.contents[version_index] == node) {
|
2016-04-15 21:33:31 -07:00
|
|
|
ts_stack__update_slice(self, previous_slice, trees);
|
2016-04-15 21:28:00 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2016-04-15 21:28:00 -07:00
|
|
|
StackVersion version = ts_stack__add_version(self, node);
|
|
|
|
|
if (version == STACK_VERSION_NONE)
|
|
|
|
|
return false;
|
|
|
|
|
StackSlice slice = {.version = version, .trees = *trees };
|
|
|
|
|
return array_push(&self->slices, slice);
|
2015-06-03 09:44:13 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-15 21:28:00 -07:00
|
|
|
INLINE StackPopResult stack__iter(Stack *self, StackVersion version,
|
|
|
|
|
StackIterateCallback callback, void *payload) {
|
|
|
|
|
size_t previous_version_count = self->heads.size;
|
|
|
|
|
array_clear(&self->slices);
|
|
|
|
|
|
2016-03-07 16:03:23 -08:00
|
|
|
PopPath pop_path = {
|
2016-04-10 14:12:24 -07:00
|
|
|
.node = *array_get(&self->heads, version),
|
2016-03-31 12:03:07 -07:00
|
|
|
.trees = array_new(),
|
2016-04-15 21:28:00 -07:00
|
|
|
.tree_count = 0,
|
2016-03-31 12:03:07 -07:00
|
|
|
.is_pending = true,
|
2015-11-20 00:01:53 -08:00
|
|
|
};
|
2016-04-11 23:12:50 -07:00
|
|
|
array_clear(&self->pop_paths);
|
2016-03-07 16:03:23 -08:00
|
|
|
if (!array_push(&self->pop_paths, pop_path))
|
2016-01-19 18:07:24 -08:00
|
|
|
goto error;
|
2015-05-25 20:21:13 -07:00
|
|
|
|
2016-04-15 21:28:00 -07:00
|
|
|
while (self->pop_paths.size > 0) {
|
2016-03-07 16:03:23 -08:00
|
|
|
for (size_t i = 0, size = self->pop_paths.size; i < size; i++) {
|
2016-02-17 14:45:00 -08:00
|
|
|
PopPath *path = &self->pop_paths.contents[i];
|
2015-11-20 00:01:53 -08:00
|
|
|
StackNode *node = path->node;
|
2016-04-15 21:28:00 -07:00
|
|
|
bool is_done = node == self->base_node;
|
|
|
|
|
|
|
|
|
|
StackIterateAction action = callback(
|
|
|
|
|
payload, node->state, path->tree_count, is_done, path->is_pending);
|
|
|
|
|
|
|
|
|
|
bool should_pop = action & StackIteratePop;
|
|
|
|
|
bool should_stop = action & StackIterateStop || node->link_count == 0;
|
|
|
|
|
|
|
|
|
|
if (should_pop) {
|
|
|
|
|
TreeArray trees =
|
|
|
|
|
should_stop ? path->trees : ts_tree_array_copy(&path->trees);
|
|
|
|
|
array_reverse(&trees);
|
|
|
|
|
if (!ts_stack__add_slice(self, previous_version_count, node, &trees))
|
|
|
|
|
goto error;
|
2016-03-07 16:03:23 -08:00
|
|
|
}
|
2015-11-20 00:01:53 -08:00
|
|
|
|
2016-04-15 21:28:00 -07:00
|
|
|
if (should_stop) {
|
|
|
|
|
if (!should_pop)
|
|
|
|
|
ts_tree_array_delete(&path->trees);
|
2016-04-11 23:12:50 -07:00
|
|
|
array_erase(&self->pop_paths, i);
|
|
|
|
|
i--, size--;
|
2015-05-30 20:26:45 -07:00
|
|
|
continue;
|
2016-03-07 16:03:23 -08:00
|
|
|
}
|
|
|
|
|
|
2016-04-15 21:28:00 -07:00
|
|
|
for (size_t j = 1; j <= node->link_count; j++) {
|
2016-02-23 17:35:50 -08:00
|
|
|
PopPath *next_path;
|
2016-04-11 22:41:06 -07:00
|
|
|
StackLink link;
|
2016-04-15 21:28:00 -07:00
|
|
|
if (j == node->link_count) {
|
2016-04-11 22:41:06 -07:00
|
|
|
link = node->links[0];
|
2016-03-10 11:51:38 -08:00
|
|
|
next_path = &self->pop_paths.contents[i];
|
2016-02-23 17:35:50 -08:00
|
|
|
} else {
|
2016-04-11 22:41:06 -07:00
|
|
|
link = node->links[j];
|
2016-03-10 11:51:38 -08:00
|
|
|
if (!array_push(&self->pop_paths, self->pop_paths.contents[i]))
|
2016-02-23 17:35:50 -08:00
|
|
|
goto error;
|
|
|
|
|
next_path = array_back(&self->pop_paths);
|
2016-03-07 16:03:23 -08:00
|
|
|
next_path->trees = ts_tree_array_copy(&next_path->trees);
|
2016-02-23 17:35:50 -08:00
|
|
|
}
|
2015-11-20 00:01:53 -08:00
|
|
|
|
2016-04-11 22:41:06 -07:00
|
|
|
next_path->node = link.node;
|
|
|
|
|
if (!link.is_pending)
|
2016-03-31 12:03:07 -07:00
|
|
|
next_path->is_pending = false;
|
2016-04-11 23:12:50 -07:00
|
|
|
if (!link.tree->extra && link.tree->symbol != ts_builtin_sym_error)
|
2016-04-15 21:28:00 -07:00
|
|
|
next_path->tree_count++;
|
2016-04-11 23:12:50 -07:00
|
|
|
if (!array_push(&next_path->trees, link.tree))
|
|
|
|
|
goto error;
|
2016-04-11 22:41:06 -07:00
|
|
|
ts_tree_retain(link.tree);
|
2015-05-30 20:26:45 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-05-25 20:21:13 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-15 21:28:00 -07:00
|
|
|
return (StackPopResult){ StackPopSucceeded, self->slices };
|
2016-01-19 18:07:24 -08:00
|
|
|
|
|
|
|
|
error:
|
2016-03-07 16:03:23 -08:00
|
|
|
for (size_t i = 0; i < self->pop_paths.size; i++)
|
|
|
|
|
array_delete(&self->pop_paths.contents[i].trees);
|
|
|
|
|
array_clear(&self->slices);
|
2016-04-04 12:25:57 -07:00
|
|
|
return (StackPopResult){.status = StackPopFailed };
|
2016-03-07 16:03:23 -08:00
|
|
|
}
|
|
|
|
|
|
2016-04-15 21:33:31 -07:00
|
|
|
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->pop_paths);
|
|
|
|
|
array_init(&self->node_pool);
|
|
|
|
|
self->tree_selection_payload = NULL;
|
|
|
|
|
self->tree_selection_function = ts_stack__default_tree_selection;
|
|
|
|
|
|
|
|
|
|
if (!array_grow(&self->heads, 4))
|
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
if (!array_grow(&self->slices, 4))
|
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
if (!array_grow(&self->pop_paths, 4))
|
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
if (!array_grow(&self->node_pool, 20))
|
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
self->base_node =
|
|
|
|
|
stack_node_new(NULL, NULL, false, 0, ts_length_zero(), &self->node_pool);
|
|
|
|
|
stack_node_retain(self->base_node);
|
|
|
|
|
if (!self->base_node)
|
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
array_push(&self->heads, self->base_node);
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
if (self) {
|
|
|
|
|
if (self->heads.contents)
|
|
|
|
|
array_delete(&self->heads);
|
|
|
|
|
if (self->slices.contents)
|
|
|
|
|
array_delete(&self->slices);
|
|
|
|
|
if (self->pop_paths.contents)
|
|
|
|
|
array_delete(&self->pop_paths);
|
|
|
|
|
if (self->node_pool.contents)
|
|
|
|
|
array_delete(&self->node_pool);
|
|
|
|
|
ts_free(self);
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ts_stack_delete(Stack *self) {
|
|
|
|
|
if (self->pop_paths.contents)
|
|
|
|
|
array_delete(&self->slices);
|
|
|
|
|
if (self->pop_paths.contents)
|
|
|
|
|
array_delete(&self->pop_paths);
|
|
|
|
|
stack_node_release(self->base_node, &self->node_pool);
|
|
|
|
|
for (size_t i = 0; i < self->heads.size; i++)
|
|
|
|
|
stack_node_release(self->heads.contents[i], &self->node_pool);
|
|
|
|
|
array_clear(&self->heads);
|
|
|
|
|
if (self->node_pool.contents) {
|
|
|
|
|
for (size_t i = 0; i < self->node_pool.size; i++)
|
|
|
|
|
ts_free(self->node_pool.contents[i]);
|
|
|
|
|
array_delete(&self->node_pool);
|
|
|
|
|
}
|
|
|
|
|
array_delete(&self->heads);
|
|
|
|
|
ts_free(self);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t ts_stack_version_count(const Stack *self) {
|
|
|
|
|
return self->heads.size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TSStateId ts_stack_top_state(const Stack *self, StackVersion version) {
|
|
|
|
|
return (*array_get(&self->heads, version))->state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TSLength ts_stack_top_position(const Stack *self, StackVersion version) {
|
|
|
|
|
return (*array_get(&self->heads, version))->position;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ts_stack_push(Stack *self, StackVersion version, TSTree *tree,
|
|
|
|
|
bool is_pending, TSStateId state) {
|
|
|
|
|
StackNode *node = *array_get(&self->heads, version);
|
|
|
|
|
TSLength position = ts_length_add(node->position, ts_tree_total_size(tree));
|
|
|
|
|
StackNode *new_node =
|
|
|
|
|
stack_node_new(node, tree, is_pending, state, position, &self->node_pool);
|
|
|
|
|
if (!new_node)
|
|
|
|
|
return false;
|
|
|
|
|
stack_node_release(node, &self->node_pool);
|
|
|
|
|
self->heads.contents[version] = new_node;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-15 21:28:00 -07:00
|
|
|
StackPopResult ts_stack_iterate(Stack *self, StackVersion version,
|
|
|
|
|
StackIterateCallback callback, void *payload) {
|
|
|
|
|
return stack__iter(self, version, callback, payload);
|
2016-04-04 11:59:10 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-15 21:28:00 -07:00
|
|
|
INLINE StackIterateAction pop_count_callback(void *payload, TSStateId state,
|
|
|
|
|
size_t tree_count, bool is_done,
|
|
|
|
|
bool is_pending) {
|
2016-03-07 16:03:23 -08:00
|
|
|
StackPopSession *pop_session = (StackPopSession *)payload;
|
|
|
|
|
if (pop_session->found_error)
|
2016-04-15 21:28:00 -07:00
|
|
|
return StackIterateStop;
|
2016-04-04 11:59:10 -07:00
|
|
|
if (tree_count == pop_session->goal_tree_count)
|
2016-04-15 21:28:00 -07:00
|
|
|
return StackIteratePop | StackIterateStop;
|
2016-04-04 11:59:10 -07:00
|
|
|
if (state == ts_parse_state_error) {
|
|
|
|
|
pop_session->found_error = true;
|
2016-04-15 21:28:00 -07:00
|
|
|
return StackIteratePop | StackIterateStop;
|
2016-03-10 11:51:38 -08:00
|
|
|
}
|
2016-04-15 21:28:00 -07:00
|
|
|
return StackIterateNone;
|
2016-03-07 16:03:23 -08:00
|
|
|
}
|
|
|
|
|
|
2016-04-04 12:25:57 -07:00
|
|
|
StackPopResult ts_stack_pop_count(Stack *self, StackVersion version,
|
|
|
|
|
size_t count) {
|
|
|
|
|
StackPopSession session = {.goal_tree_count = count, .found_error = false };
|
2016-04-15 21:28:00 -07:00
|
|
|
StackPopResult pop = stack__iter(self, version, pop_count_callback, &session);
|
2016-04-03 23:46:43 -07:00
|
|
|
if (pop.status && session.found_error) {
|
|
|
|
|
pop.status = StackPopStoppedAtError;
|
2016-04-18 09:55:54 -07:00
|
|
|
StackSlice stopped_slice = array_pop(&pop.slices);
|
|
|
|
|
for (size_t i = 0; i < pop.slices.size; i++) {
|
|
|
|
|
StackSlice slice = pop.slices.contents[i];
|
2016-04-03 23:46:43 -07:00
|
|
|
ts_tree_array_delete(&slice.trees);
|
2016-04-04 12:25:57 -07:00
|
|
|
ts_stack_remove_version(self, slice.version);
|
2016-04-18 09:55:54 -07:00
|
|
|
stopped_slice.version--;
|
2016-03-10 21:26:21 -08:00
|
|
|
}
|
2016-04-18 09:55:54 -07:00
|
|
|
pop.slices.size = 1;
|
|
|
|
|
pop.slices.contents[0] = stopped_slice;
|
2016-03-07 16:03:23 -08:00
|
|
|
}
|
2016-04-03 23:46:43 -07:00
|
|
|
return pop;
|
2016-03-07 16:03:23 -08:00
|
|
|
}
|
|
|
|
|
|
2016-04-15 21:28:00 -07:00
|
|
|
INLINE StackIterateAction pop_pending_callback(void *payload, TSStateId state,
|
|
|
|
|
size_t tree_count, bool is_done,
|
|
|
|
|
bool is_pending) {
|
2016-04-11 23:12:50 -07:00
|
|
|
if (tree_count >= 1) {
|
|
|
|
|
if (is_pending) {
|
2016-04-15 21:28:00 -07:00
|
|
|
return StackIteratePop | StackIterateStop;
|
2016-04-11 23:12:50 -07:00
|
|
|
} else {
|
2016-04-15 21:28:00 -07:00
|
|
|
return StackIterateStop;
|
2016-04-11 23:12:50 -07:00
|
|
|
}
|
|
|
|
|
} else {
|
2016-04-15 21:28:00 -07:00
|
|
|
return StackIterateNone;
|
2016-04-11 23:12:50 -07:00
|
|
|
}
|
2015-05-25 20:21:13 -07:00
|
|
|
}
|
2015-06-18 15:04:03 -07:00
|
|
|
|
2016-04-04 12:25:57 -07:00
|
|
|
StackPopResult ts_stack_pop_pending(Stack *self, StackVersion version) {
|
2016-04-15 21:28:00 -07:00
|
|
|
StackPopResult pop = stack__iter(self, version, pop_pending_callback, NULL);
|
2016-04-10 14:12:24 -07:00
|
|
|
if (pop.slices.size > 0) {
|
|
|
|
|
ts_stack_renumber_version(self, pop.slices.contents[0].version, version);
|
|
|
|
|
pop.slices.contents[0].version = version;
|
|
|
|
|
}
|
|
|
|
|
return pop;
|
2016-03-31 12:03:07 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-15 21:28:00 -07:00
|
|
|
INLINE StackIterateAction pop_all_callback(void *payload, TSStateId state,
|
|
|
|
|
size_t tree_count, bool is_done,
|
|
|
|
|
bool is_pending) {
|
|
|
|
|
return is_done ? (StackIteratePop | StackIterateStop) : StackIterateNone;
|
2015-06-18 15:04:03 -07:00
|
|
|
}
|
2015-12-08 12:20:50 -08:00
|
|
|
|
2016-04-04 12:25:57 -07:00
|
|
|
TreeArray ts_stack_pop_all(Stack *self, StackVersion version) {
|
2016-04-15 21:28:00 -07:00
|
|
|
StackPopResult pop = stack__iter(self, version, pop_all_callback, NULL);
|
2016-04-04 11:44:45 -07:00
|
|
|
if (pop.status != StackPopSucceeded)
|
|
|
|
|
return (TreeArray)array_new();
|
|
|
|
|
assert(pop.slices.size == 1);
|
2016-04-10 14:12:24 -07:00
|
|
|
ts_stack_renumber_version(self, pop.slices.contents[0].version, version);
|
2016-04-04 11:44:45 -07:00
|
|
|
return pop.slices.contents[0].trees;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-15 21:33:31 -07:00
|
|
|
void ts_stack_remove_version(Stack *self, StackVersion version) {
|
|
|
|
|
StackNode *node = *array_get(&self->heads, version);
|
|
|
|
|
stack_node_release(node, &self->node_pool);
|
|
|
|
|
array_erase(&self->heads, version);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ts_stack_renumber_version(Stack *self, StackVersion v1, StackVersion v2) {
|
|
|
|
|
assert(v2 < v1);
|
|
|
|
|
assert((size_t)v1 < self->heads.size);
|
|
|
|
|
stack_node_release(self->heads.contents[v2], &self->node_pool);
|
|
|
|
|
self->heads.contents[v2] = self->heads.contents[v1];
|
|
|
|
|
array_erase(&self->heads, v1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ts_stack_merge(Stack *self) {
|
|
|
|
|
for (size_t i = 0; i < self->heads.size; i++) {
|
|
|
|
|
StackNode *node = self->heads.contents[i];
|
|
|
|
|
for (size_t j = 0; j < i; j++) {
|
|
|
|
|
StackNode *prior_node = self->heads.contents[j];
|
|
|
|
|
if (prior_node->state == node->state &&
|
|
|
|
|
prior_node->position.chars == node->position.chars) {
|
|
|
|
|
for (size_t k = 0; k < node->link_count; k++) {
|
|
|
|
|
StackLink link = node->links[k];
|
|
|
|
|
stack_node_add_link(prior_node, link);
|
|
|
|
|
}
|
|
|
|
|
ts_stack_remove_version(self, i--);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-04 11:59:10 -07:00
|
|
|
void ts_stack_clear(Stack *self) {
|
|
|
|
|
stack_node_retain(self->base_node);
|
|
|
|
|
for (size_t i = 0; i < self->heads.size; i++)
|
|
|
|
|
stack_node_release(self->heads.contents[i], &self->node_pool);
|
|
|
|
|
array_clear(&self->heads);
|
|
|
|
|
array_push(&self->heads, self->base_node);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-17 12:08:06 -08:00
|
|
|
void ts_stack_set_tree_selection_callback(Stack *self, void *payload,
|
|
|
|
|
TreeSelectionFunction function) {
|
|
|
|
|
self->tree_selection_payload = payload;
|
|
|
|
|
self->tree_selection_function = function;
|
2015-12-08 12:20:50 -08:00
|
|
|
}
|
2016-01-28 21:18:57 -08:00
|
|
|
|
2016-04-02 22:18:44 -07:00
|
|
|
int ts_stack_print_dot_graph(Stack *self, const char **symbol_names, FILE *f) {
|
|
|
|
|
fprintf(f, "digraph stack {\n");
|
|
|
|
|
fprintf(f, "rankdir=\"RL\";\n");
|
2016-02-23 00:08:55 -08:00
|
|
|
|
2016-02-25 21:46:13 -08:00
|
|
|
Array(StackNode *)visited_nodes;
|
2016-02-23 17:35:50 -08:00
|
|
|
array_init(&visited_nodes);
|
|
|
|
|
|
2016-02-23 00:08:55 -08:00
|
|
|
array_clear(&self->pop_paths);
|
2016-02-23 09:45:27 -08:00
|
|
|
for (size_t i = 0; i < self->heads.size; i++) {
|
|
|
|
|
StackNode *node = self->heads.contents[i];
|
2016-03-07 16:03:23 -08:00
|
|
|
size_t color_count = sizeof(COLORS) / sizeof(COLORS[0]);
|
|
|
|
|
const char *color = COLORS[i % color_count];
|
2016-04-02 22:18:44 -07:00
|
|
|
fprintf(f, "node_%p [color=%s];\n", node, color);
|
2016-02-25 21:46:13 -08:00
|
|
|
array_push(&self->pop_paths, ((PopPath){.node = node }));
|
2016-02-23 09:45:27 -08:00
|
|
|
}
|
2016-02-23 00:08:55 -08:00
|
|
|
|
|
|
|
|
bool all_paths_done = false;
|
|
|
|
|
while (!all_paths_done) {
|
|
|
|
|
all_paths_done = true;
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < self->pop_paths.size; i++) {
|
|
|
|
|
PopPath *path = &self->pop_paths.contents[i];
|
|
|
|
|
StackNode *node = path->node;
|
|
|
|
|
|
2016-02-23 17:35:50 -08:00
|
|
|
for (size_t j = 0; j < visited_nodes.size; j++) {
|
|
|
|
|
if (visited_nodes.contents[j] == node) {
|
|
|
|
|
node = NULL;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-23 00:08:55 -08:00
|
|
|
if (!node)
|
|
|
|
|
continue;
|
|
|
|
|
all_paths_done = false;
|
|
|
|
|
|
2016-04-02 22:18:44 -07:00
|
|
|
fprintf(f, "node_%p [label=", node);
|
2016-03-07 16:03:23 -08:00
|
|
|
if (node->state == ts_parse_state_error)
|
2016-04-02 22:18:44 -07:00
|
|
|
fprintf(f, "\"?\"");
|
2016-03-02 09:55:25 -08:00
|
|
|
else
|
2016-04-02 22:18:44 -07:00
|
|
|
fprintf(f, "%d", node->state);
|
|
|
|
|
fprintf(f, "];\n");
|
2016-02-23 00:08:55 -08:00
|
|
|
|
2016-04-11 22:41:06 -07:00
|
|
|
for (int j = 0; j < node->link_count; j++) {
|
|
|
|
|
StackLink link = node->links[j];
|
|
|
|
|
fprintf(f, "node_%p -> node_%p [", node, link.node);
|
|
|
|
|
if (link.is_pending)
|
2016-04-04 12:25:57 -07:00
|
|
|
fprintf(f, "style=dashed ");
|
2016-04-02 23:02:21 -07:00
|
|
|
fprintf(f, "label=\"");
|
2016-02-24 17:23:58 -08:00
|
|
|
|
2016-04-11 22:41:06 -07:00
|
|
|
if (link.tree->symbol == ts_builtin_sym_error) {
|
2016-04-02 22:18:44 -07:00
|
|
|
fprintf(f, "ERROR");
|
2016-03-02 09:55:25 -08:00
|
|
|
} else {
|
2016-04-11 22:41:06 -07:00
|
|
|
const char *name = symbol_names[link.tree->symbol];
|
2016-03-02 09:55:25 -08:00
|
|
|
for (const char *c = name; *c; c++) {
|
2016-04-02 22:18:44 -07:00
|
|
|
if (*c == '\"' || *c == '\\')
|
|
|
|
|
fprintf(f, "\\");
|
|
|
|
|
fprintf(f, "%c", *c);
|
2016-02-24 17:23:58 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-02 22:18:44 -07:00
|
|
|
fprintf(f, "\"];\n");
|
2016-02-23 00:08:55 -08:00
|
|
|
|
2016-02-23 17:35:50 -08:00
|
|
|
if (j == 0) {
|
2016-04-11 22:41:06 -07:00
|
|
|
path->node = link.node;
|
2016-02-23 17:35:50 -08:00
|
|
|
} else {
|
|
|
|
|
if (!array_push(&self->pop_paths, *path))
|
|
|
|
|
goto error;
|
|
|
|
|
PopPath *next_path = array_back(&self->pop_paths);
|
2016-04-11 22:41:06 -07:00
|
|
|
next_path->node = link.node;
|
2016-02-23 17:35:50 -08:00
|
|
|
}
|
2016-02-23 00:08:55 -08:00
|
|
|
}
|
2016-02-23 17:35:50 -08:00
|
|
|
|
|
|
|
|
if (!array_push(&visited_nodes, node))
|
|
|
|
|
goto error;
|
2016-02-23 00:08:55 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-02 22:18:44 -07:00
|
|
|
fprintf(f, "}\n");
|
2016-02-23 00:08:55 -08:00
|
|
|
|
2016-02-23 17:35:50 -08:00
|
|
|
array_delete(&visited_nodes);
|
2016-02-23 00:08:55 -08:00
|
|
|
|
|
|
|
|
error:
|
2016-04-02 22:18:44 -07:00
|
|
|
return -1;
|
2016-02-23 00:08:55 -08:00
|
|
|
}
|