| 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 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 end with ')' |
| 46 | if not input_string.endswith(')'): |
| 47 | raise ValueError("tree missing") |
| 48 | |
| 49 | # Remove the outer parentheses |
| 50 | content = input_string[1:-1] |
| 51 | |
| 52 | # Edge Case: Empty content after removing parentheses |
| 53 | if not content: |
| 54 | raise ValueError("tree with no nodes") |
| 55 | |
| 56 | # Edge Case: Content that doesn't start with ';' |
| 57 | if not content.startswith(';'): |
| 58 | raise ValueError("tree missing") |
| 59 | |
| 60 | return _parse_tree(content) |
| 61 | |
| 62 | |
| 63 | def _parse_tree(content: str) -> SgfTree: |
| 64 | """ |
| 65 | Parse the content of an SGF tree. |
| 66 | |
| 67 | Args: |
| 68 | content: The content of the SGF tree (without outer parentheses) |
| 69 | |
| 70 | Returns: |
| 71 | SgfTree: A tree structure representing the SGF data |
| 72 | """ |
| 73 | # Edge Case: Content that doesn't start with ';' |
| 74 | if not content.startswith(';'): |
| 75 | raise ValueError("tree missing") |
| 76 | |
| 77 | i = 1 # Skip the initial ';' |
| 78 | properties = {} |
| 79 | children = [] |
| 80 | |
| 81 | # Parse properties |
| 82 | while i < len(content) and content[i] != '(' and content[i] != ';': |
| 83 | # Parse property key |
| 84 | key_start = i |
| 85 | while i < len(content) and content[i].isalpha(): |
| 86 | i += 1 |
| 87 | |
| 88 | # Edge Case: Property key is empty |
| 89 | if i == key_start: |
| 90 | raise ValueError("properties without delimiter") |
| 91 | |
| 92 | key = content[key_start:i] |
| 93 | |
| 94 | # Edge Case: Property key is not uppercase |
| 95 | if key != key.upper(): |
| 96 | raise ValueError("property must be in uppercase") |
| 97 | |
| 98 | # Parse property values |
| 99 | values = [] |
| 100 | |
| 101 | # Edge Case: No values after key |
| 102 | if i >= len(content) or content[i] != '[': |
| 103 | raise ValueError("properties without delimiter") |
| 104 | |
| 105 | while i < len(content) and content[i] == '[': |
| 106 | i += 1 # Skip '[' |
| 107 | value_start = i |
| 108 | |
| 109 | # Parse value, handling escapes |
| 110 | while i < len(content) and content[i] != ']': |
| 111 | if content[i] == '\\': |
| 112 | i += 2 # Skip escape character and the escaped character |
| 113 | else: |
| 114 | i += 1 |
| 115 | |
| 116 | # Edge Case: Unclosed value bracket |
| 117 | if i >= len(content) or content[i] != ']': |
| 118 | raise ValueError("properties without delimiter") |
| 119 | |
| 120 | value = content[value_start:i] |
| 121 | values.append(_unescape_text(value)) |
| 122 | i += 1 # Skip ']' |
| 123 | |
| 124 | properties[key] = values |
| 125 | |
| 126 | # Parse children |
| 127 | while i < len(content) and content[i] == '(': |
| 128 | # Find matching closing parenthesis |
| 129 | paren_count = 1 |
| 130 | start = i + 1 |
| 131 | i += 1 |
| 132 | |
| 133 | while i < len(content) and paren_count > 0: |
| 134 | if content[i] == '(': |
| 135 | paren_count += 1 |
| 136 | elif content[i] == ')': |
| 137 | paren_count -= 1 |
| 138 | i += 1 |
| 139 | |
| 140 | # Edge Case: Unmatched parenthesis |
| 141 | if paren_count != 0: |
| 142 | raise ValueError("tree missing") |
| 143 | |
| 144 | # Extract child content (excluding the closing parenthesis) |
| 145 | child_content = content[start:i-1] |
| 146 | children.append(_parse_tree(child_content)) |
| 147 | |
| 148 | # Handle consecutive nodes (shorthand notation) |
| 149 | if i < len(content) and content[i] == ';': |
| 150 | # Create a child node with the remaining content |
| 151 | child_content = content[i:] |
| 152 | children.append(_parse_tree(child_content)) |
| 153 | |
| 154 | return SgfTree(properties, children) |
| 155 | |
| 156 | |
| 157 | def _unescape_text(text: str) -> str: |
| 158 | """ |
| 159 | Process SGF text according to SGF specification. |
| 160 | |
| 161 | Args: |
| 162 | text: Raw text from SGF value |
| 163 | |
| 164 | Returns: |
| 165 | str: Processed text with escape sequences handled |
| 166 | """ |
| 167 | result = [] |
| 168 | i = 0 |
| 169 | while i < len(text): |
| 170 | if text[i] == '\\': |
| 171 | i += 1 # Skip the escape character |
| 172 | if i < len(text): |
| 173 | # If next character is newline, remove the backslash and newline |
| 174 | if text[i] == '\n': |
| 175 | # Remove the backslash and skip the newline |
| 176 | pass |
| 177 | else: |
| 178 | # Insert the escaped character as-is |
| 179 | result.append(text[i]) |
| 180 | i += 1 |
| 181 | else: |
| 182 | # Convert whitespace (except newline) to space |
| 183 | if text[i] != '\n' and text[i].isspace(): |
| 184 | result.append(' ') |
| 185 | else: |
| 186 | result.append(text[i]) |
| 187 | i += 1 |
| 188 | |
| 189 | return ''.join(result) |
| 190 | |
| 191 | # Handled Edge Cases: Empty input, missing opening parenthesis, missing closing parenthesis, |
| 192 | # empty content after removing parentheses, content not starting with semicolon, |
| 193 | # property key is empty, property key not uppercase, no values after key, |
| 194 | # unclosed value bracket, unmatched parenthesis |