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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use super::errors::SgfParseError;
use super::props::SgfProp;
use super::sgf_node::SgfNode;
pub fn parse(text: &str) -> Result<Vec<SgfNode>, SgfParseError> {
let mut nodes: Vec<SgfNode> = vec![];
let mut text = text.trim();
while !text.is_empty() {
let (node, new_text) = parse_game_tree(text, true)?;
nodes.push(node);
text = new_text.trim();
}
if nodes.is_empty() {
return Err(SgfParseError::ParseError(text.to_string()));
}
Ok(nodes)
}
fn parse_game_tree(mut text: &str, is_root: bool) -> Result<(SgfNode, &str), SgfParseError> {
if !text.starts_with('(') {
return Err(SgfParseError::ParseError(text.to_string()));
}
text = text[1..].trim();
let (node, new_text) = parse_node(text, is_root)?;
text = new_text.trim();
if !text.starts_with(')') {
return Err(SgfParseError::ParseError(text.to_string()));
}
Ok((node, &text[1..]))
}
fn parse_node(mut text: &str, is_root: bool) -> Result<(SgfNode, &str), SgfParseError> {
if !text.starts_with(';') {
return Err(SgfParseError::ParseError(text.to_string()));
}
text = text[1..].trim();
let mut props: Vec<SgfProp> = vec![];
while let Some(c) = text.chars().next() {
if !c.is_ascii_uppercase() {
break;
}
let (prop, new_text) =
parse_property(text).map_err(|_| SgfParseError::ParseError(text.to_string()))?;
text = new_text;
props.push(prop);
}
text = text.trim();
let mut children: Vec<SgfNode> = vec![];
while text.starts_with('(') {
let (node, new_text) = parse_game_tree(text, false)?;
text = new_text.trim();
children.push(node);
}
if text.starts_with(';') {
let (node, new_text) = parse_node(text, false)?;
text = new_text;
children.push(node);
}
let node = SgfNode::new(props, children, is_root)
.map_err(|_| SgfParseError::ParseError(text.to_string()))?;
Ok((node, text))
}
fn parse_property(mut text: &str) -> Result<(SgfProp, &str), SgfParseError> {
let (prop_ident, prop_ident_dropped) = parse_prop_ident(text)?;
text = prop_ident_dropped;
let (prop_values, prop_values_dropped) = parse_prop_values(text)?;
text = prop_values_dropped;
Ok((SgfProp::new(prop_ident, prop_values)?, text))
}
fn parse_prop_ident(mut text: &str) -> Result<(String, &str), SgfParseError> {
let mut prop_ident = vec![];
loop {
match text.chars().next() {
Some('[') => break,
Some(c) if c.is_ascii_uppercase() => {
prop_ident.push(c);
text = &text[1..];
}
_ => return Err(SgfParseError::ParseError(text.to_string())),
}
}
Ok((prop_ident.iter().collect(), text))
}
fn parse_prop_values(text: &str) -> Result<(Vec<String>, &str), SgfParseError> {
let mut prop_values = vec![];
let mut text = text;
loop {
let mut chars = text.chars();
match chars.next() {
Some('[') => {
let (value, new_text) = parse_value(chars.as_str())?;
text = new_text;
prop_values.push(value);
}
Some(c) if c.is_whitespace() => text = chars.as_str(),
_ => break,
}
}
Ok((prop_values, text))
}
fn parse_value(text: &str) -> Result<(String, &str), SgfParseError> {
let mut prop_value = vec![];
let mut chars = text.chars();
let mut escaped = false;
loop {
match chars.next() {
Some(']') if !escaped => break,
Some('\\') if !escaped => escaped = true,
Some(c) => {
escaped = false;
prop_value.push(c);
}
None => return Err(SgfParseError::ParseError(text.to_string())),
}
}
Ok((prop_value.iter().collect(), chars.as_str()))
}
#[cfg(test)]
mod test {
use super::*;
fn load_test_sgf() -> Result<Vec<SgfNode>, Box<dyn std::error::Error>> {
let mut sgf_path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
sgf_path.push("resources/test/ff4_ex.sgf");
let data = std::fs::read_to_string(sgf_path)?;
Ok(parse(&data)?)
}
fn node_depth(mut sgf_node: &SgfNode) -> u64 {
let mut depth = 1;
while sgf_node.children().count() > 0 {
depth += 1;
sgf_node = sgf_node.children().next().unwrap();
}
depth
}
#[test]
pub fn sgf_has_two_gametrees() {
let sgf_nodes = load_test_sgf().unwrap();
assert_eq!(sgf_nodes.len(), 2);
}
#[test]
pub fn gametree_one_has_five_variations() {
let sgf_nodes = load_test_sgf().unwrap();
assert_eq!(sgf_nodes[0].children().count(), 5);
}
#[test]
pub fn gametree_one_has_size_19() {
let sgf_nodes = load_test_sgf().unwrap();
match sgf_nodes[0].get_property("SZ") {
Some(SgfProp::SZ(size)) => assert_eq!(size, &(19, 19)),
_ => unreachable!("Expected size property"),
}
}
#[test]
pub fn gametree_variation_depths() {
let sgf_nodes = load_test_sgf().unwrap();
let children: Vec<_> = sgf_nodes[0].children().collect();
assert_eq!(node_depth(children[0]), 13);
assert_eq!(node_depth(children[1]), 4);
assert_eq!(node_depth(children[2]), 4);
}
#[test]
pub fn gametree_two_has_one_variation() {
let sgf_nodes = load_test_sgf().unwrap();
assert_eq!(sgf_nodes[1].children().count(), 1);
}
}