Add {next,prev}_concrete_sibling Node methods

This commit is contained in:
Max Brunsfeld 2015-09-08 21:43:37 -07:00
parent c3f3f19ea8
commit 245daffbc4
7 changed files with 208 additions and 142 deletions

View file

@ -62,69 +62,6 @@ TSNode ts_node_parent(TSNode this) {
return ts_node_make(tree, position);
}
TSNode ts_node_prev_sibling(TSNode this) {
const TSTree *tree = ts_node__tree(this);
TSLength position = ts_node__offset(this);
do {
TSTree *parent = tree->context.parent;
if (!parent)
break;
for (size_t i = tree->context.index - 1; i + 1 > 0; i--) {
const TSTree *child = parent->children[i];
position = ts_length_sub(position, ts_tree_total_size(child));
if (child->options.type == TSNodeTypeNormal)
return ts_node_make(child, position);
if (child->named_child_count > 0)
return ts_node_child(ts_node_make(child, position),
child->named_child_count - 1);
}
tree = parent;
} while (!ts_tree_is_visible(tree));
return ts_node__null();
}
TSNode ts_node_next_sibling(TSNode this) {
const TSTree *tree = ts_node__tree(this);
TSLength position = ts_node__offset(this);
do {
TSTree *parent = tree->context.parent;
if (!parent)
break;
const TSTree *prev_child = tree;
for (size_t i = tree->context.index + 1; i < parent->child_count; i++) {
const TSTree *child = parent->children[i];
position = ts_length_add(position, ts_tree_total_size(prev_child));
if (child->options.type == TSNodeTypeNormal)
return ts_node_make(child, position);
if (child->named_child_count > 0)
return ts_node_child(ts_node_make(child, position), 0);
prev_child = child;
}
tree = parent;
} while (!ts_tree_is_visible(tree));
return ts_node__null();
}
size_t ts_node_concrete_child_count(TSNode this) {
return ts_node__tree(this)->visible_child_count;
}
bool ts_node_is_concrete(TSNode this) {
return ts_node__tree(this)->options.type == TSNodeTypeConcrete;
}
size_t ts_node_child_count(TSNode this) {
return ts_node__tree(this)->named_child_count;
}
static TSNode ts_node__child_with_type(TSNode this, size_t child_index,
TSNodeType type) {
const TSTree *tree = ts_node__tree(this);
@ -161,15 +98,63 @@ static TSNode ts_node__child_with_type(TSNode this, size_t child_index,
return ts_node__null();
}
TSNode ts_node_child(TSNode this, size_t child_index) {
return ts_node__child_with_type(this, child_index, TSNodeTypeNormal);
static TSNode ts_node__prev_sibling_with_type(TSNode this, TSNodeType type) {
const TSTree *tree = ts_node__tree(this);
TSLength position = ts_node__offset(this);
do {
size_t index = tree->context.index;
position = ts_length_sub(position, tree->context.offset);
tree = tree->context.parent;
if (!tree)
break;
for (size_t i = index - 1; i + 1 > 0; i--) {
const TSTree *child = tree->children[i];
TSLength child_position = ts_length_add(position, child->context.offset);
if (child->options.type >= type)
return ts_node_make(child, child_position);
size_t grandchild_count = (type == TSNodeTypeNormal)
? child->named_child_count
: child->visible_child_count;
if (grandchild_count > 0)
return ts_node__child_with_type(ts_node_make(child, child_position),
grandchild_count - 1, type);
}
} while (!ts_tree_is_visible(tree));
return ts_node__null();
}
TSNode ts_node_concrete_child(TSNode this, size_t child_index) {
return ts_node__child_with_type(this, child_index, TSNodeTypeConcrete);
static TSNode ts_node__next_sibling_with_type(TSNode this, TSNodeType type) {
const TSTree *tree = ts_node__tree(this);
TSLength position = ts_node__offset(this);
do {
size_t index = tree->context.index;
position = ts_length_sub(position, tree->context.offset);
tree = tree->context.parent;
if (!tree)
break;
for (size_t i = index + 1; i < tree->child_count; i++) {
const TSTree *child = tree->children[i];
TSLength child_position = ts_length_add(position, child->context.offset);
if (child->options.type >= type)
return ts_node_make(child, child_position);
size_t grandchild_count = (type == TSNodeTypeNormal)
? child->named_child_count
: child->visible_child_count;
if (grandchild_count > 0)
return ts_node__child_with_type(ts_node_make(child, child_position), 0,
type);
}
} while (!ts_tree_is_visible(tree));
return ts_node__null();
}
TSNode ts_node_find_for_range(TSNode this, size_t min, size_t max) {
static TSNode ts_node__find_for_range_with_type(TSNode this, size_t min, size_t max, TSNodeType type) {
const TSTree *tree = ts_node__tree(this), *last_visible_tree = tree;
TSLength position = ts_node__offset(this), last_visible_position = position;
@ -183,7 +168,7 @@ TSNode ts_node_find_for_range(TSNode this, size_t min, size_t max) {
break;
if (position.chars + child->padding.chars + child->size.chars > max) {
tree = child;
if (ts_tree_is_visible(child)) {
if (child->options.type >= type) {
last_visible_tree = tree;
last_visible_position = position;
}
@ -197,6 +182,50 @@ TSNode ts_node_find_for_range(TSNode this, size_t min, size_t max) {
return ts_node_make(last_visible_tree, last_visible_position);
}
TSNode ts_node_next_sibling(TSNode this) {
return ts_node__next_sibling_with_type(this, TSNodeTypeNormal);
}
TSNode ts_node_prev_sibling(TSNode this) {
return ts_node__prev_sibling_with_type(this, TSNodeTypeNormal);
}
TSNode ts_node_next_concrete_sibling(TSNode this) {
return ts_node__next_sibling_with_type(this, TSNodeTypeConcrete);
}
TSNode ts_node_prev_concrete_sibling(TSNode this) {
return ts_node__prev_sibling_with_type(this, TSNodeTypeConcrete);
}
size_t ts_node_concrete_child_count(TSNode this) {
return ts_node__tree(this)->visible_child_count;
}
bool ts_node_is_concrete(TSNode this) {
return ts_node__tree(this)->options.type == TSNodeTypeConcrete;
}
size_t ts_node_child_count(TSNode this) {
return ts_node__tree(this)->named_child_count;
}
TSNode ts_node_child(TSNode this, size_t child_index) {
return ts_node__child_with_type(this, child_index, TSNodeTypeNormal);
}
TSNode ts_node_concrete_child(TSNode this, size_t child_index) {
return ts_node__child_with_type(this, child_index, TSNodeTypeConcrete);
}
TSNode ts_node_find_for_range(TSNode this, size_t min, size_t max) {
return ts_node__find_for_range_with_type(this, min, max, TSNodeTypeNormal);
}
TSNode ts_node_find_concrete_for_range(TSNode this, size_t min, size_t max) {
return ts_node__find_for_range_with_type(this, min, max, TSNodeTypeConcrete);
}
TSNode ts_node_find_for_pos(TSNode this, size_t position) {
return ts_node_find_for_range(this, position, position);
}

View file

@ -20,80 +20,69 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength size, TSLength padding,
.padding = padding,
.options = {.type = node_type },
};
if (sym == ts_builtin_sym_error) {
result->options.fragile_left = true;
result->options.fragile_right = true;
}
return result;
}
TSTree *ts_tree_make_error(TSLength size, TSLength padding, char lookahead_char) {
TSTree *result =
ts_tree_make_leaf(ts_builtin_sym_error, size, padding, TSNodeTypeNormal);
ts_tree_set_fragile_left(result);
ts_tree_set_fragile_right(result);
result->lookahead_char = lookahead_char;
return result;
}
TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count,
TSTree **children, TSNodeType node_type) {
TSTree *result = malloc(sizeof(TSTree));
/*
* Determine the new node's size, padding and visible child count based on
* the given child nodes.
*/
TSLength size = ts_length_zero(), padding = ts_length_zero();
size_t visible_child_count = 0, named_child_count = 0;
static void ts_tree__set_children(TSTree *this, TSTree **children,
size_t child_count) {
this->children = children;
this->child_count = child_count;
for (size_t i = 0; i < child_count; i++) {
TSTree *child = children[i];
ts_tree_retain(child);
child->context.parent = result;
child->context.parent = this;
child->context.index = i;
child->context.offset = ts_tree_total_size(this);
if (i == 0) {
padding = child->padding;
size = child->size;
this->padding = child->padding;
this->size = child->size;
} else {
size = ts_length_add(ts_length_add(size, child->padding), child->size);
this->size =
ts_length_add(ts_length_add(this->size, child->padding), child->size);
}
switch (child->options.type) {
case TSNodeTypeNormal:
visible_child_count++;
named_child_count++;
this->visible_child_count++;
this->named_child_count++;
break;
case TSNodeTypeConcrete:
visible_child_count++;
this->visible_child_count++;
break;
case TSNodeTypeHidden:
visible_child_count += child->visible_child_count;
named_child_count += child->named_child_count;
this->visible_child_count += child->visible_child_count;
this->named_child_count += child->named_child_count;
break;
}
}
TSTreeOptions options = (TSTreeOptions){
.type = node_type,
};
if (symbol == ts_builtin_sym_error) {
options.fragile_left = true;
options.fragile_left = true;
} else if (child_count > 0) {
options.fragile_left = children[0]->options.fragile_left;
options.fragile_right = children[child_count - 1]->options.fragile_right;
if (child_count > 0) {
if (children[0]->options.fragile_left)
this->options.fragile_left = true;
if (children[child_count - 1]->options.fragile_right)
this->options.fragile_right = true;
}
}
*result = (TSTree){
.ref_count = 1,
.context = {.parent = NULL, .index = 0 },
.symbol = symbol,
.children = children,
.child_count = child_count,
.visible_child_count = visible_child_count,
.named_child_count = named_child_count,
.size = size,
.padding = padding,
.options = options,
};
TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count,
TSTree **children, TSNodeType node_type) {
TSTree *result =
ts_tree_make_leaf(symbol, ts_length_zero(), ts_length_zero(), node_type);
ts_tree__set_children(result, children, child_count);
return result;
}
@ -197,36 +186,11 @@ void ts_tree_prepend_children(TSTree *tree, size_t count, TSTree **children) {
if (count == 0)
return;
tree->size = ts_length_add(tree->size, tree->padding);
size_t visible_count = 0, named_count = 0;
for (size_t i = 0; i < count; i++) {
if (i == 0)
tree->padding = children[i]->padding;
else
tree->size = ts_length_add(tree->size, children[i]->padding);
tree->size = ts_length_add(tree->size, children[i]->size);
switch (children[i]->options.type) {
case TSNodeTypeNormal:
visible_count++;
named_count++;
break;
case TSNodeTypeConcrete:
visible_count++;
break;
case TSNodeTypeHidden:
break;
}
}
size_t new_child_count = count + tree->child_count;
TSTree **new_children = realloc(children, new_child_count * sizeof(TSTree *));
memcpy(new_children + count, tree->children,
tree->child_count * sizeof(TSTree *));
free(tree->children);
tree->children = new_children;
tree->visible_child_count += visible_count;
tree->named_child_count += named_count;
tree->child_count += count;
ts_tree__set_children(tree, new_children, new_child_count);
}

View file

@ -19,6 +19,7 @@ struct TSTree {
struct {
struct TSTree *parent;
size_t index;
TSLength offset;
} context;
size_t child_count;
size_t visible_child_count;