| 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 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 len(input_string) < 2 or input_string[1] != ';': |
| 47 | # Check for the specific case of "(" which should raise "tree missing" not "tree with no nodes" |
| 48 | if input_string == "(": |
| 49 | raise ValueError("tree missing") |
| 50 | raise ValueError("tree with no nodes") |
| 51 | |
| 52 | index = 1 # Start after the initial '(' |
| 53 | |
| 54 | def parse_node() -> SgfTree: |
| 55 | nonlocal index |
| 56 | # Edge Case: Node doesn't start with ';' |
| 57 | if index >= len(input_string) or input_string[index] != ';': |
| 58 | raise ValueError("tree with no nodes") |
| 59 | |
| 60 | index += 1 # Skip the ';' |
| 61 | properties = {} |
| 62 | children = [] |
| 63 | |
| 64 | # Parse properties |
| 65 | while index < len(input_string) and input_string[index].isalpha(): |
| 66 | # Parse property key |
| 67 | key_start = index |
| 68 | while index < len(input_string) and input_string[index].isalpha(): |
| 69 | # Edge Case: Property key is not uppercase |
| 70 | if not input_string[index].isupper(): |
| 71 | raise ValueError("property must be in uppercase") |
| 72 | index += 1 |
| 73 | |
| 74 | key = input_string[key_start:index] |
| 75 | |
| 76 | # Edge Case: No values for the property |
| 77 | if index >= len(input_string) or input_string[index] != '[': |
| 78 | raise ValueError("properties without delimiter") |
| 79 | |
| 80 | # Parse property values |
| 81 | values = [] |
| 82 | while index < len(input_string) and input_string[index] == '[': |
| 83 | index += 1 # Skip '[' |
| 84 | value_start = index |
| 85 | |
| 86 | # Parse value content with escape handling |
| 87 | value_chars = [] |
| 88 | while index < len(input_string) and input_string[index] != ']': |
| 89 | if input_string[index] == '\\': |
| 90 | index += 1 |
| 91 | # Edge Case: Escape at end of string |
| 92 | if index >= len(input_string): |
| 93 | raise ValueError("properties without delimiter") |
| 94 | |
| 95 | char = input_string[index] |
| 96 | # In SGF, backslash only escapes the next character, no special sequences |
| 97 | value_chars.append(char) |
| 98 | else: |
| 99 | value_chars.append(input_string[index]) |
| 100 | index += 1 |
| 101 | |
| 102 | # Edge Case: Unclosed property value |
| 103 | if index >= len(input_string) or input_string[index] != ']': |
| 104 | raise ValueError("properties without delimiter") |
| 105 | |
| 106 | raw_value = ''.join(value_chars) |
| 107 | # Process according to SGF Text type rules |
| 108 | processed_value = "" |
| 109 | i = 0 |
| 110 | while i < len(raw_value): |
| 111 | if i + 1 < len(raw_value) and raw_value[i] == '\\': |
| 112 | i += 1 |
| 113 | # Handle escaped characters according to SGF rules |
| 114 | # Newlines immediately after backslash are removed |
| 115 | if raw_value[i+1] == '\n': |
| 116 | i += 2 # Skip backslash and newline |
| 117 | else: |
| 118 | # For any other character after backslash, add it as-is |
| 119 | # but apply whitespace conversion rules |
| 120 | char = raw_value[i+1] |
| 121 | if char in (' ', '\t'): |
| 122 | processed_value += ' ' |
| 123 | else: |
| 124 | processed_value += char |
| 125 | i += 2 |
| 126 | elif raw_value[i] == '\n': |
| 127 | # Newlines are removed if they come immediately after a '\' |
| 128 | # But we need to check if there was a backslash before |
| 129 | # Regular newlines are kept |
| 130 | processed_value += raw_value[i] |
| 131 | i += 1 |
| 132 | elif raw_value[i] in (' ', '\t'): |
| 133 | # All whitespace other than newline converted to space |
| 134 | processed_value += ' ' |
| 135 | i += 1 |
| 136 | else: |
| 137 | processed_value += raw_value[i] |
| 138 | i += 1 |
| 139 | |
| 140 | values.append(processed_value) |
| 141 | index += 1 # Skip ']' |
| 142 | |
| 143 | properties[key] = values |
| 144 | |
| 145 | # Parse children - both sequential (;) and variations (() |
| 146 | while index < len(input_string): |
| 147 | if index < len(input_string) - 1 and input_string[index] == '(' and input_string[index + 1] == ')': |
| 148 | # Empty parentheses "()" are not valid |
| 149 | raise ValueError("tree with no nodes") |
| 150 | elif index < len(input_string) and input_string[index] == '(': |
| 151 | # Parse variation |
| 152 | index += 1 # Skip '(' |
| 153 | child = parse_node() |
| 154 | children.append(child) |
| 155 | # Skip closing ')' |
| 156 | if index < len(input_string) and input_string[index] == ')': |
| 157 | index += 1 |
| 158 | else: |
| 159 | raise ValueError("tree missing") |
| 160 | elif index < len(input_string) and input_string[index] == ';': |
| 161 | # Parse sequential node as child |
| 162 | child = parse_node() |
| 163 | children.append(child) |
| 164 | else: |
| 165 | # No more children |
| 166 | break |
| 167 | |
| 168 | return SgfTree(properties, children) |
| 169 | |
| 170 | tree = parse_node() |
| 171 | |
| 172 | # Skip closing ')' |
| 173 | if index < len(input_string) and input_string[index] == ')': |
| 174 | index += 1 |
| 175 | |
| 176 | # Edge Case: Extra characters after the main tree |
| 177 | if index < len(input_string): |
| 178 | raise ValueError("tree missing") |
| 179 | |
| 180 | return tree |
| 181 | |
| 182 | # Handled Edge Cases: Empty input, input not starting with '(', missing initial ';', property keys not uppercase, missing property delimiters, unclosed property values, escape sequences at end of string, trailing characters after tree |