| 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 | # Parse the content |
| 45 | return _parse_tree(content) |
| 46 | |
| 47 | |
| 48 | def _parse_tree(content: str) -> SgfTree: |
| 49 | # Find the first node |
| 50 | if not content or content[0] != ';': |
| 51 | raise ValueError("tree with no nodes") |
| 52 | |
| 53 | # Find the end of the first node |
| 54 | i = 1 |
| 55 | properties = {} |
| 56 | |
| 57 | # Parse properties |
| 58 | while i < len(content) and content[i] != '(' and content[i] != ';': |
| 59 | # Parse key |
| 60 | key_start = i |
| 61 | while i < len(content) and content[i].isalpha() and content[i].isupper(): |
| 62 | i += 1 |
| 63 | |
| 64 | # Edge Case: Key is not uppercase or empty |
| 65 | if i == key_start: |
| 66 | raise ValueError("property must be in uppercase") |
| 67 | |
| 68 | key = content[key_start:i] |
| 69 | |
| 70 | # Edge Case: No values for the property |
| 71 | if i >= len(content) or content[i] != '[': |
| 72 | raise ValueError("properties without delimiter") |
| 73 | |
| 74 | # Parse values |
| 75 | values = [] |
| 76 | while i < len(content) and content[i] == '[': |
| 77 | i += 1 # Skip '[' |
| 78 | value_start = i |
| 79 | |
| 80 | # Parse value with escape handling |
| 81 | value_chars = [] |
| 82 | while i < len(content) and content[i] != ']': |
| 83 | if content[i] == '\\': |
| 84 | i += 1 |
| 85 | if i < len(content): |
| 86 | # Handle escaped characters according to SGF Text type rules |
| 87 | if content[i] == 'n' and (i+1 >= len(content) or content[i+1] == ']' or content[i+1].isspace()): |
| 88 | # Special case for \n at end or before whitespace |
| 89 | value_chars.append('\\n') |
| 90 | elif content[i] == 't' and (i+1 >= len(content) or content[i+1] == ']' or content[i+1].isspace()): |
| 91 | # Special case for \t at end or before whitespace |
| 92 | value_chars.append('\\t') |
| 93 | else: |
| 94 | value_chars.append(content[i]) |
| 95 | else: |
| 96 | # Edge Case: Escape at end of string |
| 97 | raise ValueError("properties without delimiter") |
| 98 | else: |
| 99 | value_chars.append(content[i]) |
| 100 | i += 1 |
| 101 | |
| 102 | # Edge Case: Unclosed value bracket |
| 103 | if i >= len(content) or content[i] != ']': |
| 104 | raise ValueError("properties without delimiter") |
| 105 | |
| 106 | # Process the value according to SGF Text type rules |
| 107 | value = _process_sgf_text(''.join(value_chars)) |
| 108 | values.append(value) |
| 109 | i += 1 # Skip ']' |
| 110 | |
| 111 | properties[key] = values |
| 112 | |
| 113 | # Parse children |
| 114 | children = [] |
| 115 | while i < len(content) and content[i] == '(': |
| 116 | # Find matching parenthesis |
| 117 | paren_count = 1 |
| 118 | start = i + 1 |
| 119 | i += 1 |
| 120 | |
| 121 | while i < len(content) and paren_count > 0: |
| 122 | if content[i] == '(': |
| 123 | paren_count += 1 |
| 124 | elif content[i] == ')': |
| 125 | paren_count -= 1 |
| 126 | i += 1 |
| 127 | |
| 128 | # Edge Case: Unmatched parenthesis |
| 129 | if paren_count != 0: |
| 130 | raise ValueError("tree missing") |
| 131 | |
| 132 | # Parse child tree |
| 133 | child_content = content[start:i-1] |
| 134 | children.append(_parse_tree(child_content)) |
| 135 | |
| 136 | # Handle any remaining nodes in sequence |
| 137 | if i < len(content) and content[i] == ';': |
| 138 | # This is a sequential node, create a child with it |
| 139 | remaining_content = content[i:] |
| 140 | children.append(_parse_tree(remaining_content)) |
| 141 | |
| 142 | return SgfTree(properties, children) |
| 143 | |
| 144 | |
| 145 | def _process_sgf_text(text: str) -> str: |
| 146 | """Process SGF Text type according to specifications""" |
| 147 | result = [] |
| 148 | i = 0 |
| 149 | while i < len(text): |
| 150 | if text[i] == '\\': |
| 151 | i += 1 |
| 152 | if i < len(text): |
| 153 | # Check if next character is newline |
| 154 | if text[i] == 'n' and (i+1 >= len(text) or text[i+1] == '\n'): |
| 155 | # Handle \n sequence |
| 156 | i += 1 |
| 157 | if i < len(text) and text[i] == '\n': |
| 158 | # Skip the actual newline as per SGF spec |
| 159 | pass |
| 160 | else: |
| 161 | # Put back the n if it wasn't followed by actual newline |
| 162 | result.append('n') |
| 163 | elif text[i] == 't': |
| 164 | # Handle \t sequence |
| 165 | result.append('\t') |
| 166 | i += 1 |
| 167 | elif text[i].isspace(): |
| 168 | # Any whitespace after \ is inserted as-is |
| 169 | result.append(text[i]) |
| 170 | i += 1 |
| 171 | else: |
| 172 | # Any non-whitespace character after \ is inserted as-is |
| 173 | result.append(text[i]) |
| 174 | i += 1 |
| 175 | else: |
| 176 | # Trailing backslash - should not happen in valid SGF but we'll ignore it |
| 177 | pass |
| 178 | elif text[i] == '\n': |
| 179 | # Newlines are removed if they come immediately after a \, otherwise remain |
| 180 | # Since we're processing after escape handling, this newline should remain |
| 181 | result.append(text[i]) |
| 182 | i += 1 |
| 183 | elif text[i].isspace() and text[i] != '\n': |
| 184 | # All whitespace other than newline converted to spaces |
| 185 | result.append(' ') |
| 186 | i += 1 |
| 187 | else: |
| 188 | result.append(text[i]) |
| 189 | i += 1 |
| 190 | |
| 191 | return ''.join(result) |
| 192 | # Handled Edge Cases: Empty input, missing parentheses, empty content, missing semicolon, lowercase properties, missing delimiters, unclosed brackets, unmatched parentheses, trailing escape characters |