Allow lisp-style comments in tree queries

This commit is contained in:
Max Brunsfeld 2019-09-11 12:16:05 -07:00
parent 4fa0b02d67
commit 33f89522f6
2 changed files with 73 additions and 33 deletions

View file

@ -62,38 +62,6 @@ fn test_query_errors_on_invalid_symbols() {
});
}
#[test]
fn test_query_capture_names() {
allocations::record(|| {
let language = get_language("javascript");
let query = Query::new(
language,
r#"
(if_statement
condition: (binary_expression
left: * @left-operand
operator: "||"
right: * @right-operand)
consequence: (statement_block) @body)
(while_statement
condition:* @loop-condition)
"#,
)
.unwrap();
assert_eq!(
query.capture_names(),
&[
"left-operand".to_string(),
"right-operand".to_string(),
"body".to_string(),
"loop-condition".to_string(),
]
);
});
}
#[test]
fn test_query_exec_with_simple_pattern() {
allocations::record(|| {
@ -302,6 +270,67 @@ fn test_query_exec_too_many_match_permutations_to_track() {
});
}
#[test]
fn test_query_capture_names() {
allocations::record(|| {
let language = get_language("javascript");
let query = Query::new(
language,
r#"
(if_statement
condition: (binary_expression
left: * @left-operand
operator: "||"
right: * @right-operand)
consequence: (statement_block) @body)
(while_statement
condition:* @loop-condition)
"#,
)
.unwrap();
assert_eq!(
query.capture_names(),
&[
"left-operand".to_string(),
"right-operand".to_string(),
"body".to_string(),
"loop-condition".to_string(),
]
);
});
}
#[test]
fn test_query_comments() {
allocations::record(|| {
let language = get_language("javascript");
let query = Query::new(
language,
"
; this is my first comment
; i have two comments here
(function_declaration
; there is also a comment here
; and here
name: (identifier) @fn-name)",
)
.unwrap();
let source = "function one() { }";
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),
&[(0, vec![("fn-name", "one")]),],
);
});
}
fn collect_matches<'a>(
matches: impl Iterator<Item = QueryMatch<'a>>,
query: &'a Query,