2015-05-25 20:21:13 -07:00
|
|
|
#include "tree_sitter/parser.h"
|
|
|
|
|
#include "runtime/tree.h"
|
2015-05-30 20:26:45 -07:00
|
|
|
#include "runtime/tree_vector.h"
|
2015-05-25 20:21:13 -07:00
|
|
|
#include "runtime/parse_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>
|
|
|
|
|
|
2015-06-03 09:44:13 -07:00
|
|
|
#define MAX_POP_PATH_COUNT 8
|
|
|
|
|
#define INITIAL_HEAD_CAPACITY 3
|
2015-06-18 15:04:03 -07:00
|
|
|
#define STARTING_TREE_CAPACITY 10
|
2015-06-03 09:44:13 -07:00
|
|
|
|
|
|
|
|
typedef struct ParseStackNode {
|
|
|
|
|
ParseStackEntry entry;
|
|
|
|
|
struct ParseStackNode *successors[MAX_POP_PATH_COUNT];
|
|
|
|
|
short unsigned int successor_count;
|
|
|
|
|
short unsigned int ref_count;
|
|
|
|
|
} ParseStackNode;
|
2015-05-25 20:21:13 -07:00
|
|
|
|
|
|
|
|
struct ParseStack {
|
|
|
|
|
ParseStackNode **heads;
|
|
|
|
|
int head_count;
|
|
|
|
|
int head_capacity;
|
2015-06-03 09:44:13 -07:00
|
|
|
ParseStackPopResult last_pop_results[MAX_POP_PATH_COUNT];
|
2015-07-15 09:21:53 -07:00
|
|
|
TreeSelectionCallback tree_selection_callback;
|
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
|
|
|
*/
|
|
|
|
|
|
2015-07-15 09:21:53 -07:00
|
|
|
ParseStack *ts_parse_stack_new(TreeSelectionCallback tree_selection_callback) {
|
2015-05-25 20:21:13 -07:00
|
|
|
ParseStack *this = malloc(sizeof(ParseStack));
|
|
|
|
|
*this = (ParseStack) {
|
|
|
|
|
.heads = calloc(INITIAL_HEAD_CAPACITY, sizeof(ParseStackNode *)),
|
|
|
|
|
.head_count = 1,
|
|
|
|
|
.head_capacity = INITIAL_HEAD_CAPACITY,
|
2015-07-15 09:21:53 -07:00
|
|
|
.tree_selection_callback = tree_selection_callback,
|
2015-05-25 20:21:13 -07:00
|
|
|
};
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ts_parse_stack_delete(ParseStack *this) {
|
2015-06-03 09:44:13 -07:00
|
|
|
free(this->heads);
|
2015-05-25 20:21:13 -07:00
|
|
|
free(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2015-06-03 09:44:13 -07:00
|
|
|
* Section: Reading from the stack
|
2015-05-25 20:21:13 -07:00
|
|
|
*/
|
|
|
|
|
|
2015-06-18 15:04:03 -07:00
|
|
|
TSStateId ts_parse_stack_top_state(const ParseStack *this, int head) {
|
|
|
|
|
ParseStackEntry *entry = ts_parse_stack_head((ParseStack *)this, head);
|
|
|
|
|
return entry ? entry->state : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TSTree *ts_parse_stack_top_tree(const ParseStack *this, int head) {
|
|
|
|
|
ParseStackEntry *entry = ts_parse_stack_head((ParseStack *)this, head);
|
|
|
|
|
return entry ? entry->tree : NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ParseStackEntry *ts_parse_stack_head(ParseStack *this, int head) {
|
2015-06-03 09:44:13 -07:00
|
|
|
assert(head < this->head_count);
|
|
|
|
|
ParseStackNode *node = this->heads[head];
|
|
|
|
|
return node ? &node->entry : NULL;
|
2015-05-25 20:21:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ts_parse_stack_head_count(const ParseStack *this) {
|
|
|
|
|
return this->head_count;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-03 09:44:13 -07:00
|
|
|
int ts_parse_stack_entry_next_count(const ParseStackEntry *entry) {
|
|
|
|
|
return ((const ParseStackNode *)entry)->successor_count;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-18 15:04:03 -07:00
|
|
|
ParseStackEntry *ts_parse_stack_entry_next(const ParseStackEntry *entry, int i) {
|
2015-06-03 09:44:13 -07:00
|
|
|
return &((const ParseStackNode *)entry)->successors[i]->entry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Section: Manipulating nodes (Private)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static void stack_node_retain(ParseStackNode *this) {
|
|
|
|
|
if (!this) return;
|
|
|
|
|
assert(this->ref_count != 0);
|
|
|
|
|
this->ref_count++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool stack_node_release(ParseStackNode *this) {
|
|
|
|
|
if (!this) return false;
|
|
|
|
|
assert(this->ref_count != 0);
|
|
|
|
|
this->ref_count--;
|
|
|
|
|
if (this->ref_count == 0) {
|
|
|
|
|
for (int i = 0; i < this->successor_count; i++)
|
|
|
|
|
stack_node_release(this->successors[i]);
|
|
|
|
|
ts_tree_release(this->entry.tree);
|
|
|
|
|
free(this);
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static ParseStackNode *stack_node_new(ParseStackNode *next, TSStateId state, TSTree *tree) {
|
|
|
|
|
ParseStackNode *this = malloc(sizeof(ParseStackNode));
|
|
|
|
|
ts_tree_retain(tree);
|
|
|
|
|
stack_node_retain(next);
|
|
|
|
|
*this = (ParseStackNode) {
|
|
|
|
|
.ref_count = 1,
|
|
|
|
|
.successor_count = 1,
|
|
|
|
|
.successors = {next, NULL, NULL},
|
|
|
|
|
.entry = {
|
|
|
|
|
.state = state,
|
|
|
|
|
.tree = tree,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-15 09:21:53 -07:00
|
|
|
static void ts_parse_stack_add_node_successor(ParseStack *this, ParseStackNode *node, ParseStackNode *new_successor) {
|
|
|
|
|
for (int i = 0; i < node->successor_count; i++) {
|
|
|
|
|
ParseStackNode *successor = node->successors[i];
|
|
|
|
|
if (successor == new_successor)
|
2015-06-03 09:44:13 -07:00
|
|
|
return;
|
2015-07-15 09:21:53 -07:00
|
|
|
if (successor->entry.state == new_successor->entry.state) {
|
|
|
|
|
if (successor->entry.tree != new_successor->entry.tree)
|
|
|
|
|
successor->entry.tree = this->tree_selection_callback.callback(
|
|
|
|
|
this->tree_selection_callback.data,
|
|
|
|
|
successor->entry.tree,
|
|
|
|
|
new_successor->entry.tree
|
|
|
|
|
);
|
|
|
|
|
for (int j = 0; j < new_successor->successor_count; j++)
|
|
|
|
|
ts_parse_stack_add_node_successor(this, successor, new_successor->successors[j]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stack_node_retain(new_successor);
|
|
|
|
|
node->successors[node->successor_count] = new_successor;
|
|
|
|
|
node->successor_count++;
|
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-06-03 09:44:13 -07:00
|
|
|
static int ts_parse_stack_add_head(ParseStack *this, ParseStackNode *node) {
|
|
|
|
|
if (this->head_count == this->head_capacity) {
|
|
|
|
|
this->head_capacity += 3;
|
|
|
|
|
this->heads = realloc(this->heads, this->head_capacity * sizeof(ParseStackNode *));
|
|
|
|
|
}
|
|
|
|
|
int new_index = this->head_count++;
|
|
|
|
|
this->heads[new_index] = node;
|
|
|
|
|
stack_node_retain(node);
|
|
|
|
|
return new_index;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-08 17:34:21 -07:00
|
|
|
static int ts_parse_stack_find_or_add_head(ParseStack *this, ParseStackNode *node) {
|
|
|
|
|
for (int i = 0; i < this->head_count; i++)
|
|
|
|
|
if (this->heads[i] == node) {
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
return ts_parse_stack_add_head(this, node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ts_parse_stack_remove_head(ParseStack *this, int head_index) {
|
2015-06-03 09:44:13 -07:00
|
|
|
stack_node_release(this->heads[head_index]);
|
|
|
|
|
for (int i = head_index; i < this->head_count - 1; i++) {
|
|
|
|
|
this->heads[head_index] = this->heads[head_index + 1];
|
|
|
|
|
}
|
|
|
|
|
this->head_count--;
|
|
|
|
|
}
|
2015-05-25 20:21:13 -07:00
|
|
|
|
2015-06-03 09:44:13 -07:00
|
|
|
static bool ts_parse_stack_merge_head(ParseStack *this, int head_index, TSStateId state, TSTree *tree) {
|
|
|
|
|
for (int i = 0; i < head_index; i++) {
|
|
|
|
|
ParseStackNode *head = this->heads[i];
|
|
|
|
|
if (head->entry.state == state) {
|
2015-07-15 09:21:53 -07:00
|
|
|
if (head->entry.tree != tree) {
|
|
|
|
|
head->entry.tree = this->tree_selection_callback.callback(
|
|
|
|
|
this->tree_selection_callback.data,
|
|
|
|
|
head->entry.tree,
|
|
|
|
|
tree
|
|
|
|
|
);
|
2015-06-03 09:44:13 -07:00
|
|
|
}
|
2015-07-15 09:21:53 -07:00
|
|
|
ts_parse_stack_add_node_successor(this, head, this->heads[head_index]);
|
|
|
|
|
ts_parse_stack_remove_head(this, head_index);
|
|
|
|
|
return true;
|
2015-06-03 09:44:13 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Section: Mutating the stack (Public)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
bool ts_parse_stack_push(ParseStack *this, int head_index, TSStateId state, TSTree *tree) {
|
2015-05-25 20:21:13 -07:00
|
|
|
assert(head_index < this->head_count);
|
2015-06-03 09:44:13 -07:00
|
|
|
if (ts_parse_stack_merge_head(this, head_index, state, tree))
|
2015-05-25 20:27:51 -07:00
|
|
|
return true;
|
|
|
|
|
this->heads[head_index] = stack_node_new(this->heads[head_index], state, tree);
|
2015-05-25 20:21:13 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-08 17:34:21 -07:00
|
|
|
void ts_parse_stack_add_alternative(ParseStack *this, int head_index, TSTree *tree) {
|
|
|
|
|
assert(head_index < this->head_count);
|
|
|
|
|
ParseStackEntry *entry = &this->heads[head_index]->entry;
|
2015-07-15 09:21:53 -07:00
|
|
|
entry->tree = this->tree_selection_callback.callback(
|
|
|
|
|
this->tree_selection_callback.data,
|
|
|
|
|
entry->tree,
|
|
|
|
|
tree
|
|
|
|
|
);
|
2015-07-08 17:34:21 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-03 09:44:13 -07:00
|
|
|
int ts_parse_stack_split(ParseStack *this, int head_index) {
|
|
|
|
|
assert(head_index < this->head_count);
|
|
|
|
|
return ts_parse_stack_add_head(this, this->heads[head_index]);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-18 15:04:03 -07:00
|
|
|
ParseStackPopResultList ts_parse_stack_pop(ParseStack *this, int head_index, int child_count, bool count_extra) {
|
2015-06-03 09:44:13 -07:00
|
|
|
ParseStackNode *previous_head = this->heads[head_index];
|
2015-05-30 20:26:45 -07:00
|
|
|
|
|
|
|
|
int path_count = 1;
|
2015-06-18 15:04:03 -07:00
|
|
|
int capacity = (child_count == -1) ? STARTING_TREE_CAPACITY : child_count;
|
2015-06-03 09:44:13 -07:00
|
|
|
size_t tree_counts_by_path[MAX_POP_PATH_COUNT] = {child_count};
|
|
|
|
|
ParseStackNode *nodes_by_path[MAX_POP_PATH_COUNT] = {previous_head};
|
2015-06-18 15:04:03 -07:00
|
|
|
TreeVector trees_by_path[MAX_POP_PATH_COUNT] = {tree_vector_new(capacity)};
|
2015-07-08 17:34:21 -07:00
|
|
|
bool is_shared_by_path[MAX_POP_PATH_COUNT] = {false};
|
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;
|
|
|
|
|
int current_path_count = path_count;
|
|
|
|
|
for (int path = 0; path < current_path_count; path++) {
|
2015-06-18 15:04:03 -07:00
|
|
|
ParseStackNode *node = nodes_by_path[path];
|
|
|
|
|
if (!node || (trees_by_path[path].size == tree_counts_by_path[path]))
|
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
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Children that are 'extra' do not count towards the total child count.
|
|
|
|
|
*/
|
2015-06-18 15:04:03 -07:00
|
|
|
if (ts_tree_is_extra(node->entry.tree) && !count_extra)
|
2015-06-03 09:44:13 -07:00
|
|
|
tree_counts_by_path[path]++;
|
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.
|
|
|
|
|
*/
|
2015-07-08 17:34:21 -07:00
|
|
|
if (is_shared_by_path[path]) {
|
|
|
|
|
trees_by_path[path] = tree_vector_copy(&trees_by_path[path]);
|
|
|
|
|
is_shared_by_path[path] = false;
|
|
|
|
|
}
|
2015-06-03 09:44:13 -07:00
|
|
|
tree_vector_push(&trees_by_path[path], node->entry.tree);
|
2015-05-30 20:26:45 -07:00
|
|
|
|
|
|
|
|
for (int i = 0; i < node->successor_count; i++) {
|
|
|
|
|
int next_path;
|
|
|
|
|
if (i > 0) {
|
2015-06-03 09:44:13 -07:00
|
|
|
if (path_count == MAX_POP_PATH_COUNT) break;
|
2015-05-30 20:26:45 -07:00
|
|
|
next_path = path_count;
|
2015-06-03 09:44:13 -07:00
|
|
|
tree_counts_by_path[next_path] = tree_counts_by_path[path];
|
2015-07-08 17:34:21 -07:00
|
|
|
trees_by_path[next_path] = trees_by_path[path];
|
|
|
|
|
is_shared_by_path[next_path] = true;
|
2015-05-30 20:26:45 -07:00
|
|
|
path_count++;
|
|
|
|
|
} else {
|
|
|
|
|
next_path = path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nodes_by_path[next_path] = node->successors[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-05-25 20:21:13 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-03 09:44:13 -07:00
|
|
|
for (int path = 0; path < path_count; path++) {
|
|
|
|
|
tree_vector_reverse(&trees_by_path[path]);
|
2015-07-08 17:34:21 -07:00
|
|
|
int index = -1;
|
2015-06-03 09:44:13 -07:00
|
|
|
if (path == 0) {
|
2015-05-30 20:26:45 -07:00
|
|
|
stack_node_retain(nodes_by_path[path]);
|
2015-06-03 09:44:13 -07:00
|
|
|
this->heads[head_index] = nodes_by_path[path];
|
|
|
|
|
index = head_index;
|
|
|
|
|
} else {
|
2015-07-08 17:34:21 -07:00
|
|
|
index = ts_parse_stack_find_or_add_head(this, nodes_by_path[path]);
|
2015-05-30 20:26:45 -07:00
|
|
|
}
|
2015-05-25 20:21:13 -07:00
|
|
|
|
2015-06-03 09:44:13 -07:00
|
|
|
this->last_pop_results[path] = (ParseStackPopResult) {
|
|
|
|
|
.index = index,
|
|
|
|
|
.tree_count = trees_by_path[path].size,
|
|
|
|
|
.trees = trees_by_path[path].contents,
|
|
|
|
|
};
|
2015-05-30 20:26:45 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-03 09:44:13 -07:00
|
|
|
stack_node_release(previous_head);
|
|
|
|
|
return (ParseStackPopResultList) {
|
|
|
|
|
.size = path_count,
|
|
|
|
|
.contents = this->last_pop_results,
|
2015-05-25 20:21:13 -07:00
|
|
|
};
|
|
|
|
|
}
|
2015-06-18 15:04:03 -07:00
|
|
|
|
|
|
|
|
void ts_parse_stack_shrink(ParseStack *this, int head_index, int count) {
|
|
|
|
|
ParseStackNode *head = this->heads[head_index];
|
|
|
|
|
ParseStackNode *new_head = head;
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
if (new_head->successor_count == 0) break;
|
|
|
|
|
new_head = new_head->successors[0];
|
|
|
|
|
}
|
|
|
|
|
stack_node_retain(new_head);
|
|
|
|
|
stack_node_release(head);
|
|
|
|
|
this->heads[head_index] = new_head;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ts_parse_stack_clear(ParseStack *this) {
|
|
|
|
|
for (int i = 0; i < this->head_count; i++)
|
|
|
|
|
stack_node_release(this->heads[i]);
|
|
|
|
|
this->head_count = 1;
|
|
|
|
|
this->heads[0] = NULL;
|
|
|
|
|
}
|