| 26 | 28 | |
| 27 | 29 | |
| 28 | 30 | def parse(input_string: str) -> SgfTree: |
| 29 | | pass |
| 31 | """ |
| 32 | Parse an SGF string and return a tree structure of properties. |
| 33 | |
| 34 | Args: |
| 35 | input_string: A string in SGF format |
| 36 | |
| 37 | Returns: |
| 38 | SgfTree: The parsed SGF tree |
| 39 | |
| 40 | Raises: |
| 41 | ValueError: If the input is malformed or doesn't follow SGF format rules |
| 42 | """ |
| 43 | # Edge Case: Empty input |
| 44 | if not input_string: |
| 45 | raise ValueError("tree missing") |
| 46 | |
| 47 | # Edge Case: Input doesn't start with '(' |
| 48 | if not input_string.startswith('('): |
| 49 | raise ValueError("tree missing") |
| 50 | |
| 51 | # Edge Case: Input doesn't end with ')' |
| 52 | if not input_string.endswith(')'): |
| 53 | raise ValueError("tree missing") |
| 54 | |
| 55 | # Remove the outer parentheses |
| 56 | content = input_string[1:-1] |
| 57 | |
| 58 | # Edge Case: Empty content after removing parentheses |
| 59 | if not content: |
| 60 | raise ValueError("tree with no nodes") |
| 61 | |
| 62 | # Parse the tree |
| 63 | tree, _ = _parse_tree(content, 0) |
| 64 | return tree |
| 65 | |
| 66 | |
| 67 | def _parse_tree(content: str, index: int) -> tuple[SgfTree, int]: |
| 68 | """ |
| 69 | Parse a tree from the content string starting at index. |
| 70 | |
| 71 | Args: |
| 72 | content: The SGF content string |
| 73 | index: The starting index to parse from |
| 74 | |
| 75 | Returns: |
| 76 | tuple: (SgfTree, next_index) |
| 77 | """ |
| 78 | # Edge Case: Missing semicolon at the beginning of a node |
| 79 | if index >= len(content) or content[index] != ';': |
| 80 | raise ValueError("tree missing") |
| 81 | |
| 82 | index += 1 # Skip the semicolon |
| 83 | |
| 84 | # Parse properties of the current node |
| 85 | properties = {} |
| 86 | while index < len(content) and content[index].isalpha(): |
| 87 | # Parse key |
| 88 | key_start = index |
| 89 | while index < len(content) and content[index].isalpha(): |
| 90 | index += 1 |
| 91 | |
| 92 | key = content[key_start:index] |
| 93 | |
| 94 | # Edge Case: Key is not in uppercase |
| 95 | if not key.isupper(): |
| 96 | raise ValueError("property must be in uppercase") |
| 97 | |
| 98 | # Edge Case: Missing opening bracket for property value |
| 99 | if index >= len(content) or content[index] != '[': |
| 100 | raise ValueError("properties without delimiter") |
| 101 | |
| 102 | # Parse values |
| 103 | values = [] |
| 104 | while index < len(content) and content[index] == '[': |
| 105 | index += 1 # Skip opening bracket |
| 106 | |
| 107 | # Parse value content, handling escapes |
| 108 | value_chars = [] |
| 109 | while index < len(content) and content[index] != ']': |
| 110 | if content[index] == '\\': |
| 111 | # Handle escape sequences |
| 112 | index += 1 |
| 113 | if index >= len(content): |
| 114 | raise ValueError("properties without delimiter") |
| 115 | |
| 116 | # Check if the next character is a newline |
| 117 | if content[index] == '\n': |
| 118 | # Newline immediately after backslash is removed |
| 119 | # Don't add anything to value_chars |
| 120 | pass |
| 121 | elif content[index].isspace(): |
| 122 | # Other whitespace after backslash is converted to spaces |
| 123 | value_chars.append(' ') |
| 124 | else: |
| 125 | # Any non-whitespace character after backslash is inserted as-is |
| 126 | value_chars.append(content[index]) |
| 127 | else: |
| 128 | # Convert whitespace (except newlines) to spaces |
| 129 | if content[index].isspace() and content[index] != '\n': |
| 130 | value_chars.append(' ') |
| 131 | else: |
| 132 | value_chars.append(content[index]) |
| 133 | index += 1 |
| 134 | |
| 135 | # Edge Case: Missing closing bracket |
| 136 | if index >= len(content) or content[index] != ']': |
| 137 | raise ValueError("properties without delimiter") |
| 138 | |
| 139 | values.append(''.join(value_chars)) |
| 140 | index += 1 # Skip closing bracket |
| 141 | |
| 142 | properties[key] = values |
| 143 | |
| 144 | # Parse children - either variations in parentheses or sequential nodes |
| 145 | children = [] |
| 146 | |
| 147 | # First handle variations (nodes in parentheses) |
| 148 | while index < len(content) and content[index] == '(': |
| 149 | # Parse child tree |
| 150 | child_tree, index = _parse_tree(content, index+1) # Skip opening parenthesis |
| 151 | |
| 152 | # Find closing parenthesis |
| 153 | if index >= len(content) or content[index] != ')': |
| 154 | raise ValueError("tree missing") |
| 155 | |
| 156 | children.append(child_tree) |
| 157 | index += 1 # Skip closing parenthesis |
| 158 | |
| 159 | # Then handle sequential nodes (nodes separated by semicolons) |
| 160 | if index < len(content) and content[index] == ';': |
| 161 | # Parse the next sequential node |
| 162 | next_node, index = _parse_tree(content, index) |
| 163 | children.append(next_node) |
| 164 | |
| 165 | return SgfTree(properties, children), index |
| 166 | |
| 167 | # Handled Edge Cases: Empty input, Input doesn't start with '(', Input doesn't end with ')', Empty content after removing parentheses, Missing semicolon at the beginning of a node, Key is not in uppercase, Missing opening bracket for property value, Missing closing bracket, Missing closing parenthesis for tree" |