| 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 to parse from |
| 72 | |
| 73 | Returns: |
| 74 | tuple: A tuple containing the parsed SgfTree and the next index to parse from |
| 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: Property key is not in uppercase |
| 91 | if index == key_start: |
| 92 | raise ValueError("property must be in uppercase") |
| 93 | |
| 94 | key = content[key_start:index] |
| 95 | |
| 96 | # Edge Case: No delimiter after key |
| 97 | if index >= len(content) or content[index] != '[': |
| 98 | raise ValueError("properties without delimiter") |
| 99 | |
| 100 | # Parse values |
| 101 | values = [] |
| 102 | while index < len(content) and content[index] == '[': |
| 103 | index += 1 # Skip '[' |
| 104 | value, index = _parse_value(content, index) |
| 105 | values.append(value) |
| 106 | |
| 107 | # Edge Case: No closing ']' for value |
| 108 | if index >= len(content) or content[index] != ']': |
| 109 | raise ValueError("properties without delimiter") |
| 110 | index += 1 # Skip ']' |
| 111 | |
| 112 | properties[key] = values |
| 113 | |
| 114 | # Parse children (both variations and nested nodes) |
| 115 | children = [] |
| 116 | while index < len(content): |
| 117 | if content[index] == '(': |
| 118 | # Handle variations (child nodes in parentheses) |
| 119 | child, index = _parse_tree(content, index + 1) # Skip '(' |
| 120 | children.append(child) |
| 121 | |
| 122 | # Skip the closing ')' |
| 123 | if index >= len(content) or content[index] != ')': |
| 124 | raise ValueError("tree missing") |
| 125 | index += 1 # Skip the closing ')' |
| 126 | elif content[index] == ';': |
| 127 | # Handle nested nodes (shorthand notation - semicolon indicates child) |
| 128 | # In SGF, ";A[B];C[D]" means the first node has a child node with property C[D] |
| 129 | child, index = _parse_tree(content, index) # Don't skip, let _parse_tree handle the semicolon |
| 130 | children.append(child) |
| 131 | else: |
| 132 | break |
| 133 | |
| 134 | return SgfTree(properties, children), index |
| 135 | |
| 136 | |
| 137 | def _parse_value(content: str, index: int) -> tuple[str, int]: |
| 138 | """ |
| 139 | Parse a value from the content string starting at index. |
| 140 | |
| 141 | Args: |
| 142 | content: The SGF content string |
| 143 | index: The starting index to parse from |
| 144 | |
| 145 | Returns: |
| 146 | tuple: A tuple containing the parsed value string and the next index to parse from |
| 147 | """ |
| 148 | result = [] |
| 149 | while index < len(content) and content[index] != ']': |
| 150 | if content[index] == '\\': |
| 151 | # Handle escape character |
| 152 | index += 1 |
| 153 | if index >= len(content): |
| 154 | # Edge Case: Escape character at end of content |
| 155 | result.append('\\') |
| 156 | break |
| 157 | |
| 158 | char = content[index] |
| 159 | if char == '\n': |
| 160 | # Newlines are removed if they come immediately after a \ |
| 161 | # So we don't append anything, just skip the newline |
| 162 | pass |
| 163 | if char in 'ntr': |
| 164 | # Standard escape sequences: \n, \t, \r |
| 165 | # Convert to corresponding characters |
| 166 | if char == 'n': |
| 167 | result.append('\n') |
| 168 | elif char == 't': |
| 169 | result.append('\t') |
| 170 | elif char == 'r': |
| 171 | result.append('\r') |
| 172 | elif char == '\\': |
| 173 | # \\ becomes \ (first backslash consumes second) |
| 174 | result.append('\\') |
| 175 | elif char == ']': |
| 176 | # \] becomes ] (backslash consumed) |
| 177 | result.append(']') |
| 178 | else: |
| 179 | # All other escape sequences preserve the backslash |
| 180 | result.append('\\') |
| 181 | result.append(char) |
| 182 | elif content[index] == '\n': |
| 183 | # Newlines remain as newlines unless immediately after a \ |
| 184 | result.append(content[index]) |
| 185 | elif content[index].isspace() and content[index] != '\n': |
| 186 | # All whitespace other than newline converted to spaces |
| 187 | result.append(' ') |
| 188 | else: |
| 189 | # Regular character |
| 190 | result.append(content[index]) |
| 191 | index += 1 |
| 192 | |
| 193 | return ''.join(result), index |
| 194 | # 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, Property key is not in uppercase, No delimiter after key, No closing ']' for value, Escape character at end of content, Tree missing closing parenthesis |
| 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, Property key is not in uppercase, No delimiter after key, No closing ']' for value |
| 196 | # Handled Edge Cases: Escape character at end of content, Newline escaped with backslash, No escape sequences for whitespace like \t, Whitespace converted to spaces, Regular characters after escape |