Terminate failed query matches before descending whenever possible

When iterating over captures, this prevents reasonable queries from 
forcing the tree cursor to buffer matches unnecessarily.
This commit is contained in:
Max Brunsfeld 2019-09-18 11:37:49 -07:00
parent 374a7ac81e
commit 186b08381c
4 changed files with 203 additions and 96 deletions

View file

@ -244,13 +244,74 @@ TSNode ts_tree_cursor_current_node(const TSTreeCursor *_self) {
);
}
static inline TSFieldId ts_tree_cursor__current_field_info(
TSFieldId ts_tree_cursor_current_status(
const TSTreeCursor *_self,
const TSFieldMapEntry **field_map,
const TSFieldMapEntry **field_map_end,
uint32_t *child_index
bool *can_have_later_siblings,
bool *can_have_later_siblings_with_this_field
) {
const TreeCursor *self = (const TreeCursor *)_self;
TSFieldId result = 0;
*can_have_later_siblings = false;
*can_have_later_siblings_with_this_field = false;
// Walk up the tree, visiting the current node and its invisible ancestors,
// because fields can refer to nodes through invisible *wrapper* nodes,
for (unsigned i = self->stack.size - 1; i > 0; i--) {
TreeCursorEntry *entry = &self->stack.contents[i];
TreeCursorEntry *parent_entry = &self->stack.contents[i - 1];
// Stop walking up when a visible ancestor is found.
if (i != self->stack.size - 1) {
if (ts_subtree_visible(*entry->subtree)) break;
const TSSymbol *alias_sequence = ts_language_alias_sequence(
self->tree->language,
parent_entry->subtree->ptr->production_id
);
if (alias_sequence && alias_sequence[entry->structural_child_index]) {
break;
}
}
if (ts_subtree_child_count(*parent_entry->subtree) > entry->child_index) {
*can_have_later_siblings = true;
}
if (ts_subtree_extra(*entry->subtree)) break;
const TSFieldMapEntry *field_map, *field_map_end;
ts_language_field_map(
self->tree->language,
parent_entry->subtree->ptr->production_id,
&field_map, &field_map_end
);
// Look for a field name associated with the current node.
if (!result) {
for (const TSFieldMapEntry *i = field_map; i < field_map_end; i++) {
if (!i->inherited && i->child_index == entry->structural_child_index) {
result = i->field_id;
*can_have_later_siblings_with_this_field = false;
break;
}
}
}
// Determine if there other later siblings with the same field name.
if (result) {
for (const TSFieldMapEntry *i = field_map; i < field_map_end; i++) {
if (i->field_id == result && i->child_index > entry->structural_child_index) {
*can_have_later_siblings_with_this_field = true;
break;
}
}
}
}
return result;
}
TSFieldId ts_tree_cursor_current_field_id(const TSTreeCursor *_self) {
const TreeCursor *self = (const TreeCursor *)_self;
// Walk up the tree, visiting the current node and its invisible ancestors.
for (unsigned i = self->stack.size - 1; i > 0; i--) {
@ -271,14 +332,14 @@ static inline TSFieldId ts_tree_cursor__current_field_info(
if (ts_subtree_extra(*entry->subtree)) break;
const TSFieldMapEntry *field_map, *field_map_end;
ts_language_field_map(
self->tree->language,
parent_entry->subtree->ptr->production_id,
field_map, field_map_end
&field_map, &field_map_end
);
for (const TSFieldMapEntry *i = *field_map; i < *field_map_end; i++) {
for (const TSFieldMapEntry *i = field_map; i < field_map_end; i++) {
if (!i->inherited && i->child_index == entry->structural_child_index) {
*child_index = entry->structural_child_index;
return i->field_id;
}
}
@ -286,44 +347,6 @@ static inline TSFieldId ts_tree_cursor__current_field_info(
return 0;
}
TSFieldId ts_tree_cursor_current_field_id_ext(
const TSTreeCursor *self,
bool *field_has_additional
) {
uint32_t child_index;
const TSFieldMapEntry *field_map, *field_map_end;
TSFieldId field_id = ts_tree_cursor__current_field_info(
self,
&field_map,
&field_map_end,
&child_index
);
// After finding the field, check if any other later children have
// the same field name.
if (field_id) {
for (const TSFieldMapEntry *i = field_map; i < field_map_end; i++) {
if (i->field_id == field_id && i->child_index > child_index) {
*field_has_additional = true;
}
}
}
return field_id;
}
TSFieldId ts_tree_cursor_current_field_id(const TSTreeCursor *self) {
uint32_t child_index;
const TSFieldMapEntry *field_map, *field_map_end;
return ts_tree_cursor__current_field_info(
self,
&field_map,
&field_map_end,
&child_index
);
}
const char *ts_tree_cursor_current_field_name(const TSTreeCursor *_self) {
TSFieldId id = ts_tree_cursor_current_field_id(_self);
if (id) {