Skip to content

Commit

Permalink
Fix query engine OneOrMoreMatcher to skip over in-between trivia (#…
Browse files Browse the repository at this point in the history
…1133)

The `OneOrMoreMatcher` would stop matching whenever it encountered a
trivia item in a sequence. Since the parser may arbitrarily insert
trivia items anywhere, this meant that the matched results would be
incomplete.

This was found with this query from the Solidity bindings rules:

```
@specifier [InheritanceSpecifier [InheritanceTypes . @Parents [_]+ .]]
```

Depending on the formatting of the parsed code, the parser would
generate the children of `InheritanceTypes` with interleaved trivia
items, eg. newlines if the inherited contracts/interfaces are enumerated
in separate lines. Because `[_]+` would stop on the first trivia, the
overall query would never match.
  • Loading branch information
ggiraldez authored Oct 30, 2024
1 parent df12184 commit 52e10f9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
12 changes: 10 additions & 2 deletions crates/metaslang/cst/src/query/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,16 @@ impl<T: KindTypes + 'static> Matcher<T> for OneOrMoreMatcher<T> {
} else {
let tail = self.children.last_mut().unwrap();
if let Some(child_matcher_result) = tail.next() {
if !child_matcher_result.cursor.is_completed() {
self.result_for_next_repetition = Some(child_matcher_result.clone());
// Skip over trivia before saving the result for next repetition
let mut cursor = child_matcher_result.cursor.clone();
while !cursor.is_completed() && cursor.node().is_trivia() {
cursor.irrevocably_go_to_next_sibling();
}
if !cursor.is_completed() {
self.result_for_next_repetition = Some(MatcherResult {
cursor,
..child_matcher_result
});
}
return Some(child_matcher_result);
}
Expand Down
36 changes: 36 additions & 0 deletions crates/testlang/outputs/cargo/tests/src/query/engine_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,42 @@ fn test_one_or_more() {
);
}

#[test]
fn test_one_or_more_anonymous() {
run_query_test(
&common_test_tree(),
"[TreeNode (@x [_])+ .]",
query_matches! {
{x: ["A", "B", "C", "DE"]}
{x: ["B", "C", "DE"]}
{x: ["C", "DE"]}
{x: ["DE"]}
},
);
}

#[test]
fn test_one_or_more_anonymous_both_adjacent() {
run_query_test(
&common_test_tree(),
"[TreeNode . (@x [_])+ .]",
query_matches! {
{x: ["A", "B", "C", "DE"]}
},
);
}

#[test]
fn test_one_or_more_anonymous_both_adjacent_with_trivia() {
run_query_test(
&common_test_tree_with_trivia(),
"[TreeNodeChild . @children [_]+ .]",
query_matches! {
{children: ["D", "E"]}
},
);
}

#[test]
fn test_zero_or_more() {
run_query_test(
Expand Down

0 comments on commit 52e10f9

Please sign in to comment.