| 24 | 24 | def __ne__(self, other): |
| 25 | 25 | return not self == other |
| 26 | 26 | |
| 27 | def _process_sgf_text(text: str) -> str: |
| 28 | """Process SGF text according to the rules""" |
| 29 | result = [] |
| 30 | i = 0 |
| 31 | while i < len(text): |
| 32 | if text[i] == '\\': |
| 33 | i += 1 |
| 34 | if i >= len(text): |
| 35 | # Trailing backslash is just a backslash |
| 36 | result.append('\\') |
| 37 | break |
| 38 | |
| 39 | char = text[i] |
| 40 | if char == '\n': |
| 41 | # Newline immediately after \\ is removed |
| 42 | pass |
| 43 | elif char.isspace(): |
| 44 | # Other whitespace after \\ is converted to space |
| 45 | result.append(' ') |
| 46 | else: |
| 47 | # Any non-whitespace character after \\ is inserted as-is |
| 48 | result.append(char) |
| 49 | else: |
| 50 | if text[i] == '\n': |
| 51 | result.append('\n') |
| 52 | elif text[i].isspace() and text[i] != '\n': |
| 53 | result.append(' ') |
| 54 | else: |
| 55 | result.append(text[i]) |
| 56 | i += 1 |
| 57 | |
| 58 | return ''.join(result) |
| 59 | |
| 27 | 60 | |
| 28 | 61 | def parse(input_string: str) -> SgfTree: |
| 29 | | pass |
| 62 | # Edge Case: Empty input |
| 63 | if not input_string: |
| 64 | raise ValueError("tree missing") |
| 65 | |
| 66 | # Edge Case: Input doesn't start with '(' |
| 67 | if not input_string.startswith('('): |
| 68 | raise ValueError("tree missing") |
| 69 | |
| 70 | # Edge Case: Input doesn't end with ')' |
| 71 | if not input_string.endswith(')'): |
| 72 | raise ValueError("tree missing") |
| 73 | |
| 74 | # Remove the outer parentheses |
| 75 | content = input_string[1:-1] |
| 76 | |
| 77 | # Edge Case: Empty tree with no nodes |
| 78 | if not content: |
| 79 | raise ValueError("tree with no nodes") |
| 80 | |
| 81 | # Parse the tree |
| 82 | tree, _ = _parse_tree(content, 0) |
| 83 | return tree |
| 84 | |
| 85 | |
| 86 | def _parse_tree(content: str, index: int) -> tuple[SgfTree, int]: |
| 87 | # Edge Case: Unexpected end of content |
| 88 | if index >= len(content): |
| 89 | raise ValueError("tree missing") |
| 90 | |
| 91 | # Edge Case: Node doesn't start with ';' |
| 92 | if content[index] != ';': |
| 93 | raise ValueError("tree missing") |
| 94 | |
| 95 | index += 1 # Skip the ';' |
| 96 | |
| 97 | # Parse properties |
| 98 | properties = {} |
| 99 | while index < len(content) and content[index].isalpha(): |
| 100 | # Parse key |
| 101 | key_start = index |
| 102 | while index < len(content) and content[index].isalpha(): |
| 103 | if not content[index].isupper(): |
| 104 | raise ValueError("property must be in uppercase") |
| 105 | index += 1 |
| 106 | |
| 107 | # Edge Case: Key is empty |
| 108 | if index == key_start: |
| 109 | raise ValueError("properties without delimiter") |
| 110 | |
| 111 | key = content[key_start:index] |
| 112 | |
| 113 | # Parse values |
| 114 | values = [] |
| 115 | while index < len(content) and content[index] == '[': |
| 116 | index += 1 # Skip '[' |
| 117 | |
| 118 | # Parse value - collect raw characters including backslashes |
| 119 | value_parts = [] |
| 120 | while index < len(content) and content[index] != ']': |
| 121 | value_parts.append(content[index]) |
| 122 | index += 1 |
| 123 | |
| 124 | # Edge Case: Unclosed value bracket |
| 125 | if index >= len(content) or content[index] != ']': |
| 126 | raise ValueError("properties without delimiter") |
| 127 | |
| 128 | value = ''.join(value_parts) |
| 129 | # Process according to SGF text rules |
| 130 | processed_value = _process_sgf_text(value) |
| 131 | # Split by newlines to create multiple values if needed |
| 132 | if '\n' in processed_value: |
| 133 | split_values = processed_value.split('\n') |
| 134 | values.extend(split_values) |
| 135 | else: |
| 136 | values.append(processed_value) |
| 137 | index += 1 # Skip ']' |
| 138 | |
| 139 | # Edge Case: No values for a property |
| 140 | if not values: |
| 141 | raise ValueError("properties without delimiter") |
| 142 | |
| 143 | properties[key] = values |
| 144 | |
| 145 | # Parse children (both sequential nodes and variations) |
| 146 | children = [] |
| 147 | |
| 148 | # Handle sequential nodes (starting with ';') |
| 149 | while index < len(content) and content[index] == ';': |
| 150 | child, index = _parse_tree(content, index) |
| 151 | children.append(child) |
| 152 | |
| 153 | # Handle variations (starting with '(') |
| 154 | while index < len(content) and content[index] == '(': |
| 155 | child, index = _parse_tree(content, index+1) |
| 156 | children.append(child) |
| 157 | # Skip the closing ')' |
| 158 | if index >= len(content) or content[index] != ')': |
| 159 | raise ValueError("tree missing") |
| 160 | index += 1 |
| 161 | |
| 162 | return SgfTree(properties, children), index |
| 163 | |
| 164 | |
| 165 | |
| 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 property, Trailing backslash, Newline immediately after backslash, Whitespace after backslash, Non-whitespace character after backslash |