| 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 = content[:node_end] | |
| 44 | remaining_content = content[node_end:] | |
| 45 | ||
| 46 | properties = parse_properties(first_node_content) | |
| 47 | children = [] | |
| 48 | ||
| 49 | # Parse children | |
| 50 | while remaining_content: | |
| 51 | if remaining_content.startswith('('): | |
| 52 | # Find the matching closing parenthesis for this child | |
| 53 | balance = 1 | |
| 54 | i = 1 | |
| 55 | # Add a safety check to prevent infinite loop | |
| 56 | max_iterations = len(remaining_content) | |
| 57 | iterations = 0 | |
| 58 | while i < len(remaining_content) and balance > 0 and iterations < max_iterations: | |
| 59 | if remaining_content[i] == '(': | |
| 60 | balance += 1 | |
| 61 | elif remaining_content[i] == ')': | |
| 62 | balance -= 1 | |
| 63 | i += 1 | |
| 64 | iterations += 1 | |
| 65 | if balance != 0: | |
| 66 | raise ValueError("tree missing") | |
| 67 | child_tree = parse(remaining_content[:i]) | |
| 68 | children.append(child_tree) | |
| 69 | remaining_content = remaining_content[i:] | |
| 70 | else: | |
| 71 | raise ValueError("tree missing") | |
| 72 | ||
| 73 | return SgfTree(properties=properties, children=children) | |
| 74 | ||
| 75 | ||
| 76 | def find_node_end(content): | |
| 77 | # Find where the first node ends (before any children) | |
| 78 | i = 0 | |
| 79 | while i < len(content): | |
| 80 | if content[i] == '(': | |
| 81 | return i | |
| 82 | i += 1 | |
| 83 | return len(content) | |
| 84 | ||
| 85 | ||
| 86 | def parse_properties(content): | |
| 87 | properties = {} | |
| 88 | i = 0 | |
| 89 | # Add a safety check to prevent infinite loop | |
| 90 | max_iterations = len(content) + 10 # Some buffer for safety | |
| 91 | iterations = 0 | |
| 92 | while i < len(content) and iterations < max_iterations: | |
| 93 | if content[i] != ';': | |
| 94 | raise ValueError("properties without delimiter") | |
| 95 | i += 1 | |
| 96 | ||
| 97 | # Parse key | |
| 98 | key_start = i | |
| 99 | while i < len(content) and content[i].isalpha(): | |
| 100 | i += 1 | |
| 101 | key = content[key_start:i] | |
| 102 | ||
| 103 | if not key: | |
| 104 | raise ValueError("properties without delimiter") | |
| 105 | ||
| 106 | if not key.isupper(): | |
| 107 | raise ValueError("property must be in uppercase") | |
| 108 | ||
| 109 | # Parse values | |
| 110 | values = [] | |
| 111 | while i < len(content) and content[i] == '[': | |
| 112 | i += 1 # Skip '[' | |
| 113 | value_start = i | |
| 114 | # Find the closing ']' | |
| 115 | while i < len(content) and content[i] != ']': | |
| 116 | i += 1 | |
| 117 | if i >= len(content): | |
| 118 | raise ValueError("properties without delimiter") | |
| 119 | value = parse_text_value(content[value_start:i]) | |
| 120 | values.append(value) | |
| 121 | i += 1 # Skip ']' | |
| 122 | ||
| 123 | if not values: | |
| 124 | raise ValueError("properties without delimiter") | |
| 125 | ||
| 126 | properties[key] = values | |
| 127 | iterations += 1 | |
| 128 | ||
| 129 | # Additional safety check | |
| 130 | if iterations >= max_iterations: | |
| 131 | raise ValueError("properties without delimiter") | |
| 132 | ||
| 133 | return properties | |
| 134 | ||
| 135 | ||
| 136 | def parse_text_value(text): | |
| 137 | result = [] | |
| 138 | i = 0 | |
| 139 | # Add a safety check to prevent infinite loop | |
| 140 | max_iterations = len(text) + 10 # Some buffer for safety | |
| 141 | iterations = 0 | |
| 142 | while i < len(text) and iterations < max_iterations: | |
| 143 | if text[i] == '\\': | |
| 144 | # Handle escape sequence | |
| 145 | i += 1 | |
| 146 | if i < len(text): | |
| 147 | char = text[i] | |
| 148 | if char == 'n': | |
| 149 | result.append('\n') | |
| 150 | elif char == 't': | |
| 151 | result.append('\t') | |
| 152 | elif char == 'r': | |
| 153 | result.append('\r') | |
| 154 | elif char.isspace(): | |
| 155 | # Whitespace after \ is removed if it's a newline, otherwise converted to space | |
| 156 | if char == '\n': | |
| 157 | pass # Remove it | |
| 158 | else: | |
| 159 | result.append(' ') | |
| 160 | else: | |
| 161 | result.append(char) | |
| 162 | i += 1 | |
| 163 | else: | |
| 164 | # Trailing backslash | |
| 165 | result.append('\\') | |
| 166 | i += 1 | |
| 167 | else: | |
| 168 | char = text[i] | |
| 169 | if char == '\n': | |
| 170 | result.append('\n') | |
| 171 | elif char.isspace(): | |
| 172 | result.append(' ') | |
| 173 | else: | |
| 174 | result.append(char) | |
| 175 | i += 1 | |
| 176 | iterations += 1 | |
| 177 | ||
| 178 | # Additional safety check | |
| 179 | if iterations >= max_iterations: | |
| 180 | raise ValueError("Invalid text value") | |
| 181 | ||
| 182 | return ''.join(result) | |
| 183 | ||
| 184 | # Handled Edge Cases: Empty input, missing parentheses, no nodes, missing delimiters, lowercase properties, malformed properties, escape sequences |
| Test Name | Status |
|---|---|
test_escaped_closing_bracket_within_property_value_becomes_just_a_closing_bracket | Fail |
test_all_lowercase_property | Pass |
test_empty_input | Pass |
test_escaped_backslash_in_property_value_becomes_just_a_backslash | Pass |
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.
