| 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 containing SGF formatted data |
| 34 | |
| 35 | Returns: |
| 36 | SgfTree: A tree structure representing the SGF data |
| 37 | |
| 38 | Raises: |
| 39 | ValueError: If the input is invalid according to SGF format rules |
| 40 | """ |
| 41 | # Edge Case: Empty input or input that doesn't start with '(' |
| 42 | if not input_string or not input_string.startswith('('): |
| 43 | raise ValueError("tree missing") |
| 44 | |
| 45 | # Edge Case: Input that doesn't end with ')' |
| 46 | if not input_string.endswith(')'): |
| 47 | raise ValueError("tree missing") |
| 48 | |
| 49 | # Remove the outer parentheses |
| 50 | content = input_string[1:-1] |
| 51 | |
| 52 | # Edge Case: Empty content after removing parentheses |
| 53 | if not content: |
| 54 | raise ValueError("tree with no nodes") |
| 55 | |
| 56 | # Edge Case: Content that doesn't start with ';' |
| 57 | if not content.startswith(';'): |
| 58 | raise ValueError("tree with no nodes") |
| 59 | |
| 60 | return _parse_tree(content) |
| 61 | |
| 62 | |
| 63 | def _parse_tree(content: str) -> SgfTree: |
| 64 | """ |
| 65 | Parse a tree from SGF content. |
| 66 | |
| 67 | Args: |
| 68 | content: SGF content string starting with ';' |
| 69 | |
| 70 | Returns: |
| 71 | SgfTree: Parsed tree structure |
| 72 | """ |
| 73 | # Skip the initial semicolon |
| 74 | index = 1 |
| 75 | |
| 76 | # Parse properties |
| 77 | properties = {} |
| 78 | while index < len(content) and content[index].isalpha(): |
| 79 | # Check if property key is uppercase |
| 80 | if not content[index].isupper(): |
| 81 | raise ValueError("property must be in uppercase") |
| 82 | |
| 83 | # Parse key |
| 84 | key_start = index |
| 85 | while index < len(content) and content[index].isalpha() and content[index].isupper(): |
| 86 | index += 1 |
| 87 | |
| 88 | key = content[key_start:index] |
| 89 | |
| 90 | # Edge Case: Property key not followed by '[' |
| 91 | if index >= len(content) or content[index] != '[': |
| 92 | raise ValueError("properties without delimiter") |
| 93 | |
| 94 | # Parse values |
| 95 | values = [] |
| 96 | while index < len(content) and content[index] == '[': |
| 97 | index += 1 # Skip '[' |
| 98 | value_start = index |
| 99 | |
| 100 | # Parse value, handling escapes |
| 101 | while index < len(content) and content[index] != ']': |
| 102 | if content[index] == '\\': |
| 103 | index += 2 # Skip escape and next character |
| 104 | else: |
| 105 | index += 1 |
| 106 | |
| 107 | # Edge Case: Unclosed value bracket |
| 108 | if index >= len(content): |
| 109 | raise ValueError("properties without delimiter") |
| 110 | |
| 111 | value = content[value_start:index] |
| 112 | value = _unescape_text(value) |
| 113 | values.append(value) |
| 114 | index += 1 # Skip ']' |
| 115 | |
| 116 | properties[key] = values |
| 117 | |
| 118 | # Parse children |
| 119 | children = [] |
| 120 | while index < len(content): |
| 121 | if content[index] == '(': |
| 122 | # Find matching parenthesis for this child |
| 123 | paren_count = 1 |
| 124 | child_start = index |
| 125 | index += 1 |
| 126 | |
| 127 | while index < len(content) and paren_count > 0: |
| 128 | if content[index] == '(': |
| 129 | paren_count += 1 |
| 130 | elif content[index] == ')': |
| 131 | paren_count -= 1 |
| 132 | index += 1 |
| 133 | |
| 134 | # Edge Case: Unmatched parenthesis |
| 135 | if paren_count != 0: |
| 136 | raise ValueError("tree missing") |
| 137 | |
| 138 | child_content = content[child_start+1:index-1] # Remove outer parentheses |
| 139 | child_tree = _parse_tree(child_content) |
| 140 | children.append(child_tree) |
| 141 | elif content[index] == ';': |
| 142 | # Create a new node as a child |
| 143 | # Parse the rest of the content as a new tree |
| 144 | node_content = content[index:] |
| 145 | node_tree = _parse_tree(node_content) |
| 146 | children.append(node_tree) |
| 147 | break # After parsing a node with ';', we're done with this level |
| 148 | else: |
| 149 | index += 1 |
| 150 | |
| 151 | return SgfTree(properties, children) |
| 152 | |
| 153 | |
| 154 | def _unescape_text(text: str) -> str: |
| 155 | """ |
| 156 | Process SGF text according to SGF text type rules. |
| 157 | |
| 158 | Args: |
| 159 | text: Raw text value from SGF |
| 160 | |
| 161 | Returns: |
| 162 | str: Processed text value |
| 163 | """ |
| 164 | result = [] |
| 165 | i = 0 |
| 166 | while i < len(text): |
| 167 | if text[i] == '\\': |
| 168 | # Edge Case: Escape at end of string |
| 169 | if i + 1 >= len(text): |
| 170 | result.append('\\') |
| 171 | break |
| 172 | |
| 173 | next_char = text[i + 1] |
| 174 | # If next character is newline, remove both backslash and newline |
| 175 | if next_char == '\n': |
| 176 | i += 2 # Skip backslash and newline |
| 177 | # Add a space to replace the removed newline |
| 178 | result.append(' ') |
| 179 | continue |
| 180 | else: |
| 181 | # Any character after \ is inserted as-is |
| 182 | # But whitespace characters still follow their rules |
| 183 | if next_char.isspace() and next_char != '\n': |
| 184 | result.append(' ') |
| 185 | elif next_char == '\n': |
| 186 | # Newline after backslash is removed |
| 187 | pass |
| 188 | else: |
| 189 | result.append(next_char) |
| 190 | i += 2 |
| 191 | else: |
| 192 | # All whitespace characters other than newline are converted to spaces |
| 193 | if text[i].isspace() and text[i] != '\n': |
| 194 | result.append(' ') |
| 195 | else: |
| 196 | result.append(text[i]) |
| 197 | i += 1 |
| 198 | |
| 199 | return ''.join(result) |
| 200 | |
| 201 | # Handled Edge Cases: Empty input, missing opening parenthesis, missing closing parenthesis, empty content after removing parentheses, missing semicolon, property key not followed by '[', unclosed value bracket, unmatched parenthesis, escape at end of string |