Don't reuse any nodes that contain an error as a child node

This commit is contained in:
Max Brunsfeld 2014-10-23 12:50:37 -07:00
parent 647c3e2010
commit 3bc4971fd5
5 changed files with 83 additions and 24 deletions

View file

@ -106,7 +106,7 @@ static TSTree *break_down_right_stack(TSParser *parser) {
TSParseAction action = get_action(parser->language, state, node->symbol);
bool is_usable = (action.type != TSParseActionTypeError) &&
(node->symbol != ts_builtin_sym_error) &&
!ts_tree_has_error(node) &&
!ts_tree_is_extra(node);
if (is_usable && right_subtree_start == current_position.chars) {
ts_stack_shrink(&parser->right_stack, parser->right_stack.size - 1);

View file

@ -21,6 +21,7 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength size, TSLength padding,
TSTree *ts_tree_make_error(TSLength size, TSLength padding, char lookahead_char) {
TSTree *result = ts_tree_make_leaf(ts_builtin_sym_error, size, padding, false);
ts_tree_set_has_error(result);
result->lookahead_char = lookahead_char;
return result;
}
@ -34,6 +35,7 @@ TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count,
*/
TSLength size = ts_length_zero(), padding = ts_length_zero();
size_t visible_child_count = 0;
bool has_error = false;
for (size_t i = 0; i < child_count; i++) {
TSTree *child = children[i];
ts_tree_retain(child);
@ -49,12 +51,17 @@ TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count,
visible_child_count++;
else
visible_child_count += child->visible_child_count;
if (ts_tree_has_error(child))
has_error = true;
}
/*
* Mark the tree as hidden if it wraps a single child node.
*/
TSTreeOptions options = 0;
if (has_error)
options |= TSTreeOptionsHasError;
if (is_hidden)
options |= TSTreeOptionsHidden;
if (child_count == 1 &&
@ -197,15 +204,9 @@ static size_t tree_write_to_string(const TSTree *tree, const char **symbol_names
}
char *ts_tree_string(const TSTree *tree, const char **symbol_names) {
/*
* Determine the length of the string first, so that the right amount of
* memory can be allocated.
*/
static char SCRATCH[1];
size_t size = tree_write_to_string(tree, symbol_names, SCRATCH, 0, 1) + 1;
char *result = malloc(size * sizeof(char));
tree_write_to_string(tree, symbol_names, result, size, 1);
return result;
}

View file

@ -9,9 +9,10 @@ extern "C" {
#include "tree_sitter/parser.h"
typedef enum {
TSTreeOptionsHidden = 1,
TSTreeOptionsExtra = 2,
TSTreeOptionsWrapper = 4,
TSTreeOptionsHidden = 1 << 0,
TSTreeOptionsExtra = 1 << 1,
TSTreeOptionsWrapper = 1 << 2,
TSTreeOptionsHasError = 1 << 3,
} TSTreeOptions;
struct TSTree {
@ -39,14 +40,26 @@ static inline bool ts_tree_is_visible(const TSTree *tree) {
return !(tree->options & TSTreeOptionsHidden);
}
static inline void ts_tree_set_extra(TSTree *tree) {
tree->options = (TSTreeOptions)(tree->options | TSTreeOptionsExtra);
}
static inline bool ts_tree_is_wrapper(const TSTree *tree) {
return !!(tree->options & TSTreeOptionsWrapper);
}
static inline bool ts_tree_has_error(const TSTree *tree) {
return !!(tree->options & TSTreeOptionsHasError);
}
static inline void ts_tree_set_options(TSTree *tree, TSTreeOptions options) {
tree->options = (TSTreeOptions)(tree->options | options);
}
static inline void ts_tree_set_extra(TSTree *tree) {
ts_tree_set_options(tree, TSTreeOptionsExtra);
}
static inline void ts_tree_set_has_error(TSTree *tree) {
ts_tree_set_options(tree, TSTreeOptionsHasError);
}
TSTree *ts_tree_make_leaf(TSSymbol, TSLength, TSLength, bool);
TSTree *ts_tree_make_node(TSSymbol, size_t, TSTree **, bool);
TSTree *ts_tree_make_error(TSLength size, TSLength padding, char lookahead_char);