| 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 SGF data |
| 37 | |
| 38 | Raises: |
| 39 | ValueError: If the input is malformed according to SGF specifications |
| 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 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 | # Parse the tree |
| 57 | tree, _ = _parse_tree(content, 0) |
| 58 | return tree |
| 59 | |
| 60 | |
| 61 | def _parse_tree(content: str, index: int) -> tuple[SgfTree, int]: |
| 62 | """ |
| 63 | Parse a tree from the content string starting at the given index. |
| 64 | |
| 65 | Args: |
| 66 | content: The content string to parse |
| 67 | index: The starting index for parsing |
| 68 | |
| 69 | Returns: |
| 70 | tuple: A tuple containing the parsed SgfTree and the next index to parse |
| 71 | """ |
| 72 | # Edge Case: Content doesn't start with ';' |
| 73 | if index >= len(content) or content[index] != ';': |
| 74 | raise ValueError("tree with no nodes") |
| 75 | |
| 76 | index += 1 # Skip the ';' |
| 77 | |
| 78 | # Parse properties |
| 79 | properties = {} |
| 80 | while index < len(content) and content[index] not in '();': |
| 81 | # Parse key |
| 82 | key_start = index |
| 83 | while index < len(content) and content[index].isalpha(): |
| 84 | index += 1 |
| 85 | |
| 86 | # Edge Case: No key found |
| 87 | if index == key_start: |
| 88 | raise ValueError("properties without delimiter") |
| 89 | |
| 90 | key = content[key_start:index] |
| 91 | |
| 92 | # Edge Case: Key is not uppercase |
| 93 | if not key.isupper(): |
| 94 | raise ValueError("property must be in uppercase") |
| 95 | |
| 96 | # Parse values |
| 97 | values = [] |
| 98 | while index < len(content) and content[index] == '[': |
| 99 | index += 1 # Skip '[' |
| 100 | value_start = index |
| 101 | |
| 102 | # Parse value, handling escapes |
| 103 | while index < len(content) and content[index] != ']': |
| 104 | if content[index] == '\\': |
| 105 | index += 2 # Skip escape character and the next character |
| 106 | else: |
| 107 | index += 1 |
| 108 | |
| 109 | # Edge Case: Unclosed value bracket |
| 110 | if index >= len(content) or content[index] != ']': |
| 111 | raise ValueError("properties without delimiter") |
| 112 | |
| 113 | value = content[value_start:index] |
| 114 | # Process the value according to SGF Text type rules |
| 115 | value = _process_text_value(value) |
| 116 | values.append(value) |
| 117 | index += 1 # Skip ']' |
| 118 | |
| 119 | # Edge Case: No values found for key |
| 120 | if not values: |
| 121 | raise ValueError("properties without delimiter") |
| 122 | |
| 123 | properties[key] = values |
| 124 | |
| 125 | # Parse children |
| 126 | children = [] |
| 127 | while index < len(content) and content[index] == '(': |
| 128 | child, index = _parse_tree(content, index + 1) # Skip '(' |
| 129 | children.append(child) |
| 130 | # Skip the closing ')' |
| 131 | if index >= len(content) or content[index] != ')': |
| 132 | raise ValueError("tree missing") |
| 133 | index += 1 |
| 134 | |
| 135 | return SgfTree(properties, children), index |
| 136 | |
| 137 | |
| 138 | def _process_text_value(value: str) -> str: |
| 139 | """ |
| 140 | Process a text value according to SGF Text type rules. |
| 141 | |
| 142 | Args: |
| 143 | value: The raw text value to process |
| 144 | |
| 145 | Returns: |
| 146 | str: The processed text value |
| 147 | """ |
| 148 | result = [] |
| 149 | i = 0 |
| 150 | while i < len(value): |
| 151 | if value[i] == '\\': |
| 152 | # Handle escape character |
| 153 | i += 1 |
| 154 | if i < len(value): |
| 155 | # If next character is newline, remove both \ and newline |
| 156 | if value[i] == '\n': |
| 157 | i += 1 |
| 158 | else: |
| 159 | # Insert the next character as-is |
| 160 | result.append(value[i]) |
| 161 | i += 1 |
| 162 | else: |
| 163 | # Edge Case: Escape character at end of string |
| 164 | result.append('\\') |
| 165 | i += 1 |
| 166 | elif value[i] == '\n': |
| 167 | # Newlines are kept unless immediately after \ |
| 168 | result.append(value[i]) |
| 169 | i += 1 |
| 170 | elif value[i].isspace() and value[i] != '\n': |
| 171 | # All whitespace other than newline converted to spaces |
| 172 | result.append(' ') |
| 173 | i += 1 |
| 174 | else: |
| 175 | result.append(value[i]) |
| 176 | i += 1 |
| 177 | |
| 178 | return ''.join(result) |
| 179 | |
| 180 | # Handled Edge Cases: Empty input, missing opening parenthesis, missing closing parenthesis, |
| 181 | # empty content after removing parentheses, content not starting with semicolon, |
| 182 | # no key found, key not uppercase, unclosed value bracket, no values for key, |
| 183 | # missing tree closing parenthesis, escape character at end of string |