From 2d1ca8bc9f8a4868f220179b30dca1e9ba373620 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 17 Sep 2019 14:52:27 -0700 Subject: [PATCH] Fix match return order fom ts_query_cursor_next_match --- cli/src/tests/query_test.rs | 17 ++++++++++------- lib/binding_rust/lib.rs | 2 +- lib/src/query.c | 18 ++++++++++-------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/cli/src/tests/query_test.rs b/cli/src/tests/query_test.rs index 6e0d7913..9206cc05 100644 --- a/cli/src/tests/query_test.rs +++ b/cli/src/tests/query_test.rs @@ -266,15 +266,15 @@ fn test_query_matches_with_nesting_and_no_fields() { " (array (array - (identifier) @element-1 - (identifier) @element-2)) + (identifier) @x1 + (identifier) @x2)) ", ) .unwrap(); let source = " [[a]]; - [[c, d], [e, f, g]]; + [[c, d], [e, f, g, h]]; [[h], [i]]; "; @@ -287,10 +287,13 @@ fn test_query_matches_with_nesting_and_no_fields() { assert_eq!( collect_matches(matches, &query, source), &[ - (0, vec![("element-1", "c"), ("element-2", "d")]), - (0, vec![("element-1", "e"), ("element-2", "f")]), - (0, vec![("element-1", "f"), ("element-2", "g")]), - (0, vec![("element-1", "e"), ("element-2", "g")]), + (0, vec![("x1", "c"), ("x2", "d")]), + (0, vec![("x1", "e"), ("x2", "f")]), + (0, vec![("x1", "e"), ("x2", "g")]), + (0, vec![("x1", "f"), ("x2", "g")]), + (0, vec![("x1", "e"), ("x2", "h")]), + (0, vec![("x1", "f"), ("x2", "h")]), + (0, vec![("x1", "g"), ("x2", "h")]), ], ); }); diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index 7a21c4ef..2e0cfa0e 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -268,7 +268,7 @@ impl Parser { pub fn set_logger(&mut self, logger: Option) { let prev_logger = unsafe { ffi::ts_parser_logger(self.0) }; if !prev_logger.payload.is_null() { - unsafe { Box::from_raw(prev_logger.payload as *mut Logger) }; + drop(unsafe { Box::from_raw(prev_logger.payload as *mut Logger) }); } let c_logger; diff --git a/lib/src/query.c b/lib/src/query.c index 14e86d6b..f9978be8 100644 --- a/lib/src/query.c +++ b/lib/src/query.c @@ -843,6 +843,8 @@ TSQueryCursor *ts_query_cursor_new() { .start_point = {0, 0}, .end_point = POINT_MAX, }; + array_reserve(&self->states, MAX_STATE_COUNT); + array_reserve(&self->finished_states, MAX_STATE_COUNT); return self; } @@ -897,7 +899,7 @@ void ts_query_cursor_set_point_range( static QueryState *ts_query_cursor_copy_state( TSQueryCursor *self, - QueryState *state + const QueryState *state ) { uint32_t new_list_id = capture_list_pool_acquire(&self->capture_list_pool); if (new_list_id == NONE) return NULL; @@ -1147,14 +1149,13 @@ bool ts_query_cursor_next_match( TSQueryCursor *self, TSQueryMatch *match ) { - if (self->finished_states.size > 0) { - QueryState state = array_pop(&self->finished_states); - capture_list_pool_release(&self->capture_list_pool, state.capture_list_id); + if (self->finished_states.size == 0) { + if (!ts_query_cursor__advance(self)) { + return false; + } } - if (!ts_query_cursor__advance(self)) return false; - - const QueryState *state = array_back(&self->finished_states); + QueryState *state = &self->finished_states.contents[0]; match->id = state->id; match->pattern_index = state->pattern_index; match->capture_count = state->capture_count; @@ -1162,7 +1163,8 @@ bool ts_query_cursor_next_match( &self->capture_list_pool, state->capture_list_id ); - + capture_list_pool_release(&self->capture_list_pool, state->capture_list_id); + array_erase(&self->finished_states, 0); return true; }