query: Add postfix '+' operator for token repetition

Co-Authored-By: Patrick Thomson <patrickt@users.noreply.github.com>
This commit is contained in:
Max Brunsfeld 2020-03-12 15:10:58 -07:00
parent 05c1d44e80
commit 6f636a0357
2 changed files with 201 additions and 71 deletions

View file

@ -464,6 +464,7 @@ fn test_query_matches_with_wildcard_at_the_root() {
);
});
}
#[test]
fn test_query_with_immediate_siblings() {
allocations::record(|| {
@ -515,6 +516,73 @@ fn test_query_with_immediate_siblings() {
});
}
#[test]
fn test_query_matches_with_repeated_nodes() {
allocations::record(|| {
let language = get_language("javascript");
let query = Query::new(
language,
"
(*
(comment)+ @doc
.
(class_declaration
name: (identifier) @name))
(*
(comment)+ @doc
.
(function_declaration
name: (identifier) @name))
",
)
.unwrap();
let source = "
// one
// two
a();
// three
{
// four
// five
// six
class B {}
// seven
c();
// eight
function d() {}
}
";
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![
("doc", "// four"),
("doc", "// five"),
("doc", "// six"),
("name", "B")
]
),
(1, vec![("doc", "// eight"), ("name", "d")]),
]
);
});
}
#[test]
fn test_query_matches_in_language_with_simple_aliases() {
allocations::record(|| {