grit_pattern_matcher/pattern/
predicate_definition.rs

1use super::{
2    functions::Evaluator, patterns::Pattern, predicates::Predicate, variable::Variable, State,
3};
4use crate::context::QueryContext;
5use grit_util::{error::GritResult, AnalysisLogs};
6
7#[derive(Clone, Debug)]
8pub struct PredicateDefinition<Q: QueryContext> {
9    pub name: String,
10    pub scope: usize,
11    pub params: Vec<(String, Variable)>,
12    // this could just be a usize representing the len
13    pub local_vars: Vec<usize>,
14    pub predicate: Predicate<Q>,
15}
16
17impl<Q: QueryContext> PredicateDefinition<Q> {
18    pub fn new(
19        name: String,
20        scope: usize,
21        params: Vec<(String, Variable)>,
22        local_vars: Vec<usize>,
23        predicate: Predicate<Q>,
24    ) -> Self {
25        Self {
26            name,
27            scope,
28            params,
29            local_vars,
30            predicate,
31        }
32    }
33
34    pub fn call<'a>(
35        &'a self,
36        state: &mut State<'a, Q>,
37        context: &'a Q::ExecContext<'a>,
38        args: &'a [Option<Pattern<Q>>],
39        logs: &mut AnalysisLogs,
40    ) -> GritResult<bool> {
41        let tracker = state.enter_scope(self.scope, args);
42        let res = self.predicate.execute_func(state, context, logs)?;
43        state.exit_scope(tracker);
44        Ok(res.predicator)
45    }
46}