| 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 the content of a tree (without outer parentheses). |
| 70 | |
| 71 | Args: |
| 72 | content: The content of the tree |
| 73 | |
| 74 | Returns: |
| 75 | SgfTree: The parsed tree |
| 76 | """ |
| 77 | # Edge Case: Empty content |
| 78 | if not content: |
| 79 | raise ValueError("tree with no nodes") |
| 80 | |
| 81 | # Edge Case: Content doesn't start with ';' |
| 82 | if not content.startswith(';'): |
| 83 | raise ValueError("tree missing") |
| 84 | |
| 85 | # Find the end of the current node |
| 86 | i = 1 # Skip the initial ';' |
| 87 | properties = {} |
| 88 | |
| 89 | # Parse properties |
| 90 | while i < len(content) and content[i] != '(' and content[i] != ';': |
| 91 | # Parse key |
| 92 | key_start = i |
| 93 | while i < len(content) and content[i].isalpha(): |
| 94 | i += 1 |
| 95 | |
| 96 | # Edge Case: Key is empty |
| 97 | if i == key_start: |
| 98 | raise ValueError("properties without delimiter") |
| 99 | |
| 100 | key = content[key_start:i] |
| 101 | |
| 102 | # Edge Case: Key is not uppercase |
| 103 | if not key.isupper(): |
| 104 | raise ValueError("property must be in uppercase") |
| 105 | |
| 106 | # Parse values |
| 107 | values = [] |
| 108 | |
| 109 | # Edge Case: No values after key |
| 110 | if i >= len(content) or content[i] != '[': |
| 111 | raise ValueError("properties without delimiter") |
| 112 | |
| 113 | while i < len(content) and content[i] == '[': |
| 114 | i += 1 # Skip '[' |
| 115 | value_start = i |
| 116 | |
| 117 | # Parse value |
| 118 | value_chars = [] |
| 119 | while i < len(content) and content[i] != ']': |
| 120 | if content[i] == '\\': |
| 121 | # Handle escape character |
| 122 | i += 1 |
| 123 | if i < len(content): |
| 124 | if content[i] == 'n': |
| 125 | # Special case: \n removes both backslash and n |
| 126 | i += 1 |
| 127 | continue |
| 128 | elif content[i] in (' ', '\t', '\r'): |
| 129 | # Whitespace characters other than newline become spaces |
| 130 | value_chars.append(' ') |
| 131 | i += 1 |
| 132 | else: |
| 133 | # Any other character after backslash is inserted as-is |
| 134 | value_chars.append('\\') |
| 135 | value_chars.append(content[i]) |
| 136 | i += 1 |
| 137 | else: |
| 138 | # Edge Case: Escape character at end of input |
| 139 | raise ValueError("properties without delimiter") |
| 140 | else: |
| 141 | # Handle regular characters |
| 142 | char = content[i] |
| 143 | if char == '\n': |
| 144 | # Newlines remain as newlines |
| 145 | value_chars.append(char) |
| 146 | elif char in (' ', '\t', '\r'): |
| 147 | # All whitespace other than newline becomes space |
| 148 | value_chars.append(' ') |
| 149 | else: |
| 150 | value_chars.append(char) |
| 151 | i += 1 |
| 152 | |
| 153 | # Edge Case: Unclosed value |
| 154 | if i >= len(content) or content[i] != ']': |
| 155 | raise ValueError("properties without delimiter") |
| 156 | |
| 157 | value = ''.join(value_chars) |
| 158 | values.append(value) |
| 159 | i += 1 # Skip ']' |
| 160 | |
| 161 | properties[key] = values |
| 162 | |
| 163 | # Parse children |
| 164 | children = [] |
| 165 | |
| 166 | while i < len(content) and content[i] == '(': |
| 167 | # Find matching closing parenthesis |
| 168 | paren_count = 1 |
| 169 | start = i + 1 |
| 170 | i += 1 |
| 171 | |
| 172 | while i < len(content) and paren_count > 0: |
| 173 | if content[i] == '(': |
| 174 | paren_count += 1 |
| 175 | elif content[i] == ')': |
| 176 | paren_count -= 1 |
| 177 | i += 1 |
| 178 | |
| 179 | # Edge Case: Unmatched parenthesis |
| 180 | if paren_count != 0: |
| 181 | raise ValueError("tree missing") |
| 182 | |
| 183 | # Extract child content (excluding the outer parentheses) |
| 184 | child_content = content[start:i-1] |
| 185 | |
| 186 | # Parse child tree |
| 187 | child_tree = _parse_tree(child_content) |
| 188 | children.append(child_tree) |
| 189 | |
| 190 | # Check for additional nodes at the same level |
| 191 | if i < len(content) and content[i] == ';': |
| 192 | # This is a shorthand for a single child |
| 193 | child_content = content[i:] |
| 194 | child_tree = _parse_tree(child_content) |
| 195 | children.append(child_tree) |
| 196 | |
| 197 | return SgfTree(properties, children) |
| 198 | # 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 ';', Key is empty, Key is not uppercase, No values after key, Unclosed value, Escape character at end of input, Unmatched parenthesis" |