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;
|
2016-11-09 20:59:05 -08:00
|
|
|
Tree *tree;
|
2016-09-01 10:04:20 -07:00
|
|
|
unsigned push_count;
|
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;
|
2016-11-09 20:59:05 -08:00
|
|
|
Length 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-06-12 17:27:08 -07:00
|
|
|
unsigned error_cost;
|
2016-08-30 09:44:40 -07:00
|
|
|
unsigned error_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-11-14 12:15:24 -08:00
|
|
|
uint32_t tree_count;
|
2016-03-07 16:03:23 -08:00
|
|
|
StackNode *node;
|
2016-03-31 12:03:07 -07:00
|
|
|
bool is_pending;
|
2016-09-01 10:04:20 -07:00
|
|
|
unsigned push_count;
|
|
|
|
|
} Iterator;
|
2015-11-20 00:01:53 -08:00
|
|
|
|
2016-03-07 16:03:23 -08:00
|
|
|
typedef struct {
|
2016-11-14 12:15:24 -08:00
|
|
|
uint32_t goal_tree_count;
|
2016-03-07 16:03:23 -08:00
|
|
|
bool found_error;
|
2016-05-09 14:31:44 -07:00
|
|
|
bool found_valid_path;
|
2016-03-07 16:03:23 -08:00
|
|
|
} StackPopSession;
|
|
|
|
|
|
2016-02-25 21:46:13 -08:00
|
|
|
typedef Array(StackNode *) StackNodeArray;
|
|
|
|
|
|
2016-05-10 15:24:06 -07:00
|
|
|
typedef struct {
|
|
|
|
|
StackNode *node;
|
|
|
|
|
bool is_halted;
|
2016-08-31 17:29:14 -07:00
|
|
|
unsigned push_count;
|
2017-01-04 21:22:23 -08:00
|
|
|
const TSExternalTokenState *external_token_state;
|
2016-05-10 15:24:06 -07:00
|
|
|
} StackHead;
|
|
|
|
|
|
2016-02-17 14:45:00 -08:00
|
|
|
struct Stack {
|
2016-05-10 15:24:06 -07:00
|
|
|
Array(StackHead) heads;
|
2016-03-03 10:16:10 -08:00
|
|
|
StackSliceArray slices;
|
2016-09-01 10:04:20 -07:00
|
|
|
Array(Iterator) iterators;
|
2016-02-25 21:46:13 -08:00
|
|
|
StackNodeArray node_pool;
|
2016-03-07 16:03:23 -08:00
|
|
|
StackNode *base_node;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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++) {
|
2016-05-26 13:20:53 -07:00
|
|
|
if (self->links[i].tree)
|
|
|
|
|
ts_tree_release(self->links[i].tree);
|
2016-04-11 22:41:06 -07:00
|
|
|
stack_node_release(self->links[i].node, pool);
|
2016-03-07 16:03:23 -08:00
|
|
|
}
|
|
|
|
|
|
2016-11-05 14:39:25 -07:00
|
|
|
if (pool->size < MAX_NODE_POOL_SIZE) {
|
|
|
|
|
array_push(pool, self);
|
|
|
|
|
} else {
|
2016-03-07 16:03:23 -08:00
|
|
|
ts_free(self);
|
2016-11-05 14:39:25 -07:00
|
|
|
}
|
2016-03-07 16:03:23 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-09 20:59:05 -08:00
|
|
|
static StackNode *stack_node_new(StackNode *next, Tree *tree, bool is_pending,
|
|
|
|
|
TSStateId state, Length position,
|
2016-03-31 12:03:07 -07:00
|
|
|
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) {
|
|
|
|
|
stack_node_retain(next);
|
2016-05-29 22:36:47 -07:00
|
|
|
|
|
|
|
|
node->link_count = 1;
|
2016-09-01 10:04:20 -07:00
|
|
|
node->links[0] = (StackLink){
|
2016-10-05 14:02:49 -07:00
|
|
|
.node = next, .tree = tree, .is_pending = is_pending, .push_count = 0,
|
2016-09-01 10:04:20 -07:00
|
|
|
};
|
2016-06-26 22:45:19 -07:00
|
|
|
|
2016-08-30 09:44:40 -07:00
|
|
|
node->error_count = next->error_count;
|
2016-08-31 10:51:59 -07:00
|
|
|
node->error_cost = next->error_cost;
|
2016-05-29 22:36:47 -07:00
|
|
|
|
|
|
|
|
if (tree) {
|
|
|
|
|
ts_tree_retain(tree);
|
2016-08-31 10:51:59 -07:00
|
|
|
node->error_cost += tree->error_cost;
|
|
|
|
|
|
2016-10-05 14:02:49 -07:00
|
|
|
if (state == ERROR_STATE) {
|
2016-06-26 22:45:19 -07:00
|
|
|
if (!tree->extra) {
|
2016-08-31 10:51:59 -07:00
|
|
|
node->error_cost += ERROR_COST_PER_SKIPPED_TREE +
|
|
|
|
|
ERROR_COST_PER_SKIPPED_CHAR *
|
|
|
|
|
(tree->padding.chars + tree->size.chars) +
|
|
|
|
|
ERROR_COST_PER_SKIPPED_LINE *
|
2016-09-09 21:11:02 -07:00
|
|
|
(tree->padding.extent.row + tree->size.extent.row);
|
2016-06-26 22:45:19 -07:00
|
|
|
}
|
|
|
|
|
}
|
2016-05-29 22:36:47 -07:00
|
|
|
} else {
|
2016-08-30 09:44:40 -07:00
|
|
|
node->error_count++;
|
2016-05-29 22:36:47 -07:00
|
|
|
}
|
2016-08-31 10:51:59 -07:00
|
|
|
} else {
|
|
|
|
|
node->error_count = 0;
|
|
|
|
|
node->error_cost = 0;
|
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);
|
2016-06-12 17:27:08 -07:00
|
|
|
if (link.tree)
|
2016-05-26 13:20:53 -07:00
|
|
|
ts_tree_retain(link.tree);
|
2016-05-29 22:36:47 -07:00
|
|
|
|
2016-04-11 22:41:06 -07:00
|
|
|
self->links[self->link_count++] = (StackLink){
|
2016-09-01 10:04:20 -07:00
|
|
|
.node = link.node,
|
|
|
|
|
.tree = link.tree,
|
|
|
|
|
.is_pending = link.is_pending,
|
|
|
|
|
.push_count = link.push_count,
|
2016-03-10 11:43:13 -08:00
|
|
|
};
|
|
|
|
|
}
|
2016-03-07 16:03:23 -08:00
|
|
|
}
|
|
|
|
|
|
2016-08-31 17:29:14 -07:00
|
|
|
static StackVersion ts_stack__add_version(Stack *self, StackNode *node,
|
2017-01-07 21:45:28 -08:00
|
|
|
unsigned push_count,
|
|
|
|
|
const TSExternalTokenState *external_token_state) {
|
2016-09-01 10:04:20 -07:00
|
|
|
StackHead head = {
|
2016-11-04 09:18:38 -07:00
|
|
|
.node = node,
|
|
|
|
|
.is_halted = false,
|
|
|
|
|
.push_count = push_count,
|
2017-01-07 21:45:28 -08:00
|
|
|
.external_token_state = external_token_state,
|
2016-09-01 10:04:20 -07:00
|
|
|
};
|
2016-11-04 09:18:38 -07:00
|
|
|
array_push(&self->heads, head);
|
2016-04-15 21:28:00 -07:00
|
|
|
stack_node_retain(node);
|
|
|
|
|
return (StackVersion)(self->heads.size - 1);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-04 09:18:38 -07:00
|
|
|
static void ts_stack__add_slice(Stack *self, StackNode *node, TreeArray *trees,
|
2017-01-07 21:45:28 -08:00
|
|
|
unsigned push_count,
|
|
|
|
|
const TSExternalTokenState *external_token_state) {
|
2016-11-14 12:15:24 -08:00
|
|
|
for (uint32_t i = self->slices.size - 1; i + 1 > 0; i--) {
|
2016-04-25 21:59:40 -07:00
|
|
|
StackVersion version = self->slices.contents[i].version;
|
2016-05-10 15:24:06 -07:00
|
|
|
if (self->heads.contents[version].node == node) {
|
2016-04-25 21:59:40 -07:00
|
|
|
StackSlice slice = { *trees, version };
|
2016-11-04 09:18:38 -07:00
|
|
|
array_insert(&self->slices, i + 1, slice);
|
|
|
|
|
return;
|
2016-04-15 21:28:00 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-07-08 17:34:21 -07:00
|
|
|
|
2017-01-07 21:45:28 -08:00
|
|
|
StackVersion version = ts_stack__add_version(self, node, push_count, external_token_state);
|
2016-04-25 21:59:40 -07:00
|
|
|
StackSlice slice = { *trees, version };
|
2016-11-04 09:18:38 -07:00
|
|
|
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) {
|
|
|
|
|
array_clear(&self->slices);
|
2016-09-01 10:04:20 -07:00
|
|
|
array_clear(&self->iterators);
|
2016-04-15 21:28:00 -07:00
|
|
|
|
2016-08-31 17:29:14 -07:00
|
|
|
StackHead *head = array_get(&self->heads, version);
|
2016-09-05 21:41:33 -07:00
|
|
|
unsigned push_count = head->push_count;
|
2017-01-07 21:45:28 -08:00
|
|
|
const TSExternalTokenState *external_token_state = head->external_token_state;
|
2016-09-01 10:04:20 -07:00
|
|
|
Iterator iterator = {
|
2016-08-31 17:29:14 -07:00
|
|
|
.node = head->node,
|
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,
|
2016-09-01 10:04:20 -07:00
|
|
|
.push_count = 0,
|
2015-11-20 00:01:53 -08:00
|
|
|
};
|
2016-11-04 09:18:38 -07:00
|
|
|
array_push(&self->iterators, iterator);
|
2015-05-25 20:21:13 -07:00
|
|
|
|
2016-09-01 10:04:20 -07:00
|
|
|
while (self->iterators.size > 0) {
|
2016-11-14 12:15:24 -08:00
|
|
|
for (uint32_t i = 0, size = self->iterators.size; i < size; i++) {
|
2016-09-01 10:04:20 -07:00
|
|
|
Iterator *iterator = &self->iterators.contents[i];
|
|
|
|
|
StackNode *node = iterator->node;
|
2016-04-15 21:28:00 -07:00
|
|
|
bool is_done = node == self->base_node;
|
|
|
|
|
|
2016-05-09 14:31:44 -07:00
|
|
|
StackIterateAction action =
|
2016-10-05 14:02:49 -07:00
|
|
|
callback(payload, node->state, &iterator->trees, iterator->tree_count,
|
|
|
|
|
is_done, iterator->is_pending);
|
2016-04-15 21:28:00 -07:00
|
|
|
|
|
|
|
|
bool should_pop = action & StackIteratePop;
|
|
|
|
|
bool should_stop = action & StackIterateStop || node->link_count == 0;
|
|
|
|
|
|
|
|
|
|
if (should_pop) {
|
2016-09-01 10:04:20 -07:00
|
|
|
TreeArray trees = iterator->trees;
|
2016-06-14 14:46:49 -07:00
|
|
|
if (!should_stop)
|
2016-11-04 09:18:38 -07:00
|
|
|
ts_tree_array_copy(trees, &trees);
|
2016-04-15 21:28:00 -07:00
|
|
|
array_reverse(&trees);
|
2017-01-07 21:45:28 -08:00
|
|
|
ts_stack__add_slice(self, node, &trees, push_count + iterator->push_count,
|
|
|
|
|
external_token_state);
|
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)
|
2016-09-01 10:04:20 -07:00
|
|
|
ts_tree_array_delete(&iterator->trees);
|
|
|
|
|
array_erase(&self->iterators, i);
|
2016-04-11 23:12:50 -07:00
|
|
|
i--, size--;
|
2015-05-30 20:26:45 -07:00
|
|
|
continue;
|
2016-03-07 16:03:23 -08:00
|
|
|
}
|
|
|
|
|
|
2016-11-14 12:15:24 -08:00
|
|
|
for (uint32_t j = 1; j <= node->link_count; j++) {
|
2016-09-01 10:04:20 -07:00
|
|
|
Iterator *next_iterator;
|
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-09-01 10:04:20 -07:00
|
|
|
next_iterator = &self->iterators.contents[i];
|
2016-02-23 17:35:50 -08:00
|
|
|
} else {
|
2016-04-11 22:41:06 -07:00
|
|
|
link = node->links[j];
|
2016-11-04 09:18:38 -07:00
|
|
|
array_push(&self->iterators, self->iterators.contents[i]);
|
2016-09-01 10:04:20 -07:00
|
|
|
next_iterator = array_back(&self->iterators);
|
2016-11-04 09:18:38 -07:00
|
|
|
ts_tree_array_copy(next_iterator->trees, &next_iterator->trees);
|
2016-02-23 17:35:50 -08:00
|
|
|
}
|
2015-11-20 00:01:53 -08:00
|
|
|
|
2016-09-01 10:04:20 -07:00
|
|
|
next_iterator->node = link.node;
|
|
|
|
|
next_iterator->push_count += link.push_count;
|
2016-05-26 13:20:53 -07:00
|
|
|
if (link.tree) {
|
2016-06-14 20:25:33 -07:00
|
|
|
if (!link.tree->extra) {
|
2016-09-01 10:04:20 -07:00
|
|
|
next_iterator->tree_count++;
|
2016-06-14 20:25:33 -07:00
|
|
|
if (!link.is_pending)
|
2016-09-01 10:04:20 -07:00
|
|
|
next_iterator->is_pending = false;
|
2016-06-14 20:25:33 -07:00
|
|
|
}
|
2016-11-04 09:18:38 -07:00
|
|
|
array_push(&next_iterator->trees, link.tree);
|
2016-05-26 13:20:53 -07:00
|
|
|
ts_tree_retain(link.tree);
|
2016-06-14 20:25:33 -07:00
|
|
|
} else {
|
2016-09-01 10:04:20 -07:00
|
|
|
next_iterator->is_pending = false;
|
2016-05-26 13:20:53 -07:00
|
|
|
}
|
2015-05-30 20:26:45 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-05-25 20:21:13 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-04 09:18:38 -07:00
|
|
|
return (StackPopResult){ false, self->slices };
|
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));
|
|
|
|
|
|
|
|
|
|
array_init(&self->heads);
|
|
|
|
|
array_init(&self->slices);
|
2016-09-01 10:04:20 -07:00
|
|
|
array_init(&self->iterators);
|
2016-04-15 21:33:31 -07:00
|
|
|
array_init(&self->node_pool);
|
2016-11-04 09:18:38 -07:00
|
|
|
array_grow(&self->heads, 4);
|
|
|
|
|
array_grow(&self->slices, 4);
|
|
|
|
|
array_grow(&self->iterators, 4);
|
|
|
|
|
array_grow(&self->node_pool, MAX_NODE_POOL_SIZE);
|
2016-04-15 21:33:31 -07:00
|
|
|
|
|
|
|
|
self->base_node =
|
2016-11-09 20:59:05 -08:00
|
|
|
stack_node_new(NULL, NULL, false, 1, length_zero(), &self->node_pool);
|
2016-04-15 21:33:31 -07:00
|
|
|
stack_node_retain(self->base_node);
|
2017-01-04 21:22:23 -08:00
|
|
|
array_push(&self->heads, ((StackHead){
|
|
|
|
|
self->base_node,
|
|
|
|
|
false,
|
|
|
|
|
0,
|
|
|
|
|
NULL
|
|
|
|
|
}));
|
2016-04-15 21:33:31 -07:00
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ts_stack_delete(Stack *self) {
|
2016-05-16 10:49:22 -07:00
|
|
|
if (self->slices.contents)
|
2016-04-15 21:33:31 -07:00
|
|
|
array_delete(&self->slices);
|
2016-09-01 10:04:20 -07:00
|
|
|
if (self->iterators.contents)
|
|
|
|
|
array_delete(&self->iterators);
|
2016-04-15 21:33:31 -07:00
|
|
|
stack_node_release(self->base_node, &self->node_pool);
|
2016-11-14 12:15:24 -08:00
|
|
|
for (uint32_t i = 0; i < self->heads.size; i++)
|
2016-05-10 15:24:06 -07:00
|
|
|
stack_node_release(self->heads.contents[i].node, &self->node_pool);
|
2016-04-15 21:33:31 -07:00
|
|
|
array_clear(&self->heads);
|
|
|
|
|
if (self->node_pool.contents) {
|
2016-11-14 12:15:24 -08:00
|
|
|
for (uint32_t i = 0; i < self->node_pool.size; i++)
|
2016-04-15 21:33:31 -07:00
|
|
|
ts_free(self->node_pool.contents[i]);
|
|
|
|
|
array_delete(&self->node_pool);
|
|
|
|
|
}
|
|
|
|
|
array_delete(&self->heads);
|
|
|
|
|
ts_free(self);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-14 12:15:24 -08:00
|
|
|
uint32_t ts_stack_version_count(const Stack *self) {
|
2016-04-15 21:33:31 -07:00
|
|
|
return self->heads.size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TSStateId ts_stack_top_state(const Stack *self, StackVersion version) {
|
2016-05-10 15:24:06 -07:00
|
|
|
return array_get(&self->heads, version)->node->state;
|
2016-04-15 21:33:31 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-09 20:59:05 -08:00
|
|
|
Length ts_stack_top_position(const Stack *self, StackVersion version) {
|
2016-05-10 15:24:06 -07:00
|
|
|
return array_get(&self->heads, version)->node->position;
|
2016-04-15 21:33:31 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-31 17:29:14 -07:00
|
|
|
unsigned ts_stack_push_count(const Stack *self, StackVersion version) {
|
|
|
|
|
return array_get(&self->heads, version)->push_count;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 21:22:23 -08:00
|
|
|
void ts_stack_decrease_push_count(Stack *self, StackVersion version,
|
2016-10-05 14:02:49 -07:00
|
|
|
unsigned decrement) {
|
2016-09-01 10:04:20 -07:00
|
|
|
array_get(&self->heads, version)->push_count -= decrement;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 21:22:23 -08:00
|
|
|
const TSExternalTokenState *ts_stack_external_token_state(const Stack *self, StackVersion version) {
|
|
|
|
|
return array_get(&self->heads, version)->external_token_state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ts_stack_set_external_token_state(Stack *self, StackVersion version, const TSExternalTokenState *state) {
|
|
|
|
|
array_get(&self->heads, version)->external_token_state = state;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-31 10:51:59 -07:00
|
|
|
ErrorStatus ts_stack_error_status(const Stack *self, StackVersion version) {
|
2016-08-31 17:29:14 -07:00
|
|
|
StackHead *head = array_get(&self->heads, version);
|
2016-08-31 10:51:59 -07:00
|
|
|
return (ErrorStatus){
|
2016-08-31 17:29:14 -07:00
|
|
|
.cost = head->node->error_cost,
|
|
|
|
|
.count = head->node->error_count,
|
|
|
|
|
.push_count = head->push_count,
|
2016-08-31 10:51:59 -07:00
|
|
|
};
|
2016-06-02 14:04:48 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-30 09:44:40 -07:00
|
|
|
unsigned ts_stack_error_count(const Stack *self, StackVersion version) {
|
2016-08-31 10:51:59 -07:00
|
|
|
StackNode *node = array_get(&self->heads, version)->node;
|
|
|
|
|
return node->error_count;
|
2016-06-02 14:04:48 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-09 20:59:05 -08:00
|
|
|
bool ts_stack_push(Stack *self, StackVersion version, Tree *tree,
|
2016-04-15 21:33:31 -07:00
|
|
|
bool is_pending, TSStateId state) {
|
2016-08-31 17:29:14 -07:00
|
|
|
StackHead *head = array_get(&self->heads, version);
|
|
|
|
|
StackNode *node = head->node;
|
2016-11-09 20:59:05 -08:00
|
|
|
Length position = node->position;
|
2016-08-31 17:29:14 -07:00
|
|
|
if (tree)
|
2016-11-09 20:59:05 -08:00
|
|
|
position = length_add(position, ts_tree_total_size(tree));
|
2016-04-15 21:33:31 -07:00
|
|
|
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);
|
2016-08-31 17:29:14 -07:00
|
|
|
head->node = new_node;
|
2016-10-05 14:02:49 -07:00
|
|
|
if (state == ERROR_STATE) {
|
2016-09-01 10:04:20 -07:00
|
|
|
new_node->links[0].push_count = head->push_count;
|
2016-08-31 17:29:14 -07:00
|
|
|
head->push_count = 0;
|
2016-10-05 14:02:49 -07:00
|
|
|
} else
|
2016-08-31 17:29:14 -07:00
|
|
|
head->push_count++;
|
2016-04-15 21:33:31 -07:00
|
|
|
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,
|
2016-11-14 12:15:24 -08:00
|
|
|
TreeArray *trees, uint32_t tree_count,
|
2016-05-09 14:31:44 -07:00
|
|
|
bool is_done, bool is_pending) {
|
2016-03-07 16:03:23 -08:00
|
|
|
StackPopSession *pop_session = (StackPopSession *)payload;
|
2016-05-09 14:31:44 -07:00
|
|
|
|
|
|
|
|
if (tree_count == pop_session->goal_tree_count) {
|
|
|
|
|
pop_session->found_valid_path = true;
|
2016-04-15 21:28:00 -07:00
|
|
|
return StackIteratePop | StackIterateStop;
|
2016-05-09 14:31:44 -07:00
|
|
|
}
|
|
|
|
|
|
2016-10-05 14:02:49 -07:00
|
|
|
if (state == ERROR_STATE) {
|
2016-05-09 14:31:44 -07:00
|
|
|
if (pop_session->found_valid_path || pop_session->found_error) {
|
|
|
|
|
return StackIterateStop;
|
|
|
|
|
} else {
|
|
|
|
|
pop_session->found_error = true;
|
|
|
|
|
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,
|
2016-11-14 12:15:24 -08:00
|
|
|
uint32_t count) {
|
2016-05-09 14:31:44 -07:00
|
|
|
StackPopSession session = {
|
|
|
|
|
.goal_tree_count = count, .found_error = false, .found_valid_path = false,
|
|
|
|
|
};
|
2016-04-15 21:28:00 -07:00
|
|
|
StackPopResult pop = stack__iter(self, version, pop_count_callback, &session);
|
2016-11-04 09:18:38 -07:00
|
|
|
if (session.found_error) {
|
2016-05-09 14:31:44 -07:00
|
|
|
if (session.found_valid_path) {
|
|
|
|
|
StackSlice error_slice = pop.slices.contents[0];
|
|
|
|
|
ts_tree_array_delete(&error_slice.trees);
|
|
|
|
|
array_erase(&pop.slices, 0);
|
2016-05-30 14:08:42 -07:00
|
|
|
if (array_front(&pop.slices)->version != error_slice.version) {
|
|
|
|
|
ts_stack_remove_version(self, error_slice.version);
|
|
|
|
|
for (StackVersion i = 0; i < pop.slices.size; i++)
|
|
|
|
|
pop.slices.contents[i].version--;
|
|
|
|
|
}
|
2016-05-09 14:31:44 -07:00
|
|
|
} else {
|
2016-11-04 09:18:38 -07:00
|
|
|
pop.stopped_at_error = true;
|
2016-03-10 21:26:21 -08:00
|
|
|
}
|
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,
|
2016-05-09 14:31:44 -07:00
|
|
|
TreeArray *trees,
|
2016-11-14 12:15:24 -08:00
|
|
|
uint32_t tree_count, bool is_done,
|
2016-04-15 21:28:00 -07:00
|
|
|
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,
|
2016-11-14 12:15:24 -08:00
|
|
|
TreeArray *trees, uint32_t tree_count,
|
2016-05-09 14:31:44 -07:00
|
|
|
bool is_done, bool is_pending) {
|
2016-04-15 21:28:00 -07:00
|
|
|
return is_done ? (StackIteratePop | StackIterateStop) : StackIterateNone;
|
2015-06-18 15:04:03 -07:00
|
|
|
}
|
2015-12-08 12:20:50 -08:00
|
|
|
|
2016-04-24 00:54:20 -07:00
|
|
|
StackPopResult ts_stack_pop_all(Stack *self, StackVersion version) {
|
|
|
|
|
return stack__iter(self, version, pop_all_callback, NULL);
|
2016-04-04 11:44:45 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-15 21:33:31 -07:00
|
|
|
void ts_stack_remove_version(Stack *self, StackVersion version) {
|
2016-05-10 15:24:06 -07:00
|
|
|
StackNode *node = array_get(&self->heads, version)->node;
|
2016-04-15 21:33:31 -07:00
|
|
|
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);
|
2016-11-14 12:15:24 -08:00
|
|
|
assert((uint32_t)v1 < self->heads.size);
|
2016-05-10 15:24:06 -07:00
|
|
|
stack_node_release(self->heads.contents[v2].node, &self->node_pool);
|
2016-04-15 21:33:31 -07:00
|
|
|
self->heads.contents[v2] = self->heads.contents[v1];
|
|
|
|
|
array_erase(&self->heads, v1);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-14 17:25:55 -08:00
|
|
|
StackVersion ts_stack_copy_version(Stack *self, StackVersion version) {
|
2016-05-09 14:31:44 -07:00
|
|
|
assert(version < self->heads.size);
|
2016-11-04 09:18:38 -07:00
|
|
|
array_push(&self->heads, self->heads.contents[version]);
|
2016-05-10 15:24:06 -07:00
|
|
|
stack_node_retain(array_back(&self->heads)->node);
|
2016-05-09 14:31:44 -07:00
|
|
|
return self->heads.size - 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-28 21:22:10 -07:00
|
|
|
bool ts_stack_merge(Stack *self, StackVersion version, StackVersion new_version) {
|
2016-08-31 17:29:14 -07:00
|
|
|
StackHead *head = &self->heads.contents[version];
|
|
|
|
|
StackHead *new_head = &self->heads.contents[new_version];
|
|
|
|
|
StackNode *node = head->node;
|
|
|
|
|
StackNode *new_node = new_head->node;
|
2016-05-28 21:22:10 -07:00
|
|
|
|
|
|
|
|
if (new_node->state == node->state &&
|
2016-05-29 22:36:47 -07:00
|
|
|
new_node->position.chars == node->position.chars &&
|
2016-08-30 09:44:40 -07:00
|
|
|
new_node->error_count == node->error_count &&
|
2017-01-04 21:22:23 -08:00
|
|
|
new_node->error_cost == node->error_cost &&
|
|
|
|
|
new_head->external_token_state == head->external_token_state) {
|
2016-11-14 12:15:24 -08:00
|
|
|
for (uint32_t j = 0; j < new_node->link_count; j++)
|
2016-05-28 21:22:10 -07:00
|
|
|
stack_node_add_link(node, new_node->links[j]);
|
2016-08-31 17:29:14 -07:00
|
|
|
if (new_head->push_count > head->push_count)
|
|
|
|
|
head->push_count = new_head->push_count;
|
2016-05-28 21:22:10 -07:00
|
|
|
ts_stack_remove_version(self, new_version);
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-02 14:04:48 -07:00
|
|
|
void ts_stack_halt(Stack *self, StackVersion version) {
|
|
|
|
|
array_get(&self->heads, version)->is_halted = true;
|
|
|
|
|
}
|
2016-05-29 22:36:47 -07:00
|
|
|
|
2016-06-02 14:04:48 -07:00
|
|
|
bool ts_stack_is_halted(Stack *self, StackVersion version) {
|
|
|
|
|
return array_get(&self->heads, version)->is_halted;
|
2016-05-29 22:36:47 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-04 11:59:10 -07:00
|
|
|
void ts_stack_clear(Stack *self) {
|
|
|
|
|
stack_node_retain(self->base_node);
|
2016-11-14 12:15:24 -08:00
|
|
|
for (uint32_t i = 0; i < self->heads.size; i++)
|
2016-05-10 15:24:06 -07:00
|
|
|
stack_node_release(self->heads.contents[i].node, &self->node_pool);
|
2016-04-04 11:59:10 -07:00
|
|
|
array_clear(&self->heads);
|
2017-01-04 21:22:23 -08:00
|
|
|
array_push(&self->heads, ((StackHead){
|
|
|
|
|
self->base_node,
|
|
|
|
|
false,
|
|
|
|
|
0,
|
|
|
|
|
NULL
|
|
|
|
|
}));
|
2016-04-04 11:59:10 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-16 10:44:19 -07:00
|
|
|
bool ts_stack_print_dot_graph(Stack *self, const char **symbol_names, FILE *f) {
|
|
|
|
|
bool was_recording_allocations = ts_toggle_allocation_recording(false);
|
|
|
|
|
if (!f)
|
|
|
|
|
f = stderr;
|
|
|
|
|
|
2016-04-02 22:18:44 -07:00
|
|
|
fprintf(f, "digraph stack {\n");
|
|
|
|
|
fprintf(f, "rankdir=\"RL\";\n");
|
2016-05-01 15:24:50 -07:00
|
|
|
fprintf(f, "edge [arrowhead=none]\n");
|
2016-02-23 00:08:55 -08:00
|
|
|
|
2016-05-19 16:25:44 -07:00
|
|
|
Array(StackNode *)visited_nodes = array_new();
|
2016-02-23 17:35:50 -08:00
|
|
|
|
2016-09-01 10:04:20 -07:00
|
|
|
array_clear(&self->iterators);
|
2016-11-14 12:15:24 -08:00
|
|
|
for (uint32_t i = 0; i < self->heads.size; i++) {
|
2016-08-31 17:29:14 -07:00
|
|
|
StackHead *head = &self->heads.contents[i];
|
|
|
|
|
if (head->is_halted)
|
2016-05-10 15:24:06 -07:00
|
|
|
continue;
|
2016-11-14 12:15:24 -08:00
|
|
|
fprintf(f, "node_head_%u [shape=none, label=\"\"]\n", i);
|
2016-05-01 15:24:50 -07:00
|
|
|
fprintf(
|
2016-10-05 14:02:49 -07:00
|
|
|
f,
|
2016-11-14 12:15:24 -08:00
|
|
|
"node_head_%u -> node_%p [label=%u, fontcolor=blue, weight=10000, "
|
2016-08-31 17:29:14 -07:00
|
|
|
"labeltooltip=\"push_count: %u\"]\n",
|
|
|
|
|
i, head->node, i, head->push_count);
|
2016-11-04 09:18:38 -07:00
|
|
|
array_push(&self->iterators, ((Iterator){.node = head->node }));
|
2016-02-23 09:45:27 -08:00
|
|
|
}
|
2016-02-23 00:08:55 -08:00
|
|
|
|
2016-09-01 10:04:20 -07:00
|
|
|
bool all_iterators_done = false;
|
|
|
|
|
while (!all_iterators_done) {
|
|
|
|
|
all_iterators_done = true;
|
2016-02-23 00:08:55 -08:00
|
|
|
|
2016-11-14 12:15:24 -08:00
|
|
|
for (uint32_t i = 0; i < self->iterators.size; i++) {
|
2016-09-01 10:04:20 -07:00
|
|
|
Iterator *iterator = &self->iterators.contents[i];
|
|
|
|
|
StackNode *node = iterator->node;
|
2016-02-23 00:08:55 -08:00
|
|
|
|
2016-11-14 12:15:24 -08:00
|
|
|
for (uint32_t j = 0; j < visited_nodes.size; j++) {
|
2016-02-23 17:35:50 -08:00
|
|
|
if (visited_nodes.contents[j] == node) {
|
|
|
|
|
node = NULL;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-23 00:08:55 -08:00
|
|
|
if (!node)
|
|
|
|
|
continue;
|
2016-09-01 10:04:20 -07:00
|
|
|
all_iterators_done = false;
|
2016-02-23 00:08:55 -08:00
|
|
|
|
2016-05-01 15:24:50 -07:00
|
|
|
fprintf(f, "node_%p [", node);
|
2016-10-05 14:02:49 -07:00
|
|
|
if (node->state == ERROR_STATE)
|
2016-05-01 15:24:50 -07:00
|
|
|
fprintf(f, "label=\"?\"");
|
2016-05-26 13:20:53 -07:00
|
|
|
else if (node->link_count == 1 && node->links[0].tree &&
|
|
|
|
|
node->links[0].tree->extra)
|
2016-05-01 15:24:50 -07:00
|
|
|
fprintf(f, "shape=point margin=0 label=\"\"");
|
2016-03-02 09:55:25 -08:00
|
|
|
else
|
2016-05-29 22:36:47 -07:00
|
|
|
fprintf(f, "label=\"%d\"", node->state);
|
2016-08-31 10:51:59 -07:00
|
|
|
|
|
|
|
|
fprintf(f,
|
2016-11-14 12:15:24 -08:00
|
|
|
" tooltip=\"position: %u,%u\nerror_count: %u\nerror_cost: %u\"];\n",
|
2016-09-09 21:11:02 -07:00
|
|
|
node->position.extent.row, node->position.extent.column, node->error_count,
|
2016-08-31 17:30:16 -07:00
|
|
|
node->error_cost);
|
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-05-26 13:20:53 -07:00
|
|
|
if (link.tree && link.tree->extra)
|
2016-05-01 15:24:50 -07:00
|
|
|
fprintf(f, "fontcolor=gray ");
|
2016-02-24 17:23:58 -08:00
|
|
|
|
2016-05-26 13:20:53 -07:00
|
|
|
if (!link.tree) {
|
2016-09-01 10:04:20 -07:00
|
|
|
fprintf(f, "color=red, tooltip=\"push_count: %u\"", link.push_count);
|
2016-05-26 13:20:53 -07:00
|
|
|
} else if (link.tree->symbol == ts_builtin_sym_error) {
|
|
|
|
|
fprintf(f, "label=\"ERROR\"");
|
2016-03-02 09:55:25 -08:00
|
|
|
} else {
|
2016-05-26 13:20:53 -07:00
|
|
|
fprintf(f, "label=\"");
|
2016-08-29 16:39:14 -07:00
|
|
|
if (!link.tree->named)
|
|
|
|
|
fprintf(f, "'");
|
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-08-29 16:39:14 -07:00
|
|
|
if (!link.tree->named)
|
|
|
|
|
fprintf(f, "'");
|
2016-08-31 10:51:59 -07:00
|
|
|
fprintf(f, "\" labeltooltip=\"error_cost: %u\"",
|
|
|
|
|
link.tree->error_cost);
|
2016-02-24 17:23:58 -08:00
|
|
|
}
|
|
|
|
|
|
2016-05-26 13:20:53 -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-09-01 10:04:20 -07:00
|
|
|
iterator->node = link.node;
|
2016-02-23 17:35:50 -08:00
|
|
|
} else {
|
2016-11-04 09:18:38 -07:00
|
|
|
array_push(&self->iterators, *iterator);
|
2016-09-01 10:04:20 -07:00
|
|
|
Iterator *next_iterator = array_back(&self->iterators);
|
|
|
|
|
next_iterator->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
|
|
|
|
2016-11-05 14:39:25 -07:00
|
|
|
array_push(&visited_nodes, node);
|
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-05-16 10:44:19 -07:00
|
|
|
ts_toggle_allocation_recording(was_recording_allocations);
|
|
|
|
|
return true;
|
2016-02-23 00:08:55 -08:00
|
|
|
}
|