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
|
|
|
|
2015-11-20 00:01:53 -08:00
|
|
|
#define MAX_SUCCESSOR_COUNT 8
|
2015-06-03 09:44:13 -07:00
|
|
|
#define INITIAL_HEAD_CAPACITY 3
|
2015-06-18 15:04:03 -07:00
|
|
|
#define STARTING_TREE_CAPACITY 10
|
2016-02-04 11:15:46 -08:00
|
|
|
#define MAX_NODE_POOL_SIZE 50
|
2015-06-03 09:44:13 -07:00
|
|
|
|
2016-03-07 16:03:23 -08:00
|
|
|
#define ALWAYS_INLINE __attribute__((always_inline))
|
|
|
|
|
|
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-02-23 17:35:50 -08:00
|
|
|
StackLink successors[MAX_SUCCESSOR_COUNT];
|
2015-06-03 09:44:13 -07:00
|
|
|
short unsigned int successor_count;
|
|
|
|
|
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-03-07 16:03:23 -08:00
|
|
|
size_t extra_count;
|
|
|
|
|
StackNode *node;
|
|
|
|
|
bool is_done;
|
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 {
|
|
|
|
|
int goal_tree_count;
|
|
|
|
|
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
|
|
|
/*
|
|
|
|
|
* Section: Manipulating nodes (Private)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
for (int i = 0; i < self->successor_count; i++) {
|
|
|
|
|
ts_tree_release(self->successors[i].tree);
|
|
|
|
|
stack_node_release(self->successors[i].node, pool);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
.successor_count = 0,
|
|
|
|
|
.successors = {},
|
|
|
|
|
.state = state,
|
|
|
|
|
.position = position,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (next) {
|
|
|
|
|
ts_tree_retain(tree);
|
|
|
|
|
stack_node_retain(next);
|
2016-03-31 12:03:07 -07:00
|
|
|
node->successor_count = 1;
|
|
|
|
|
node->successors[0] = (StackLink){ next, tree, is_pending };
|
2016-03-07 16:03:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void stack_node_add_successor(StackNode *self, TSTree *new_tree,
|
2016-03-31 12:03:07 -07:00
|
|
|
bool is_pending, StackNode *new_node) {
|
2016-03-07 16:03:23 -08:00
|
|
|
for (int i = 0; i < self->successor_count; i++) {
|
|
|
|
|
StackLink successor = self->successors[i];
|
|
|
|
|
if (successor.tree == new_tree) {
|
|
|
|
|
if (successor.node == new_node)
|
|
|
|
|
return;
|
|
|
|
|
if (successor.node && new_node &&
|
|
|
|
|
successor.node->state == new_node->state) {
|
|
|
|
|
for (int j = 0; j < new_node->successor_count; j++) {
|
|
|
|
|
stack_node_add_successor(successor.node, new_node->successors[j].tree,
|
2016-03-31 12:03:07 -07:00
|
|
|
is_pending, new_node->successors[j].node);
|
2016-03-07 16:03:23 -08:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-10 11:43:13 -08:00
|
|
|
if (self->successor_count < MAX_SUCCESSOR_COUNT) {
|
|
|
|
|
stack_node_retain(new_node);
|
|
|
|
|
ts_tree_retain(new_tree);
|
|
|
|
|
self->successors[self->successor_count++] = (StackLink){
|
2016-03-31 12:03:07 -07:00
|
|
|
new_node, new_tree, is_pending,
|
2016-03-10 11:43:13 -08:00
|
|
|
};
|
|
|
|
|
}
|
2016-03-07 16:03:23 -08:00
|
|
|
}
|
|
|
|
|
|
2015-05-25 20:21:13 -07:00
|
|
|
/*
|
2015-06-03 09:44:13 -07:00
|
|
|
* Section: Stack lifecycle
|
2015-05-25 20:21:13 -07: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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stack *ts_stack_new() {
|
2016-01-18 10:44:49 -08:00
|
|
|
Stack *self = ts_calloc(1, sizeof(Stack));
|
|
|
|
|
if (!self)
|
|
|
|
|
goto error;
|
|
|
|
|
|
2016-02-17 20:41:29 -08:00
|
|
|
array_init(&self->heads);
|
2016-03-03 10:16:10 -08:00
|
|
|
array_init(&self->slices);
|
2016-02-17 20:41:29 -08:00
|
|
|
array_init(&self->pop_paths);
|
|
|
|
|
array_init(&self->node_pool);
|
2016-02-04 11:15:46 -08:00
|
|
|
self->tree_selection_payload = NULL;
|
|
|
|
|
self->tree_selection_function = ts_stack__default_tree_selection;
|
|
|
|
|
|
2016-02-17 20:41:29 -08:00
|
|
|
if (!array_grow(&self->heads, 4))
|
2016-01-18 10:44:49 -08:00
|
|
|
goto error;
|
|
|
|
|
|
2016-03-03 10:16:10 -08:00
|
|
|
if (!array_grow(&self->slices, 4))
|
2016-01-18 10:44:49 -08:00
|
|
|
goto error;
|
|
|
|
|
|
2016-02-17 20:41:29 -08:00
|
|
|
if (!array_grow(&self->pop_paths, 4))
|
2016-02-04 11:15:46 -08:00
|
|
|
goto error;
|
|
|
|
|
|
2016-02-17 20:41:29 -08:00
|
|
|
if (!array_grow(&self->node_pool, 20))
|
2016-01-18 10:44:49 -08:00
|
|
|
goto error;
|
|
|
|
|
|
2016-03-07 16:03:23 -08:00
|
|
|
self->base_node =
|
2016-03-31 12:03:07 -07:00
|
|
|
stack_node_new(NULL, NULL, false, 0, ts_length_zero(), &self->node_pool);
|
2016-03-07 16:03:23 -08:00
|
|
|
stack_node_retain(self->base_node);
|
|
|
|
|
if (!self->base_node)
|
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
array_push(&self->heads, self->base_node);
|
2016-02-08 12:08:15 -08:00
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
return self;
|
2016-01-18 10:44:49 -08:00
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
if (self) {
|
2016-02-08 12:08:15 -08:00
|
|
|
if (self->heads.contents)
|
2016-02-17 20:41:29 -08:00
|
|
|
array_delete(&self->heads);
|
2016-03-03 10:16:10 -08:00
|
|
|
if (self->slices.contents)
|
|
|
|
|
array_delete(&self->slices);
|
2016-01-18 10:44:49 -08:00
|
|
|
if (self->pop_paths.contents)
|
2016-02-17 20:41:29 -08:00
|
|
|
array_delete(&self->pop_paths);
|
2016-02-04 11:15:46 -08:00
|
|
|
if (self->node_pool.contents)
|
2016-02-17 20:41:29 -08:00
|
|
|
array_delete(&self->node_pool);
|
2016-01-18 10:44:49 -08:00
|
|
|
ts_free(self);
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
2015-05-25 20:21:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2015-06-03 09:44:13 -07:00
|
|
|
* Section: Reading from the stack
|
2015-05-25 20:21:13 -07:00
|
|
|
*/
|
|
|
|
|
|
2016-02-25 21:46:13 -08:00
|
|
|
TSStateId ts_stack_top_state(const Stack *self, int head_index) {
|
2016-03-07 16:03:23 -08:00
|
|
|
return (*array_get(&self->heads, head_index))->state;
|
2015-06-18 15:04:03 -07:00
|
|
|
}
|
|
|
|
|
|
2016-02-25 21:46:13 -08:00
|
|
|
TSLength ts_stack_top_position(const Stack *self, int head_index) {
|
2016-03-07 16:03:23 -08:00
|
|
|
return (*array_get(&self->heads, head_index))->position;
|
2015-05-25 20:21:13 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
int ts_stack_head_count(const Stack *self) {
|
2016-02-08 12:08:15 -08:00
|
|
|
return self->heads.size;
|
2015-05-25 20:21:13 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-03 10:16:10 -08:00
|
|
|
static void ts_stack__merge_slice(Stack *self, StackSlice *slice,
|
|
|
|
|
StackSlice *new_slice) {
|
2016-02-08 12:08:15 -08:00
|
|
|
bool should_update = false;
|
2016-03-03 10:16:10 -08:00
|
|
|
if (slice->trees.size < new_slice->trees.size) {
|
2016-02-08 12:08:15 -08:00
|
|
|
should_update = true;
|
2016-03-03 10:16:10 -08:00
|
|
|
} else if (slice->trees.size == new_slice->trees.size) {
|
|
|
|
|
for (size_t i = 0; i < slice->trees.size; i++) {
|
|
|
|
|
TSTree *tree = slice->trees.contents[i];
|
|
|
|
|
TSTree *new_tree = new_slice->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-03-03 10:16:10 -08:00
|
|
|
slice->trees = new_slice->trees;
|
|
|
|
|
slice->trees.size = new_slice->trees.size;
|
2016-02-08 12:08:15 -08:00
|
|
|
} else {
|
2016-03-07 16:03:23 -08:00
|
|
|
ts_tree_array_delete(&new_slice->trees);
|
2015-07-15 09:21:53 -07:00
|
|
|
}
|
2015-06-03 09:44:13 -07:00
|
|
|
}
|
|
|
|
|
|
2015-05-25 20:21:13 -07:00
|
|
|
/*
|
2015-06-03 09:44:13 -07:00
|
|
|
* Section: Mutating the stack (Private)
|
2015-05-25 20:21:13 -07:00
|
|
|
*/
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
static int ts_stack__add_head(Stack *self, StackNode *node) {
|
2016-02-17 20:41:29 -08:00
|
|
|
if (array_push(&self->heads, node)) {
|
2016-02-08 12:08:15 -08:00
|
|
|
stack_node_retain(node);
|
|
|
|
|
return self->heads.size - 1;
|
|
|
|
|
} else {
|
|
|
|
|
return -1;
|
2015-06-03 09:44:13 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 16:03:23 -08:00
|
|
|
static int ts_stack__index_of_head(Stack *self, StackNode *node) {
|
2016-02-08 12:08:15 -08:00
|
|
|
for (size_t i = 0; i < self->heads.size; i++) {
|
2016-02-17 14:45:00 -08:00
|
|
|
if (self->heads.contents[i] == node)
|
2015-07-08 17:34:21 -07:00
|
|
|
return i;
|
2016-02-08 12:08:15 -08:00
|
|
|
}
|
2015-11-20 00:01:53 -08:00
|
|
|
return -1;
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
void ts_stack_remove_head(Stack *self, int head_index) {
|
2016-02-17 20:41:29 -08:00
|
|
|
StackNode *node = *array_get(&self->heads, head_index);
|
2016-02-25 21:46:13 -08:00
|
|
|
stack_node_release(node, &self->node_pool);
|
2016-02-17 20:41:29 -08:00
|
|
|
array_erase(&self->heads, head_index);
|
2015-06-03 09:44:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Section: Mutating the stack (Public)
|
|
|
|
|
*/
|
|
|
|
|
|
2016-02-25 21:46:13 -08:00
|
|
|
StackPushResult ts_stack_push(Stack *self, int head_index, TSTree *tree,
|
2016-03-31 12:03:07 -07:00
|
|
|
bool is_pending, TSStateId state) {
|
2016-02-17 20:41:29 -08:00
|
|
|
StackNode *current_head = *array_get(&self->heads, head_index);
|
2016-03-07 16:03:23 -08:00
|
|
|
TSLength position =
|
|
|
|
|
ts_length_add(current_head->position, ts_tree_total_size(tree));
|
2016-01-19 18:07:24 -08:00
|
|
|
|
2016-02-08 12:08:15 -08:00
|
|
|
for (int i = 0; i < head_index; i++) {
|
2016-02-17 14:45:00 -08:00
|
|
|
StackNode *prior_node = self->heads.contents[i];
|
2016-03-07 16:03:23 -08:00
|
|
|
if (prior_node->state == state &&
|
|
|
|
|
prior_node->position.chars == position.chars) {
|
2016-03-31 12:03:07 -07:00
|
|
|
stack_node_add_successor(prior_node, tree, is_pending, current_head);
|
2016-02-08 12:08:15 -08:00
|
|
|
ts_stack_remove_head(self, head_index);
|
2016-03-03 10:20:05 -08:00
|
|
|
return StackPushMerged;
|
2016-02-08 12:08:15 -08:00
|
|
|
}
|
|
|
|
|
}
|
2016-01-19 18:07:24 -08:00
|
|
|
|
2016-03-31 12:03:07 -07:00
|
|
|
StackNode *new_head = stack_node_new(current_head, tree, is_pending, state,
|
|
|
|
|
position, &self->node_pool);
|
2016-01-19 18:07:24 -08:00
|
|
|
if (!new_head)
|
2016-03-03 10:20:05 -08:00
|
|
|
return StackPushFailed;
|
2016-01-19 18:07:24 -08:00
|
|
|
|
2016-02-25 21:46:13 -08:00
|
|
|
stack_node_release(current_head, &self->node_pool);
|
2016-02-17 14:45:00 -08:00
|
|
|
self->heads.contents[head_index] = new_head;
|
2016-03-03 10:20:05 -08:00
|
|
|
return StackPushContinued;
|
2015-05-25 20:21:13 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
int ts_stack_split(Stack *self, int head_index) {
|
2016-02-17 14:45:00 -08:00
|
|
|
StackNode *head = self->heads.contents[head_index];
|
|
|
|
|
return ts_stack__add_head(self, head);
|
2015-06-03 09:44:13 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-07 16:03:23 -08:00
|
|
|
static inline ALWAYS_INLINE StackSliceArray stack__pop(
|
|
|
|
|
Stack *self, int head_index, StackIterateCallback callback, void *payload) {
|
2016-03-03 10:16:10 -08:00
|
|
|
array_clear(&self->slices);
|
2016-02-17 20:41:29 -08:00
|
|
|
array_clear(&self->pop_paths);
|
2016-03-07 16:03:23 -08:00
|
|
|
StackNode *initial_head = *array_get(&self->heads, head_index);
|
2016-01-19 18:07:24 -08:00
|
|
|
|
2016-03-07 16:03:23 -08:00
|
|
|
PopPath pop_path = {
|
2016-03-31 12:03:07 -07:00
|
|
|
.node = initial_head,
|
|
|
|
|
.trees = array_new(),
|
|
|
|
|
.extra_count = 0,
|
|
|
|
|
.is_done = false,
|
|
|
|
|
.is_pending = true,
|
2015-11-20 00:01:53 -08:00
|
|
|
};
|
2016-03-07 16:03:23 -08:00
|
|
|
if (!array_grow(&pop_path.trees, STARTING_TREE_CAPACITY))
|
2016-01-19 18:07:24 -08:00
|
|
|
goto error;
|
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
|
|
|
|
2015-05-30 20:26:45 -07:00
|
|
|
bool all_paths_done = false;
|
2016-03-07 16:03:23 -08:00
|
|
|
for (size_t depth = 0; !all_paths_done; depth++) {
|
2015-05-30 20:26:45 -07:00
|
|
|
all_paths_done = true;
|
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];
|
2016-03-07 16:03:23 -08:00
|
|
|
if (path->is_done)
|
|
|
|
|
continue;
|
|
|
|
|
|
2015-11-20 00:01:53 -08:00
|
|
|
StackNode *node = path->node;
|
2016-03-07 16:03:23 -08:00
|
|
|
size_t successor_count = node->successor_count;
|
2016-03-31 12:03:07 -07:00
|
|
|
switch (callback(payload, node->state, depth - path->extra_count,
|
|
|
|
|
node == self->base_node, path->is_pending && depth > 0)) {
|
2016-03-07 16:03:23 -08:00
|
|
|
case StackIteratePop:
|
|
|
|
|
path->is_done = true;
|
|
|
|
|
continue;
|
|
|
|
|
case StackIterateAbort:
|
|
|
|
|
successor_count = 0;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-11-20 00:01:53 -08:00
|
|
|
|
2016-03-07 16:03:23 -08:00
|
|
|
if (!successor_count) {
|
|
|
|
|
ts_tree_array_delete(&path->trees);
|
|
|
|
|
array_erase(&self->pop_paths, i--);
|
|
|
|
|
size--;
|
2015-05-30 20:26:45 -07:00
|
|
|
continue;
|
2016-03-07 16:03:23 -08:00
|
|
|
}
|
|
|
|
|
|
2015-06-18 15:04:03 -07:00
|
|
|
all_paths_done = false;
|
2015-05-30 20:26:45 -07:00
|
|
|
|
2016-03-10 11:51:38 -08:00
|
|
|
for (size_t j = 1; j <= successor_count; j++) {
|
2016-02-23 17:35:50 -08:00
|
|
|
PopPath *next_path;
|
2016-03-10 11:51:38 -08:00
|
|
|
StackLink successor;
|
|
|
|
|
if (j == successor_count) {
|
|
|
|
|
successor = node->successors[0];
|
|
|
|
|
next_path = &self->pop_paths.contents[i];
|
2016-02-23 17:35:50 -08:00
|
|
|
} else {
|
2016-03-10 11:51:38 -08:00
|
|
|
successor = node->successors[j];
|
|
|
|
|
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-03-07 16:03:23 -08:00
|
|
|
next_path->node = successor.node;
|
2016-02-23 17:35:50 -08:00
|
|
|
if (!array_push(&next_path->trees, successor.tree))
|
2016-01-19 18:07:24 -08:00
|
|
|
goto error;
|
2016-03-07 16:03:23 -08:00
|
|
|
if (successor.tree->extra)
|
|
|
|
|
next_path->extra_count++;
|
2016-03-31 12:03:07 -07:00
|
|
|
if (!successor.is_pending)
|
|
|
|
|
next_path->is_pending = false;
|
2016-03-07 16:03:23 -08:00
|
|
|
ts_tree_retain(successor.tree);
|
2015-05-30 20:26:45 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-05-25 20:21:13 -07:00
|
|
|
}
|
|
|
|
|
|
2015-11-20 00:01:53 -08:00
|
|
|
for (size_t i = 0; i < self->pop_paths.size; i++) {
|
2016-02-17 14:45:00 -08:00
|
|
|
PopPath *path = &self->pop_paths.contents[i];
|
2016-03-07 16:03:23 -08:00
|
|
|
if (!path->is_done)
|
|
|
|
|
continue;
|
2015-11-20 00:01:53 -08:00
|
|
|
|
2016-03-07 16:03:23 -08:00
|
|
|
StackSlice slice = {.trees = path->trees, .head_index = -1 };
|
|
|
|
|
array_reverse(&slice.trees);
|
2015-11-20 00:01:53 -08:00
|
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
|
stack_node_retain(path->node);
|
2016-02-17 14:45:00 -08:00
|
|
|
self->heads.contents[head_index] = path->node;
|
2016-03-03 10:16:10 -08:00
|
|
|
slice.head_index = head_index;
|
2015-06-03 09:44:13 -07:00
|
|
|
} else {
|
2016-03-07 16:03:23 -08:00
|
|
|
slice.head_index = ts_stack__index_of_head(self, path->node);
|
2016-03-03 10:16:10 -08:00
|
|
|
if (slice.head_index == -1) {
|
2016-03-07 16:03:23 -08:00
|
|
|
if ((slice.head_index = ts_stack__add_head(self, path->node)) == -1)
|
2016-02-08 12:08:15 -08:00
|
|
|
goto error;
|
|
|
|
|
} else {
|
2016-03-03 10:16:10 -08:00
|
|
|
bool merged = false;
|
|
|
|
|
for (size_t j = 0; j < self->slices.size; j++) {
|
|
|
|
|
StackSlice *prior_result = &self->slices.contents[j];
|
|
|
|
|
if (prior_result->head_index == slice.head_index) {
|
|
|
|
|
ts_stack__merge_slice(self, prior_result, &slice);
|
|
|
|
|
merged = true;
|
2016-02-08 12:08:15 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-03 10:16:10 -08:00
|
|
|
if (merged)
|
2016-02-08 12:08:15 -08:00
|
|
|
continue;
|
|
|
|
|
}
|
2015-05-30 20:26:45 -07:00
|
|
|
}
|
2015-05-25 20:21:13 -07:00
|
|
|
|
2016-03-03 10:16:10 -08:00
|
|
|
if (!array_push(&self->slices, slice))
|
2016-01-19 18:07:24 -08:00
|
|
|
goto error;
|
2015-05-30 20:26:45 -07:00
|
|
|
}
|
|
|
|
|
|
2016-03-07 16:03:23 -08:00
|
|
|
if (self->slices.size)
|
|
|
|
|
stack_node_release(initial_head, &self->node_pool);
|
|
|
|
|
return 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);
|
|
|
|
|
return self->slices;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 12:03:07 -07:00
|
|
|
static inline ALWAYS_INLINE StackIterateAction
|
|
|
|
|
stack__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)
|
|
|
|
|
return StackIterateAbort;
|
|
|
|
|
|
2016-03-10 11:51:38 -08:00
|
|
|
if (pop_session->goal_tree_count > 0) {
|
|
|
|
|
if ((int)tree_count == pop_session->goal_tree_count)
|
|
|
|
|
return StackIteratePop;
|
2016-03-07 16:03:23 -08:00
|
|
|
|
2016-03-10 11:51:38 -08:00
|
|
|
if (state == ts_parse_state_error) {
|
|
|
|
|
pop_session->found_error = true;
|
|
|
|
|
return StackIteratePop;
|
|
|
|
|
}
|
|
|
|
|
} else if (is_done) {
|
2016-03-07 16:03:23 -08:00
|
|
|
return StackIteratePop;
|
2016-03-10 11:51:38 -08:00
|
|
|
}
|
2016-03-07 16:03:23 -08:00
|
|
|
|
|
|
|
|
return StackIterateContinue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 12:03:07 -07:00
|
|
|
static inline ALWAYS_INLINE StackIterateAction
|
|
|
|
|
stack__pop_pending_callback(void *payload, TSStateId state, size_t tree_count,
|
|
|
|
|
bool is_done, bool is_pending) {
|
|
|
|
|
if (tree_count >= 1) {
|
|
|
|
|
if (is_pending) {
|
|
|
|
|
return StackIteratePop;
|
|
|
|
|
} else {
|
|
|
|
|
return StackIterateAbort;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return StackIterateContinue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 16:03:23 -08:00
|
|
|
StackPopResult ts_stack_pop_count(Stack *self, int head_index, int count) {
|
|
|
|
|
StackPopSession session = {
|
2016-03-07 20:06:46 -08:00
|
|
|
.goal_tree_count = count, .found_error = false,
|
2016-03-07 16:03:23 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
StackSliceArray slices =
|
|
|
|
|
stack__pop(self, head_index, stack__pop_count_callback, &session);
|
|
|
|
|
int status;
|
|
|
|
|
if (slices.size) {
|
2016-03-10 21:26:21 -08:00
|
|
|
if (session.found_error) {
|
2016-03-07 16:03:23 -08:00
|
|
|
status = StackPopStoppedAtError;
|
2016-03-10 21:26:21 -08:00
|
|
|
array_reverse(&slices);
|
|
|
|
|
while (slices.size > 1) {
|
|
|
|
|
StackSlice slice = array_pop(&slices);
|
|
|
|
|
ts_tree_array_delete(&slice.trees);
|
|
|
|
|
ts_stack_remove_head(self, slice.head_index);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2016-03-07 16:03:23 -08:00
|
|
|
status = StackPopSucceeded;
|
2016-03-10 21:26:21 -08:00
|
|
|
}
|
2016-03-07 16:03:23 -08:00
|
|
|
} else {
|
|
|
|
|
status = StackPopFailed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (StackPopResult){.status = status, .slices = slices };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StackPopResult ts_stack_pop_until(Stack *self, int head_index,
|
|
|
|
|
StackIterateCallback callback, void *payload) {
|
|
|
|
|
StackSliceArray slices = stack__pop(self, head_index, callback, payload);
|
|
|
|
|
return (StackPopResult){.status = StackPopSucceeded, .slices = slices };
|
2015-05-25 20:21:13 -07:00
|
|
|
}
|
2015-06-18 15:04:03 -07:00
|
|
|
|
2016-03-31 12:03:07 -07:00
|
|
|
StackPopResult ts_stack_pop_pending(Stack *self, int head_index) {
|
|
|
|
|
StackSliceArray slices =
|
|
|
|
|
stack__pop(self, head_index, stack__pop_pending_callback, NULL);
|
|
|
|
|
return (StackPopResult){.status = StackPopSucceeded, .slices = slices };
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
void ts_stack_shrink(Stack *self, int head_index, int count) {
|
2016-02-17 20:41:29 -08:00
|
|
|
StackNode *head = *array_get(&self->heads, head_index);
|
2016-02-17 14:45:00 -08:00
|
|
|
StackNode *new_head = head;
|
2015-06-18 15:04:03 -07:00
|
|
|
for (int i = 0; i < count; i++) {
|
2015-07-27 18:29:48 -07:00
|
|
|
if (new_head->successor_count == 0)
|
|
|
|
|
break;
|
2016-02-23 17:35:50 -08:00
|
|
|
new_head = new_head->successors[0].node;
|
2015-06-18 15:04:03 -07:00
|
|
|
}
|
|
|
|
|
stack_node_retain(new_head);
|
2016-02-25 21:46:13 -08:00
|
|
|
stack_node_release(head, &self->node_pool);
|
2016-02-17 14:45:00 -08:00
|
|
|
self->heads.contents[head_index] = new_head;
|
2015-06-18 15:04:03 -07:00
|
|
|
}
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
void ts_stack_clear(Stack *self) {
|
2016-03-07 16:03:23 -08:00
|
|
|
stack_node_retain(self->base_node);
|
2016-02-17 14:45:00 -08:00
|
|
|
for (size_t i = 0; i < self->heads.size; i++)
|
2016-02-25 21:46:13 -08:00
|
|
|
stack_node_release(self->heads.contents[i], &self->node_pool);
|
2016-02-17 20:41:29 -08:00
|
|
|
array_clear(&self->heads);
|
2016-03-07 16:03:23 -08:00
|
|
|
array_push(&self->heads, self->base_node);
|
2015-06-18 15:04:03 -07:00
|
|
|
}
|
2015-12-08 12:20:50 -08:00
|
|
|
|
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
|
|
|
|
|
|
|
|
void ts_stack_delete(Stack *self) {
|
2016-02-04 11:15:46 -08:00
|
|
|
if (self->pop_paths.contents)
|
2016-03-03 10:16:10 -08:00
|
|
|
array_delete(&self->slices);
|
2016-02-04 11:15:46 -08:00
|
|
|
if (self->pop_paths.contents)
|
2016-02-17 20:41:29 -08:00
|
|
|
array_delete(&self->pop_paths);
|
2016-03-07 16:03:23 -08:00
|
|
|
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);
|
2016-02-17 14:45:00 -08:00
|
|
|
if (self->node_pool.contents) {
|
|
|
|
|
for (size_t i = 0; i < self->node_pool.size; i++)
|
|
|
|
|
ts_free(self->node_pool.contents[i]);
|
2016-02-17 20:41:29 -08:00
|
|
|
array_delete(&self->node_pool);
|
2016-02-17 14:45:00 -08:00
|
|
|
}
|
2016-02-17 20:41:29 -08:00
|
|
|
array_delete(&self->heads);
|
2016-01-28 21:18:57 -08:00
|
|
|
ts_free(self);
|
|
|
|
|
}
|
2016-02-23 00:08:55 -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-02-23 17:35:50 -08:00
|
|
|
for (int j = 0; j < node->successor_count; j++) {
|
|
|
|
|
StackLink successor = node->successors[j];
|
2016-04-02 23:02:21 -07:00
|
|
|
fprintf(f, "node_%p -> node_%p [", node, successor.node);
|
|
|
|
|
if (successor.is_pending) fprintf(f, "style=dashed ");
|
|
|
|
|
fprintf(f, "label=\"");
|
2016-02-24 17:23:58 -08:00
|
|
|
|
2016-03-02 09:55:25 -08:00
|
|
|
if (successor.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 {
|
|
|
|
|
const char *name = symbol_names[successor.tree->symbol];
|
|
|
|
|
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) {
|
|
|
|
|
path->node = successor.node;
|
|
|
|
|
} else {
|
|
|
|
|
if (!array_push(&self->pop_paths, *path))
|
|
|
|
|
goto error;
|
|
|
|
|
PopPath *next_path = array_back(&self->pop_paths);
|
|
|
|
|
next_path->node = successor.node;
|
|
|
|
|
}
|
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
|
|
|
}
|