Replace NodeType enum with SymbolMetadata bitfield

This will allow storing other metadata about symbols, like if they
only appear as ubiquitous tokens
This commit is contained in:
Max Brunsfeld 2015-11-22 13:32:20 -08:00
parent 53424699e4
commit f08554e958
17 changed files with 541 additions and 512 deletions

View file

@ -24,9 +24,17 @@ static inline TSLength ts_node__offset(TSNode self) {
return self.offset;
}
static inline bool ts_tree__is_relevant(const TSTree *tree, bool include_anonymous) {
return include_anonymous ? tree->options.visible : tree->options.named;
}
static inline size_t ts_tree__relevant_child_count(const TSTree *tree,
bool include_anonymous) {
return include_anonymous ? tree->visible_child_count : tree->named_child_count;
}
static inline TSNode ts_node__child(TSNode self, size_t child_index,
TSNodeType type) {
bool include_anonymous) {
const TSTree *tree = ts_node__tree(self);
TSLength position = ts_node__offset(self);
size_t offset_row = self.row;
@ -38,15 +46,14 @@ static inline TSNode ts_node__child(TSNode self, size_t child_index,
size_t index = 0;
for (size_t i = 0; i < tree->child_count; i++) {
TSTree *child = tree->children[i];
if (child->options.type >= type) {
if (ts_tree__is_relevant(child, include_anonymous)) {
if (index == child_index)
return ts_node_make(child, position, offset_row);
index++;
} else {
size_t grandchild_index = child_index - index;
size_t grandchild_count = (type == TSNodeTypeNamed)
? child->named_child_count
: child->visible_child_count;
size_t grandchild_count =
ts_tree__relevant_child_count(child, include_anonymous);
if (grandchild_index < grandchild_count) {
did_descend = true;
tree = child;
@ -63,7 +70,7 @@ static inline TSNode ts_node__child(TSNode self, size_t child_index,
return ts_node__null();
}
static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) {
static inline TSNode ts_node__prev_sibling(TSNode self, bool include_anonymous) {
const TSTree *tree = ts_node__tree(self);
TSLength offset = ts_node__offset(self);
size_t offset_row = self.row;
@ -80,21 +87,19 @@ static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) {
const TSTree *child = tree->children[i];
TSLength child_offset = ts_length_add(offset, child->context.offset);
size_t child_offset_row = offset_row + child->context.offset_point.row;
if (child->options.type >= type)
if (ts_tree__is_relevant(child, include_anonymous))
return ts_node_make(child, child_offset, child_offset_row);
size_t grandchild_count = (type == TSNodeTypeNamed)
? child->named_child_count
: child->visible_child_count;
size_t grandchild_count = ts_tree__relevant_child_count(child, include_anonymous);
if (grandchild_count > 0)
return ts_node__child(ts_node_make(child, child_offset, child_offset_row),
grandchild_count - 1, type);
grandchild_count - 1, include_anonymous);
}
} while (!ts_tree_is_visible(tree));
return ts_node__null();
}
static inline TSNode ts_node__next_sibling(TSNode self, TSNodeType type) {
static inline TSNode ts_node__next_sibling(TSNode self, bool include_anonymous) {
const TSTree *tree = ts_node__tree(self);
TSLength offset = ts_node__offset(self);
size_t offset_row = self.row;
@ -111,13 +116,12 @@ static inline TSNode ts_node__next_sibling(TSNode self, TSNodeType type) {
const TSTree *child = tree->children[i];
TSLength child_offset = ts_length_add(offset, child->context.offset);
size_t child_offset_row = offset_row + child->context.offset_point.row;
if (child->options.type >= type)
if (ts_tree__is_relevant(child, include_anonymous))
return ts_node_make(child, child_offset, child_offset_row);
size_t grandchild_count = (type == TSNodeTypeNamed)
? child->named_child_count
: child->visible_child_count;
size_t grandchild_count = ts_tree__relevant_child_count(child, include_anonymous);
if (grandchild_count > 0)
return ts_node__child(ts_node_make(child, child_offset, child_offset_row), 0, type);
return ts_node__child(ts_node_make(child, child_offset, child_offset_row), 0,
include_anonymous);
}
} while (!ts_tree_is_visible(tree));
@ -125,9 +129,10 @@ static inline TSNode ts_node__next_sibling(TSNode self, TSNodeType type) {
}
static inline TSNode ts_node__descendent_for_range(TSNode self, size_t min,
size_t max, TSNodeType type) {
size_t max,
bool include_anonymous) {
const TSTree *tree = ts_node__tree(self), *last_visible_tree = tree;
TSLength position = ts_node__offset(self), last_visible_position = position;
TSLength offset = ts_node__offset(self), last_visible_position = offset;
size_t offset_row = self.row, last_visible_row = offset_row;
bool did_descend = true;
@ -136,19 +141,19 @@ static inline TSNode ts_node__descendent_for_range(TSNode self, size_t min,
for (size_t i = 0; i < tree->child_count; i++) {
const TSTree *child = tree->children[i];
if (position.chars + child->padding.chars > min)
if (offset.chars + child->padding.chars > min)
break;
if (position.chars + child->padding.chars + child->size.chars > max) {
if (offset.chars + child->padding.chars + child->size.chars > max) {
tree = child;
if (child->options.type >= type) {
if (ts_tree__is_relevant(child, include_anonymous)) {
last_visible_tree = tree;
last_visible_position = position;
last_visible_position = offset;
last_visible_row = offset_row;
}
did_descend = true;
break;
}
position = ts_length_add(position, ts_tree_total_size(child));
offset = ts_length_add(offset, ts_tree_total_size(child));
offset_row += child->padding_point.row + child->size_point.row;
}
}
@ -201,7 +206,7 @@ bool ts_node_eq(TSNode self, TSNode other) {
}
bool ts_node_is_named(TSNode self) {
return ts_node__tree(self)->options.type == TSNodeTypeNamed;
return ts_node__tree(self)->options.named;
}
bool ts_node_has_changes(TSNode self) {
@ -210,11 +215,11 @@ bool ts_node_has_changes(TSNode self) {
TSNode ts_node_parent(TSNode self) {
const TSTree *tree = ts_node__tree(self);
TSLength position = ts_node__offset(self);
TSLength offset = ts_node__offset(self);
size_t offset_row = self.row;
do {
position = ts_length_sub(position, tree->context.offset);
offset = ts_length_sub(offset, tree->context.offset);
offset_row -= tree->context.offset_point.row;
tree = tree->context.parent;
@ -222,15 +227,15 @@ TSNode ts_node_parent(TSNode self) {
return ts_node__null();
} while (!ts_tree_is_visible(tree));
return ts_node_make(tree, position, offset_row);
return ts_node_make(tree, offset, offset_row);
}
TSNode ts_node_child(TSNode self, size_t child_index) {
return ts_node__child(self, child_index, TSNodeTypeAnonymous);
return ts_node__child(self, child_index, true);
}
TSNode ts_node_named_child(TSNode self, size_t child_index) {
return ts_node__child(self, child_index, TSNodeTypeNamed);
return ts_node__child(self, child_index, false);
}
size_t ts_node_child_count(TSNode self) {
@ -242,25 +247,25 @@ size_t ts_node_named_child_count(TSNode self) {
}
TSNode ts_node_next_sibling(TSNode self) {
return ts_node__next_sibling(self, TSNodeTypeAnonymous);
return ts_node__next_sibling(self, true);
}
TSNode ts_node_next_named_sibling(TSNode self) {
return ts_node__next_sibling(self, TSNodeTypeNamed);
return ts_node__next_sibling(self, false);
}
TSNode ts_node_prev_sibling(TSNode self) {
return ts_node__prev_sibling(self, TSNodeTypeAnonymous);
return ts_node__prev_sibling(self, true);
}
TSNode ts_node_prev_named_sibling(TSNode self) {
return ts_node__prev_sibling(self, TSNodeTypeNamed);
return ts_node__prev_sibling(self, false);
}
TSNode ts_node_descendent_for_range(TSNode self, size_t min, size_t max) {
return ts_node__descendent_for_range(self, min, max, TSNodeTypeAnonymous);
return ts_node__descendent_for_range(self, min, max, true);
}
TSNode ts_node_named_descendent_for_range(TSNode self, size_t min, size_t max) {
return ts_node__descendent_for_range(self, min, max, TSNodeTypeNamed);
return ts_node__descendent_for_range(self, min, max, false);
}