| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | |
| 5 | // Wrapped inputs share one immutable document and keep indexed captures. |
| 6 | // Partially unwrapped inputs retain original Values in a lazy sidecar. Both |
| 7 | // paths reuse the same repetition coordinates without copying subtrees. |
| 8 | struct SyntaxCaseCaptures { |
| 9 | pattern &SyntaxPattern |
| 10 | mut: |
| 11 | input &SyntaxObjectData = unsafe { nil } |
| 12 | coordinates SyntaxCaptures |
| 13 | values &SyntaxCaseValues = unsafe { nil } |
| 14 | } |
| 15 | |
| 16 | fn (captures &SyntaxCaseCaptures) value(variable int, indices []int) !Value { |
| 17 | return captures.value_at(variable, indices, 0) |
| 18 | } |
| 19 | |
| 20 | fn (captures &SyntaxCaseCaptures) value_at(variable int, indices []int, offset int) !Value { |
| 21 | if offset < 0 || offset > indices.len { return failure('syntax-case capture rank mismatch') } |
| 22 | if variable < 0 || variable >= captures.pattern.variables.len { |
| 23 | return failure('invalid syntax-case capture variable') |
| 24 | } |
| 25 | if indices.len - offset != captures.pattern.variables[variable].depth { |
| 26 | return failure('syntax-case capture rank mismatch') |
| 27 | } |
| 28 | mut path := i32(0) |
| 29 | for i in offset .. indices.len { |
| 30 | index := indices[i] |
| 31 | count := captures.coordinates.items[syntax_capture_key(variable, path)] or { return failure('missing syntax-case capture dimension') } |
| 32 | if index < 0 || index >= count { return failure('syntax-case capture index out of range') } |
| 33 | path = captures.coordinates.paths[(u64(u32(path)) << 32) | u64(u32(index))] or { return failure('missing syntax-case capture coordinate') } |
| 34 | } |
| 35 | if !isnil(captures.values) { |
| 36 | return captures.values.items[syntax_capture_key(variable, path)] or { return failure('missing syntax-case capture') } |
| 37 | } |
| 38 | index := captures.coordinates.items[syntax_capture_key(variable, path)] or { return failure('missing syntax-case capture') } |
| 39 | return wrapped_syntax(captures.input, int(index)) |
| 40 | } |
| 41 | |
| 42 | struct SyntaxCaseMatcher { |
| 43 | tree &SyntaxTree |
| 44 | pattern &SyntaxPattern |
| 45 | literals map[i32]SyntaxBinding |
| 46 | owner &Owner = unsafe { nil } |
| 47 | input &SyntaxObjectData = unsafe { nil } |
| 48 | } |
| 49 | |
| 50 | fn (matcher &SyntaxCaseMatcher) matches(mut captures SyntaxCaseCaptures, input i32) !bool { |
| 51 | if voidptr(captures.input) != voidptr(matcher.input) || voidptr(captures.pattern) != voidptr(matcher.pattern) { |
| 52 | return failure('syntax-case capture document mismatch') |
| 53 | } |
| 54 | return match_case_node(matcher, matcher.input, matcher.pattern.root, input, 0, mut captures) |
| 55 | } |
| 56 | |
| 57 | fn match_case_node[T](matcher &SyntaxCaseMatcher, source &T, id i32, input i32, path i32, mut captures SyntaxCaseCaptures) !bool { |
| 58 | if isnil(matcher.pattern.constants) { |
| 59 | return match_case_body[T, PlainSyntaxMatch](matcher, source, id, input, path, mut captures, unsafe { nil }) |
| 60 | } |
| 61 | mut memo := SyntaxMatchMemo{} |
| 62 | defer { unsafe { memo.completed.free() } } |
| 63 | return match_case_cached[T, SharedSyntaxMatch](matcher, source, id, input, path, mut captures, &memo) |
| 64 | } |
| 65 | |
| 66 | @[inline] |
| 67 | fn match_case_cached[T, M](matcher &SyntaxCaseMatcher, source &T, id i32, input i32, path i32, mut captures SyntaxCaseCaptures, cache &SyntaxMatchMemo) !bool { |
| 68 | $if M is SharedSyntaxMatch { |
| 69 | mut memo := unsafe { cache } |
| 70 | if u64(u32(id)) in matcher.pattern.constants.nodes { |
| 71 | key := syntax_match_key(id, input) |
| 72 | if result := memo.completed[key] { return result } |
| 73 | result := match_case_body[T, M](matcher, source, id, input, path, mut captures, cache)! |
| 74 | memo.completed[key] = result |
| 75 | return result |
| 76 | } |
| 77 | } |
| 78 | return match_case_body[T, M](matcher, source, id, input, path, mut captures, cache) |
| 79 | } |
| 80 | |
| 81 | fn match_case_body[T, M](matcher &SyntaxCaseMatcher, source &T, id i32, input i32, path i32, mut captures SyntaxCaseCaptures, cache &SyntaxMatchMemo) !bool { |
| 82 | pattern := matcher.tree.nodes[id] |
| 83 | node := case_input_node(source, input)! |
| 84 | match pattern.kind { |
| 85 | .symbol { |
| 86 | if literal := matcher.literals[pattern.car] { |
| 87 | if node.kind != .symbol { return false } |
| 88 | return case_input_literal(matcher, source, input, literal) |
| 89 | } |
| 90 | if matcher.tree.wildcard(id) { return true } |
| 91 | variable := matcher.pattern.indices[pattern.car] or { return failure('invalid syntax-case pattern variable') } |
| 92 | capture_case_value(source, input, variable, path, mut captures)! |
| 93 | return true |
| 94 | } |
| 95 | .pair { |
| 96 | mut cursor := id |
| 97 | mut next := input |
| 98 | for matcher.tree.nodes[cursor].kind == .pair { |
| 99 | pair := matcher.tree.nodes[cursor] |
| 100 | tail := matcher.tree.nodes[pair.cdr] |
| 101 | if tail.kind == .pair && matcher.tree.ellipsis(tail.car) { |
| 102 | variables := matcher.pattern.repeats[pair.car] |
| 103 | mut suffix := 0 |
| 104 | mut rest := tail.cdr |
| 105 | for matcher.tree.nodes[rest].kind == .pair { |
| 106 | suffix++ |
| 107 | rest = matcher.tree.nodes[rest].cdr |
| 108 | } |
| 109 | mut remaining := -1 |
| 110 | if suffix > 0 { |
| 111 | mut available := 0 |
| 112 | mut probe := next |
| 113 | mut guard := SyntaxCaseSpine{ checkpoint: next } |
| 114 | for case_input_node(source, probe)!.kind == .pair { |
| 115 | available++ |
| 116 | probe = case_input_node(source, probe)!.cdr |
| 117 | guard.step(probe)! |
| 118 | } |
| 119 | if available < suffix { return false } |
| 120 | remaining = available - suffix |
| 121 | } |
| 122 | mut count := 0 |
| 123 | mut guard := SyntaxCaseSpine{ checkpoint: next } |
| 124 | for remaining != 0 && case_input_node(source, next)!.kind == .pair { |
| 125 | item := case_input_node(source, next)! |
| 126 | child := if variables.len > 0 { |
| 127 | captures.coordinates.child_path(path, count)! |
| 128 | } else { |
| 129 | path |
| 130 | } |
| 131 | if !match_case_cached[T, M](matcher, source, pair.car, item.car, child, mut captures, cache)! { |
| 132 | return false |
| 133 | } |
| 134 | next = item.cdr |
| 135 | if remaining < 0 { guard.step(next)! } |
| 136 | count++ |
| 137 | if remaining > 0 { remaining-- } |
| 138 | } |
| 139 | for variable in variables { |
| 140 | captures.coordinates.items[syntax_capture_key(variable, path)] = i32(count) |
| 141 | } |
| 142 | cursor = tail.cdr |
| 143 | continue |
| 144 | } |
| 145 | item := case_input_node(source, next)! |
| 146 | if item.kind != .pair { return false } |
| 147 | if !match_case_cached[T, M](matcher, source, pair.car, item.car, path, mut captures, cache)! { |
| 148 | return false |
| 149 | } |
| 150 | cursor = pair.cdr |
| 151 | next = item.cdr |
| 152 | } |
| 153 | return match_case_cached[T, M](matcher, source, cursor, next, path, mut captures, cache) |
| 154 | } |
| 155 | .vector { |
| 156 | if node.kind != .vector { return false } |
| 157 | mut repeated_at := -1 |
| 158 | for position in 1 .. int(pattern.cdr) { |
| 159 | if matcher.tree.ellipsis(matcher.tree.elements[pattern.car + position]) { |
| 160 | repeated_at = position - 1 |
| 161 | break |
| 162 | } |
| 163 | } |
| 164 | fixed := int(pattern.cdr) - if repeated_at >= 0 { 2 } else { 0 } |
| 165 | if node.count < fixed || (repeated_at < 0 && node.count != fixed) { return false } |
| 166 | width := node.count - fixed |
| 167 | mut index := 0 |
| 168 | for index < int(pattern.cdr) { |
| 169 | body := matcher.tree.elements[pattern.car + index] |
| 170 | if index == repeated_at { |
| 171 | variables := matcher.pattern.repeats[body] |
| 172 | for offset in 0 .. width { |
| 173 | child := if variables.len > 0 { |
| 174 | captures.coordinates.child_path(path, offset)! |
| 175 | } else { |
| 176 | path |
| 177 | } |
| 178 | if !match_case_cached[T, M](matcher, source, body, case_input_vector_at(source, input, index + offset)!, child, mut captures, cache)! { |
| 179 | return false |
| 180 | } |
| 181 | } |
| 182 | for variable in variables { |
| 183 | captures.coordinates.items[syntax_capture_key(variable, path)] = i32(width) |
| 184 | } |
| 185 | index += 2 |
| 186 | } else { |
| 187 | position := if repeated_at >= 0 && index > repeated_at { |
| 188 | index - 2 + width |
| 189 | } else { |
| 190 | index |
| 191 | } |
| 192 | if !match_case_cached[T, M](matcher, source, body, case_input_vector_at(source, input, position)!, path, mut captures, cache)! { |
| 193 | return false |
| 194 | } |
| 195 | index++ |
| 196 | } |
| 197 | } |
| 198 | return true |
| 199 | } |
| 200 | .empty_list { return node.kind == .empty_list } |
| 201 | else { |
| 202 | if node.kind != pattern.kind { return false } |
| 203 | expected := matcher.tree.values[pattern.car] |
| 204 | actual := case_input_atom(source, input)! |
| 205 | return syntax_pattern_atom_equal(expected, actual) |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // Brent cycle detection follows the same next index as the streaming match. |
| 211 | // Trailing repetitions use one pass and no auxiliary set or preliminary walk. |
| 212 | struct SyntaxCaseSpine { |
| 213 | mut: |
| 214 | checkpoint i32 |
| 215 | distance int |
| 216 | power int = 1 |
| 217 | } |
| 218 | |
| 219 | @[inline] |
| 220 | fn (mut guard SyntaxCaseSpine) step(next i32) ! { |
| 221 | guard.distance++ |
| 222 | if next == guard.checkpoint { return failure('cyclic syntax-case repetition input') } |
| 223 | if guard.distance == guard.power { |
| 224 | guard.checkpoint = next |
| 225 | guard.distance = 0 |
| 226 | guard.power *= 2 |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // Scalar inputs need neither a registry nor a work arena. Literal identifiers |
| 231 | // only match wrapped identifiers; this path handles unwrapped scalar values. |
| 232 | fn (matcher &SyntaxCaseMatcher) match_scalar(value Value, mut captures SyntaxCaseCaptures) !bool { |
| 233 | node := matcher.tree.nodes[matcher.pattern.root] |
| 234 | if node.kind == .symbol { |
| 235 | if node.car in matcher.literals { return false } |
| 236 | if matcher.tree.wildcard(matcher.pattern.root) { return true } |
| 237 | variable := matcher.pattern.indices[node.car] or { return failure('invalid syntax-case pattern variable') } |
| 238 | capture_case_raw(value, variable, 0, mut captures) |
| 239 | return true |
| 240 | } |
| 241 | if node.kind == .empty_list { return value.tag == .nil } |
| 242 | if node.kind in [.pair, .vector] { return false } |
| 243 | return syntax_pattern_atom_equal(matcher.tree.values[node.car], value) |
| 244 | } |
| 245 | |
| 246 | // Raw pair/vector syntax needs a temporary structural view. Captures retain |
| 247 | // original Values in their own sidecar, never indexes into the freed view. |
| 248 | // Fully wrapped inputs retain the original single-document capture path. |
| 249 | fn (matcher &SyntaxCaseMatcher) matches_value(value Value, mut captures SyntaxCaseCaptures) !bool { |
| 250 | if voidptr(captures.pattern) != voidptr(matcher.pattern) { |
| 251 | return failure('syntax-case capture pattern mismatch') |
| 252 | } |
| 253 | if !isnil(captures.input) || !isnil(captures.values) || captures.coordinates.items.len != 0 || captures.coordinates.paths.len != 0 { |
| 254 | return failure('syntax-case needs a fresh capture frame') |
| 255 | } |
| 256 | if value.tag == .syntax { |
| 257 | object := syntax_object_ref(value)! |
| 258 | captures.input = object.data |
| 259 | current := SyntaxCaseMatcher{ ...matcher, input: object.data } |
| 260 | return current.matches(mut captures, object.index) |
| 261 | } |
| 262 | if value.tag !in [.pair, .vector, .nil] { |
| 263 | return matcher.match_scalar(value, mut captures) |
| 264 | } |
| 265 | mut storage := arena.new(chunk_size: 1024) |
| 266 | defer { storage.free() } |
| 267 | source := new_syntax_match_source(value, storage) |
| 268 | return match_case_node(matcher, source, matcher.pattern.root, 0, 0, mut captures) |
| 269 | } |
| 270 | |
| 271 | fn new_syntax_match_source(value Value, storage arena.Arena) &SyntaxValueSource { |
| 272 | storage.push() |
| 273 | defer { storage.pop() } |
| 274 | source := &SyntaxValueSource{ root: value, storage: storage } |
| 275 | unsafe { source.nodes.flags.set(.noslices) } |
| 276 | return source |
| 277 | } |
| 278 | |