| 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 parsed SGF data |
| 37 | |
| 38 | Raises: |
| 39 | ValueError: If the input is malformed or doesn't conform to SGF format rules |
| 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 have a matching ';' |
| 46 | if not input_string.startswith('(;'): |
| 47 | raise ValueError("tree with no nodes") |
| 48 | |
| 49 | # Parse the SGF string |
| 50 | index = 0 |
| 51 | |
| 52 | def parse_tree(): |
| 53 | nonlocal index |
| 54 | # Edge Case: Expecting '(' at the beginning of a tree |
| 55 | if index >= len(input_string) or input_string[index] != '(': |
| 56 | raise ValueError("tree missing") |
| 57 | index += 1 # Skip '(' |
| 58 | |
| 59 | # Parse nodes until we hit ')' |
| 60 | children = [] |
| 61 | while index < len(input_string) and input_string[index] == ';': |
| 62 | node = parse_node() |
| 63 | children.append(node) |
| 64 | |
| 65 | # Edge Case: No nodes in tree |
| 66 | if not children: |
| 67 | raise ValueError("tree with no nodes") |
| 68 | |
| 69 | # If there's only one node, it's the root |
| 70 | if len(children) == 1: |
| 71 | root = children[0] |
| 72 | else: |
| 73 | # Multiple nodes at the same level - this shouldn't happen in a valid SGF |
| 74 | # But if it does, we treat the first as root and the rest as its children |
| 75 | root = children[0] |
| 76 | root.children.extend(children[1:]) |
| 77 | |
| 78 | # Edge Case: Expecting ')' at the end of a tree |
| 79 | if index >= len(input_string) or input_string[index] != ')': |
| 80 | raise ValueError("tree missing") |
| 81 | index += 1 # Skip ')' |
| 82 | |
| 83 | return root |
| 84 | |
| 85 | def parse_node(): |
| 86 | nonlocal index |
| 87 | # Edge Case: Expecting ';' at the beginning of a node |
| 88 | if index >= len(input_string) or input_string[index] != ';': |
| 89 | raise ValueError("tree with no nodes") |
| 90 | index += 1 # Skip ';' |
| 91 | |
| 92 | properties = {} |
| 93 | |
| 94 | # Parse properties |
| 95 | while index < len(input_string) and input_string[index].isalpha(): |
| 96 | key, values = parse_property() |
| 97 | # Edge Case: Property key is not uppercase |
| 98 | if not key.isupper(): |
| 99 | raise ValueError("property must be in uppercase") |
| 100 | properties[key] = values |
| 101 | |
| 102 | # Parse children trees |
| 103 | children = [] |
| 104 | while index < len(input_string) and input_string[index] == '(': |
| 105 | child = parse_tree() |
| 106 | children.append(child) |
| 107 | |
| 108 | return SgfTree(properties, children) |
| 109 | |
| 110 | def parse_property(): |
| 111 | nonlocal index |
| 112 | # Parse key |
| 113 | start = index |
| 114 | while index < len(input_string) and input_string[index].isalpha(): |
| 115 | index += 1 |
| 116 | key = input_string[start:index] |
| 117 | |
| 118 | # Edge Case: Property without values |
| 119 | if index >= len(input_string) or input_string[index] != '[': |
| 120 | raise ValueError("properties without delimiter") |
| 121 | |
| 122 | # Parse values |
| 123 | values = [] |
| 124 | while index < len(input_string) and input_string[index] == '[': |
| 125 | index += 1 # Skip '[' |
| 126 | value = parse_value() |
| 127 | values.append(value) |
| 128 | # Edge Case: Missing closing ']' |
| 129 | if index >= len(input_string) or input_string[index] != ']': |
| 130 | raise ValueError("properties without delimiter") |
| 131 | index += 1 # Skip ']' |
| 132 | |
| 133 | return key, values |
| 134 | |
| 135 | def parse_value(): |
| 136 | nonlocal index |
| 137 | result = [] |
| 138 | while index < len(input_string) and input_string[index] != ']': |
| 139 | if input_string[index] == '\\': |
| 140 | index += 1 # Skip '\' |
| 141 | if index >= len(input_string): |
| 142 | # Edge Case: Escape character at end of input |
| 143 | result.append('\\') |
| 144 | break |
| 145 | char = input_string[index] |
| 146 | if char == 'n': |
| 147 | # Special case: \n should be treated as literal 'n' if not after \ |
| 148 | result.append('n') |
| 149 | elif char == 't': |
| 150 | # Special case: \t should be treated as literal 't' if not after \ |
| 151 | result.append('t') |
| 152 | elif char == '\\': |
| 153 | result.append('\\') |
| 154 | elif char.isspace(): |
| 155 | # Whitespace after \ follows normal rules |
| 156 | if char == '\n' and index > 0 and input_string[index-2] == '\\': |
| 157 | # If \n (backslash followed by newline), remove the newline |
| 158 | pass |
| 159 | else: |
| 160 | result.append(char) |
| 161 | else: |
| 162 | result.append(char) |
| 163 | elif input_string[index] == '\n' and index > 0 and input_string[index-1] == '\\': |
| 164 | # This is a continuation newline, remove it |
| 165 | pass |
| 166 | elif input_string[index] != '\n' and input_string[index].isspace(): |
| 167 | # All whitespace other than newline becomes space |
| 168 | result.append(' ') |
| 169 | else: |
| 170 | result.append(input_string[index]) |
| 171 | index += 1 |
| 172 | |
| 173 | # Join and process the result according to SGF text rules |
| 174 | text = ''.join(result) |
| 175 | # Handle continuation newlines (those immediately after \) |
| 176 | processed_text = '' |
| 177 | i = 0 |
| 178 | while i < len(text): |
| 179 | if i < len(text) - 1 and text[i] == '\\' and text[i+1] == '\n': |
| 180 | # Skip the \n sequence |
| 181 | i += 2 |
| 182 | else: |
| 183 | processed_text += text[i] |
| 184 | i += 1 |
| 185 | |
| 186 | return processed_text |
| 187 | |
| 188 | # Start parsing |
| 189 | try: |
| 190 | tree = parse_tree() |
| 191 | # Edge Case: Extra characters after the main tree |
| 192 | if index < len(input_string): |
| 193 | raise ValueError("tree missing") |
| 194 | return tree |
| 195 | except IndexError: |
| 196 | # Edge Case: Unexpected end of input during parsing |
| 197 | raise ValueError("tree missing") |
| 198 | |
| 199 | # Handled Edge Cases: Empty input, input not starting with '(', input not starting with '(;', missing ';' at node start, no nodes in tree, property key not uppercase, property without values, missing closing ']', escape character at end of input, unexpected end of input during parsing, extra characters after main tree, missing ')' at tree end |