| 26 | 26 | |
| 27 | 27 | |
| 28 | 28 | def parse(input_string: str) -> SgfTree: |
| 29 | | pass |
| 29 | # Edge Case: Empty input or input that doesn't start with '(' |
| 30 | if not input_string or input_string[0] != '(': |
| 31 | raise ValueError("tree missing") |
| 32 | |
| 33 | # Edge Case: Input that doesn't end with ')' |
| 34 | if input_string[-1] != ')': |
| 35 | raise ValueError("tree missing") |
| 36 | |
| 37 | # Remove the outer parentheses |
| 38 | content = input_string[1:-1] |
| 39 | |
| 40 | # Edge Case: Empty content after removing parentheses |
| 41 | if not content: |
| 42 | raise ValueError("tree with no nodes") |
| 43 | |
| 44 | # Parse the content |
| 45 | return _parse_tree(content) |
| 46 | |
| 47 | |
| 48 | def _parse_tree(content: str) -> SgfTree: |
| 49 | # Edge Case: Content that doesn't start with ';' |
| 50 | if not content or content[0] != ';': |
| 51 | raise ValueError("tree with no nodes") |
| 52 | |
| 53 | # Find the first node and its children |
| 54 | node_end = 1 # Start after the ';' |
| 55 | |
| 56 | # Parse properties of the current node |
| 57 | properties = {} |
| 58 | |
| 59 | while node_end < len(content): |
| 60 | char = content[node_end] |
| 61 | |
| 62 | # If we encounter a ';' or '(', it means the current node ends |
| 63 | if char in (';', '('): |
| 64 | break |
| 65 | |
| 66 | # Parse property key |
| 67 | if not char.isalpha() or not char.isupper(): |
| 68 | raise ValueError("property must be in uppercase") |
| 69 | |
| 70 | key_start = node_end |
| 71 | while node_end < len(content) and content[node_end].isalpha() and content[node_end].isupper(): |
| 72 | node_end += 1 |
| 73 | |
| 74 | key = content[key_start:node_end] |
| 75 | |
| 76 | # Edge Case: Property without values |
| 77 | if node_end >= len(content) or content[node_end] != '[': |
| 78 | raise ValueError("properties without delimiter") |
| 79 | |
| 80 | # Parse property values |
| 81 | values = [] |
| 82 | while node_end < len(content) and content[node_end] == '[': |
| 83 | node_end += 1 # Skip '[' |
| 84 | value_start = node_end |
| 85 | |
| 86 | # Parse value considering escape sequences |
| 87 | value_chars = [] |
| 88 | while node_end < len(content) and content[node_end] != ']': |
| 89 | if content[node_end] == '\\': |
| 90 | # Handle escape sequences |
| 91 | node_end += 1 |
| 92 | if node_end >= len(content): |
| 93 | raise ValueError("properties without delimiter") |
| 94 | |
| 95 | # If next character is newline after backslash, skip both |
| 96 | if content[node_end] == '\n': |
| 97 | node_end += 1 |
| 98 | else: |
| 99 | # Add the escaped character as-is |
| 100 | value_chars.append(content[node_end]) |
| 101 | node_end += 1 |
| 102 | else: |
| 103 | # Handle whitespace conversion |
| 104 | if content[node_end] == '\n': |
| 105 | value_chars.append(content[node_end]) |
| 106 | elif content[node_end].isspace(): |
| 107 | value_chars.append(' ') |
| 108 | else: |
| 109 | value_chars.append(content[node_end]) |
| 110 | node_end += 1 |
| 111 | |
| 112 | # Edge Case: Unclosed bracket |
| 113 | if node_end >= len(content) or content[node_end] != ']': |
| 114 | raise ValueError("properties without delimiter") |
| 115 | |
| 116 | value = ''.join(value_chars) |
| 117 | values.append(value) |
| 118 | node_end += 1 # Skip ']' |
| 119 | |
| 120 | # Add property to the node |
| 121 | properties[key] = values |
| 122 | |
| 123 | # Parse children |
| 124 | children = [] |
| 125 | |
| 126 | # Edge Case: Handle multiple children |
| 127 | while node_end < len(content) and content[node_end] == '(': |
| 128 | # Find matching closing parenthesis |
| 129 | paren_count = 1 |
| 130 | child_start = node_end + 1 |
| 131 | node_end += 1 |
| 132 | |
| 133 | while node_end < len(content) and paren_count > 0: |
| 134 | if content[node_end] == '(': |
| 135 | paren_count += 1 |
| 136 | elif content[node_end] == ')': |
| 137 | paren_count -= 1 |
| 138 | node_end += 1 |
| 139 | |
| 140 | # Edge Case: Unmatched parenthesis |
| 141 | if paren_count != 0: |
| 142 | raise ValueError("tree missing") |
| 143 | |
| 144 | # Parse the child tree |
| 145 | child_content = content[child_start:node_end-1] |
| 146 | children.append(_parse_tree(child_content)) |
| 147 | |
| 148 | return SgfTree(properties, children) |
| 149 | # Handled Edge Cases: Empty input, missing opening parenthesis, missing closing parenthesis, empty content after removing parentheses, content not starting with semicolon, property not in uppercase, property without values, unclosed bracket, unmatched parenthesis |