Fix bug where patterns with top-level alternatives were not considered 'rooted'

This commit is contained in:
Max Brunsfeld 2022-07-07 17:25:49 -07:00
parent 1401767689
commit 548c12fb88
5 changed files with 169 additions and 22 deletions

View file

@ -2101,7 +2101,7 @@ static TSQueryError ts_query__parse_pattern(
return e;
}
if(start_index == starting_step_index) {
if (start_index == starting_step_index) {
capture_quantifiers_replace(capture_quantifiers, &branch_capture_quantifiers);
} else {
capture_quantifiers_join_all(capture_quantifiers, &branch_capture_quantifiers);
@ -2167,10 +2167,10 @@ static TSQueryError ts_query__parse_pattern(
}
capture_quantifiers_add_all(capture_quantifiers, &child_capture_quantifiers);
child_is_immediate = false;
capture_quantifiers_clear(&child_capture_quantifiers);
child_is_immediate = false;
}
capture_quantifiers_delete(&child_capture_quantifiers);
}
@ -2630,11 +2630,13 @@ TSQuery *ts_query_new(
// Determine whether the pattern has a single root node. This affects
// decisions about whether or not to start matching the pattern when
// a query cursor has a range restriction.
// a query cursor has a range restriction or when immediately within an
// error node.
uint32_t start_depth = step->depth;
bool is_rooted = start_depth == 0;
for (uint32_t step_index = start_step_index + 1; step_index < self->steps.size; step_index++) {
QueryStep *step = &self->steps.contents[step_index];
if (step->is_dead_end) break;
if (step->depth == start_depth) {
is_rooted = false;
break;
@ -2751,6 +2753,19 @@ uint32_t ts_query_start_byte_for_pattern(
return self->patterns.contents[pattern_index].start_byte;
}
bool ts_query_is_pattern_rooted(
const TSQuery *self,
uint32_t pattern_index
) {
for (unsigned i = 0; i < self->pattern_map.size; i++) {
PatternEntry *entry = &self->pattern_map.contents[i];
if (entry->pattern_index == pattern_index) {
if (!entry->is_rooted) return false;
}
}
return true;
}
bool ts_query_is_pattern_guaranteed_at_step(
const TSQuery *self,
uint32_t byte_offset
@ -3324,26 +3339,28 @@ static inline bool ts_query_cursor__advance(
point_gt(ts_node_end_point(parent_node), self->start_point) &&
point_lt(ts_node_start_point(parent_node), self->end_point)
);
bool node_is_error = symbol != ts_builtin_sym_error;
bool node_is_error = symbol == ts_builtin_sym_error;
bool parent_is_error =
!ts_node_is_null(parent_node) &&
ts_node_symbol(parent_node) == ts_builtin_sym_error;
// Add new states for any patterns whose root node is a wildcard.
for (unsigned i = 0; i < self->query->wildcard_root_pattern_count; i++) {
PatternEntry *pattern = &self->query->pattern_map.contents[i];
if (!node_is_error) {
for (unsigned i = 0; i < self->query->wildcard_root_pattern_count; i++) {
PatternEntry *pattern = &self->query->pattern_map.contents[i];
// If this node matches the first step of the pattern, then add a new
// state at the start of this pattern.
QueryStep *step = &self->query->steps.contents[pattern->step_index];
if (
(pattern->is_rooted ?
(node_intersects_range && !node_is_error) :
(parent_intersects_range && !parent_is_error)) &&
(!step->field || field_id == step->field) &&
(!step->supertype_symbol || supertype_count > 0)
) {
ts_query_cursor__add_state(self, pattern);
// If this node matches the first step of the pattern, then add a new
// state at the start of this pattern.
QueryStep *step = &self->query->steps.contents[pattern->step_index];
if (
(pattern->is_rooted ?
node_intersects_range :
(parent_intersects_range && !parent_is_error)) &&
(!step->field || field_id == step->field) &&
(!step->supertype_symbol || supertype_count > 0)
) {
ts_query_cursor__add_state(self, pattern);
}
}
}
@ -3357,7 +3374,9 @@ static inline bool ts_query_cursor__advance(
// If this node matches the first step of the pattern, then add a new
// state at the start of this pattern.
if (
(pattern->is_rooted ? node_intersects_range : (parent_intersects_range && !parent_is_error)) &&
(pattern->is_rooted ?
node_intersects_range :
(parent_intersects_range && !parent_is_error)) &&
(!step->field || field_id == step->field)
) {
ts_query_cursor__add_state(self, pattern);