hope

Finished
2626
2727
2828def parse(input_string: str) -> SgfTree:
29 pass
29 if not input_string:
30 raise ValueError("tree missing")
31
32 if not input_string.startswith('('):
33 raise ValueError("tree missing")
34
35 tree, pos = _parse_tree(input_string, 1)
36
37 if pos >= len(input_string) or input_string[pos] != ')':
38 raise ValueError("tree missing")
39
40 pos += 1 # Skip ')'
41
42 if pos < len(input_string):
43 raise ValueError("tree missing")
44
45 return tree
46
47
48def _parse_tree(s: str, pos: int) -> tuple[SgfTree, int]:
49 if pos >= len(s) or s[pos] != ';':
50 raise ValueError("tree with no nodes")
51
52 pos += 1 # Skip ';'
53 properties = {}
54 children = []
55
56 # Parse properties
57 while pos < len(s) and s[pos].isalpha():
58 prop, pos = _parse_property(s, pos)
59 key, values = prop
60
61 if key in properties:
62 raise ValueError("properties without delimiter")
63
64 properties[key] = values
65
66 # Parse children
67 # First check for shorthand notation (consecutive nodes without parentheses)
68 if pos < len(s) and s[pos] == ';':
69 child, pos = _parse_tree(s, pos)
70 children.append(child)
71 # Then check for variations (parenthesized children)
72 elif pos < len(s) and s[pos] == '(':
73 while pos < len(s) and s[pos] == '(':
74 child, pos = _parse_tree(s, pos + 1) # Skip '('
75 children.append(child)
76
77 if pos >= len(s) or s[pos] != ')':
78 raise ValueError("tree missing")
79 pos += 1 # Skip ')'
80
81 return SgfTree(properties, children), pos
82
83
84def _parse_property(s: str, pos: int) -> tuple[tuple[str, list[str]], int]:
85 # Parse key
86 key_start = pos
87 while pos < len(s) and s[pos].isalpha():
88 pos += 1
89
90 if pos == key_start:
91 raise ValueError("properties without delimiter")
92
93 key = s[key_start:pos]
94
95 # Check if property key is uppercase
96 if not key.isupper():
97 raise ValueError("property must be in uppercase")
98
99 if pos >= len(s) or s[pos] != '[':
100 raise ValueError("properties without delimiter")
101
102 # Parse values
103 values = []
104 while pos < len(s) and s[pos] == '[':
105 pos += 1 # Skip '['
106 value, pos = _parse_value(s, pos)
107 values.append(value)
108
109 if pos >= len(s) or s[pos] != ']':
110 raise ValueError("properties without delimiter")
111 pos += 1 # Skip ']'
112
113 return (key, values), pos
114
115
116def _parse_value(s: str, pos: int) -> tuple[str, int]:
117 result = []
118 while pos < len(s) and s[pos] != ']':
119 if s[pos] == '\\':
120 pos += 1 # Skip '\\'
121 if pos >= len(s):
122 result.append('\\')
123 break
124
125 char = s[pos]
126 if char == 'n':
127 # \n - escaped newline is removed and replaced with space
128 result.append(' ')
129 elif char.isspace():
130 # Other whitespace after backslash becomes space
131 result.append(' ')
132 else:
133 # Non-whitespace character after backslash is inserted as-is with backslash
134 result.append('\\' + char)
135 elif s[pos] == '\n':
136 # Unescaped newline
137 result.append('\n')
138 elif s[pos].isspace() and s[pos] != '\n':
139 # Non-newline whitespace converted to space
140 result.append(' ')
141 else:
142 # Regular character
143 result.append(s[pos])
144 pos += 1
145
146 return ''.join(result), pos
Test NameStatus
test_escaped_backslash_in_property_value_becomes_just_a_backslash
Fail
test_escaped_closing_bracket_within_property_value_becomes_just_a_closing_bracket
Fail
test_escaped_newline_in_property_value_is_converted_to_nothing_at_all
Fail
test_escaped_t_and_n_in_property_value_are_just_letters_not_whitespace
Fail
test_mixing_various_kinds_of_whitespace_and_escaped_characters_in_property_value
Fail
test_opening_bracket_within_property_value_doesn_t_need_to_be_escaped
Fail
test_all_lowercase_property
Pass
test_empty_input
Pass
test_escaped_tab_in_property_value_is_converted_to_space
Pass
test_multiple_properties
Pass
test_multiple_property_values
Pass
test_node_without_properties
Pass
test_node_without_tree
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.