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-02-23 17:35:50 -08:00
|
|
|
typedef struct StackNode StackNode;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
StackNode *node;
|
|
|
|
|
TSTree *tree;
|
|
|
|
|
} StackLink;
|
|
|
|
|
|
|
|
|
|
struct StackNode {
|
2015-09-18 18:04:52 -07:00
|
|
|
StackEntry entry;
|
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 {
|
|
|
|
|
size_t goal_tree_count;
|
|
|
|
|
StackNode *node;
|
2016-02-17 20:41:29 -08:00
|
|
|
TreeArray trees;
|
2015-11-20 00:01:53 -08:00
|
|
|
bool is_shared;
|
|
|
|
|
} PopPath;
|
|
|
|
|
|
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;
|
|
|
|
|
StackPopResultArray pop_results;
|
|
|
|
|
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-02-08 12:08:15 -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);
|
|
|
|
|
array_init(&self->pop_results);
|
|
|
|
|
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-02-17 20:41:29 -08:00
|
|
|
if (!array_grow(&self->pop_results, 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-02-17 20:41:29 -08:00
|
|
|
array_push(&self->heads, NULL);
|
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-01-18 10:44:49 -08:00
|
|
|
if (self->pop_results.contents)
|
2016-02-17 20:41:29 -08:00
|
|
|
array_delete(&self->pop_results);
|
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) {
|
|
|
|
|
StackEntry *entry = ts_stack_head((Stack *)self, head_index);
|
2015-06-18 15:04:03 -07:00
|
|
|
return entry ? entry->state : 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 21:46:13 -08:00
|
|
|
TSLength ts_stack_top_position(const Stack *self, int head_index) {
|
|
|
|
|
StackEntry *entry = ts_stack_head((Stack *)self, head_index);
|
2015-12-02 07:53:15 -08:00
|
|
|
return entry ? entry->position : ts_length_zero();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 21:46:13 -08:00
|
|
|
StackEntry *ts_stack_head(Stack *self, int head_index) {
|
|
|
|
|
StackNode *node = self->heads.contents[head_index];
|
2015-06-03 09:44:13 -07:00
|
|
|
return node ? &node->entry : NULL;
|
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
|
|
|
}
|
|
|
|
|
|
2015-09-18 18:04:52 -07:00
|
|
|
int ts_stack_entry_next_count(const StackEntry *entry) {
|
|
|
|
|
return ((const StackNode *)entry)->successor_count;
|
2015-06-03 09:44:13 -07:00
|
|
|
}
|
|
|
|
|
|
2016-02-25 21:46:13 -08:00
|
|
|
StackEntry *ts_stack_entry_next(const StackEntry *entry, int successor_index) {
|
|
|
|
|
return &((const StackNode *)entry)->successors[successor_index].node->entry;
|
2015-06-03 09:44:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Section: Manipulating nodes (Private)
|
|
|
|
|
*/
|
|
|
|
|
|
2015-10-14 21:52:13 -07:00
|
|
|
static void stack_node_retain(StackNode *self) {
|
|
|
|
|
if (!self)
|
2015-07-27 18:29:48 -07:00
|
|
|
return;
|
2015-10-14 21:52:13 -07:00
|
|
|
assert(self->ref_count != 0);
|
|
|
|
|
self->ref_count++;
|
2015-06-03 09:44:13 -07:00
|
|
|
}
|
|
|
|
|
|
2016-02-25 21:46:13 -08:00
|
|
|
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++) {
|
|
|
|
|
stack_node_release(self->successors[i].node, pool);
|
|
|
|
|
ts_tree_release(self->successors[i].tree);
|
2016-02-23 17:35:50 -08:00
|
|
|
}
|
2016-02-04 11:15:46 -08:00
|
|
|
|
2016-02-25 21:46:13 -08:00
|
|
|
if (pool->size >= MAX_NODE_POOL_SIZE)
|
|
|
|
|
ts_free(self);
|
2016-02-04 11:15:46 -08:00
|
|
|
else
|
2016-02-25 21:46:13 -08:00
|
|
|
array_push(pool, self);
|
2015-06-03 09:44:13 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 21:46:13 -08:00
|
|
|
static StackNode *stack_node_new(StackNode *next, TSTree *tree, TSStateId state,
|
|
|
|
|
StackNodeArray *pool) {
|
2015-10-28 12:10:45 -07:00
|
|
|
assert(tree->ref_count > 0);
|
2016-02-04 11:15:46 -08:00
|
|
|
StackNode *node;
|
2016-02-25 21:46:13 -08:00
|
|
|
if (pool->size == 0) {
|
2016-02-04 11:15:46 -08:00
|
|
|
node = ts_malloc(sizeof(StackNode));
|
|
|
|
|
if (!node)
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
2016-02-25 21:46:13 -08:00
|
|
|
node = array_pop(pool);
|
2016-02-04 11:15:46 -08:00
|
|
|
}
|
2016-01-19 18:07:24 -08:00
|
|
|
|
2015-06-03 09:44:13 -07:00
|
|
|
ts_tree_retain(tree);
|
|
|
|
|
stack_node_retain(next);
|
2015-12-02 07:53:15 -08:00
|
|
|
TSLength position = ts_tree_total_size(tree);
|
2015-12-04 20:20:29 -08:00
|
|
|
if (next)
|
2015-12-02 07:53:15 -08:00
|
|
|
position = ts_length_add(next->entry.position, position);
|
2016-02-04 11:15:46 -08:00
|
|
|
*node = (StackNode){
|
2015-06-03 09:44:13 -07:00
|
|
|
.ref_count = 1,
|
|
|
|
|
.successor_count = 1,
|
2016-02-25 21:46:13 -08:00
|
|
|
.successors = { { next, tree } },
|
2016-02-23 17:35:50 -08:00
|
|
|
.entry = {.state = state, .position = position },
|
2015-06-03 09:44:13 -07:00
|
|
|
};
|
2016-02-04 11:15:46 -08:00
|
|
|
return node;
|
2015-06-03 09:44:13 -07:00
|
|
|
}
|
|
|
|
|
|
2016-02-08 12:08:15 -08:00
|
|
|
static void ts_stack__clear_pop_result(Stack *self, StackPopResult *result) {
|
2016-02-21 22:31:04 -08:00
|
|
|
for (size_t i = 0; i < result->trees.size; i++)
|
|
|
|
|
ts_tree_release(result->trees.contents[i]);
|
|
|
|
|
array_delete(&result->trees);
|
2016-02-08 12:08:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ts_stack__add_alternative_pop_result(Stack *self,
|
|
|
|
|
StackPopResult *result,
|
|
|
|
|
StackPopResult *new_result) {
|
|
|
|
|
bool should_update = false;
|
2016-02-21 22:31:04 -08:00
|
|
|
if (result->trees.size < new_result->trees.size) {
|
2016-02-08 12:08:15 -08:00
|
|
|
should_update = true;
|
2016-02-21 22:31:04 -08:00
|
|
|
} else if (result->trees.size == new_result->trees.size) {
|
|
|
|
|
for (size_t i = 0; i < result->trees.size; i++) {
|
|
|
|
|
TSTree *tree = result->trees.contents[i];
|
|
|
|
|
TSTree *new_tree = new_result->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) {
|
|
|
|
|
ts_stack__clear_pop_result(self, result);
|
|
|
|
|
result->trees = new_result->trees;
|
2016-02-21 22:31:04 -08:00
|
|
|
result->trees.size = new_result->trees.size;
|
2016-02-08 12:08:15 -08:00
|
|
|
} else {
|
|
|
|
|
ts_stack__clear_pop_result(self, new_result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 21:46:13 -08:00
|
|
|
static void stack_node__add_successor(StackNode *self, TSTree *new_tree,
|
2016-02-23 17:35:50 -08:00
|
|
|
StackNode *new_node) {
|
|
|
|
|
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->entry.state == new_node->entry.state) {
|
|
|
|
|
for (int j = 0; j < new_node->successor_count; j++) {
|
2016-02-25 21:46:13 -08:00
|
|
|
stack_node__add_successor(successor.node, new_node->successors[j].tree,
|
|
|
|
|
new_node->successors[j].node);
|
2016-02-23 17:35:50 -08:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-07-15 09:21:53 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-23 17:35:50 -08:00
|
|
|
stack_node_retain(new_node);
|
|
|
|
|
ts_tree_retain(new_tree);
|
|
|
|
|
self->successors[self->successor_count++] = (StackLink){
|
2016-02-25 21:46:13 -08:00
|
|
|
new_node, new_tree,
|
2016-02-23 17:35:50 -08: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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-20 00:01:53 -08:00
|
|
|
static int ts_stack__find_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,
|
|
|
|
|
TSStateId state) {
|
2015-12-02 07:53:15 -08:00
|
|
|
TSLength position = ts_tree_total_size(tree);
|
2016-02-17 20:41:29 -08:00
|
|
|
StackNode *current_head = *array_get(&self->heads, head_index);
|
2016-02-17 14:45:00 -08:00
|
|
|
if (current_head)
|
|
|
|
|
position = ts_length_add(current_head->entry.position, position);
|
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];
|
|
|
|
|
StackEntry prior_entry = prior_node->entry;
|
|
|
|
|
if (prior_entry.state == state &&
|
|
|
|
|
ts_length_eq(prior_entry.position, position)) {
|
2016-02-23 17:35:50 -08:00
|
|
|
stack_node__add_successor(prior_node, tree, current_head);
|
2016-02-08 12:08:15 -08:00
|
|
|
ts_stack_remove_head(self, head_index);
|
|
|
|
|
return StackPushResultMerged;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-19 18:07:24 -08:00
|
|
|
|
2016-02-25 21:46:13 -08:00
|
|
|
StackNode *new_head =
|
|
|
|
|
stack_node_new(current_head, tree, state, &self->node_pool);
|
2016-01-19 18:07:24 -08:00
|
|
|
if (!new_head)
|
|
|
|
|
return StackPushResultFailed;
|
|
|
|
|
|
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-01-19 18:07:24 -08:00
|
|
|
return StackPushResultContinued;
|
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-02-17 20:41:29 -08:00
|
|
|
StackPopResultArray ts_stack_pop(Stack *self, int head_index, int child_count,
|
|
|
|
|
bool count_extra) {
|
|
|
|
|
array_clear(&self->pop_results);
|
|
|
|
|
array_clear(&self->pop_paths);
|
2016-01-19 18:07:24 -08:00
|
|
|
|
2016-02-17 20:41:29 -08:00
|
|
|
StackNode *previous_head = *array_get(&self->heads, head_index);
|
2015-06-18 15:04:03 -07:00
|
|
|
int capacity = (child_count == -1) ? STARTING_TREE_CAPACITY : child_count;
|
2015-11-20 00:01:53 -08:00
|
|
|
PopPath initial_path = {
|
2016-02-17 20:41:29 -08:00
|
|
|
.goal_tree_count = child_count, .node = previous_head, .is_shared = false,
|
2015-11-20 00:01:53 -08:00
|
|
|
};
|
2016-02-17 20:41:29 -08:00
|
|
|
array_init(&initial_path.trees);
|
2015-11-20 00:01:53 -08:00
|
|
|
|
2016-02-17 20:41:29 -08:00
|
|
|
if (!array_grow(&initial_path.trees, capacity))
|
2016-01-19 18:07:24 -08:00
|
|
|
goto error;
|
|
|
|
|
|
2016-02-17 20:41:29 -08:00
|
|
|
if (!array_push(&self->pop_paths, initial_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
|
|
|
* Reduce along every possible path in parallel. Stop when the given number
|
|
|
|
|
* of child trees have been collected along every path.
|
2015-05-25 20:21:13 -07:00
|
|
|
*/
|
2015-05-30 20:26:45 -07:00
|
|
|
bool all_paths_done = false;
|
|
|
|
|
while (!all_paths_done) {
|
|
|
|
|
all_paths_done = true;
|
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];
|
2015-11-20 00:01:53 -08:00
|
|
|
StackNode *node = path->node;
|
|
|
|
|
|
|
|
|
|
if (!node || path->trees.size == path->goal_tree_count)
|
2015-05-30 20:26:45 -07:00
|
|
|
continue;
|
2015-06-18 15:04:03 -07:00
|
|
|
all_paths_done = false;
|
2015-05-30 20:26:45 -07:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If a node has more than one successor, create new paths for each of
|
|
|
|
|
* the additional successors.
|
|
|
|
|
*/
|
2016-02-23 17:35:50 -08:00
|
|
|
for (int j = 0; j < node->successor_count; j++) {
|
|
|
|
|
StackLink successor = node->successors[j];
|
|
|
|
|
|
|
|
|
|
PopPath *next_path;
|
|
|
|
|
if (j == 0) {
|
|
|
|
|
next_path = path;
|
|
|
|
|
} else {
|
|
|
|
|
if (!array_push(&self->pop_paths, *path))
|
|
|
|
|
goto error;
|
|
|
|
|
next_path = array_back(&self->pop_paths);
|
|
|
|
|
next_path->is_shared = true;
|
|
|
|
|
}
|
2015-11-20 00:01:53 -08:00
|
|
|
|
2016-02-23 17:35:50 -08:00
|
|
|
if (next_path->is_shared) {
|
|
|
|
|
next_path->trees = (TreeArray)array_copy(&path->trees);
|
|
|
|
|
next_path->trees.size--;
|
|
|
|
|
for (size_t j = 0; j < next_path->trees.size; j++)
|
|
|
|
|
ts_tree_retain(next_path->trees.contents[j]);
|
|
|
|
|
next_path->is_shared = false;
|
|
|
|
|
}
|
2015-11-20 00:01:53 -08:00
|
|
|
|
2016-02-23 17:35:50 -08:00
|
|
|
next_path->node = successor.node;
|
|
|
|
|
ts_tree_retain(successor.tree);
|
|
|
|
|
if (!array_push(&next_path->trees, successor.tree))
|
2016-01-19 18:07:24 -08:00
|
|
|
goto error;
|
|
|
|
|
|
2016-02-23 17:35:50 -08:00
|
|
|
/*
|
2016-02-25 21:46:13 -08:00
|
|
|
* Children that are 'extra' do not count towards the total child
|
|
|
|
|
* count.
|
2016-02-23 17:35:50 -08:00
|
|
|
*/
|
|
|
|
|
if (successor.tree->extra && !count_extra)
|
|
|
|
|
next_path->goal_tree_count++;
|
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];
|
2015-11-20 00:01:53 -08:00
|
|
|
|
|
|
|
|
if (!path->is_shared)
|
2016-02-17 20:41:29 -08:00
|
|
|
array_reverse(&path->trees);
|
2015-11-20 00:01:53 -08:00
|
|
|
|
|
|
|
|
StackPopResult result = {
|
2016-02-23 09:45:27 -08:00
|
|
|
.trees = path->trees, .head_index = -1,
|
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;
|
2015-11-20 00:01:53 -08:00
|
|
|
result.head_index = head_index;
|
2015-06-03 09:44:13 -07:00
|
|
|
} else {
|
2015-11-20 00:01:53 -08:00
|
|
|
result.head_index = ts_stack__find_head(self, path->node);
|
2016-02-08 12:08:15 -08:00
|
|
|
if (result.head_index == -1) {
|
2015-11-20 00:01:53 -08:00
|
|
|
result.head_index = ts_stack__add_head(self, path->node);
|
2016-02-08 12:08:15 -08:00
|
|
|
if (result.head_index == -1)
|
|
|
|
|
goto error;
|
|
|
|
|
} else {
|
|
|
|
|
bool merged_result = false;
|
|
|
|
|
for (size_t j = 0; j < self->pop_results.size; j++) {
|
2016-02-17 14:45:00 -08:00
|
|
|
StackPopResult *prior_result = &self->pop_results.contents[j];
|
2016-02-08 12:08:15 -08:00
|
|
|
if (prior_result->head_index == result.head_index) {
|
|
|
|
|
ts_stack__add_alternative_pop_result(self, prior_result, &result);
|
|
|
|
|
merged_result = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (merged_result)
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2015-05-30 20:26:45 -07:00
|
|
|
}
|
2015-05-25 20:21:13 -07:00
|
|
|
|
2016-02-17 20:41:29 -08:00
|
|
|
if (!array_push(&self->pop_results, result))
|
2016-01-19 18:07:24 -08:00
|
|
|
goto error;
|
2015-05-30 20:26:45 -07:00
|
|
|
}
|
|
|
|
|
|
2016-02-25 21:46:13 -08:00
|
|
|
stack_node_release(previous_head, &self->node_pool);
|
2015-11-20 00:01:53 -08:00
|
|
|
return self->pop_results;
|
2016-01-19 18:07:24 -08:00
|
|
|
|
|
|
|
|
error:
|
2016-02-17 20:41:29 -08:00
|
|
|
array_delete(&initial_path.trees);
|
|
|
|
|
StackPopResultArray result;
|
|
|
|
|
array_init(&result);
|
2016-02-17 14:45:00 -08:00
|
|
|
return result;
|
2015-05-25 20:21:13 -07:00
|
|
|
}
|
2015-06-18 15:04:03 -07:00
|
|
|
|
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-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);
|
|
|
|
|
array_push(&self->heads, NULL);
|
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-02-17 20:41:29 -08:00
|
|
|
array_delete(&self->pop_results);
|
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-01-28 21:18:57 -08:00
|
|
|
ts_stack_clear(self);
|
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-02-23 17:35:50 -08:00
|
|
|
static const char *COLORS[] = {
|
2016-02-23 09:45:27 -08:00
|
|
|
"red", "blue", "orange", "green", "purple",
|
|
|
|
|
};
|
|
|
|
|
|
2016-02-23 17:35:50 -08:00
|
|
|
static size_t COLOR_COUNT = sizeof(COLORS) / sizeof(COLORS[0]);
|
|
|
|
|
|
2016-02-23 00:08:55 -08:00
|
|
|
size_t ts_stack__write_dot_graph(Stack *self, char *string, size_t n,
|
|
|
|
|
const char **symbol_names) {
|
|
|
|
|
char *cursor = string;
|
|
|
|
|
char **s = n > 0 ? &cursor : &string;
|
|
|
|
|
cursor += snprintf(*s, n, "digraph stack {\n");
|
|
|
|
|
cursor += snprintf(*s, n, "rankdir=\"RL\";\n");
|
|
|
|
|
|
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-02-23 17:35:50 -08:00
|
|
|
const char *color = COLORS[i % COLOR_COUNT];
|
2016-02-23 09:45:27 -08:00
|
|
|
cursor += snprintf(*s, n, "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-02-25 21:46:13 -08:00
|
|
|
cursor +=
|
|
|
|
|
snprintf(*s, n, "node_%p [label=%d];\n", node, node->entry.state);
|
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-02-25 21:46:13 -08:00
|
|
|
cursor +=
|
|
|
|
|
snprintf(*s, n, "node_%p -> node_%p [label=\"", node, successor.node);
|
2016-02-24 17:23:58 -08:00
|
|
|
|
|
|
|
|
const char *name = symbol_names[successor.tree->symbol];
|
|
|
|
|
for (const char *c = name; *c; c++) {
|
|
|
|
|
if (*c == '\"' || *c == '\\') {
|
|
|
|
|
**s = '\\';
|
|
|
|
|
cursor++;
|
|
|
|
|
}
|
|
|
|
|
**s = *c;
|
|
|
|
|
cursor++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cursor += snprintf(*s, n, "\"];\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-02-23 17:35:50 -08:00
|
|
|
cursor += snprintf(*s, n, "node_%p [label=0];\n", NULL);
|
2016-02-23 00:08:55 -08:00
|
|
|
cursor += snprintf(*s, n, "}\n");
|
|
|
|
|
|
2016-02-23 17:35:50 -08:00
|
|
|
array_delete(&visited_nodes);
|
2016-02-23 00:08:55 -08:00
|
|
|
return cursor - string;
|
|
|
|
|
|
|
|
|
|
error:
|
2016-02-23 17:35:50 -08:00
|
|
|
array_delete(&visited_nodes);
|
2016-02-23 00:08:55 -08:00
|
|
|
return (size_t)-1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *ts_stack_dot_graph(Stack *self, const char **symbol_names) {
|
|
|
|
|
static char SCRATCH[1];
|
|
|
|
|
char *result = NULL;
|
|
|
|
|
size_t size = ts_stack__write_dot_graph(self, SCRATCH, 0, symbol_names) + 1;
|
2016-02-23 09:45:27 -08:00
|
|
|
if (size == (size_t)-1)
|
2016-02-23 00:08:55 -08:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
result = ts_malloc(size * sizeof(char));
|
|
|
|
|
if (!result)
|
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
size = ts_stack__write_dot_graph(self, result, size, symbol_names);
|
2016-02-23 09:45:27 -08:00
|
|
|
if (size == (size_t)-1)
|
2016-02-23 00:08:55 -08:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
if (result)
|
|
|
|
|
ts_free(result);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|