| 26 | 26 | |
| 27 | 27 | |
| 28 | 28 | def parse(input_string: str) -> SgfTree: |
| 29 | | pass |
| 29 | """ |
| 30 | Parse an SGF (Smart Game Format) string and return a tree structure of properties. |
| 31 | |
| 32 | Args: |
| 33 | input_string: A string containing SGF formatted data |
| 34 | |
| 35 | Returns: |
| 36 | SgfTree: A tree structure representing the parsed SGF data |
| 37 | |
| 38 | Raises: |
| 39 | ValueError: If the input string is invalid SGF format |
| 40 | """ |
| 41 | # Edge Case: Empty input or doesn't start with '(' |
| 42 | if not input_string or input_string[0] != '(': |
| 43 | raise ValueError("tree missing") |
| 44 | |
| 45 | # Edge Case: Doesn't end with ')' - but check if it's just "(" which should be "tree with no nodes" |
| 46 | if input_string[-1] != ')': |
| 47 | if input_string == "(": # Special case: just opening parenthesis |
| 48 | raise ValueError("tree with no nodes") |
| 49 | else: |
| 50 | raise ValueError("tree missing") |
| 51 | |
| 52 | # Edge Case: Only parentheses with no content - this is "tree missing" not "tree with no nodes" |
| 53 | if len(input_string) <= 2: |
| 54 | raise ValueError("tree missing") |
| 55 | |
| 56 | # Remove outer parentheses |
| 57 | content = input_string[1:-1] |
| 58 | |
| 59 | return _parse_tree(content) |
| 60 | |
| 61 | |
| 62 | def _parse_tree(content: str) -> SgfTree: |
| 63 | """ |
| 64 | Parse the content inside the outer parentheses of an SGF string. |
| 65 | |
| 66 | Args: |
| 67 | content: SGF content without outer parentheses |
| 68 | |
| 69 | Returns: |
| 70 | SgfTree: Parsed tree structure |
| 71 | """ |
| 72 | # Edge Case: Empty content after removing parentheses |
| 73 | if not content: |
| 74 | raise ValueError("tree with no nodes") |
| 75 | |
| 76 | # Edge Case: Doesn't start with semicolon (node marker) |
| 77 | if content[0] != ';': |
| 78 | raise ValueError("tree with no nodes") |
| 79 | |
| 80 | i = 1 # Skip the initial semicolon |
| 81 | properties = {} |
| 82 | children = [] |
| 83 | |
| 84 | # Parse properties of the current node |
| 85 | while i < len(content) and content[i] != '(' and content[i] != ';': |
| 86 | # Parse property key |
| 87 | key_start = i |
| 88 | while i < len(content) and content[i].isalpha(): |
| 89 | i += 1 |
| 90 | |
| 91 | # Edge Case: No key found |
| 92 | if i == key_start: |
| 93 | raise ValueError("properties without delimiter") |
| 94 | |
| 95 | key = content[key_start:i] |
| 96 | |
| 97 | # Edge Case: Key is not uppercase |
| 98 | if key != key.upper() or not key.isalpha(): |
| 99 | raise ValueError("property must be in uppercase") |
| 100 | |
| 101 | # Parse property values |
| 102 | values = [] |
| 103 | |
| 104 | # Edge Case: No values after key |
| 105 | if i >= len(content) or content[i] != '[': |
| 106 | raise ValueError("properties without delimiter") |
| 107 | |
| 108 | while i < len(content) and content[i] == '[': |
| 109 | i += 1 # Skip '[' |
| 110 | value_start = i |
| 111 | |
| 112 | # Parse value, handling escapes |
| 113 | while i < len(content) and content[i] != ']': |
| 114 | if content[i] == '\\': |
| 115 | i += 2 # Skip escape character and the escaped character |
| 116 | else: |
| 117 | i += 1 |
| 118 | |
| 119 | # Edge Case: Unclosed value bracket |
| 120 | if i >= len(content): |
| 121 | raise ValueError("properties without delimiter") |
| 122 | |
| 123 | value = content[value_start:i] |
| 124 | value = _unescape_value(value) |
| 125 | values.append(value) |
| 126 | i += 1 # Skip ']' |
| 127 | |
| 128 | properties[key] = values |
| 129 | |
| 130 | # Parse children - handle both variations (in parentheses) and sequential nodes (with semicolons) |
| 131 | while i < len(content): |
| 132 | if content[i] == '(': |
| 133 | # Find matching closing parenthesis for variation |
| 134 | paren_count = 1 |
| 135 | start = i + 1 |
| 136 | i += 1 |
| 137 | |
| 138 | while i < len(content) and paren_count > 0: |
| 139 | if content[i] == '(': |
| 140 | paren_count += 1 |
| 141 | elif content[i] == ')': |
| 142 | paren_count -= 1 |
| 143 | i += 1 |
| 144 | |
| 145 | # Edge Case: Unmatched parenthesis |
| 146 | if paren_count != 0: |
| 147 | raise ValueError("tree missing") |
| 148 | |
| 149 | # Extract child content (excluding the closing parenthesis) |
| 150 | child_content = content[start:i-1] |
| 151 | children.append(_parse_tree(child_content)) |
| 152 | elif content[i] == ';': |
| 153 | # Handle sequential node - create a new node with properties that follow |
| 154 | # For sequential nodes, we need to parse just the next node, not the rest |
| 155 | # This is a bit tricky - we need to find where this sequential node ends |
| 156 | # It ends when we hit another '(' or ';' or the end of content |
| 157 | node_end = i + 1 |
| 158 | while node_end < len(content) and content[node_end] != '(' and content[node_end] != ';': |
| 159 | node_end += 1 |
| 160 | |
| 161 | # Parse just this node |
| 162 | node_content = content[i:node_end] |
| 163 | children.append(_parse_tree(node_content)) |
| 164 | |
| 165 | # Move to the end of this node |
| 166 | i = node_end |
| 167 | continue |
| 168 | else: |
| 169 | i += 1 |
| 170 | |
| 171 | return SgfTree(properties, children) |
| 172 | |
| 173 | |
| 174 | def _unescape_value(value: str) -> str: |
| 175 | """ |
| 176 | Process escape sequences in SGF text values according to SGF specification. |
| 177 | |
| 178 | Args: |
| 179 | value: Raw SGF text value |
| 180 | |
| 181 | Returns: |
| 182 | str: Processed text value |
| 183 | """ |
| 184 | result = [] |
| 185 | i = 0 |
| 186 | |
| 187 | while i < len(value): |
| 188 | if value[i] == '\\': |
| 189 | # Edge Case: Escape at end of string |
| 190 | if i + 1 >= len(value): |
| 191 | result.append('\\') |
| 192 | break |
| 193 | |
| 194 | next_char = value[i + 1] |
| 195 | |
| 196 | # Handle newline removal after backslash |
| 197 | if next_char == '\n': |
| 198 | # Skip the backslash and newline |
| 199 | i += 2 |
| 200 | continue |
| 201 | |
| 202 | # For escaped whitespace, convert to space (except newline) |
| 203 | if next_char.isspace() and next_char != '\n': |
| 204 | result.append(' ') |
| 205 | i += 2 |
| 206 | else: |
| 207 | # Insert non-whitespace character as-is |
| 208 | result.append(next_char) |
| 209 | i += 2 |
| 210 | else: |
| 211 | # Convert unescaped whitespace characters (except newlines) to spaces |
| 212 | if value[i].isspace() and value[i] != '\n': |
| 213 | result.append(' ') |
| 214 | else: |
| 215 | # Keep all other characters as they are |
| 216 | result.append(value[i]) |
| 217 | i += 1 |
| 218 | |
| 219 | return ''.join(result) |
| 220 | |
| 221 | # Handled Edge Cases: Empty input, missing opening parenthesis, missing closing parenthesis, |
| 222 | # only parentheses with no content, empty content after removing parentheses, |
| 223 | # missing semicolon at node start, no key found, key not uppercase, |
| 224 | # no values after key, unclosed value bracket, unmatched parenthesis, |
| 225 | # escape at end of string |