From 4c17af3ecdb0137b6d025af1552a00ffdea56c55 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 26 Sep 2019 15:58:41 -0700 Subject: [PATCH] Allow queries with no patterns --- cli/src/tests/query_test.rs | 10 ++++++++++ lib/src/query.c | 4 +--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cli/src/tests/query_test.rs b/cli/src/tests/query_test.rs index 65f89bfb..c9907cfa 100644 --- a/cli/src/tests/query_test.rs +++ b/cli/src/tests/query_test.rs @@ -1029,6 +1029,16 @@ fn test_query_capture_names() { }); } +#[test] +fn test_query_with_no_patterns() { + allocations::record(|| { + let language = get_language("javascript"); + let query = Query::new(language, "").unwrap(); + assert!(query.capture_names().is_empty()); + assert_eq!(query.pattern_count(), 0); + }); +} + #[test] fn test_query_comments() { allocations::record(|| { diff --git a/lib/src/query.c b/lib/src/query.c index 84921b40..10a436f4 100644 --- a/lib/src/query.c +++ b/lib/src/query.c @@ -789,7 +789,7 @@ TSQuery *ts_query_new( Stream stream = stream_new(source, source_len); stream_skip_whitespace(&stream); uint32_t start_step_index; - for (;;) { + while (stream.input < stream.end) { start_step_index = self->steps.size; uint32_t capture_count = 0; array_push(&self->start_bytes_by_pattern, stream.input - source); @@ -824,8 +824,6 @@ TSQuery *ts_query_new( if (capture_count > self->max_capture_count) { self->max_capture_count = capture_count; } - - if (stream.input == stream.end) break; } ts_query__finalize_steps(self);