jackson

Finished
2626
2727
2828def parse(input_string: str) -> SgfTree:
29 pass
29 # Edge Case: Empty input
30 if not input_string:
31 raise ValueError("tree missing")
32
33 # Edge Case: Input doesn't start with '('
34 if not input_string.startswith('('):
35 raise ValueError("tree missing")
36
37 # Edge Case: Input doesn't end with ')'
38 if not input_string.endswith(')'):
39 raise ValueError("tree missing")
40
41 # Remove the outer parentheses
42 content = input_string[1:-1]
43
44 # Edge Case: Empty tree with no nodes
45 if not content:
46 raise ValueError("tree with no nodes")
47
48 # Parse the tree
49 tree, _ = _parse_tree(content, 0)
50 return tree
51
52
53def _parse_tree(content: str, index: int) -> tuple[SgfTree, int]:
54 # Edge Case: Unexpected end of content
55 if index >= len(content):
56 raise ValueError("tree missing")
57
58 # Edge Case: Node doesn't start with ';'
59 if content[index] != ';':
60 raise ValueError("tree missing")
61
62 index += 1 # Skip the ';'
63
64 # Parse properties
65 properties = {}
66 while index < len(content):
67 # Check if we've reached the end of properties (start of children)
68 if content[index] in '();':
69 break
70
71 # Parse key - must be uppercase
72 key_start = index
73 while index < len(content) and content[index].isalpha():
74 if not content[index].isupper():
75 raise ValueError("property must be in uppercase")
76 index += 1
77
78 # Edge Case: Key is empty or not found
79 if index == key_start:
80 raise ValueError("properties without delimiter")
81
82 key = content[key_start:index]
83
84 # Parse values
85 values = []
86 has_values = False
87 while index < len(content) and content[index] == '[':
88 has_values = True
89 index += 1 # Skip '['
90
91 # Parse value with escape sequences
92 value_parts = []
93 while index < len(content) and content[index] != ']':
94 if content[index] == '\\':
95 index += 1
96 if index >= len(content):
97 raise ValueError("properties without delimiter")
98
99 char = content[index]
100 # In SGF, backslash escapes the next character
101 # For certain characters like :, the backslash is preserved
102 if char == ':':
103 value_parts.append('\\')
104 value_parts.append(char)
105 else:
106 # For other characters like ], the backslash is consumed
107 value_parts.append(char)
108 else:
109 # Handle whitespace according to SGF rules
110 if content[index] == '\n':
111 value_parts.append('\n')
112 elif content[index] in ' \t\r':
113 value_parts.append(' ')
114 else:
115 value_parts.append(content[index])
116 index += 1
117
118 # Edge Case: Unclosed value bracket
119 if index >= len(content) or content[index] != ']':
120 raise ValueError("properties without delimiter")
121
122 value = ''.join(value_parts)
123 values.append(value)
124 index += 1 # Skip ']'
125
126 # Edge Case: No values for a key
127 if not has_values:
128 raise ValueError("properties without delimiter")
129
130 properties[key] = values
131
132 # Check for proper delimiter after property
133 if index < len(content) and content[index] not in '();[' and not content[index].isupper():
134 # Invalid character after property
135 raise ValueError("properties without delimiter")
136 elif index < len(content) and content[index].isalpha() and not content[index].isupper():
137 # Lowercase letter after property
138 raise ValueError("property must be in uppercase")
139 elif index < len(content) and content[index] == '[':
140 # Another value for the same property
141 pass
142 # Note: Consecutive properties without semicolons are allowed in SGF
143
144 # Parse children
145 children = []
146 while index < len(content):
147 if content[index] == '(':
148 # Parse variation (child node in parentheses)
149 index += 1 # Skip '('
150 child, index = _parse_tree(content, index)
151 children.append(child)
152
153 # Skip the closing ')'
154 if index >= len(content) or content[index] != ')':
155 raise ValueError("tree missing")
156 index += 1
157 elif content[index] == ';':
158 # Parse sequential node (child node without parentheses)
159 child, index = _parse_tree(content, index)
160 children.append(child)
161 else:
162 # No more children
163 break
164
165 return SgfTree(properties, children), index
166# Handled Edge Cases: Empty input, Input doesn't start with '(', Input doesn't end with ')', Empty tree with no nodes, Unexpected end of content, Node doesn't start with ';', Key is empty, Key is not uppercase, Unclosed value bracket, No values for a key, Missing closing parenthesis for child nodes
Test NameStatus
test_escaped_newline_in_property_value_is_converted_to_nothing_at_all
Fail
test_escaped_tab_in_property_value_is_converted_to_space
Fail
test_mixing_various_kinds_of_whitespace_and_escaped_characters_in_property_value
Fail
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_t_and_n_in_property_value_are_just_letters_not_whitespace
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.