From fa43ce01a6674e02df0980a5e90fff8f1802328a Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 16 Oct 2019 11:54:32 -0700 Subject: [PATCH] Allow queries to capture ERROR nodes --- cli/src/tests/query_test.rs | 30 ++++++++++++++++++++++++++++++ lib/src/query.c | 1 + 2 files changed, 31 insertions(+) diff --git a/cli/src/tests/query_test.rs b/cli/src/tests/query_test.rs index 1d7554c0..9120ce88 100644 --- a/cli/src/tests/query_test.rs +++ b/cli/src/tests/query_test.rs @@ -365,6 +365,36 @@ fn test_query_matches_with_many() { }); } +#[test] +fn test_query_matches_capturing_error_nodes() { + allocations::record(|| { + let language = get_language("javascript"); + let query = Query::new( + language, + " + (ERROR (identifier) @the-error-identifier) @the-error + ", + ) + .unwrap(); + + let source = "function a(b,, c, d :e:) {}"; + + let mut parser = Parser::new(); + parser.set_language(language).unwrap(); + let tree = parser.parse(source, None).unwrap(); + let mut cursor = QueryCursor::new(); + let matches = cursor.matches(&query, tree.root_node(), to_callback(source)); + + assert_eq!( + collect_matches(matches, &query, source), + &[( + 0, + vec![("the-error", ":e:"), ("the-error-identifier", "e"),] + ),] + ); + }); +} + #[test] fn test_query_matches_in_language_with_simple_aliases() { allocations::record(|| { diff --git a/lib/src/query.c b/lib/src/query.c index c2ba3d30..db966dc1 100644 --- a/lib/src/query.c +++ b/lib/src/query.c @@ -328,6 +328,7 @@ static TSSymbol ts_query_intern_node_name( uint32_t length, TSSymbolType symbol_type ) { + if (!strncmp(name, "ERROR", length)) return ts_builtin_sym_error; uint32_t symbol_count = ts_language_symbol_count(self->language); for (TSSymbol i = 0; i < symbol_count; i++) { if (ts_language_symbol_type(self->language, i) != symbol_type) continue;