This commit is contained in:
Max Brunsfeld 2014-03-19 22:59:07 -07:00
parent fbe8b0a905
commit cbc66b359e
3 changed files with 21 additions and 36 deletions

View file

@ -269,13 +269,6 @@ static ts_lr_parser * ts_lr_parser_make() {
return result;
}
// static const char * ts_symbol_names[];
// static void dump_stack(ts_lr_parser *parser) {
// for (size_t i = 0; i < parser->stack.size; i++) {
// printf("\n%ld %s", i, ts_symbol_names[parser->stack.entries[i].node->symbol]);
// }
// }
static size_t ts_lr_parser_breakdown_stack(ts_lr_parser *parser, ts_input_edit *edit) {
if (parser->stack.size == 0) return 0;

View file

@ -47,11 +47,11 @@ typedef struct {
void (* release_fn)(void *data);
} ts_input;
typedef struct {
size_t position;
size_t bytes_inserted;
size_t bytes_removed;
} ts_input_edit;
typedef struct {
size_t position;
size_t bytes_inserted;
size_t bytes_removed;
} ts_input_edit;
typedef struct {
const ts_tree * (* parse_fn)(void *data, ts_input input, ts_input_edit *edit);
@ -71,7 +71,6 @@ void ts_document_set_parser(ts_document *doc, ts_parser parser);
void ts_document_set_input(ts_document *doc, ts_input input);
void ts_document_set_input_string(ts_document *doc, const char *text);
void ts_document_edit(ts_document *doc, ts_input_edit edit);
const ts_tree * ts_document_tree(const ts_document *doc);
const char * ts_document_string(const ts_document *doc);
const char * ts_document_symbol_name(const ts_document *document, const ts_tree *tree);

View file

@ -81,38 +81,31 @@ ts_tree ** ts_tree_children(const ts_tree *tree, size_t *count) {
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;
char **destination = (limit > 0) ? &cursor : &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)
return snprintf(*destination, limit, "(NULL)");
if (!tree->is_hidden && !is_beginning)
cursor += snprintf(*destination, limit, " ");
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;
if (tree->symbol == ts_builtin_sym_error) {
cursor += snprintf(*destination, limit, "(ERROR)");
return cursor - string;
}
if (!tree->is_hidden)
cursor += snprintf(*destination, limit, "(%s", symbol_names[tree->symbol]);
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;
cursor += tree_write_to_string(child, symbol_names, *destination, limit, 0);
}
if (!tree->is_hidden) {
result += snprintf(cursor, limit, ")");
}
if (!tree->is_hidden)
cursor += snprintf(*destination, limit, ")");
return result;
return cursor - string;
}
char * ts_tree_string(const ts_tree *tree, const char **symbol_names) {