oak_dot/ast/
mod.rs

1// 使用标准库的 Vec,不需要 alloc
2use core::range::Range;
3
4/// DOT 文件的根节点
5#[derive(Debug, Clone, PartialEq)]
6pub struct DotRoot {
7    pub graphs: Vec<Graph>,
8}
9
10/// 图定义
11#[derive(Debug, Clone, PartialEq)]
12pub struct Graph {
13    pub strict: bool,
14    pub graph_type: GraphType,
15    pub id: Option<String>,
16    pub statements: Vec<Statement>,
17    pub span: Range<usize>,
18}
19
20/// 图类型
21#[derive(Debug, Clone, PartialEq)]
22pub enum GraphType {
23    Graph,
24    Digraph,
25}
26
27/// 语句
28#[derive(Debug, Clone, PartialEq)]
29pub enum Statement {
30    Node(NodeStatement),
31    Edge(EdgeStatement),
32    Attribute(AttributeStatement),
33    Subgraph(SubgraphStatement),
34    Assignment(AssignmentStatement),
35}
36
37/// 节点语句
38#[derive(Debug, Clone, PartialEq)]
39pub struct NodeStatement {
40    pub node_id: NodeId,
41    pub attributes: Vec<Attribute>,
42    pub span: Range<usize>,
43}
44
45/// 边语句
46#[derive(Debug, Clone, PartialEq)]
47pub struct EdgeStatement {
48    pub left: EdgeOperand,
49    pub edges: Vec<(EdgeOp, EdgeOperand)>,
50    pub attributes: Vec<Attribute>,
51    pub span: Range<usize>,
52}
53
54/// 边操作数
55#[derive(Debug, Clone, PartialEq)]
56pub enum EdgeOperand {
57    Node(NodeId),
58    Subgraph(SubgraphStatement),
59}
60
61/// 边操作符
62#[derive(Debug, Clone, PartialEq)]
63pub enum EdgeOp {
64    Directed,   // ->
65    Undirected, // --
66}
67
68/// 属性语句
69#[derive(Debug, Clone, PartialEq)]
70pub struct AttributeStatement {
71    pub target: AttributeTarget,
72    pub attributes: Vec<Attribute>,
73    pub span: Range<usize>,
74}
75
76/// 属性目标
77#[derive(Debug, Clone, PartialEq)]
78pub enum AttributeTarget {
79    Graph,
80    Node,
81    Edge,
82}
83
84/// 子图语句
85#[derive(Debug, Clone, PartialEq)]
86pub struct SubgraphStatement {
87    pub id: Option<String>,
88    pub statements: Vec<Statement>,
89    pub span: Range<usize>,
90}
91
92/// 赋值语句
93#[derive(Debug, Clone, PartialEq)]
94pub struct AssignmentStatement {
95    pub id: String,
96    pub value: String,
97    pub span: Range<usize>,
98}
99
100/// 节点 ID
101#[derive(Debug, Clone, PartialEq)]
102pub struct NodeId {
103    pub id: String,
104    pub port: Option<Port>,
105    pub span: Range<usize>,
106}
107
108/// 端口
109#[derive(Debug, Clone, PartialEq)]
110pub struct Port {
111    pub id: Option<String>,
112    pub compass: Option<Compass>,
113    pub span: Range<usize>,
114}
115
116/// 方位
117#[derive(Debug, Clone, PartialEq)]
118pub enum Compass {
119    N,
120    NE,
121    E,
122    SE,
123    S,
124    SW,
125    W,
126    NW,
127    C,
128}
129
130/// 属性
131#[derive(Debug, Clone, PartialEq)]
132pub struct Attribute {
133    pub name: String,
134    pub value: Option<String>,
135    pub span: Range<usize>,
136}