| 26 | 26 | |
| 27 | 27 | |
| 28 | 28 | def parse(input_string: str) -> SgfTree: |
| 29 | | pass |
| 29 | # Edge Case: Empty input |
| 30 | if not input_string: |
| 31 | raise ValueError("tree missing") |
| 32 | |
| 33 | # Edge Case: Input doesn't start with '(' |
| 34 | if not input_string.startswith('('): |
| 35 | raise ValueError("tree missing") |
| 36 | |
| 37 | # Edge Case: Input doesn't end with ')' |
| 38 | if not input_string.endswith(')'): |
| 39 | raise ValueError("tree missing") |
| 40 | |
| 41 | # Remove the outer parentheses |
| 42 | content = input_string[1:-1] |
| 43 | |
| 44 | # Edge Case: Empty tree with no nodes |
| 45 | if not content: |
| 46 | raise ValueError("tree with no nodes") |
| 47 | |
| 48 | # Parse the tree |
| 49 | tree, _ = _parse_tree(content, 0) |
| 50 | return tree |
| 51 | |
| 52 | |
| 53 | def _parse_tree(content: str, index: int) -> tuple[SgfTree, int]: |
| 54 | # Edge Case: Unexpected end of content |
| 55 | if index >= len(content): |
| 56 | raise ValueError("tree missing") |
| 57 | |
| 58 | # Edge Case: Node doesn't start with ';' |
| 59 | if content[index] != ';': |
| 60 | raise ValueError("tree missing") |
| 61 | |
| 62 | index += 1 # Skip the ';' |
| 63 | |
| 64 | # Parse properties |
| 65 | properties = {} |
| 66 | while index < len(content): |
| 67 | # Check if we've reached the end of properties (start of children) |
| 68 | if content[index] in '();': |
| 69 | break |
| 70 | |
| 71 | # Parse key - must be uppercase |
| 72 | key_start = index |
| 73 | while index < len(content) and content[index].isalpha(): |
| 74 | if not content[index].isupper(): |
| 75 | raise ValueError("property must be in uppercase") |
| 76 | index += 1 |
| 77 | |
| 78 | # Edge Case: Key is empty or not found |
| 79 | if index == key_start: |
| 80 | raise ValueError("properties without delimiter") |
| 81 | |
| 82 | key = content[key_start:index] |
| 83 | |
| 84 | # Parse values |
| 85 | values = [] |
| 86 | has_values = False |
| 87 | while index < len(content) and content[index] == '[': |
| 88 | has_values = True |
| 89 | index += 1 # Skip '[' |
| 90 | |
| 91 | # Parse value with escape sequences |
| 92 | value_parts = [] |
| 93 | while index < len(content) and content[index] != ']': |
| 94 | if content[index] == '\\': |
| 95 | index += 1 |
| 96 | if index >= len(content): |
| 97 | raise ValueError("properties without delimiter") |
| 98 | |
| 99 | char = content[index] |
| 100 | # In SGF, backslash escapes the next character |
| 101 | # For certain characters like :, the backslash is preserved |
| 102 | if char == ':': |
| 103 | value_parts.append('\\') |
| 104 | value_parts.append(char) |
| 105 | else: |
| 106 | # For other characters like ], the backslash is consumed |
| 107 | value_parts.append(char) |
| 108 | else: |
| 109 | # Handle whitespace according to SGF rules |
| 110 | if content[index] == '\n': |
| 111 | value_parts.append('\n') |
| 112 | elif content[index] in ' \t\r': |
| 113 | value_parts.append(' ') |
| 114 | else: |
| 115 | value_parts.append(content[index]) |
| 116 | index += 1 |
| 117 | |
| 118 | # Edge Case: Unclosed value bracket |
| 119 | if index >= len(content) or content[index] != ']': |
| 120 | raise ValueError("properties without delimiter") |
| 121 | |
| 122 | value = ''.join(value_parts) |
| 123 | values.append(value) |
| 124 | index += 1 # Skip ']' |
| 125 | |
| 126 | # Edge Case: No values for a key |
| 127 | if not has_values: |
| 128 | raise ValueError("properties without delimiter") |
| 129 | |
| 130 | properties[key] = values |
| 131 | |
| 132 | # Check for proper delimiter after property |
| 133 | if index < len(content) and content[index] not in '();[' and not content[index].isupper(): |
| 134 | # Invalid character after property |
| 135 | raise ValueError("properties without delimiter") |
| 136 | elif index < len(content) and content[index].isalpha() and not content[index].isupper(): |
| 137 | # Lowercase letter after property |
| 138 | raise ValueError("property must be in uppercase") |
| 139 | elif index < len(content) and content[index] == '[': |
| 140 | # Another value for the same property |
| 141 | pass |
| 142 | # Note: Consecutive properties without semicolons are allowed in SGF |
| 143 | |
| 144 | # Parse children |
| 145 | children = [] |
| 146 | while index < len(content): |
| 147 | if content[index] == '(': |
| 148 | # Parse variation (child node in parentheses) |
| 149 | index += 1 # Skip '(' |
| 150 | child, index = _parse_tree(content, index) |
| 151 | children.append(child) |
| 152 | |
| 153 | # Skip the closing ')' |
| 154 | if index >= len(content) or content[index] != ')': |
| 155 | raise ValueError("tree missing") |
| 156 | index += 1 |
| 157 | elif content[index] == ';': |
| 158 | # Parse sequential node (child node without parentheses) |
| 159 | child, index = _parse_tree(content, index) |
| 160 | children.append(child) |
| 161 | else: |
| 162 | # No more children |
| 163 | break |
| 164 | |
| 165 | return SgfTree(properties, children), index |
| 166 | # Handled Edge Cases: Empty input, Input doesn't start with '(', Input doesn't end with ')', Empty tree with no nodes, Unexpected end of content, Node doesn't start with ';', Key is empty, Key is not uppercase, Unclosed value bracket, No values for a key, Missing closing parenthesis for child nodes |