| 26 | 26 | |
| 27 | 27 | |
| 28 | 28 | def parse(input_string: str) -> SgfTree: |
| 29 | | pass |
| 29 | # Edge Case: Empty input or doesn't start with '(' |
| 30 | if not input_string or input_string[0] != '(': |
| 31 | raise ValueError("tree missing") |
| 32 | |
| 33 | # Edge Case: Doesn't end with ')' |
| 34 | if input_string[-1] != ')': |
| 35 | raise ValueError("tree missing") |
| 36 | |
| 37 | # Remove the outer parentheses |
| 38 | content = input_string[1:-1] |
| 39 | |
| 40 | # Edge Case: Empty content after removing parentheses |
| 41 | if not content: |
| 42 | raise ValueError("tree with no nodes") |
| 43 | |
| 44 | # Edge Case: Doesn't start with ';' |
| 45 | if content[0] != ';': |
| 46 | raise ValueError("tree with no nodes") |
| 47 | |
| 48 | return _parse_tree(content) |
| 49 | |
| 50 | |
| 51 | def _parse_tree(content: str) -> SgfTree: |
| 52 | # Find the first node and its properties |
| 53 | if not content or content[0] != ';': |
| 54 | raise ValueError("tree with no nodes") |
| 55 | |
| 56 | # Parse the current node's properties |
| 57 | properties, remaining = _parse_properties(content[1:]) |
| 58 | |
| 59 | children = [] |
| 60 | |
| 61 | # Process any children |
| 62 | while remaining: |
| 63 | if remaining[0] == '(': # Start of a child tree |
| 64 | child_tree, remaining = _parse_subtree(remaining) |
| 65 | children.append(child_tree) |
| 66 | else: |
| 67 | # More properties in the same node (shorthand) |
| 68 | next_props, remaining = _parse_properties(remaining) |
| 69 | # Merge properties |
| 70 | for key, values in next_props.items(): |
| 71 | if key in properties: |
| 72 | properties[key].extend(values) |
| 73 | else: |
| 74 | properties[key] = values |
| 75 | |
| 76 | return SgfTree(properties, children) |
| 77 | |
| 78 | |
| 79 | def _parse_subtree(content: str) -> tuple[SgfTree, str]: |
| 80 | if not content or content[0] != '(': |
| 81 | raise ValueError("tree missing") |
| 82 | |
| 83 | # Find matching closing parenthesis |
| 84 | depth = 1 |
| 85 | i = 1 |
| 86 | while i < len(content) and depth > 0: |
| 87 | if content[i] == '(': |
| 88 | depth += 1 |
| 89 | elif content[i] == ')': |
| 90 | depth -= 1 |
| 91 | i += 1 |
| 92 | |
| 93 | # Edge Case: Unmatched parenthesis |
| 94 | if depth != 0: |
| 95 | raise ValueError("tree missing") |
| 96 | |
| 97 | # Parse the content inside the parentheses |
| 98 | inner_content = content[1:i-1] |
| 99 | tree = _parse_tree(inner_content) |
| 100 | |
| 101 | return tree, content[i:] |
| 102 | |
| 103 | |
| 104 | def _parse_properties(content: str) -> tuple[dict, str]: |
| 105 | properties = {} |
| 106 | i = 0 |
| 107 | |
| 108 | while i < len(content): |
| 109 | # Check if we've reached the end of properties for this node |
| 110 | if content[i] in '();': |
| 111 | break |
| 112 | |
| 113 | # Parse property key |
| 114 | key_start = i |
| 115 | while i < len(content) and content[i].isalpha() and content[i].isupper(): |
| 116 | i += 1 |
| 117 | |
| 118 | # Edge Case: No key found or key is not uppercase |
| 119 | if i == key_start: |
| 120 | raise ValueError("property must be in uppercase") |
| 121 | |
| 122 | key = content[key_start:i] |
| 123 | |
| 124 | # Edge Case: Key not followed by '[' |
| 125 | if i >= len(content) or content[i] != '[': |
| 126 | raise ValueError("properties without delimiter") |
| 127 | |
| 128 | # Parse values |
| 129 | values = [] |
| 130 | while i < len(content) and content[i] == '[': |
| 131 | i += 1 # Skip '[' |
| 132 | value_start = i |
| 133 | |
| 134 | # Parse value, handling escapes |
| 135 | value_chars = [] |
| 136 | while i < len(content) and content[i] != ']': |
| 137 | if content[i] == '\\': |
| 138 | i += 1 |
| 139 | # Edge Case: Escape at end of input |
| 140 | if i >= len(content): |
| 141 | raise ValueError("properties without delimiter") |
| 142 | |
| 143 | char = content[i] |
| 144 | if char == 'n': # \n should be treated as literal 'n' according to SGF spec |
| 145 | value_chars.append('\\n') |
| 146 | elif char == 't': # \t should be treated as literal 't' according to SGF spec |
| 147 | value_chars.append('\\t') |
| 148 | elif char == 'r': # \r should be treated as literal 'r' according to SGF spec |
| 149 | value_chars.append('\\r') |
| 150 | elif char == '\\': # \\ should be treated as literal '\' |
| 151 | value_chars.append('\\') |
| 152 | elif char.isspace(): |
| 153 | # According to SGF spec, whitespace after \ follows normal rules |
| 154 | if char == '\n': |
| 155 | # Newlines are removed if they come immediately after \ |
| 156 | pass |
| 157 | else: |
| 158 | # All other whitespace becomes space |
| 159 | value_chars.append(' ') |
| 160 | else: |
| 161 | # Any non-whitespace character after \ is inserted as-is |
| 162 | value_chars.append(char) |
| 163 | else: |
| 164 | char = content[i] |
| 165 | if char == '\n': |
| 166 | # Newlines remain as newlines |
| 167 | value_chars.append(char) |
| 168 | elif char.isspace() and char != '\n': |
| 169 | # All whitespace other than newline becomes space |
| 170 | value_chars.append(' ') |
| 171 | else: |
| 172 | value_chars.append(char) |
| 173 | i += 1 |
| 174 | |
| 175 | # Edge Case: Unmatched '[' |
| 176 | if i >= len(content) or content[i] != ']': |
| 177 | raise ValueError("properties without delimiter") |
| 178 | |
| 179 | value = ''.join(value_chars) |
| 180 | values.append(value) |
| 181 | i += 1 # Skip ']' |
| 182 | |
| 183 | properties[key] = values |
| 184 | |
| 185 | return properties, content[i:] |
| 186 | |
| 187 | # Handled Edge Cases: Empty input, missing parentheses, empty content after removing parentheses, missing semicolon, unmatched parenthesis, no key found, key not uppercase, key not followed by '[', escape at end of input, unmatched '[' |