From beb5eec7d9c96c19d5902fb805fd41ca289b4a99 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 11 Sep 2019 14:44:49 -0700 Subject: [PATCH] Fix handling of single-node patterns in tree queries --- cli/src/tests/query_test.rs | 31 +++++++++++++++++++++++++++++++ lib/src/query.c | 3 ++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/cli/src/tests/query_test.rs b/cli/src/tests/query_test.rs index 6ab77d1f..927df294 100644 --- a/cli/src/tests/query_test.rs +++ b/cli/src/tests/query_test.rs @@ -311,6 +311,37 @@ fn test_query_exec_too_many_match_permutations_to_track() { }); } +#[test] +fn test_query_exec_with_anonymous_tokens() { + allocations::record(|| { + let language = get_language("javascript"); + let query = Query::new( + language, + r#" + ";" @ punctuation + "&&" @ operator + "#, + ) + .unwrap(); + + let source = "foo(a && b);"; + + let mut parser = Parser::new(); + parser.set_language(language).unwrap(); + let tree = parser.parse(&source, None).unwrap(); + let context = query.context(); + let matches = context.exec(tree.root_node()); + + assert_eq!( + collect_matches(matches, &query, source), + &[ + (1, vec![("operator", "&&")]), + (0, vec![("punctuation", ";")]), + ] + ); + }); +} + #[test] fn test_query_capture_names() { allocations::record(|| { diff --git a/lib/src/query.c b/lib/src/query.c index d728238d..042b5d9e 100644 --- a/lib/src/query.c +++ b/lib/src/query.c @@ -413,6 +413,7 @@ static TSQueryError ts_query_parse_pattern( .depth = depth, .symbol = symbol, .field = 0, + .capture_id = NONE, })); if (stream->next != '"') return TSQueryErrorSyntax; @@ -756,7 +757,7 @@ bool ts_query_context_next(TSQueryContext *self) { array_push(&self->states, ((QueryState) { .pattern_index = slice->pattern_index, - .step_index = slice->step_index + 1, + .step_index = slice->step_index, .start_depth = self->depth, .capture_list_id = capture_list_id, .capture_count = 0,