Fix incremental parsing

Stop collapsing hidden symbols upon reducing them.
Sadly, this messes up the ability to re-use parse
trees. Instead, for now, hide these nodes when
stringifying parse trees
This commit is contained in:
Max Brunsfeld 2014-03-19 19:27:31 -07:00
parent 7e94a4f1b2
commit fbe8b0a905
8 changed files with 89 additions and 68 deletions

View file

@ -37,6 +37,10 @@ void ts_document_edit(ts_document *document, ts_input_edit edit) {
document->tree = ts_parser_parse(&document->parser, document->input, &edit);
}
const char * ts_document_symbol_name(const ts_document *document, const ts_tree *tree) {
return document->parser.symbol_names[tree->symbol];
}
typedef struct {
const char *string;
size_t position;

View file

@ -49,26 +49,14 @@ void ts_stack_shrink(ts_stack *stack, size_t new_size) {
stack->size = new_size;
}
ts_tree * ts_stack_reduce(ts_stack *stack, ts_symbol symbol, int immediate_child_count, const int *collapse_flags) {
size_t new_stack_size = stack->size - immediate_child_count;
ts_tree * ts_stack_reduce(ts_stack *stack, ts_symbol symbol, int child_count, const int *collapse_flags) {
size_t new_stack_size = stack->size - child_count;
int child_count = 0;
for (int i = 0; i < immediate_child_count; i++) {
ts_tree *child = stack->entries[new_stack_size + i].node;
if (collapse_flags[i]) {
size_t grandchild_count;
ts_tree_children(child, &grandchild_count);
child_count += grandchild_count;
} else {
child_count++;
}
}
int child_index = 0;
size_t size = 0, offset = 0;
ts_tree **children = malloc(child_count * sizeof(ts_tree *));
for (int i = 0; i < immediate_child_count; i++) {
for (int i = 0; i < child_count; i++) {
ts_tree *child = stack->entries[new_stack_size + i].node;
child->is_hidden = collapse_flags[i];
if (i == 0) {
offset = child->offset;
size = child->size;
@ -76,15 +64,7 @@ ts_tree * ts_stack_reduce(ts_stack *stack, ts_symbol symbol, int immediate_child
size += child->offset + child->size;
}
if (collapse_flags[i]) {
size_t grandchild_count;
ts_tree ** grandchildren = ts_tree_children(child, &grandchild_count);
memcpy(children + child_index, grandchildren, (grandchild_count * sizeof(ts_tree *)));
child_index += grandchild_count;
} else {
children[child_index] = child;
child_index++;
}
children[i] = child;
}
ts_tree *lookahead = ts_tree_make_node(symbol, child_count, children, size, offset);

View file

@ -3,11 +3,13 @@
#include <stdio.h>
static ts_tree * ts_tree_make(ts_symbol symbol, size_t size, size_t offset) {
ts_tree *result = (ts_tree *)malloc(sizeof(ts_tree));
result->ref_count = 1;
result->symbol = symbol;
result->size = size;
result->offset = offset;
ts_tree *result = malloc(sizeof(ts_tree));
*result = (ts_tree) {
.ref_count = 1,
.symbol = symbol,
.size = size,
.offset = offset,
};
return result;
}
@ -77,33 +79,47 @@ ts_tree ** ts_tree_children(const ts_tree *tree, size_t *count) {
return tree->data.children.contents;
}
static size_t tree_write_to_string(const ts_tree *tree, const char **symbol_names, char *string, size_t limit) {
static const char *NULL_TREE_STRING = "(NULL)";
static const char *ERROR_TREE_STRING = "(ERROR)";
static size_t tree_write_to_string(const ts_tree *tree, const char **symbol_names, char *string, size_t limit, int is_beginning) {
char *cursor = string;
size_t result = 0;
if (!tree)
return snprintf(string, limit, "%s", NULL_TREE_STRING);
if (tree->symbol == ts_builtin_sym_error)
return snprintf(string, limit, "%s", ERROR_TREE_STRING);
size_t result = snprintf(string, limit, "(%s", symbol_names[tree->symbol]);
char *cursor = string + result;
for (size_t i = 0; i < tree->data.children.count; i++) {
ts_tree *child = tree->data.children.contents[i];
result += snprintf(cursor, limit, " ");
result += tree_write_to_string(child, symbol_names, cursor + 1, limit);
cursor = (limit > 0) ? string + result : string;
if (!tree) {
return snprintf(cursor, limit, "(NULL)");
}
if (!tree->is_hidden) {
if (!is_beginning) {
result += snprintf(cursor, limit, " ");
if (limit > 0) cursor = string + result;
}
if (tree->symbol == ts_builtin_sym_error) {
result += snprintf(cursor, limit, "(ERROR)");
return result;
}
result += snprintf(cursor, limit, "(%s", symbol_names[tree->symbol]);
if (limit > 0) cursor = string + result;
}
return result + snprintf(cursor, limit, ")");
for (size_t i = 0; i < tree->data.children.count; i++) {
ts_tree *child = tree->data.children.contents[i];
result += tree_write_to_string(child, symbol_names, cursor, limit, 0);
if (limit > 0) cursor = string + result;
}
if (!tree->is_hidden) {
result += snprintf(cursor, limit, ")");
}
return result;
}
static char SCRATCH_STRING[1];
char * ts_tree_string(const ts_tree *tree, const char **symbol_names) {
size_t size = tree_write_to_string(tree, symbol_names, SCRATCH_STRING, 0) + 1;
static char SCRATCH_STRING[100];
size_t size = tree_write_to_string(tree, symbol_names, SCRATCH_STRING, 0, 1) + 1;
char *result = malloc(size * sizeof(char));
tree_write_to_string(tree, symbol_names, result, size);
tree_write_to_string(tree, symbol_names, result, size, 1);
return result;
}