| 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 invalid according to SGF format rules |
| 40 | """ |
| 41 | # Edge Case: Empty input or whitespace only input |
| 42 | if not input_string or input_string.isspace(): |
| 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 content after removing parentheses |
| 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 | def parse_node(content: str) -> tuple[SgfTree, str]: |
| 65 | """ |
| 66 | Parse a single node from the content. |
| 67 | |
| 68 | Args: |
| 69 | content: The content to parse |
| 70 | |
| 71 | Returns: |
| 72 | tuple: (parsed node, remaining content) |
| 73 | """ |
| 74 | # Edge Case: Node doesn't start with ';' |
| 75 | if not content.startswith(';'): |
| 76 | raise ValueError("tree with no nodes") |
| 77 | |
| 78 | content = content[1:] # Remove the ';' |
| 79 | properties = {} |
| 80 | |
| 81 | # Parse properties |
| 82 | while content and content[0].isalpha() and content[0].isupper(): |
| 83 | # Parse property key |
| 84 | key = '' |
| 85 | while content and content[0].isalpha() and content[0].isupper(): |
| 86 | key += content[0] |
| 87 | content = content[1:] |
| 88 | |
| 89 | # Edge Case: Property key is not in uppercase |
| 90 | if not key.isupper() or not key.isalpha(): |
| 91 | raise ValueError("property must be in uppercase") |
| 92 | |
| 93 | # Edge Case: Missing opening bracket for property value |
| 94 | if not content or content[0] != '[': |
| 95 | raise ValueError("properties without delimiter") |
| 96 | |
| 97 | # Parse property values |
| 98 | values = [] |
| 99 | while content and content[0] == '[': |
| 100 | content = content[1:] # Remove '[' |
| 101 | value = '' |
| 102 | |
| 103 | # Parse value until closing bracket |
| 104 | while content and content[0] != ']': |
| 105 | if content[0] == '\\': |
| 106 | # Handle escape sequences |
| 107 | content = content[1:] # Remove '\' |
| 108 | if content: |
| 109 | # According to SGF Text type specification: |
| 110 | # Any non-whitespace character after \ is inserted as-is |
| 111 | # Any whitespace character after \ follows the above rules |
| 112 | char = content[0] |
| 113 | if char == 'n' or char == 'r' or char == 't': |
| 114 | # These are not escape sequences in SGF, just literal characters |
| 115 | value += char |
| 116 | else: |
| 117 | value += char |
| 118 | content = content[1:] |
| 119 | else: |
| 120 | # Edge Case: Escape character at end of input |
| 121 | raise ValueError("properties without delimiter") |
| 122 | else: |
| 123 | char = content[0] |
| 124 | # According to SGF Text type specification: |
| 125 | # Newlines are removed if they come immediately after a \, otherwise they remain as newlines |
| 126 | # All whitespace characters other than newline are converted to spaces |
| 127 | if char == '\n': |
| 128 | value += char |
| 129 | elif char.isspace() and char != '\n': |
| 130 | value += ' ' |
| 131 | else: |
| 132 | value += char |
| 133 | content = content[1:] |
| 134 | |
| 135 | # Edge Case: Missing closing bracket |
| 136 | if not content or content[0] != ']': |
| 137 | raise ValueError("properties without delimiter") |
| 138 | |
| 139 | content = content[1:] # Remove ']' |
| 140 | values.append(value) |
| 141 | |
| 142 | properties[key] = values |
| 143 | |
| 144 | return SgfTree(properties), content |
| 145 | |
| 146 | def parse_tree(content: str) -> tuple[SgfTree, str]: |
| 147 | """ |
| 148 | Parse a tree (node with possible children) from the content. |
| 149 | |
| 150 | Args: |
| 151 | content: The content to parse |
| 152 | |
| 153 | Returns: |
| 154 | tuple: (parsed tree, remaining content) |
| 155 | """ |
| 156 | node, content = parse_node(content) |
| 157 | |
| 158 | # Parse children |
| 159 | children = [] |
| 160 | while content and content[0] == '(': |
| 161 | child, content = parse_tree(content[1:-1] if content.endswith(')') else content[1:]) |
| 162 | children.append(child) |
| 163 | |
| 164 | # If we've parsed a child, we expect either another '(' for another child |
| 165 | # or we've consumed the content |
| 166 | if content and content[0] == ')': |
| 167 | content = content[1:] |
| 168 | break |
| 169 | |
| 170 | node.children = children |
| 171 | return node, content |
| 172 | |
| 173 | # Handle the case where we have variations |
| 174 | if content.startswith(';'): |
| 175 | tree, remaining = parse_node(content) |
| 176 | |
| 177 | # Check for variations |
| 178 | children = [] |
| 179 | while remaining: |
| 180 | if remaining.startswith('('): |
| 181 | # Find matching closing parenthesis |
| 182 | paren_count = 1 |
| 183 | index = 1 |
| 184 | while index < len(remaining) and paren_count > 0: |
| 185 | if remaining[index] == '(': |
| 186 | paren_count += 1 |
| 187 | elif remaining[index] == ')': |
| 188 | paren_count -= 1 |
| 189 | index += 1 |
| 190 | |
| 191 | if paren_count != 0: |
| 192 | # Edge Case: Mismatched parentheses |
| 193 | raise ValueError("tree missing") |
| 194 | |
| 195 | # Parse the variation |
| 196 | variation_content = remaining[1:index-1] |
| 197 | variation, _ = parse_tree(variation_content) |
| 198 | children.append(variation) |
| 199 | remaining = remaining[index:] |
| 200 | else: |
| 201 | break |
| 202 | |
| 203 | tree.children = children |
| 204 | # Edge Case: Unexpected content after parsing |
| 205 | if remaining: |
| 206 | raise ValueError("tree missing") |
| 207 | |
| 208 | return tree |
| 209 | else: |
| 210 | # Edge Case: Invalid format |
| 211 | raise ValueError("tree with no nodes") |
| 212 | # Handled Edge Cases: Empty input, missing parentheses, missing semicolon, invalid property format, missing brackets, escape sequences, mismatched parentheses, unexpected content |
| 213 | # Handled Edge Cases: Empty input or whitespace only input, input doesn't start with '(', input doesn't end with ')', empty content after removing parentheses, content doesn't start with ';', node doesn't start with ';', property key is not in uppercase, missing opening bracket for property value, missing closing bracket, escape character at end of input, mismatched parentheses, unexpected content after parsing, invalid format |