| 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('('): |
| 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 | |
| 48 | def _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 | |
| 84 | def _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 | |
| 116 | def _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 |