| 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 |
| 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 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 missing") |
| 63 | |
| 64 | return _parse_tree(content) |
| 65 | |
| 66 | |
| 67 | def _parse_tree(content: str) -> SgfTree: |
| 68 | """ |
| 69 | Parse a tree from its content (without outer parentheses). |
| 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: Content is just ';' |
| 78 | if content == ';': |
| 79 | return SgfTree() |
| 80 | |
| 81 | # Find the first node |
| 82 | if not content.startswith(';'): |
| 83 | raise ValueError("tree missing") |
| 84 | |
| 85 | # Parse the first node |
| 86 | node_end = 1 # Start after the ';' |
| 87 | properties = {} |
| 88 | |
| 89 | # Parse properties of the first node |
| 90 | while node_end < len(content) and content[node_end] != '(' and content[node_end] != ';': |
| 91 | # Parse key |
| 92 | key_start = node_end |
| 93 | while node_end < len(content) and content[node_end].isalpha(): |
| 94 | node_end += 1 |
| 95 | |
| 96 | # Edge Case: Property key is not in uppercase |
| 97 | key = content[key_start:node_end] |
| 98 | if not key.isupper(): |
| 99 | raise ValueError("property must be in uppercase") |
| 100 | |
| 101 | # Edge Case: No properties |
| 102 | if not key: |
| 103 | raise ValueError("properties without delimiter") |
| 104 | |
| 105 | # Parse values |
| 106 | values = [] |
| 107 | while node_end < len(content) and content[node_end] == '[': |
| 108 | node_end += 1 # Skip '[' |
| 109 | value_start = node_end |
| 110 | |
| 111 | # Parse value, handling escapes |
| 112 | while node_end < len(content) and content[node_end] != ']': |
| 113 | if content[node_end] == '\\': |
| 114 | node_end += 2 # Skip escape and next character |
| 115 | else: |
| 116 | node_end += 1 |
| 117 | |
| 118 | # Edge Case: Unclosed bracket |
| 119 | if node_end >= len(content) or content[node_end] != ']': |
| 120 | raise ValueError("properties without delimiter") |
| 121 | |
| 122 | value = content[value_start:node_end] |
| 123 | # Process the value according to SGF text type rules |
| 124 | value = _process_text(value) |
| 125 | values.append(value) |
| 126 | node_end += 1 # Skip ']' |
| 127 | |
| 128 | # Edge Case: No values for a property |
| 129 | if not values: |
| 130 | raise ValueError("properties without delimiter") |
| 131 | |
| 132 | properties[key] = values |
| 133 | |
| 134 | children = [] |
| 135 | |
| 136 | # Parse children |
| 137 | while node_end < len(content) and content[node_end] == '(': |
| 138 | # Find matching parenthesis |
| 139 | paren_count = 1 |
| 140 | child_start = node_end |
| 141 | node_end += 1 |
| 142 | |
| 143 | while node_end < len(content) and paren_count > 0: |
| 144 | if content[node_end] == '(': |
| 145 | paren_count += 1 |
| 146 | elif content[node_end] == ')': |
| 147 | paren_count -= 1 |
| 148 | node_end += 1 |
| 149 | |
| 150 | # Edge Case: Mismatched parentheses |
| 151 | if paren_count != 0: |
| 152 | raise ValueError("tree missing") |
| 153 | |
| 154 | # Parse child tree |
| 155 | child_content = content[child_start:node_end] |
| 156 | child_tree = parse(child_content) # This will add the parentheses back |
| 157 | children.append(child_tree) |
| 158 | |
| 159 | # Handle sequence nodes (nodes that start with ';') |
| 160 | if node_end < len(content) and content[node_end] == ';': |
| 161 | # Parse the rest as a child |
| 162 | rest_content = content[node_end:] |
| 163 | child_tree = _parse_tree(rest_content) |
| 164 | children.append(child_tree) |
| 165 | |
| 166 | return SgfTree(properties, children) |
| 167 | |
| 168 | |
| 169 | def _process_text(text: str) -> str: |
| 170 | """ |
| 171 | Process text according to SGF text type rules. |
| 172 | |
| 173 | Args: |
| 174 | text: The raw text value from SGF |
| 175 | |
| 176 | Returns: |
| 177 | str: The processed text |
| 178 | """ |
| 179 | result = [] |
| 180 | i = 0 |
| 181 | while i < len(text): |
| 182 | if text[i] == '\\': |
| 183 | # Check if next character exists |
| 184 | if i + 1 < len(text): |
| 185 | next_char = text[i+1] |
| 186 | # If next character is newline, remove both |
| 187 | if next_char == '\n': |
| 188 | i += 2 # Skip backslash and newline |
| 189 | else: |
| 190 | # Any non-whitespace character after \ is inserted as-is |
| 191 | # Any whitespace character after \ is converted to space |
| 192 | if next_char.isspace(): |
| 193 | result.append(' ') |
| 194 | else: |
| 195 | result.append(next_char) |
| 196 | i += 2 |
| 197 | else: |
| 198 | # Edge Case: Escape at end of string |
| 199 | i += 1 |
| 200 | else: |
| 201 | char = text[i] |
| 202 | # All whitespace characters other than newline are converted to spaces |
| 203 | if char != '\n' and char.isspace(): |
| 204 | result.append(' ') |
| 205 | else: |
| 206 | result.append(char) |
| 207 | i += 1 |
| 208 | |
| 209 | return ''.join(result) |
| 210 | |
| 211 | # 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 is just ';', Property key is not in uppercase, No properties, Unclosed bracket, No values for a property, Mismatched parentheses, Escape at end of string |