grit_pattern_matcher/
context.rsuse crate::{
binding::Binding,
file_owners::FileOwners,
pattern::{
AstLeafNodePattern, AstNodePattern, CallBuiltIn, CallbackPattern, CodeSnippet, File,
GritFunctionDefinition, Pattern, PatternDefinition, PredicateDefinition, ResolvedPattern,
State,
},
};
use grit_util::{error::GritResult, AnalysisLogs, Ast, AstNode, Language};
pub trait QueryContext: Clone + std::fmt::Debug + Sized + 'static {
type Node<'a>: AstNode + Clone;
type NodePattern: AstNodePattern<Self>;
type LeafNodePattern: AstLeafNodePattern<Self>;
type ExecContext<'a>: ExecContext<'a, Self>;
type Binding<'a>: Binding<'a, Self>;
type CodeSnippet: CodeSnippet<Self>;
type ResolvedPattern<'a>: ResolvedPattern<'a, Self>;
type Language<'a>: Language<Node<'a> = Self::Node<'a>>;
type File<'a>: File<'a, Self>;
type Tree<'a>: Ast<Node<'a> = Self::Node<'a>> + Clone;
}
pub trait ExecContext<'a, Q: QueryContext> {
fn pattern_definitions(&self) -> &[PatternDefinition<Q>];
fn predicate_definitions(&self) -> &[PredicateDefinition<Q>];
fn function_definitions(&self) -> &[GritFunctionDefinition<Q>];
fn ignore_limit_pattern(&self) -> bool;
fn call_built_in(
&self,
call: &'a CallBuiltIn<Q>,
context: &'a Self,
state: &mut State<'a, Q>,
logs: &mut AnalysisLogs,
) -> GritResult<Q::ResolvedPattern<'a>>;
fn call_callback<'b>(
&self,
call: &'a CallbackPattern,
context: &'a Self,
binding: &'b Q::ResolvedPattern<'a>,
state: &mut State<'a, Q>,
logs: &mut AnalysisLogs,
) -> GritResult<bool>;
fn load_file(
&self,
file: &Q::File<'a>,
state: &mut State<'a, Q>,
logs: &mut AnalysisLogs,
) -> GritResult<bool>;
fn files(&self) -> &FileOwners<Q::Tree<'a>>;
fn language(&self) -> &Q::Language<'a>;
fn exec_step(
&'a self,
step: &'a Pattern<Q>,
binding: &Q::ResolvedPattern<'a>,
state: &mut State<'a, Q>,
logs: &mut AnalysisLogs,
) -> GritResult<bool>;
fn name(&self) -> Option<&str>;
}
#[derive(Debug)]
pub struct StaticDefinitions<'a, Q: QueryContext> {
pattern_definitions: &'a [PatternDefinition<Q>],
predicate_definitions: &'a [PredicateDefinition<Q>],
function_definitions: &'a [GritFunctionDefinition<Q>],
pub skippable_indexes: Vec<usize>,
}
impl<'a, Q: QueryContext> StaticDefinitions<'a, Q> {
pub fn new(
pattern_definitions: &'a [PatternDefinition<Q>],
predicate_definitions: &'a [PredicateDefinition<Q>],
function_definitions: &'a [GritFunctionDefinition<Q>],
) -> Self {
StaticDefinitions {
pattern_definitions,
predicate_definitions,
function_definitions,
skippable_indexes: vec![],
}
}
pub fn get_pattern(&self, index: usize) -> Option<&PatternDefinition<Q>> {
if self.skippable_indexes.contains(&index) {
return None;
}
self.pattern_definitions.get(index)
}
pub fn get_predicate(&self, index: usize) -> Option<&PredicateDefinition<Q>> {
self.predicate_definitions.get(index)
}
pub fn get_function(&self, index: usize) -> Option<&GritFunctionDefinition<Q>> {
self.function_definitions.get(index)
}
}
impl<'a, Q: QueryContext> Default for StaticDefinitions<'a, Q> {
fn default() -> Self {
Self {
pattern_definitions: &[],
predicate_definitions: &[],
function_definitions: &[],
skippable_indexes: vec![],
}
}
}