-
Notifications
You must be signed in to change notification settings - Fork 499
/
tree.rs
137 lines (114 loc) · 3.14 KB
/
tree.rs
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use {super::*, std::borrow::Cow};
/// Construct a `Tree` from a symbolic expression literal. This macro, and the
/// Tree type, are only used in the Parser unit tests, providing a concise
/// notation for representing the expected results of parsing a given string.
macro_rules! tree {
{ ($($child:tt)*) } => {
$crate::tree::Tree::List(vec![$(tree!($child),)*])
};
{ $atom:ident } => {
$crate::tree::Tree::atom(stringify!($atom))
};
{ $atom:literal } => {
$crate::tree::Tree::atom(format!("\"{}\"", $atom))
};
{ # } => {
$crate::tree::Tree::atom("#")
};
{ ? } => {
$crate::tree::Tree::atom("?")
};
{ + } => {
$crate::tree::Tree::atom("+")
};
{ * } => {
$crate::tree::Tree::atom("*")
};
{ && } => {
$crate::tree::Tree::atom("&&")
};
{ == } => {
$crate::tree::Tree::atom("==")
};
{ != } => {
$crate::tree::Tree::atom("!=")
};
}
/// A `Tree` is either…
#[derive(Debug, PartialEq)]
pub(crate) enum Tree<'text> {
/// …an atom containing text, or…
Atom(Cow<'text, str>),
/// …a list containing zero or more `Tree`s.
List(Vec<Self>),
}
impl<'text> Tree<'text> {
/// Construct an Atom from a text scalar
pub(crate) fn atom(text: impl Into<Cow<'text, str>>) -> Self {
Self::Atom(text.into())
}
/// Construct a List from an iterable of trees
pub(crate) fn list(children: impl IntoIterator<Item = Self>) -> Self {
Self::List(children.into_iter().collect())
}
/// Convenience function to create an atom containing quoted text
pub(crate) fn string(contents: impl AsRef<str>) -> Self {
Self::atom(format!("\"{}\"", contents.as_ref()))
}
/// Push a child node into self, turning it into a List if it was an Atom
pub(crate) fn push(self, tree: impl Into<Self>) -> Self {
match self {
Self::List(mut children) => {
children.push(tree.into());
Self::List(children)
}
Self::Atom(text) => Self::List(vec![Self::Atom(text), tree.into()]),
}
}
/// Extend a self with a tail of Trees, turning self into a List if it was an
/// Atom
pub(crate) fn extend<I, T>(self, tail: I) -> Self
where
I: IntoIterator<Item = T>,
T: Into<Self>,
{
// Tree::List(children.into_iter().collect())
let mut head = match self {
Self::List(children) => children,
Self::Atom(text) => vec![Self::Atom(text)],
};
for child in tail {
head.push(child.into());
}
Self::List(head)
}
/// Like `push`, but modify self in-place
pub(crate) fn push_mut(&mut self, tree: impl Into<Self>) {
*self = mem::replace(self, Self::List(Vec::new())).push(tree.into());
}
}
impl Display for Tree<'_> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::List(children) => {
write!(f, "(")?;
for (i, child) in children.iter().enumerate() {
if i > 0 {
write!(f, " ")?;
}
write!(f, "{child}")?;
}
write!(f, ")")
}
Self::Atom(text) => write!(f, "{text}"),
}
}
}
impl<'text, T> From<T> for Tree<'text>
where
T: Into<Cow<'text, str>>,
{
fn from(text: T) -> Self {
Self::Atom(text.into())
}
}