From cbc66b359e782ee0f68168e51c3b184d96b41e14 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 19 Mar 2014 22:59:07 -0700 Subject: [PATCH] Cleanup --- include/tree_sitter/parser.h | 7 ------- include/tree_sitter/runtime.h | 11 +++++----- src/runtime/tree.c | 39 ++++++++++++++--------------------- 3 files changed, 21 insertions(+), 36 deletions(-) diff --git a/include/tree_sitter/parser.h b/include/tree_sitter/parser.h index 39bc53c2..e3df0c34 100644 --- a/include/tree_sitter/parser.h +++ b/include/tree_sitter/parser.h @@ -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; diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index be4b5470..179395b4 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -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); diff --git a/src/runtime/tree.c b/src/runtime/tree.c index ee813904..37f223f3 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -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) {