pattern_compiler/pattern.rs
1use std::hash::Hash;
2use std::fmt::Debug;
3
4/// Length of `nodes` should ALWAYS be `variables.len() * clauses`
5#[derive(Debug)]
6pub struct ExpandedClauseNodes<V, K> {
7 pub variables: Vec<V>,
8 pub clauses: usize,
9 pub nodes: Vec<K>,
10}
11
12pub trait PatternProvider: Debug {
13
14 /// A reference to a unique node in the pattern graph.
15 /// Every `PatternNodeKey` should belong to ONE and ONLY one
16 /// `PatternNodeKind`.
17 type PatternNodeKey: Copy + Hash + Debug + PartialEq + Eq;
18
19 /// The type of pattern node.
20 type PatternNodeKind: Copy + Hash + Debug + PartialEq + Eq;
21
22 /// A variable in the output CFG.
23 /// The provider is responsible for creating these as specializations
24 /// are performed by `expand_clause_nodes`.
25 type CfgVariable: Copy + Hash + Debug + PartialEq + Eq;
26
27 const WILDCARD: Self::PatternNodeKind;
28
29 fn get_root(&self)
30 -> ExpandedClauseNodes<
31 Self::CfgVariable, Self::PatternNodeKey>;
32
33 /// Used to determine if the given `key` should be included in the
34 /// specialization on `kind`.
35 ///
36 /// When passed a wildcard as kind, we are specializing on the default
37 /// matrix. It should match wildcards ONLY.
38 ///
39 /// ## Invariants
40 /// * Every `PatternNodeKey` should match on one and ONLY one
41 /// `PatternNodeKind`.
42 fn kind_includes(&self, kind: Self::PatternNodeKind,
43 key: Self::PatternNodeKey) -> bool;
44
45 /// After clauses have been selected for specialization, this will
46 /// be called with the set of all nodes that should be specialized on.
47 fn expand_clause_nodes(&mut self, clause_nodes: Vec<Self::PatternNodeKey>)
48 -> ExpandedClauseNodes<
49 Self::CfgVariable, Self::PatternNodeKey>;
50
51 /// Every `PatternNodeKey` should belong to one and only one
52 /// `PatternNodeKind`.
53 fn get_kind(&self, key: Self::PatternNodeKey) -> Self::PatternNodeKind;
54
55 fn is_wildcard(&self, kind: Self::PatternNodeKind) -> bool {
56 kind == Self::WILDCARD
57 }
58 fn get_wildcard(&self) -> Self::PatternNodeKind {
59 Self::WILDCARD
60 }
61
62}