-
Notifications
You must be signed in to change notification settings - Fork 0
/
sgfparsing.py
163 lines (114 loc) · 4.26 KB
/
sgfparsing.py
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
from functools import partial
import string
class InvalidSgfException(Exception):
pass
class CantProcessSgfException(Exception):
pass
class Node:
"""represent a node in the game tree"""
def __init__(self):
self.properties = []
self.children = []
def tokenize(file):
"""generator which output tokens consumed by tree
in a well formed sgf should be one of the following
("special","(")
("special",")")
("special",";")
("property_name","XX")
("property_value","xx")
"""
try:
f = iter(partial(file.read, 1), "")
last_char = " "
uppers = set(string.ascii_uppercase)
while True:
# Skip space characters
while last_char.isspace():
last_char = next(f)
# Return property name
if last_char in uppers:
property_name = []
while last_char in uppers:
property_name.append(last_char)
last_char = next(f)
yield "property_name", "".join(property_name)
# Return property value
elif last_char == "[":
try:
last_char = next(f)
property_value = []
while last_char != "]":
# Skip first "]" for comment property value
if last_char == "\\":
property_value.append(next(f))
else:
property_value.append(last_char)
last_char = next(f)
yield "property_value", "".join(property_value)
except StopIteration:
raise InvalidSgfException("unclosed property value")
last_char = next(f)
# Return special token
else:
yield "special", last_char
last_char = next(f)
except StopIteration:
return
def tree(tokens):
"""generate a tree of game node, input is the output of tokenize"""
# To remove the debug print
def log(*args):
pass
# log = print
current_node = Node()
tree_node = current_node
stack = []
try:
token = next(tokens)
while True:
# log("stack", [to_sgf(n) for n in stack])
log("processing1", token)
if token == ("special", "("):
log("found (")
stack.append(current_node)
current_node.children.append(Node())
current_node = current_node.children[-1]
token = next(tokens)
if token != ("special", ";"):
raise InvalidSgfException("semi-colon expected")
token = next(tokens)
elif token == ("special", ")"):
log("found )")
if len(stack) < 1:
raise InvalidSgfException("unexpected right parenthesis")
current_node = stack.pop()
token = next(tokens)
elif token == ("special", ";"):
log("yielding node")
current_node.children.append(Node())
current_node = current_node.children[-1]
token = next(tokens)
else:
# print(token, file=sys.stderr)
type, value = token
if type != "property_name":
raise InvalidSgfException("unknown token" + repr(token))
log("testing property_name", token, repr(type))
while type == "property_name":
v = value
values = []
token = next(tokens)
type, value = token
log("consumed", token)
while type == "property_value":
values.append(value)
token = next(tokens)
type, value = token
log("testing", token)
current_node.properties.append((v, values))
log("next token", token)
except StopIteration:
if len(stack) != 0:
raise InvalidSgfException("unclosed right parenthesis")
return tree_node