From 59fd19069587cec2e4e25b02a77e8a99bf5077ea Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Sun, 24 May 2015 14:43:54 -0700 Subject: [PATCH] Don't store lookahead_char field for non-error nodes --- src/runtime/tree.c | 37 +++++++++++++++++++++++++------------ src/runtime/tree.h | 14 +++++++++----- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/runtime/tree.c b/src/runtime/tree.c index 4a0f1c44..31fccd5f 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -13,7 +13,6 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength size, TSLength padding, .size = size, .child_count = 0, .children = NULL, - .lookahead_char = 0, .padding = padding, .options = is_hidden ? TSTreeOptionsHidden : 0, }; return result; @@ -122,9 +121,11 @@ void ts_tree_release(TSTree *tree) { if (tree->ref_count == 0) { size_t count; TSTree **children = ts_tree_children(tree, &count); - for (size_t i = 0; i < count; i++) - ts_tree_release(children[i]); - free(tree->children); + if (children) { + for (size_t i = 0; i < count; i++) + ts_tree_release(children[i]); + free(tree->children); + } free(tree); } } @@ -136,8 +137,8 @@ TSLength ts_tree_total_size(const TSTree *tree) { bool ts_tree_eq(const TSTree *node1, const TSTree *node2) { if (node1->symbol != node2->symbol) return false; - if (node1->lookahead_char != node2->lookahead_char) - return false; + if (node1->symbol == ts_builtin_sym_error) + return node1->lookahead_char == node2->lookahead_char; if (node1->child_count != node2->child_count) return false; if (node1->visible_child_count != node2->visible_child_count) @@ -149,15 +150,27 @@ bool ts_tree_eq(const TSTree *node1, const TSTree *node2) { } TSTree **ts_tree_children(const TSTree *tree, size_t *count) { - if (count) - *count = tree->child_count; - return tree->children; + if (tree->symbol == ts_builtin_sym_error) { + if (count) + *count = 0; + return NULL; + } else { + if (count) + *count = tree->child_count; + return tree->children; + } } TSTreeChild *ts_tree_visible_children(const TSTree *tree, size_t *count) { - if (count) - *count = tree->visible_child_count; - return (TSTreeChild *)(tree + 1); + if (tree->symbol == ts_builtin_sym_error) { + if (count) + *count = 0; + return NULL; + } else { + if (count) + *count = tree->visible_child_count; + return (TSTreeChild *)(tree + 1); + } } static size_t write_lookahead_to_string(char *string, size_t limit, diff --git a/src/runtime/tree.h b/src/runtime/tree.h index e9ecf7c2..f230990c 100644 --- a/src/runtime/tree.h +++ b/src/runtime/tree.h @@ -19,13 +19,17 @@ typedef enum { struct TSTree { TSSymbol symbol; TSTreeOptions options; - size_t ref_count; TSLength padding; TSLength size; - char lookahead_char; - size_t child_count; - size_t visible_child_count; - struct TSTree **children; + union { + struct { + struct TSTree **children; + size_t child_count; + size_t visible_child_count; + }; + char lookahead_char; + }; + unsigned short int ref_count; }; typedef struct {