layout/gv/parser/
ast.rs

1//! An AST that represents the GraphViz file format.
2
3// "first : <f0>"
4#[derive(Debug, Clone)]
5pub struct NodeId {
6    pub name: String,
7    pub port: Option<String>,
8}
9impl NodeId {
10    pub fn new(name: &str, port: &Option<String>) -> Self {
11        Self {
12            name: name.to_string(),
13            port: port.clone(),
14        }
15    }
16}
17
18// [a=b; c=d; ... ]
19#[derive(Debug, Clone)]
20pub struct AttributeList {
21    pub list: Vec<(String, String)>,
22}
23
24impl AttributeList {
25    pub fn new() -> Self {
26        Self { list: Vec::new() }
27    }
28    pub fn add_attr(&mut self, from: &str, to: &str) {
29        self.list.push((from.to_string(), to.to_string()));
30    }
31
32    pub fn iter(&self) -> std::slice::Iter<(String, String)> {
33        self.list.iter()
34    }
35}
36
37impl Default for AttributeList {
38    fn default() -> Self {
39        Self::new()
40    }
41}
42
43// (graph | node | edge)
44#[derive(Debug, Clone)]
45pub enum AttrStmtTarget {
46    Graph,
47    Node,
48    Edge,
49}
50// (graph | node | edge) [ ... ]
51#[derive(Debug, Clone)]
52pub struct AttrStmt {
53    pub target: AttrStmtTarget,
54    pub list: AttributeList,
55}
56
57impl AttrStmt {
58    pub fn new(target: AttrStmtTarget, list: AttributeList) -> Self {
59        Self { target, list }
60    }
61}
62
63// node-name [ ... ]
64#[derive(Debug, Clone)]
65pub struct NodeStmt {
66    pub id: NodeId,
67    pub list: AttributeList,
68}
69
70impl NodeStmt {
71    pub fn new(id: NodeId) -> Self {
72        Self {
73            id,
74            list: AttributeList::new(),
75        }
76    }
77    pub fn new_with_list(id: NodeId, list: AttributeList) -> Self {
78        Self { id, list }
79    }
80}
81
82// (-> | -- )
83#[derive(Debug, Clone)]
84pub enum ArrowKind {
85    Arrow,
86    Line,
87}
88
89// a -> b -> c [...]
90#[derive(Debug, Clone)]
91pub struct EdgeStmt {
92    pub from: NodeId,
93    pub to: Vec<(NodeId, ArrowKind)>,
94    pub list: AttributeList,
95}
96
97impl EdgeStmt {
98    pub fn new(from: NodeId) -> Self {
99        Self {
100            from,
101            to: Vec::new(),
102            list: AttributeList::new(),
103        }
104    }
105
106    pub fn insert(&mut self, n: NodeId, ak: ArrowKind) {
107        self.to.push((n, ak));
108    }
109}
110
111#[derive(Debug, Clone)]
112pub enum Stmt {
113    Edge(EdgeStmt),
114    Node(NodeStmt),
115    Attribute(AttrStmt),
116    SubGraph(Graph),
117}
118
119// { ... }
120#[derive(Debug, Clone)]
121pub struct StmtList {
122    pub list: Vec<Stmt>,
123}
124
125impl StmtList {
126    pub fn new() -> Self {
127        Self { list: Vec::new() }
128    }
129}
130
131impl Default for StmtList {
132    fn default() -> Self {
133        Self::new()
134    }
135}
136
137#[derive(Debug, Clone)]
138pub struct Graph {
139    pub name: String,
140    pub list: StmtList,
141}
142
143impl Graph {
144    pub fn new(name: &str) -> Self {
145        Self {
146            name: name.to_string(),
147            list: StmtList::new(),
148        }
149    }
150}