This repository was archived by the owner on Mar 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsgf_parsing.py
More file actions
91 lines (79 loc) · 2.74 KB
/
Copy pathsgf_parsing.py
File metadata and controls
91 lines (79 loc) · 2.74 KB
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
class SgfTree:
def __init__(self, properties=None, children=None):
self.properties = properties or {}
self.children = children or []
if any(not k.isupper() or v == [] for k, v in self.properties.items()):
raise ValueError(r".+")
def __eq__(self, other):
if not isinstance(other, SgfTree):
return False
for k, v in self.properties.items():
if k not in other.properties:
return False
if other.properties[k] != v:
return False
for k in other.properties.keys():
if k not in self.properties:
return False
if len(self.children) != len(other.children):
return False
for a, b in zip(self.children, other.children):
if a != b:
return False
return True
def __ne__(self, other):
return not self == other
def parse(input_string):
if not input_string.startswith("(;") or not input_string.endswith(")"):
raise ValueError(r".+")
input_string = input_string[2:-1].replace("\t", " ")
properties = {}
children = None
is_key = True
key = ""
val = ""
i = 0
while i < len(input_string):
if is_key and i + 1 == len(input_string):
properties[key] = []
elif is_key and input_string[i] == "[":
properties[key] = []
is_key = False
elif not is_key and input_string[i] == "]":
properties[key].append(val)
val = ""
if i + 1 < len(input_string) and input_string[i + 1] == "[":
i += 1
elif i + 1 < len(input_string) and input_string[i + 1] == ";":
children = [parse("(" + input_string[i + 1:] + ")")]
break
elif i + 1 < len(input_string) and input_string[i + 1] == "(":
children = parse_children(input_string[i + 1:])
break
else:
is_key = True
key = ""
elif not is_key and input_string[i:i + 2] == "\\]":
val += "]"
i += 1
elif is_key:
key += input_string[i]
else:
val += input_string[i]
i += 1
return SgfTree(properties, children)
def parse_children(input_string):
count = 0
indices = []
for i in range(len(input_string)):
if input_string[i] == "(":
if count == 0:
indices.append(i)
count += 1
elif input_string[i] == ")":
count -= 1
if count < 0:
raise ValueError(r".+")
indices.append(len(input_string))
return [parse(input_string[indices[i]:indices[i + 1]])
for i in range(len(indices) - 1)]