future

Finished
2626
2727
2828def parse(input_string: str) -> SgfTree:
29 pass
29 """
30 Parse an SGF string and return a tree structure of properties.
31
32 Args:
33 input_string: A string in SGF format
34
35 Returns:
36 SgfTree: The parsed SGF tree
37
38 Raises:
39 ValueError: If the input is invalid according to SGF format rules
40 """
41 # Edge Case: Empty input
42 if not input_string:
43 raise ValueError("tree missing")
44
45 # Edge Case: Input doesn't start with '('
46 if not input_string.startswith('('):
47 raise ValueError("tree missing")
48
49 # Edge Case: Input doesn't end with ')'
50 if not input_string.endswith(')'):
51 raise ValueError("tree missing")
52
53 # Remove the outer parentheses
54 content = input_string[1:-1]
55
56 # Edge Case: Empty tree with no nodes
57 if not content:
58 raise ValueError("tree with no nodes")
59
60 # Edge Case: Tree doesn't start with ';'
61 if not content.startswith(';'):
62 raise ValueError("tree missing")
63
64 return _parse_tree(content)
65
66
67def _parse_tree(content: str) -> SgfTree:
68 """
69 Parse a tree from its content string.
70
71 Args:
72 content: The content of the tree without outer parentheses
73
74 Returns:
75 SgfTree: The parsed tree
76 """
77 # Edge Case: Content is just ';'
78 if content == ';':
79 return SgfTree()
80
81 # Find the first node
82 if not content.startswith(';'):
83 raise ValueError("tree missing")
84
85 # Parse the first node
86 node_end = 1 # Start after the ';'
87 properties = {}
88
89 # Parse properties of the first node
90 while node_end < len(content) and content[node_end] != '(' and content[node_end] != ';':
91 # Parse key
92 key_start = node_end
93 while node_end < len(content) and content[node_end].isalpha():
94 node_end += 1
95
96 # Edge Case: Key is not in uppercase
97 key = content[key_start:node_end]
98 if not key.isupper():
99 raise ValueError("property must be in uppercase")
100
101 # Edge Case: Key is empty
102 if not key:
103 raise ValueError("properties without delimiter")
104
105 # Parse values
106 values = []
107 while node_end < len(content) and content[node_end] == '[':
108 node_end += 1 # Skip '['
109 value_start = node_end
110
111 # Parse value, handling escapes
112 while node_end < len(content) and content[node_end] != ']':
113 if content[node_end] == '\\':
114 node_end += 2 # Skip escape and next character
115 else:
116 node_end += 1
117
118 # Edge Case: Unclosed bracket
119 if node_end >= len(content) or content[node_end] != ']':
120 raise ValueError("properties without delimiter")
121
122 value = content[value_start:node_end]
123 # Process the value according to SGF text type rules
124 value = _process_text(value)
125 values.append(value)
126 node_end += 1 # Skip ']'
127
128 # Edge Case: No values for property
129 if not values:
130 raise ValueError("properties without delimiter")
131
132 properties[key] = values
133
134 # Parse children
135 children = []
136 while node_end < len(content) and content[node_end] == '(':
137 # Find matching parenthesis
138 paren_count = 1
139 child_start = node_end
140 node_end += 1
141
142 while node_end < len(content) and paren_count > 0:
143 if content[node_end] == '(':
144 paren_count += 1
145 elif content[node_end] == ')':
146 paren_count -= 1
147 node_end += 1
148
149 # Edge Case: Mismatched parentheses
150 if paren_count != 0:
151 raise ValueError("tree missing")
152
153 # Parse child tree
154 child_content = content[child_start:node_end]
155 child_tree = parse(child_content) # This will add the parentheses back
156 children.append(child_tree)
157
158 # Handle sequence of nodes (shorthand notation)
159 if node_end < len(content) and content[node_end] == ';':
160 # Parse the rest as a child
161 remaining_content = content[node_end:]
162 child_tree = _parse_tree(remaining_content)
163 children.append(child_tree)
164
165 return SgfTree(properties, children)
166
167
168def _process_text(text: str) -> str:
169 """
170 Process text according to SGF Text type rules.
171
172 Args:
173 text: The raw text value from SGF
174
175 Returns:
176 str: The processed text
177 """
178 result = []
179 i = 0
180 while i < len(text):
181 if text[i] == '\\':
182 # Edge Case: Escape at end of string
183 if i + 1 >= len(text):
184 result.append('\\')
185 break
186 next_char = text[i + 1]
187 # If next character is newline, remove the backslash and newline
188 if next_char == '\n':
189 i += 2 # Skip backslash and newline
190 else:
191 # Any non-whitespace character after \ is inserted as-is
192 # Any whitespace character after \ follows the above rules
193 if next_char.isspace() and next_char != '\n':
194 result.append(' ')
195 else:
196 result.append(next_char)
197 i += 2
198 else:
199 # All whitespace characters other than newline are converted to spaces
200 if text[i].isspace() and text[i] != '\n':
201 result.append(' ')
202 else:
203 result.append(text[i])
204 i += 1
205
206 return ''.join(result)
207
208# Handled Edge Cases: Empty input, Input doesn't start with '(', Input doesn't end with ')', Empty tree with no nodes, Tree doesn't start with ';', Content is just ';', Key is not in uppercase, Key is empty, Unclosed bracket, No values for property, Mismatched parentheses, Escape at end of string
Test NameStatus
test_all_lowercase_property
Pass
test_empty_input
Pass
test_escaped_backslash_in_property_value_becomes_just_a_backslash
Pass
test_escaped_closing_bracket_within_property_value_becomes_just_a_closing_bracket
Pass
test_escaped_newline_in_property_value_is_converted_to_nothing_at_all
Pass
test_escaped_t_and_n_in_property_value_are_just_letters_not_whitespace
Pass
test_escaped_tab_in_property_value_is_converted_to_space
Pass
test_mixing_various_kinds_of_whitespace_and_escaped_characters_in_property_value
Pass
test_multiple_properties
Pass
test_multiple_property_values
Pass
test_node_without_properties
Pass
test_node_without_tree
Pass
test_opening_bracket_within_property_value_doesn_t_need_to_be_escaped
Pass
test_parentheses_in_property_value_don_t_need_to_be_escaped
Pass
test_properties_without_delimiter
Pass
test_semicolon_in_property_value_doesn_t_need_to_be_escaped
Pass
test_single_node_tree
Pass
test_tree_with_no_nodes
Pass
test_two_child_trees
Pass
test_two_nodes
Pass
test_upper_and_lowercase_property
Pass
test_within_property_values_newlines_remain_as_newlines
Pass
test_within_property_values_whitespace_characters_such_as_tab_are_converted_to_spaces
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.