1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use ::std::collections::HashMap;

use ::petgraph::Graph;
use ::petgraph::graph::NodeIndex;

mod generate_dot;

use super::pattern::PatternProvider;

pub type CfgNodeIndex = NodeIndex;

//#[derive(Debug, Clone)]
//pub struct Leaf<P> where P: PatternProvider {
//    bindings: HashMap<P::CfgVariable, P::PatternNodeKey>,
//}

#[derive(Debug, Clone)]
pub struct PatternCfg<P> where P: PatternProvider {
    pub entry: CfgNodeIndex,
    pub graph: Graph<CfgNodeKind<P::CfgVariable>, CfgEdge<P>>,
    pub leaves: HashMap<usize, NodeIndex>,
    pub leaf_bindings: HashMap<NodeIndex, HashMap<P::CfgVariable, P::PatternNodeKey>>,
}

impl<P> PatternCfg<P> where P: PatternProvider {

    pub fn new() -> Self {
        let mut graph = Graph::new();
        PatternCfg {
            entry: graph.add_node(CfgNodeKind::Root),
            graph: graph,
            leaves: HashMap::new(),
            leaf_bindings: HashMap::new(),
        }
    }

    pub fn add_fail(&mut self) -> CfgNodeIndex {
        self.graph.add_node(CfgNodeKind::Fail)
    }

    pub fn add_leaf(&mut self, num: usize) -> CfgNodeIndex {
        assert!(!self.leaves.contains_key(&num));
        let index = self.graph.add_node(CfgNodeKind::Leaf(num));
        self.leaves.insert(num, index);
        index
    }

    pub fn get_entry(&self) -> CfgNodeIndex {
        self.entry
    }

    pub fn add_node(&mut self, var: P::CfgVariable) -> CfgNodeIndex {
        self.graph.add_node(CfgNodeKind::Match(var))
    }

    pub fn add_edge(&mut self, parent: CfgNodeIndex, child: CfgNodeIndex,
                    edge: CfgEdge<P>) {
        self.graph.add_edge(parent, child, edge);
    }

    pub fn add_child(&mut self, parent: CfgNodeIndex, typ: CfgEdge<P>,
                     var: P::CfgVariable) -> CfgNodeIndex {
        let child = self.graph.add_node(CfgNodeKind::Match(var));
        self.graph.add_edge(parent, child, typ);
        child
    }

}

#[derive(Clone)]
pub struct CfgEdge<P> where P: PatternProvider {
    //_provider: ::std::marker::PhantomData<P>,
    pub kind: P::PatternNodeKind,
    pub variable_binds: Vec<P::CfgVariable>,
    //pub pattern_node: super::pattern::PatternNodeIndex,
}
impl<P> ::std::fmt::Debug for CfgEdge<P> where P: PatternProvider {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        write!(f, "{:?} {:?}", self.kind, self.variable_binds)
    }
}

#[derive(Debug, Clone)]
pub enum CfgNodeKind<CVT> {
    Root,
    Match(CVT),
    Fail,
    Leaf(usize),
}