| 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 data |
| 34 | |
| 35 | Returns: |
| 36 | SgfTree: A tree structure representing the SGF data |
| 37 | |
| 38 | Raises: |
| 39 | ValueError: If the input is malformed or doesn't conform to SGF format |
| 40 | """ |
| 41 | # Edge Case: Empty input or input that doesn't start with '(' |
| 42 | if not input_string or not input_string.startswith('('): |
| 43 | raise ValueError("tree missing") |
| 44 | |
| 45 | # Edge Case: Input that starts with '(' but doesn't end with ')' |
| 46 | if not input_string.endswith(')'): |
| 47 | raise ValueError("tree missing") |
| 48 | |
| 49 | # Remove the outer parentheses |
| 50 | content = input_string[1:-1] |
| 51 | |
| 52 | # Edge Case: Empty content after removing parentheses |
| 53 | if not content: |
| 54 | raise ValueError("tree with no nodes") |
| 55 | |
| 56 | # Edge Case: Content that doesn't start with ';' |
| 57 | if not content.startswith(';'): |
| 58 | raise ValueError("tree with no nodes") |
| 59 | |
| 60 | return _parse_tree(content) |
| 61 | |
| 62 | |
| 63 | def _parse_tree(content: str) -> SgfTree: |
| 64 | """ |
| 65 | Parse a tree from SGF content. |
| 66 | |
| 67 | Args: |
| 68 | content: SGF content string starting with ';' |
| 69 | |
| 70 | Returns: |
| 71 | SgfTree: Parsed tree structure |
| 72 | """ |
| 73 | # Skip the initial ';' |
| 74 | index = 1 |
| 75 | properties = {} |
| 76 | children = [] |
| 77 | |
| 78 | # Parse properties |
| 79 | while index < len(content) and content[index] not in '();': |
| 80 | # Parse key |
| 81 | key_start = index |
| 82 | while index < len(content) and content[index].isalpha(): |
| 83 | index += 1 |
| 84 | |
| 85 | # Edge Case: Key is not all uppercase |
| 86 | key = content[key_start:index] |
| 87 | if not key.isupper(): |
| 88 | raise ValueError("property must be in uppercase") |
| 89 | |
| 90 | # Edge Case: Key without values |
| 91 | if index >= len(content) or content[index] != '[': |
| 92 | raise ValueError("properties without delimiter") |
| 93 | |
| 94 | # Parse values |
| 95 | values = [] |
| 96 | while index < len(content) and content[index] == '[': |
| 97 | index += 1 # Skip '[' |
| 98 | value_start = index |
| 99 | |
| 100 | # Parse value, handling escapes |
| 101 | while index < len(content) and content[index] != ']': |
| 102 | if content[index] == '\\': |
| 103 | index += 2 # Skip escape and next character |
| 104 | else: |
| 105 | index += 1 |
| 106 | |
| 107 | # Edge Case: Unclosed value bracket |
| 108 | if index >= len(content): |
| 109 | raise ValueError("properties without delimiter") |
| 110 | |
| 111 | value = _parse_value(content[value_start:index]) |
| 112 | values.append(value) |
| 113 | index += 1 # Skip ']' |
| 114 | |
| 115 | properties[key] = values |
| 116 | |
| 117 | # Parse children |
| 118 | while index < len(content) and content[index] == '(': |
| 119 | # Find matching parenthesis |
| 120 | paren_count = 1 |
| 121 | start = index + 1 |
| 122 | index += 1 |
| 123 | |
| 124 | while index < len(content) and paren_count > 0: |
| 125 | if content[index] == '(': |
| 126 | paren_count += 1 |
| 127 | elif content[index] == ')': |
| 128 | paren_count -= 1 |
| 129 | index += 1 |
| 130 | |
| 131 | # Edge Case: Unmatched parenthesis |
| 132 | if paren_count != 0: |
| 133 | raise ValueError("tree missing") |
| 134 | |
| 135 | child_content = content[start:index-1] |
| 136 | children.append(_parse_tree(child_content)) |
| 137 | |
| 138 | return SgfTree(properties, children) |
| 139 | |
| 140 | |
| 141 | def _parse_value(value: str) -> str: |
| 142 | """ |
| 143 | Parse an SGF value, handling escape sequences and whitespace. |
| 144 | |
| 145 | Args: |
| 146 | value: Raw SGF value string |
| 147 | |
| 148 | Returns: |
| 149 | str: Processed value according to SGF text type rules |
| 150 | """ |
| 151 | result = [] |
| 152 | i = 0 |
| 153 | |
| 154 | while i < len(value): |
| 155 | if value[i] == '\\': |
| 156 | # Handle escape sequences |
| 157 | i += 1 |
| 158 | if i < len(value): |
| 159 | # If next character is newline, remove it |
| 160 | if value[i] == '\n': |
| 161 | i += 1 |
| 162 | else: |
| 163 | # Insert any non-whitespace character as-is |
| 164 | # For whitespace characters, convert to space |
| 165 | if value[i].isspace() and value[i] != '\n': |
| 166 | result.append(' ') |
| 167 | i += 1 |
| 168 | else: |
| 169 | result.append(value[i]) |
| 170 | i += 1 |
| 171 | else: |
| 172 | # Convert all whitespace (except newline) to spaces |
| 173 | if value[i].isspace() and value[i] != '\n': |
| 174 | result.append(' ') |
| 175 | i += 1 |
| 176 | else: |
| 177 | result.append(value[i]) |
| 178 | i += 1 |
| 179 | |
| 180 | return ''.join(result) |
| 181 | |
| 182 | # Handled Edge Cases: Empty input, missing opening parenthesis, missing closing parenthesis, |
| 183 | # empty content after removing parentheses, missing semicolon, key not in uppercase, |
| 184 | # key without values, unclosed value bracket, unmatched parenthesis |
| 185 | # Handled Edge Cases: Key not in uppercase, key without values |
| 186 | # Handled Edge Cases: Unclosed value bracket |
| 187 | # Handled Edge Cases: Unmatched parenthesis |