| 26 | 26 | |
| 27 | 27 | |
| 28 | 28 | def parse(input_string: str) -> SgfTree: |
| 29 | | pass |
| 29 | """ |
| 30 | Parse an SGF (Smart Game Format) string and return a tree structure of properties. |
| 31 | |
| 32 | Args: |
| 33 | input_string: A string containing SGF formatted data |
| 34 | |
| 35 | Returns: |
| 36 | SgfTree: A tree structure representing the SGF data |
| 37 | |
| 38 | Raises: |
| 39 | ValueError: If the input string is malformed or doesn't conform to SGF format |
| 40 | """ |
| 41 | # Edge Case: Empty input string |
| 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 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 | return _parse_tree(content) |
| 65 | |
| 66 | |
| 67 | def _parse_tree(content: str) -> SgfTree: |
| 68 | """ |
| 69 | Parse a tree from SGF content. |
| 70 | |
| 71 | Args: |
| 72 | content: SGF content string starting with ';' |
| 73 | |
| 74 | Returns: |
| 75 | SgfTree: Parsed tree |
| 76 | """ |
| 77 | # Edge Case: Content is just ';' |
| 78 | if content == ';': |
| 79 | return SgfTree() |
| 80 | |
| 81 | i = 1 # Skip the initial ';' |
| 82 | properties = {} |
| 83 | |
| 84 | # Parse properties |
| 85 | while i < len(content) and content[i] != '(' and content[i] != ';' and content[i] != ')': |
| 86 | # Parse property key |
| 87 | key_start = i |
| 88 | while i < len(content) and content[i].isalpha(): |
| 89 | i += 1 |
| 90 | |
| 91 | # Edge Case: No key found |
| 92 | if i == key_start: |
| 93 | raise ValueError("properties without delimiter") |
| 94 | |
| 95 | key = content[key_start:i] |
| 96 | |
| 97 | # Edge Case: Key is not uppercase |
| 98 | if not key.isupper(): |
| 99 | raise ValueError("property must be in uppercase") |
| 100 | |
| 101 | # Parse property values |
| 102 | values = [] |
| 103 | |
| 104 | # Edge Case: No values after key |
| 105 | if i >= len(content) or content[i] != '[': |
| 106 | raise ValueError("properties without delimiter") |
| 107 | |
| 108 | while i < len(content) and content[i] == '[': |
| 109 | i += 1 # Skip '[' |
| 110 | value_start = i |
| 111 | |
| 112 | # Parse value, including escaped characters |
| 113 | while i < len(content): |
| 114 | if content[i] == ']': |
| 115 | # Check if this ] is escaped |
| 116 | backslash_count = 0 |
| 117 | j = i - 1 |
| 118 | while j >= value_start and content[j] == '\\': |
| 119 | backslash_count += 1 |
| 120 | j -= 1 |
| 121 | # If odd number of backslashes, this ] is escaped |
| 122 | if backslash_count % 2 == 1: |
| 123 | i += 1 # Skip the escaped ] and continue |
| 124 | continue |
| 125 | else: |
| 126 | break # Found unescaped closing bracket |
| 127 | i += 1 |
| 128 | |
| 129 | # Edge Case: Unclosed value bracket |
| 130 | if i >= len(content) or content[i] != ']': |
| 131 | raise ValueError("properties without delimiter") |
| 132 | |
| 133 | value = content[value_start:i] |
| 134 | value = _unescape_value(value) |
| 135 | values.append(value) |
| 136 | i += 1 # Skip ']' |
| 137 | |
| 138 | properties[key] = values |
| 139 | |
| 140 | # Parse children |
| 141 | children = [] |
| 142 | |
| 143 | while i < len(content) and content[i] == '(': |
| 144 | # Find matching parenthesis |
| 145 | paren_count = 1 |
| 146 | start = i + 1 |
| 147 | i += 1 |
| 148 | |
| 149 | while i < len(content) and paren_count > 0: |
| 150 | if content[i] == '(': |
| 151 | paren_count += 1 |
| 152 | elif content[i] == ')': |
| 153 | paren_count -= 1 |
| 154 | i += 1 |
| 155 | |
| 156 | # Edge Case: Unmatched parenthesis |
| 157 | if paren_count != 0: |
| 158 | raise ValueError("tree missing") |
| 159 | |
| 160 | child_content = content[start:i-1] |
| 161 | child_tree = _parse_tree(child_content) |
| 162 | children.append(child_tree) |
| 163 | |
| 164 | # Handle additional nodes in sequence |
| 165 | if i < len(content) and content[i] == ';': |
| 166 | next_tree = _parse_tree(content[i:]) |
| 167 | children.append(next_tree) |
| 168 | |
| 169 | return SgfTree(properties, children) |
| 170 | |
| 171 | |
| 172 | def _unescape_value(value: str) -> str: |
| 173 | """ |
| 174 | Unescape SGF text value according to SGF specification. |
| 175 | |
| 176 | Args: |
| 177 | value: Escaped SGF text value |
| 178 | |
| 179 | Returns: |
| 180 | str: Unescaped text value |
| 181 | """ |
| 182 | result = [] |
| 183 | i = 0 |
| 184 | while i < len(value): |
| 185 | if value[i] == '\\': |
| 186 | i += 1 |
| 187 | if i < len(value): |
| 188 | # If next character is newline, remove both \ and newline |
| 189 | if value[i] == '\n': |
| 190 | i += 1 |
| 191 | continue |
| 192 | # Any character after \ is inserted as-is |
| 193 | result.append(value[i]) |
| 194 | i += 1 |
| 195 | else: |
| 196 | # Edge Case: Escape at end of string |
| 197 | result.append('\\') |
| 198 | i += 1 |
| 199 | else: |
| 200 | # Convert whitespace (except newline) to space |
| 201 | if value[i] != '\n' and value[i].isspace(): |
| 202 | result.append(' ') |
| 203 | else: |
| 204 | result.append(value[i]) |
| 205 | i += 1 |
| 206 | |
| 207 | return ''.join(result) |
| 208 | # Handled Edge Cases: Empty input string, Input doesn't start with '(', Input doesn't end with ')', Empty content after removing parentheses, Content doesn't start with ';', Content is just ';', No key found, Key is not uppercase, No values after key, Unclosed value bracket, Unmatched parenthesis, Escape at end of string |