| 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: A tree structure representing the SGF data |
| 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 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 | # Parse the tree |
| 57 | tree, _ = _parse_tree(content, 0) |
| 58 | return tree |
| 59 | |
| 60 | |
| 61 | def _parse_tree(content: str, index: int) -> tuple[SgfTree, int]: |
| 62 | """ |
| 63 | Parse a tree from the content string starting at index. |
| 64 | |
| 65 | Args: |
| 66 | content: The SGF content string |
| 67 | index: The starting index for parsing |
| 68 | |
| 69 | Returns: |
| 70 | tuple: A tuple containing the parsed SgfTree and the next index to parse |
| 71 | """ |
| 72 | # Edge Case: Content doesn't start with ';' |
| 73 | if index >= len(content) or content[index] != ';': |
| 74 | raise ValueError("tree with no nodes") |
| 75 | |
| 76 | index += 1 # Skip the ';' |
| 77 | |
| 78 | # Parse properties of the current node |
| 79 | properties = {} |
| 80 | while index < len(content) and content[index] not in '();': |
| 81 | # Parse key |
| 82 | key_start = index |
| 83 | while index < len(content) and content[index].isalpha() and content[index].isupper(): |
| 84 | index += 1 |
| 85 | |
| 86 | # Edge Case: No key found or key is not uppercase |
| 87 | if index == key_start: |
| 88 | raise ValueError("property must be in uppercase") |
| 89 | |
| 90 | key = content[key_start:index] |
| 91 | |
| 92 | # Parse values |
| 93 | values = [] |
| 94 | while index < len(content) and content[index] == '[': |
| 95 | index += 1 # Skip '[' |
| 96 | value_start = index |
| 97 | |
| 98 | # Parse value, handling escapes |
| 99 | while index < len(content) and content[index] != ']': |
| 100 | if content[index] == '\\': |
| 101 | index += 1 # Skip escape character |
| 102 | index += 1 |
| 103 | |
| 104 | # Edge Case: Unclosed value bracket |
| 105 | if index >= len(content): |
| 106 | raise ValueError("properties without delimiter") |
| 107 | |
| 108 | value = content[value_start:index] |
| 109 | # Process the value according to SGF text type rules |
| 110 | value = _process_text(value) |
| 111 | values.append(value) |
| 112 | index += 1 # Skip ']' |
| 113 | |
| 114 | # Edge Case: No values found for a key |
| 115 | if not values: |
| 116 | raise ValueError("properties without delimiter") |
| 117 | |
| 118 | properties[key] = values |
| 119 | |
| 120 | children = [] |
| 121 | |
| 122 | # Parse children |
| 123 | while index < len(content) and content[index] == '(': |
| 124 | child, index = _parse_tree(content, index + 1) # Skip '(' |
| 125 | children.append(child) |
| 126 | # Skip ')' |
| 127 | if index >= len(content) or content[index] != ')': |
| 128 | raise ValueError("tree missing") |
| 129 | index += 1 |
| 130 | |
| 131 | return SgfTree(properties, children), index |
| 132 | |
| 133 | |
| 134 | def _process_text(text: str) -> str: |
| 135 | """ |
| 136 | Process text according to SGF Text type rules. |
| 137 | |
| 138 | Args: |
| 139 | text: The raw text to process |
| 140 | |
| 141 | Returns: |
| 142 | str: The processed text |
| 143 | """ |
| 144 | result = [] |
| 145 | i = 0 |
| 146 | while i < len(text): |
| 147 | if text[i] == '\\': |
| 148 | i += 1 |
| 149 | if i < len(text): |
| 150 | # If next character is newline, remove the backslash and newline |
| 151 | if text[i] == '\n': |
| 152 | i += 1 # Skip newline |
| 153 | else: |
| 154 | # Insert any non-whitespace character as-is |
| 155 | # For whitespace characters, convert to space |
| 156 | if text[i].isspace(): |
| 157 | result.append(' ') |
| 158 | else: |
| 159 | result.append(text[i]) |
| 160 | i += 1 |
| 161 | else: |
| 162 | # All whitespace characters other than newline are converted to spaces |
| 163 | if text[i] != '\n' and text[i].isspace(): |
| 164 | result.append(' ') |
| 165 | else: |
| 166 | result.append(text[i]) |
| 167 | i += 1 |
| 168 | |
| 169 | return ''.join(result) |
| 170 | |
| 171 | # Handled Edge Cases: Empty input, input not starting with '(', input not ending with ')', empty content after removing parentheses, content not starting with ';', no key found, key not uppercase, unclosed value bracket, no values found for a key, missing closing parenthesis for children |
| 172 | # Handled Edge Cases: Empty input, input not starting with '(', input not ending with ')', empty content after removing parentheses, content not starting with ';', no key found, key not uppercase, unclosed value bracket, no values found for a key, missing closing parenthesis for children |
| 173 | # Handled Edge Cases: Newlines after backslash, whitespace conversion to spaces, escape character handling |