Skip to content

Commit

Permalink
Add until modifier to within pattern
Browse files Browse the repository at this point in the history
This update introduces an `until` modifier to the `within` pattern, similar to the `contains ... until` functionality. The `until` modifier allows for more flexible pattern matching by specifying an endpoint condition.

Changes include:
- Updated `WithinCompiler` to handle the `until` modifier.
- Modified `Within` struct to include an optional `until` pattern.
- Adjusted the `Matcher` implementation for `Within` to break the matching process when the `until` condition is met.
  • Loading branch information
mentatbot[bot] committed Aug 3, 2024
1 parent f79bc30 commit 42292ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
8 changes: 6 additions & 2 deletions crates/core/src/pattern_compiler/within_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ impl NodeCompiler for WithinCompiler {
.child_by_field_name("pattern")
.ok_or_else(|| anyhow!("missing pattern of pattern within"))?;
let within = PatternCompiler::from_node(&within, context)?;
Ok(Within::new(within))
let until = node
.child_by_field_name("until")
.map(|n| PatternCompiler::from_node(&n, context))
.transpose()?;
Ok(Within::new(within, until))
}
}
}
18 changes: 15 additions & 3 deletions crates/grit-pattern-matcher/src/pattern/within.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ use grit_util::{AnalysisLogs, AstNode};
#[derive(Debug, Clone)]
pub struct Within<Q: QueryContext> {
pub pattern: Pattern<Q>,
pub until: Option<Pattern<Q>>,
}

impl<Q: QueryContext> Within<Q> {
pub fn new(pattern: Pattern<Q>) -> Self {
Self { pattern }
pub fn new(pattern: Pattern<Q>, until: Option<Pattern<Q>>) -> Self {
Self { pattern, until }
}
}

Expand Down Expand Up @@ -61,6 +62,17 @@ impl<Q: QueryContext> Matcher<Q> for Within<Q> {
} else {
cur_state = state;
}

if let Some(until) = &self.until {
if until.execute(
&ResolvedPattern::from_node_binding(n),

Check failure on line 68 in crates/grit-pattern-matcher/src/pattern/within.rs

View workflow job for this annotation

GitHub Actions / clippy

use of moved value: `n`

error[E0382]: use of moved value: `n` --> crates/grit-pattern-matcher/src/pattern/within.rs:68:57 | 53 | for n in node.ancestors() { | - move occurs because `n` has type `<Q as context::QueryContext>::Node<'_>`, which does not implement the `Copy` trait ... 56 | &ResolvedPattern::from_node_binding(n), | - value moved here ... 68 | &ResolvedPattern::from_node_binding(n), | ^ value used here after move | note: consider changing this parameter type in method `from_node_binding` to borrow instead if owning the value isn't necessary --> crates/grit-pattern-matcher/src/pattern/resolved_pattern.rs:35:32 | 35 | fn from_node_binding(node: Q::Node<'a>) -> Self { | ----------------- ^^^^^^^^^^^ this parameter takes ownership of the value | | | in this method help: consider cloning the value if the performance cost is acceptable | 56 | &ResolvedPattern::from_node_binding(n.clone()), | ++++++++
&mut cur_state,
context,
logs,
)? {
break;
}
}
}
if did_match {
*init_state = cur_state;
Expand All @@ -69,4 +81,4 @@ impl<Q: QueryContext> Matcher<Q> for Within<Q> {
Ok(false)
}
}
}
}

0 comments on commit 42292ba

Please sign in to comment.