rockstar

Finished
33 self.properties = properties or {}
44 self.children = children or []
55
6
7
68 def __eq__(self, other):
79 if not isinstance(other, SgfTree):
810 return False
2628
2729
2830def parse(input_string: str) -> SgfTree:
29 pass
31 """
32 Parse an SGF string and return a tree structure of properties.
33
34 Args:
35 input_string: A string in SGF format
36
37 Returns:
38 SgfTree: The parsed SGF tree
39
40 Raises:
41 ValueError: If the input is malformed or doesn't follow SGF format rules
42 """
43 # Edge Case: Empty input
44 if not input_string:
45 raise ValueError("tree missing")
46
47 # Edge Case: Input doesn't start with '('
48 if not input_string.startswith('('):
49 raise ValueError("tree missing")
50
51 # Edge Case: Input doesn't end with ')'
52 if not input_string.endswith(')'):
53 raise ValueError("tree missing")
54
55 # Remove the outer parentheses
56 content = input_string[1:-1]
57
58 # Edge Case: Empty content after removing parentheses
59 if not content:
60 raise ValueError("tree with no nodes")
61
62 # Parse the tree
63 tree, _ = _parse_tree(content, 0)
64 return tree
65
66
67def _parse_tree(content: str, index: int) -> tuple[SgfTree, int]:
68 """
69 Parse a tree from the content string starting at index.
70
71 Args:
72 content: The SGF content string
73 index: The starting index to parse from
74
75 Returns:
76 tuple: (SgfTree, next_index)
77 """
78 # Edge Case: Missing semicolon at the beginning of a node
79 if index >= len(content) or content[index] != ';':
80 raise ValueError("tree missing")
81
82 index += 1 # Skip the semicolon
83
84 # Parse properties of the current node
85 properties = {}
86 while index < len(content) and content[index].isalpha():
87 # Parse key
88 key_start = index
89 while index < len(content) and content[index].isalpha():
90 index += 1
91
92 key = content[key_start:index]
93
94 # Edge Case: Key is not in uppercase
95 if not key.isupper():
96 raise ValueError("property must be in uppercase")
97
98 # Edge Case: Missing opening bracket for property value
99 if index >= len(content) or content[index] != '[':
100 raise ValueError("properties without delimiter")
101
102 # Parse values
103 values = []
104 while index < len(content) and content[index] == '[':
105 index += 1 # Skip opening bracket
106
107 # Parse value content, handling escapes
108 value_chars = []
109 while index < len(content) and content[index] != ']':
110 if content[index] == '\\':
111 # Handle escape sequences
112 index += 1
113 if index >= len(content):
114 raise ValueError("properties without delimiter")
115
116 # Check if the next character is a newline
117 if content[index] == '\n':
118 # Newline immediately after backslash is removed
119 # Don't add anything to value_chars
120 pass
121 elif content[index].isspace():
122 # Other whitespace after backslash is converted to spaces
123 value_chars.append(' ')
124 else:
125 # Any non-whitespace character after backslash is inserted as-is
126 value_chars.append(content[index])
127 else:
128 # Convert whitespace (except newlines) to spaces
129 if content[index].isspace() and content[index] != '\n':
130 value_chars.append(' ')
131 else:
132 value_chars.append(content[index])
133 index += 1
134
135 # Edge Case: Missing closing bracket
136 if index >= len(content) or content[index] != ']':
137 raise ValueError("properties without delimiter")
138
139 values.append(''.join(value_chars))
140 index += 1 # Skip closing bracket
141
142 properties[key] = values
143
144 # Parse children - either variations in parentheses or sequential nodes
145 children = []
146
147 # First handle variations (nodes in parentheses)
148 while index < len(content) and content[index] == '(':
149 # Parse child tree
150 child_tree, index = _parse_tree(content, index+1) # Skip opening parenthesis
151
152 # Find closing parenthesis
153 if index >= len(content) or content[index] != ')':
154 raise ValueError("tree missing")
155
156 children.append(child_tree)
157 index += 1 # Skip closing parenthesis
158
159 # Then handle sequential nodes (nodes separated by semicolons)
160 if index < len(content) and content[index] == ';':
161 # Parse the next sequential node
162 next_node, index = _parse_tree(content, index)
163 children.append(next_node)
164
165 return SgfTree(properties, children), index
166
167# Handled Edge Cases: Empty input, Input doesn't start with '(', Input doesn't end with ')', Empty content after removing parentheses, Missing semicolon at the beginning of a node, Key is not in uppercase, Missing opening bracket for property value, Missing closing bracket, Missing closing parenthesis for tree"
Test NameStatus
test_all_lowercase_property
Pass
test_empty_input
Pass
test_escaped_backslash_in_property_value_becomes_just_a_backslash
Pass
test_escaped_closing_bracket_within_property_value_becomes_just_a_closing_bracket
Pass
test_escaped_newline_in_property_value_is_converted_to_nothing_at_all
Pass
test_escaped_t_and_n_in_property_value_are_just_letters_not_whitespace
Pass
test_escaped_tab_in_property_value_is_converted_to_space
Pass
test_mixing_various_kinds_of_whitespace_and_escaped_characters_in_property_value
Pass
test_multiple_properties
Pass
test_multiple_property_values
Pass
test_node_without_properties
Pass
test_node_without_tree
Pass
test_opening_bracket_within_property_value_doesn_t_need_to_be_escaped
Pass
test_parentheses_in_property_value_don_t_need_to_be_escaped
Pass
test_properties_without_delimiter
Pass
test_semicolon_in_property_value_doesn_t_need_to_be_escaped
Pass
test_single_node_tree
Pass
test_tree_with_no_nodes
Pass
test_two_child_trees
Pass
test_two_nodes
Pass
test_upper_and_lowercase_property
Pass
test_within_property_values_newlines_remain_as_newlines
Pass
test_within_property_values_whitespace_characters_such_as_tab_are_converted_to_spaces
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.