| 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 | # Parse the tree |
| 61 | tree, _ = _parse_tree(content, 0) |
| 62 | return tree |
| 63 | |
| 64 | |
| 65 | def _parse_tree(content: str, index: int) -> tuple[SgfTree, int]: |
| 66 | """ |
| 67 | Parse a tree from the content string starting at index. |
| 68 | |
| 69 | Args: |
| 70 | content: The SGF content string |
| 71 | index: The starting index for parsing |
| 72 | |
| 73 | Returns: |
| 74 | tuple: A tuple containing the parsed SgfTree and the next index to parse |
| 75 | """ |
| 76 | # Edge Case: No semicolon to start a node |
| 77 | if index >= len(content) or content[index] != ';': |
| 78 | raise ValueError("tree missing") |
| 79 | |
| 80 | index += 1 # Skip the semicolon |
| 81 | |
| 82 | # Parse properties of the current node |
| 83 | properties = {} |
| 84 | while index < len(content) and content[index] not in '();': |
| 85 | # Parse key |
| 86 | key_start = index |
| 87 | while index < len(content) and content[index].isalpha() and content[index].isupper(): |
| 88 | index += 1 |
| 89 | |
| 90 | # Edge Case: Key is not uppercase |
| 91 | if index == key_start: |
| 92 | raise ValueError("property must be in uppercase") |
| 93 | |
| 94 | key = content[key_start:index] |
| 95 | |
| 96 | # Parse values |
| 97 | values = [] |
| 98 | while index < len(content) and content[index] == '[': |
| 99 | index += 1 # Skip opening bracket |
| 100 | value_start = index |
| 101 | |
| 102 | # Parse value, handling escapes |
| 103 | value_chars = [] |
| 104 | while index < len(content) and content[index] != ']': |
| 105 | if content[index] == '\\': |
| 106 | # Add the backslash and the next character |
| 107 | value_chars.append('\\') |
| 108 | index += 1 |
| 109 | if index >= len(content): |
| 110 | raise ValueError("properties without delimiter") |
| 111 | value_chars.append(content[index]) |
| 112 | else: |
| 113 | value_chars.append(content[index]) |
| 114 | index += 1 |
| 115 | |
| 116 | # Edge Case: Unclosed bracket |
| 117 | if index >= len(content) or content[index] != ']': |
| 118 | raise ValueError("properties without delimiter") |
| 119 | |
| 120 | # Process the value according to SGF text rules |
| 121 | value = _process_sgf_text(''.join(value_chars)) |
| 122 | values.append(value) |
| 123 | index += 1 # Skip closing bracket |
| 124 | |
| 125 | # Edge Case: No values for a property |
| 126 | if not values: |
| 127 | raise ValueError("properties without delimiter") |
| 128 | |
| 129 | properties[key] = values |
| 130 | |
| 131 | # Continue parsing properties until we hit a delimiter |
| 132 | if index < len(content) and content[index] in ';()': |
| 133 | break |
| 134 | |
| 135 | # Parse children |
| 136 | children = [] |
| 137 | |
| 138 | # Handle variations (children in parentheses) |
| 139 | while index < len(content) and content[index] == '(': |
| 140 | child, index = _parse_tree(content, index+1) # Skip opening parenthesis |
| 141 | children.append(child) |
| 142 | |
| 143 | # Skip closing parenthesis |
| 144 | if index < len(content) and content[index] == ')': |
| 145 | index += 1 |
| 146 | |
| 147 | # Handle linear sequence (children separated by semicolons) |
| 148 | # If we encounter a semicolon after parsing properties, it indicates a child node |
| 149 | if index < len(content) and content[index] == ';': |
| 150 | child, index = _parse_tree(content, index) |
| 151 | children.append(child) |
| 152 | |
| 153 | return SgfTree(properties, children), index |
| 154 | |
| 155 | |
| 156 | def _process_sgf_text(text: str) -> str: |
| 157 | """ |
| 158 | Process SGF text according to SGF specification. |
| 159 | |
| 160 | Args: |
| 161 | text: Raw text from SGF property value |
| 162 | |
| 163 | Returns: |
| 164 | str: Processed text according to SGF rules |
| 165 | """ |
| 166 | result = [] |
| 167 | i = 0 |
| 168 | while i < len(text): |
| 169 | if text[i] == '\\': |
| 170 | i += 1 |
| 171 | if i < len(text): |
| 172 | # If next character is 'n' and it represents a newline, remove the backslash and newline |
| 173 | # In the parsed string, '\n' is a single character (newline) |
| 174 | if text[i] == '\n': |
| 175 | i += 1 # Skip newline |
| 176 | continue |
| 177 | else: |
| 178 | # For other characters, add them as-is |
| 179 | result.append(text[i]) |
| 180 | i += 1 |
| 181 | else: |
| 182 | # Trailing backslash |
| 183 | result.append('\\') |
| 184 | i += 1 |
| 185 | else: |
| 186 | # Convert all whitespace (except newline) to spaces |
| 187 | if text[i] != '\n' and text[i].isspace(): |
| 188 | result.append(' ') |
| 189 | else: |
| 190 | result.append(text[i]) |
| 191 | i += 1 |
| 192 | |
| 193 | return ''.join(result) |
| 194 | |
| 195 | # Handled Edge Cases: Empty input, Input doesn't start with '(', Input doesn't end with ')', Empty tree with no nodes, No semicolon to start a node, Key is not uppercase, Unclosed bracket, No values for a property |