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
90
91
92
93
94
95
96
97
98
99
100
use reading::{DisplayJoint, MaybeConcept, MightExpand, Pair};
use std::{fmt, rc::Rc};
#[derive(Clone)]
pub struct SyntaxTree {
syntax: String,
concept: Option<usize>,
expansion: Option<(Rc<SyntaxTree>, Rc<SyntaxTree>)>,
}
impl MaybeConcept for SyntaxTree {
fn get_concept(&self) -> Option<usize> {
self.concept
}
}
impl PartialEq<SyntaxTree> for SyntaxTree {
fn eq(&self, other: &SyntaxTree) -> bool {
self.to_string() == other.to_string()
}
}
impl PartialEq<Rc<SyntaxTree>> for SyntaxTree {
fn eq(&self, other: &Rc<SyntaxTree>) -> bool {
self.to_string() == other.to_string()
}
}
impl MightExpand<SyntaxTree> for SyntaxTree {
fn get_expansion(&self) -> Option<(Rc<SyntaxTree>, Rc<SyntaxTree>)> {
self.expansion.clone()
}
}
impl DisplayJoint for SyntaxTree {
fn display_joint(&self) -> String {
match self.get_expansion() {
Some(_) => "(".to_string() + &self.to_string() + ")",
None => self.to_string(),
}
}
}
impl fmt::Display for SyntaxTree {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.syntax)
}
}
impl Pair<SyntaxTree> for SyntaxTree {
fn from_pair(
syntax: (String, Option<usize>),
lefthand: &Rc<SyntaxTree>,
righthand: &Rc<SyntaxTree>,
) -> SyntaxTree {
SyntaxTree {
syntax: syntax.0,
concept: syntax.1,
expansion: Some((lefthand.clone(), righthand.clone())),
}
}
}
impl From<(String, Option<usize>)> for SyntaxTree {
fn from(syntax: (String, Option<usize>)) -> SyntaxTree {
SyntaxTree {
syntax: syntax.0,
concept: syntax.1,
expansion: None,
}
}
}