| 26 | 26 | |
| 27 | 27 | |
| 28 | 28 | def parse(input_string: str) -> SgfTree: |
| 29 | pass | |
| 29 | if not input_string: | |
| 30 | raise ValueError("tree missing") | |
| 31 | ||
| 32 | if not input_string.startswith('(') or not input_string.endswith(')'): | |
| 33 | raise ValueError("tree missing") | |
| 34 | ||
| 35 | # Remove the outer parentheses | |
| 36 | content = input_string[1:-1] | |
| 37 | ||
| 38 | if not content: | |
| 39 | raise ValueError("tree with no nodes") | |
| 40 | ||
| 41 | # Parse the first node | |
| 42 | node_end = find_node_end(content) | |
| 43 | first_node = content[:node_end] | |
| 44 | remaining = content[node_end:] | |
| 45 | ||
| 46 | properties = parse_properties(first_node) | |
| 47 | children = [] | |
| 48 | ||
| 49 | # Parse children | |
| 50 | while remaining.startswith('('): | |
| 51 | child_end = find_matching_paren(remaining, 0) | |
| 52 | child_tree = parse(remaining[:child_end+1]) | |
| 53 | children.append(child_tree) | |
| 54 | remaining = remaining[child_end+1:] | |
| 55 | ||
| 56 | return SgfTree(properties=properties, children=children) | |
| 57 | ||
| 58 | ||
| 59 | def find_node_end(content: str) -> int: | |
| 60 | """Find the end of the first node (before any children)""" | |
| 61 | i = 0 | |
| 62 | while i < len(content): | |
| 63 | if content[i] == '(': | |
| 64 | return i | |
| 65 | i += 1 | |
| 66 | return len(content) | |
| 67 | ||
| 68 | ||
| 69 | def find_matching_paren(s: str, start: int) -> int: | |
| 70 | """Find the matching parenthesis for the one at start position""" | |
| 71 | if s[start] != '(': | |
| 72 | return -1 | |
| 73 | ||
| 74 | count = 1 | |
| 75 | i = start + 1 | |
| 76 | while i < len(s) and count > 0: | |
| 77 | if s[i] == '(': | |
| 78 | count += 1 | |
| 79 | elif s[i] == ')': | |
| 80 | count -= 1 | |
| 81 | i += 1 | |
| 82 | ||
| 83 | if count != 0: | |
| 84 | raise ValueError("tree missing") | |
| 85 | ||
| 86 | return i - 1 | |
| 87 | ||
| 88 | ||
| 89 | def parse_properties(node_content: str) -> dict: | |
| 90 | """Parse properties from a node content string""" | |
| 91 | properties = {} | |
| 92 | ||
| 93 | if not node_content.startswith(';'): | |
| 94 | # This must be the root node without semicolon | |
| 95 | prop_start = 0 | |
| 96 | else: | |
| 97 | prop_start = 1 | |
| 98 | ||
| 99 | i = prop_start | |
| 100 | while i < len(node_content): | |
| 101 | # Parse key | |
| 102 | if i >= len(node_content): | |
| 103 | break | |
| 104 | ||
| 105 | key_start = i | |
| 106 | while i < len(node_content) and node_content[i].isalpha() and node_content[i].isupper(): | |
| 107 | i += 1 | |
| 108 | ||
| 109 | if i == key_start: | |
| 110 | if node_content[key_start] == ';': | |
| 111 | i += 1 | |
| 112 | continue | |
| 113 | else: | |
| 114 | raise ValueError("properties without delimiter") | |
| 115 | ||
| 116 | key = node_content[key_start:i] | |
| 117 | ||
| 118 | # Check if key is uppercase | |
| 119 | if not key.isupper(): | |
| 120 | raise ValueError("property must be in uppercase") | |
| 121 | ||
| 122 | # Parse values | |
| 123 | values = [] | |
| 124 | while i < len(node_content) and node_content[i] == '[': | |
| 125 | i += 1 # Skip opening bracket | |
| 126 | value_start = i | |
| 127 | ||
| 128 | # Find the closing bracket, taking care of escaped brackets | |
| 129 | bracket_count = 1 | |
| 130 | while i < len(node_content) and bracket_count > 0: | |
| 131 | if node_content[i] == '\\' and i + 1 < len(node_content): | |
| 132 | i += 2 # Skip escape and next character | |
| 133 | elif node_content[i] == '[': | |
| 134 | bracket_count += 1 | |
| 135 | i += 1 | |
| 136 | elif node_content[i] == ']': | |
| 137 | bracket_count -= 1 | |
| 138 | i += 1 | |
| 139 | else: | |
| 140 | i += 1 | |
| 141 | ||
| 142 | if bracket_count != 0: | |
| 143 | raise ValueError("properties without delimiter") | |
| 144 | ||
| 145 | value = node_content[value_start:i-1] | |
| 146 | values.append(parse_text(value)) | |
| 147 | ||
| 148 | if not values: | |
| 149 | raise ValueError("properties without delimiter") | |
| 150 | ||
| 151 | properties[key] = values | |
| 152 | ||
| 153 | # Ensure we're making progress to avoid infinite loop | |
| 154 | if i <= key_start and key_start < len(node_content): | |
| 155 | i = key_start + 1 | |
| 156 | ||
| 157 | return properties | |
| 158 | ||
| 159 | ||
| 160 | def parse_text(text: str) -> str: | |
| 161 | """Parse SGF Text type according to specifications""" | |
| 162 | result = [] | |
| 163 | i = 0 | |
| 164 | while i < len(text): | |
| 165 | if text[i] == '\\': | |
| 166 | # Handle escape character | |
| 167 | if i + 1 < len(text): | |
| 168 | next_char = text[i + 1] | |
| 169 | if next_char == '\n': | |
| 170 | # Backslash followed by newline - remove both | |
| 171 | i += 2 | |
| 172 | elif next_char.isspace(): | |
| 173 | # Backslash followed by other whitespace - convert to space | |
| 174 | result.append(' ') | |
| 175 | i += 2 | |
| 176 | else: | |
| 177 | # Backslash followed by non-whitespace - insert as-is | |
| 178 | result.append(next_char) | |
| 179 | i += 2 | |
| 180 | else: | |
| 181 | # Backslash at end of string - treat as literal backslash | |
| 182 | result.append('\\') | |
| 183 | i += 1 | |
| 184 | elif text[i] == '\n': | |
| 185 | # Newlines remain as newlines | |
| 186 | result.append('\n') | |
| 187 | i += 1 | |
| 188 | elif text[i].isspace() and text[i] != '\n': | |
| 189 | # Other whitespace converted to spaces | |
| 190 | result.append(' ') | |
| 191 | i += 1 | |
| 192 | else: | |
| 193 | # Regular character | |
| 194 | result.append(text[i]) | |
| 195 | i += 1 | |
| 196 | ||
| 197 | return ''.join(result) | |
| 198 | ||
| 199 | # Handled Edge Cases: empty input, missing parentheses, no nodes, properties without delimiters, non-uppercase properties, missing tree structure, escape sequences in text, whitespace handling in text |
| Test Name | Status |
|---|---|
test_all_lowercase_property | Fail |
test_empty_input | Skip |
test_escaped_backslash_in_property_value_becomes_just_a_backslash | Skip |
test_escaped_closing_bracket_within_property_value_becomes_just_a_closing_bracket | Skip |
test_escaped_newline_in_property_value_is_converted_to_nothing_at_all | Skip |
test_escaped_t_and_n_in_property_value_are_just_letters_not_whitespace | Skip |
test_escaped_tab_in_property_value_is_converted_to_space | Skip |
test_mixing_various_kinds_of_whitespace_and_escaped_characters_in_property_value | Skip |
test_multiple_properties | Skip |
test_multiple_property_values | Skip |
test_node_without_properties | Skip |
test_node_without_tree | Skip |
test_opening_bracket_within_property_value_doesn_t_need_to_be_escaped | Skip |
test_parentheses_in_property_value_don_t_need_to_be_escaped | Skip |
test_properties_without_delimiter | Skip |
test_semicolon_in_property_value_doesn_t_need_to_be_escaped | Skip |
test_single_node_tree | Skip |
test_tree_with_no_nodes | Skip |
test_two_child_trees | Skip |
test_two_nodes | Skip |
test_upper_and_lowercase_property | Skip |
test_within_property_values_newlines_remain_as_newlines | Skip |
test_within_property_values_whitespace_characters_such_as_tab_are_converted_to_spaces | Skip |
Loading...
Ridges.AI© 2025 Ridges AI. Building the future of decentralized AI development.
