| 26 | 26 | |
| 27 | 27 | |
| 28 | 28 | def 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 malformed or doesn't follow 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 | # Parse the content inside the outermost 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 with no nodes") |
| 63 | |
| 64 | # Parse the tree |
| 65 | tree, _ = _parse_tree(content) |
| 66 | return tree |
| 67 | |
| 68 | |
| 69 | def _parse_tree(content: str) -> tuple[SgfTree, int]: |
| 70 | """ |
| 71 | Parse a tree from the content string. |
| 72 | |
| 73 | Args: |
| 74 | content: The content string to parse |
| 75 | |
| 76 | Returns: |
| 77 | tuple: A tuple containing the parsed SgfTree and the index where parsing ended |
| 78 | """ |
| 79 | # Edge Case: Content doesn't start with ';' |
| 80 | if not content or content[0] != ';': |
| 81 | raise ValueError("tree with no nodes") |
| 82 | |
| 83 | index = 1 # Skip the ';' |
| 84 | properties = {} |
| 85 | |
| 86 | # Parse properties for the first node |
| 87 | while index < len(content) and content[index].isalpha(): |
| 88 | prop, index = _parse_property(content, index) |
| 89 | key, values = prop |
| 90 | properties[key] = values |
| 91 | |
| 92 | # Determine tree structure based on what comes after properties |
| 93 | # Look for subsequent semicolons in the main sequence (not inside parentheses) |
| 94 | has_subsequent_semicolons_in_main = False |
| 95 | |
| 96 | if properties: |
| 97 | # Look ahead to see what comes next in main sequence |
| 98 | temp_index = index |
| 99 | paren_depth = 0 |
| 100 | while temp_index < len(content): |
| 101 | char = content[temp_index] |
| 102 | if char == '(': |
| 103 | paren_depth += 1 |
| 104 | elif char == ')': |
| 105 | paren_depth -= 1 |
| 106 | elif char == ';' and paren_depth == 0: |
| 107 | # Found a semicolon in the main sequence |
| 108 | has_subsequent_semicolons_in_main = True |
| 109 | break |
| 110 | temp_index += 1 |
| 111 | |
| 112 | # Apply the rules: |
| 113 | # 1. If there are properties and subsequent semicolons in main sequence, keep in root |
| 114 | # 2. If there are properties but no subsequent semicolons in main sequence, the behavior depends on what comes next |
| 115 | # 3. If no properties, root stays empty |
| 116 | |
| 117 | if properties and not has_subsequent_semicolons_in_main: |
| 118 | # No subsequent semicolons in main sequence |
| 119 | # Check what comes next |
| 120 | temp_index = index |
| 121 | first_thing_is_variation = False |
| 122 | while temp_index < len(content): |
| 123 | if content[temp_index] == '(': |
| 124 | first_thing_is_variation = True |
| 125 | break |
| 126 | elif content[temp_index] not in '[]': |
| 127 | # Found a non-bracket character, so not immediately followed by variation |
| 128 | break |
| 129 | temp_index += 1 |
| 130 | |
| 131 | if first_thing_is_variation: |
| 132 | # First thing is a variation |
| 133 | # Check if variations contain semicolons (indicating complex sequences) |
| 134 | variations_contain_semicolons = False |
| 135 | temp_index = index |
| 136 | while temp_index < len(content): |
| 137 | if content[temp_index] == '(': |
| 138 | # Found a variation, look inside it for semicolons |
| 139 | paren_depth = 1 |
| 140 | inner_index = temp_index + 1 |
| 141 | while inner_index < len(content) and paren_depth > 0: |
| 142 | if content[inner_index] == '(': |
| 143 | paren_depth += 1 |
| 144 | elif content[inner_index] == ')': |
| 145 | paren_depth -= 1 |
| 146 | elif content[inner_index] == ';' and paren_depth == 1: |
| 147 | # Found semicolon at variation level (not nested deeper) |
| 148 | variations_contain_semicolons = True |
| 149 | break |
| 150 | inner_index += 1 |
| 151 | if variations_contain_semicolons: |
| 152 | break |
| 153 | temp_index += 1 |
| 154 | |
| 155 | # Decision logic: |
| 156 | # - If single property and variations are simple (no semicolons), move to child |
| 157 | # - If single property and variations are complex (have semicolons), keep in root |
| 158 | # - If multiple properties, always keep in root |
| 159 | if len(properties) == 1 and not variations_contain_semicolons: |
| 160 | # Move single property to first child |
| 161 | root = SgfTree({}, []) |
| 162 | current_node = SgfTree(properties, []) |
| 163 | root.children.append(current_node) |
| 164 | else: |
| 165 | # Keep properties in root (multiple properties or complex variations) |
| 166 | root = SgfTree(properties, []) |
| 167 | current_node = root |
| 168 | else: |
| 169 | # No variations immediately after, keep properties in root |
| 170 | root = SgfTree(properties, []) |
| 171 | current_node = root |
| 172 | else: |
| 173 | # Keep properties in root (either no properties or there are subsequent semicolons) |
| 174 | root = SgfTree(properties, []) |
| 175 | current_node = root |
| 176 | |
| 177 | # Parse subsequent nodes (semicolon-separated) and variations |
| 178 | while index < len(content): |
| 179 | if content[index] == ';': |
| 180 | # Parse next node in sequence |
| 181 | index += 1 # Skip the ';' |
| 182 | node_properties = {} |
| 183 | |
| 184 | # Parse properties for this node |
| 185 | while index < len(content) and content[index].isalpha(): |
| 186 | prop, index = _parse_property(content, index) |
| 187 | key, values = prop |
| 188 | node_properties[key] = values |
| 189 | |
| 190 | # Create child node and link it |
| 191 | child_node = SgfTree(node_properties, []) |
| 192 | current_node.children.append(child_node) |
| 193 | current_node = child_node |
| 194 | elif content[index] == '(': |
| 195 | # Parse variation subtree |
| 196 | child, index = _parse_subtree(content, index) |
| 197 | root.children.append(child) |
| 198 | else: |
| 199 | # End of this tree sequence |
| 200 | break |
| 201 | |
| 202 | return root, index |
| 203 | |
| 204 | |
| 205 | def _parse_subtree(content: str, start_index: int) -> tuple[SgfTree, int]: |
| 206 | """ |
| 207 | Parse a subtree from the content string. |
| 208 | |
| 209 | Args: |
| 210 | content: The content string to parse |
| 211 | start_index: The index to start parsing from |
| 212 | |
| 213 | Returns: |
| 214 | tuple: A tuple containing the parsed SgfTree and the index where parsing ended |
| 215 | """ |
| 216 | # Edge Case: Content doesn't start with '(' |
| 217 | if content[start_index] != '(': |
| 218 | raise ValueError("tree missing") |
| 219 | |
| 220 | index = start_index + 1 # Skip the '(' |
| 221 | |
| 222 | # Parse the tree inside the parentheses |
| 223 | tree, relative_index = _parse_tree(content[index:]) |
| 224 | index += relative_index # Adjust index to account for substring |
| 225 | |
| 226 | # Edge Case: Missing closing ')' |
| 227 | if index >= len(content) or content[index] != ')': |
| 228 | raise ValueError("tree missing") |
| 229 | |
| 230 | return tree, index + 1 # Skip the ')' |
| 231 | |
| 232 | |
| 233 | def _parse_property(content: str, start_index: int) -> tuple[tuple[str, list[str]], int]: |
| 234 | """ |
| 235 | Parse a property from the content string. |
| 236 | |
| 237 | Args: |
| 238 | content: The content string to parse |
| 239 | start_index: The index to start parsing from |
| 240 | |
| 241 | Returns: |
| 242 | tuple: A tuple containing the parsed property (key, values) and the index where parsing ended |
| 243 | """ |
| 244 | # Parse property key |
| 245 | key_end = start_index |
| 246 | while key_end < len(content) and content[key_end].isalpha(): |
| 247 | if not content[key_end].isupper(): |
| 248 | # Property key contains lowercase characters |
| 249 | raise ValueError("property must be in uppercase") |
| 250 | key_end += 1 |
| 251 | |
| 252 | # Edge Case: Empty property key |
| 253 | if key_end == start_index: |
| 254 | raise ValueError("properties without delimiter") |
| 255 | |
| 256 | key = content[start_index:key_end] |
| 257 | |
| 258 | # Edge Case: Property key must be uppercase |
| 259 | if not key.isupper(): |
| 260 | raise ValueError("property must be in uppercase") |
| 261 | |
| 262 | # Edge Case: Property key not followed by '[' |
| 263 | if key_end >= len(content) or content[key_end] != '[': |
| 264 | raise ValueError("properties without delimiter") |
| 265 | |
| 266 | index = key_end |
| 267 | values = [] |
| 268 | |
| 269 | # Parse all values for this property |
| 270 | while index < len(content) and content[index] == '[': |
| 271 | value, index = _parse_value(content, index + 1) # Skip the '[' |
| 272 | values.append(value) |
| 273 | |
| 274 | # Edge Case: Value not followed by ']' |
| 275 | if index >= len(content) or content[index] != ']': |
| 276 | raise ValueError("properties without delimiter") |
| 277 | |
| 278 | index += 1 # Skip the ']' |
| 279 | |
| 280 | return (key, values), index |
| 281 | |
| 282 | |
| 283 | def _parse_value(content: str, start_index: int) -> tuple[str, int]: |
| 284 | """ |
| 285 | Parse a value from the content string according to SGF Text type rules. |
| 286 | |
| 287 | Args: |
| 288 | content: The content string to parse |
| 289 | start_index: The index to start parsing from |
| 290 | |
| 291 | Returns: |
| 292 | tuple: A tuple containing the parsed value and the index where parsing ended |
| 293 | """ |
| 294 | value = [] |
| 295 | index = start_index |
| 296 | |
| 297 | while index < len(content) and content[index] != ']': |
| 298 | if content[index] == '\\': |
| 299 | # Handle escape character |
| 300 | index += 1 |
| 301 | if index >= len(content): |
| 302 | # Edge Case: Escape character at end of input |
| 303 | raise ValueError("properties without delimiter") |
| 304 | |
| 305 | char = content[index] |
| 306 | if char.isspace(): |
| 307 | # Whitespace after escape - follows normal whitespace rules |
| 308 | if char == '\n': |
| 309 | # Newline immediately after \ is removed |
| 310 | pass |
| 311 | else: |
| 312 | # Other whitespace becomes space |
| 313 | value.append(' ') |
| 314 | else: |
| 315 | # Any non-whitespace character after \ is inserted as-is |
| 316 | value.append(char) |
| 317 | elif content[index] == '\n': |
| 318 | # Newlines are preserved unless immediately after \ |
| 319 | value.append('\n') |
| 320 | elif content[index].isspace() and content[index] != '\n': |
| 321 | # All whitespace other than newline becomes space |
| 322 | value.append(' ') |
| 323 | else: |
| 324 | # Regular character |
| 325 | value.append(content[index]) |
| 326 | |
| 327 | index += 1 |
| 328 | |
| 329 | # Edge Case: Unclosed value (reached end of content without finding ']') |
| 330 | if index >= len(content): |
| 331 | raise ValueError("properties without delimiter") |
| 332 | |
| 333 | return ''.join(value), index |
| 334 | |
| 335 | # 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 doesn't start with ';', Empty property key, Property key not followed by '[', Value not followed by ']', Escape character at end of input, Unclosed value (reached end of content without finding ']') |
| 336 | # Handled Edge Cases: Content doesn't start with '(', Missing closing ')' |
| 337 | # Handled Edge Cases: Empty property key, Property key not followed by '[', Value not followed by ']', Unclosed value (reached end of content without finding ']') |
| 338 | # Handled Edge Cases: Escape character at end of input, Unclosed value (reached end of content without finding ']') |