| 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 |
| 57 | if not content: |
| 58 | raise ValueError("tree with no nodes") |
| 59 | |
| 60 | # Edge Case: Content doesn't start with ';' |
| 61 | if not content.startswith(';'): |
| 62 | raise ValueError("tree with no nodes") |
| 63 | |
| 64 | return _parse_tree(content) |
| 65 | |
| 66 | |
| 67 | def _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: Empty content |
| 78 | if not content: |
| 79 | raise ValueError("tree with no nodes") |
| 80 | |
| 81 | # Edge Case: Content doesn't start with ';' |
| 82 | if not content.startswith(';'): |
| 83 | raise ValueError("tree with no nodes") |
| 84 | |
| 85 | # Parse the first node |
| 86 | node_content, remaining = _parse_node_content(content[1:]) # Skip the initial ';' |
| 87 | properties = _parse_properties(node_content) |
| 88 | |
| 89 | children = [] |
| 90 | |
| 91 | # Parse children |
| 92 | while remaining: |
| 93 | if remaining.startswith('('): |
| 94 | # Parse a subtree |
| 95 | subtree_content, remaining = _parse_subtree(remaining) |
| 96 | children.append(_parse_tree(subtree_content)) |
| 97 | else: |
| 98 | # Parse a sequence of nodes (shorthand notation) |
| 99 | node_content, remaining = _parse_node_content(remaining) |
| 100 | node_properties = _parse_properties(node_content) |
| 101 | # Create a chain of single-child nodes |
| 102 | if children: # If we already have children, append to the last one |
| 103 | # Find the deepest child in the chain |
| 104 | current = children[-1] |
| 105 | while current.children: |
| 106 | current = current.children[0] |
| 107 | current.children.append(SgfTree(properties=node_properties)) |
| 108 | else: # If no children yet, create the first one |
| 109 | children.append(SgfTree(properties=node_properties)) |
| 110 | |
| 111 | return SgfTree(properties=properties, children=children) |
| 112 | |
| 113 | |
| 114 | def _parse_subtree(content: str) -> tuple[str, str]: |
| 115 | """ |
| 116 | Parse a subtree from content that starts with '('. |
| 117 | |
| 118 | Args: |
| 119 | content: String starting with '(' |
| 120 | |
| 121 | Returns: |
| 122 | tuple: (subtree_content, remaining_content) |
| 123 | """ |
| 124 | # Edge Case: Content doesn't start with '(' |
| 125 | if not content.startswith('('): |
| 126 | raise ValueError("tree missing") |
| 127 | |
| 128 | balance = 0 |
| 129 | i = 0 |
| 130 | |
| 131 | for i, char in enumerate(content): |
| 132 | if char == '(': |
| 133 | balance += 1 |
| 134 | elif char == ')': |
| 135 | balance -= 1 |
| 136 | # Edge Case: Found closing parenthesis for the current subtree |
| 137 | if balance == 0: |
| 138 | return content[1:i], content[i+1:] # Exclude the outer parentheses |
| 139 | |
| 140 | # Edge Case: Unmatched parentheses |
| 141 | raise ValueError("tree missing") |
| 142 | |
| 143 | |
| 144 | def _parse_node_content(content: str) -> tuple[str, str]: |
| 145 | """ |
| 146 | Parse the content of a single node (properties) and return remaining content. |
| 147 | |
| 148 | Args: |
| 149 | content: String containing node properties |
| 150 | |
| 151 | Returns: |
| 152 | tuple: (node_properties_content, remaining_content) |
| 153 | """ |
| 154 | # Find the end of this node's properties |
| 155 | i = 0 |
| 156 | while i < len(content): |
| 157 | # Stop at the beginning of a child node/subtree |
| 158 | if content[i] in ';(': |
| 159 | break |
| 160 | i += 1 |
| 161 | |
| 162 | return content[:i], content[i:] |
| 163 | |
| 164 | |
| 165 | def _parse_properties(content: str) -> dict: |
| 166 | """ |
| 167 | Parse properties from a node's content. |
| 168 | |
| 169 | Args: |
| 170 | content: String containing properties in format KEY[value][value]... |
| 171 | |
| 172 | Returns: |
| 173 | dict: Dictionary of properties |
| 174 | """ |
| 175 | properties = {} |
| 176 | i = 0 |
| 177 | |
| 178 | while i < len(content): |
| 179 | # Parse property key |
| 180 | key_start = i |
| 181 | |
| 182 | # Edge Case: Key must be uppercase |
| 183 | while i < len(content) and content[i].isalpha(): |
| 184 | if not content[i].isupper(): |
| 185 | raise ValueError("property must be in uppercase") |
| 186 | i += 1 |
| 187 | |
| 188 | # Edge Case: Empty key |
| 189 | if i == key_start: |
| 190 | raise ValueError("properties without delimiter") |
| 191 | |
| 192 | key = content[key_start:i] |
| 193 | |
| 194 | # Edge Case: Key already exists |
| 195 | if key in properties: |
| 196 | raise ValueError("property must be in uppercase") # Using same error as example |
| 197 | |
| 198 | values = [] |
| 199 | |
| 200 | # Parse values |
| 201 | while i < len(content) and content[i] == '[': |
| 202 | i += 1 # Skip '[' |
| 203 | value_start = i |
| 204 | |
| 205 | # Parse value content, handling escapes |
| 206 | while i < len(content): |
| 207 | if content[i] == ']': |
| 208 | # Check if this is an escaped ']' |
| 209 | if i > 0 and content[i-1] == '\\': |
| 210 | # Count backslashes before this bracket |
| 211 | backslash_count = 0 |
| 212 | j = i - 1 |
| 213 | while j >= 0 and content[j] == '\\': |
| 214 | backslash_count += 1 |
| 215 | j -= 1 |
| 216 | # If odd number of backslashes, this bracket is escaped |
| 217 | if backslash_count % 2 == 1: |
| 218 | i += 1 |
| 219 | continue |
| 220 | break |
| 221 | i += 1 |
| 222 | |
| 223 | # Edge Case: Unclosed value bracket |
| 224 | if i >= len(content) or content[i] != ']': |
| 225 | raise ValueError("properties without delimiter") |
| 226 | |
| 227 | value_content = content[value_start:i] |
| 228 | values.append(_parse_text_value(value_content)) |
| 229 | i += 1 # Skip ']' |
| 230 | |
| 231 | # Edge Case: No values for property |
| 232 | if not values: |
| 233 | raise ValueError("properties without delimiter") |
| 234 | |
| 235 | properties[key] = values |
| 236 | |
| 237 | return properties |
| 238 | |
| 239 | |
| 240 | def _parse_text_value(content: str) -> str: |
| 241 | """ |
| 242 | Parse an SGF Text value, handling escapes and whitespace. |
| 243 | |
| 244 | Args: |
| 245 | content: Raw content of a text value |
| 246 | |
| 247 | Returns: |
| 248 | str: Processed text value |
| 249 | """ |
| 250 | result = [] |
| 251 | i = 0 |
| 252 | |
| 253 | while i < len(content): |
| 254 | if content[i] == '\\': |
| 255 | # Handle escape sequences |
| 256 | i += 1 |
| 257 | # Edge Case: Escape at end of string |
| 258 | if i >= len(content): |
| 259 | # Trailing backslash is just ignored in SGF |
| 260 | break |
| 261 | |
| 262 | char = content[i] |
| 263 | # Newlines are removed if they come immediately after a backslash |
| 264 | if char == '\n': |
| 265 | # Do nothing - newline is removed |
| 266 | pass |
| 267 | # All whitespace characters other than newline are converted to spaces |
| 268 | elif char in ' \t\r': |
| 269 | result.append(' ') |
| 270 | else: |
| 271 | # Any non-whitespace character after \ is inserted as-is |
| 272 | result.append(char) |
| 273 | else: |
| 274 | # Regular character |
| 275 | result.append(content[i]) |
| 276 | i += 1 |
| 277 | |
| 278 | return ''.join(result) |
| 279 | |
| 280 | # Handled Edge Cases: Empty input, missing parentheses, missing semicolon, empty tree, unmatched parentheses, invalid keys, duplicate keys, missing values, unclosed brackets, trailing escape, escaped newlines, whitespace conversion |