| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | import arena as alloc_arena |
| 5 | |
| 6 | struct SyntaxTemplate { |
| 7 | root i32 |
| 8 | mut: |
| 9 | sharing &SyntaxTemplateSharing = unsafe { nil } |
| 10 | // The same graph fragment may occur at different ellipsis depths. |
| 11 | repeats map[u64][]SyntaxDriver |
| 12 | } |
| 13 | |
| 14 | struct TemplateJob { |
| 15 | id i32 |
| 16 | depth int |
| 17 | nesting int |
| 18 | escaped bool |
| 19 | } |
| 20 | |
| 21 | struct SyntaxDriver { |
| 22 | variable i32 |
| 23 | offset i32 |
| 24 | } |
| 25 | |
| 26 | fn (tree &SyntaxTree) compile_template(root i32, pattern &SyntaxPattern) !SyntaxTemplate { |
| 27 | return tree.compile_template_grammar(root, pattern, false) |
| 28 | } |
| 29 | |
| 30 | fn (tree &SyntaxTree) compile_case_template_shape(root i32, pattern &SyntaxPattern) !SyntaxTemplate { |
| 31 | return tree.compile_template_grammar(root, pattern, true) |
| 32 | } |
| 33 | |
| 34 | fn (tree &SyntaxTree) compile_template_grammar(root i32, pattern &SyntaxPattern, consecutive bool) !SyntaxTemplate { |
| 35 | mut template := SyntaxTemplate{ root: root } |
| 36 | mut pending := [TemplateJob{root, 0, 0, false}] |
| 37 | mut visits := map[u64]int{} |
| 38 | defer { unsafe { visits.free() } } |
| 39 | for pending.len > 0 { |
| 40 | job := pending.pop() |
| 41 | if job.nesting > 512 || job.depth > 512 { |
| 42 | return failure('syntax template nesting limit exceeded') |
| 43 | } |
| 44 | node := tree.nodes[job.id] |
| 45 | if !tree.unique_subtrees && node.kind in [.pair, .vector] |
| 46 | && syntax_grammar_seen(mut visits, job.id, job.depth, job.escaped, job.nesting) { continue } |
| 47 | match node.kind { |
| 48 | .symbol { |
| 49 | if !job.escaped && tree.ellipsis(job.id) { |
| 50 | return failure('misplaced ellipsis in syntax-rules template') |
| 51 | } |
| 52 | if variable := pattern.indices[node.car] { |
| 53 | if pattern.variables[variable].depth > job.depth { |
| 54 | return failure('pattern variable needs more template ellipses') |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | .pair { |
| 59 | if !job.escaped && tree.ellipsis(node.car) { |
| 60 | escaped_body := tree.escaped_template(job.id)! |
| 61 | pending << TemplateJob{escaped_body, job.depth, job.nesting + 1, true} |
| 62 | continue |
| 63 | } |
| 64 | mut cursor := job.id |
| 65 | for tree.nodes[cursor].kind == .pair { |
| 66 | pair := tree.nodes[cursor] |
| 67 | tail := tree.nodes[pair.cdr] |
| 68 | if !job.escaped && tail.kind == .pair && tree.ellipsis(tail.car) { |
| 69 | levels, after := tree.template_list_repeat(tail.cdr, consecutive) |
| 70 | template.add_repeat_levels(tree, pattern, pair.car, job.depth, levels, consecutive)! |
| 71 | pending << TemplateJob{pair.car, job.depth + levels, job.nesting + 1, job.escaped} |
| 72 | cursor = after |
| 73 | } else { |
| 74 | pending << TemplateJob{pair.car, job.depth, job.nesting + 1, job.escaped} |
| 75 | cursor = pair.cdr |
| 76 | } |
| 77 | } |
| 78 | if tree.nodes[cursor].kind != .empty_list { |
| 79 | pending << TemplateJob{cursor, job.depth, job.nesting + 1, job.escaped} |
| 80 | } |
| 81 | } |
| 82 | .vector { |
| 83 | mut i := 0 |
| 84 | for i < int(node.cdr) { |
| 85 | body := tree.elements[node.car + i] |
| 86 | if !job.escaped && i + 1 < int(node.cdr) && tree.ellipsis(tree.elements[node.car + i + 1]) { |
| 87 | levels, after := tree.template_vector_repeat(node, i + 2, consecutive) |
| 88 | template.add_repeat_levels(tree, pattern, body, job.depth, levels, consecutive)! |
| 89 | pending << TemplateJob{body, job.depth + levels, job.nesting + 1, job.escaped} |
| 90 | i = after |
| 91 | } else { |
| 92 | pending << TemplateJob{body, job.depth, job.nesting + 1, job.escaped} |
| 93 | i++ |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | else {} |
| 98 | } |
| 99 | } |
| 100 | if !consecutive && !tree.unique_subtrees { |
| 101 | storage := alloc_arena.new(chunk_size: 1024) |
| 102 | defer { release_frozen_syntax_scratch(storage) } |
| 103 | traits, _ := tree.template_region_traits(root, pattern, storage)! |
| 104 | template.sharing = template_sharing(tree, traits, false) |
| 105 | } |
| 106 | return template |
| 107 | } |
| 108 | |
| 109 | fn (tree &SyntaxTree) template_list_repeat(tail i32, consecutive bool) (int, i32) { |
| 110 | mut levels := 1 |
| 111 | mut cursor := tail |
| 112 | if consecutive { |
| 113 | for tree.nodes[cursor].kind == .pair && tree.ellipsis(tree.nodes[cursor].car) { |
| 114 | levels++ |
| 115 | cursor = tree.nodes[cursor].cdr |
| 116 | } |
| 117 | } |
| 118 | return levels, cursor |
| 119 | } |
| 120 | |
| 121 | fn (tree &SyntaxTree) template_vector_repeat(node SyntaxTreeNode, start int, consecutive bool) (int, int) { |
| 122 | mut levels := 1 |
| 123 | mut cursor := start |
| 124 | if consecutive { |
| 125 | for cursor < int(node.cdr) && tree.ellipsis(tree.elements[node.car + cursor]) { |
| 126 | levels++ |
| 127 | cursor++ |
| 128 | } |
| 129 | } |
| 130 | return levels, cursor |
| 131 | } |
| 132 | |
| 133 | fn (mut template SyntaxTemplate) add_repeat(tree &SyntaxTree, pattern &SyntaxPattern, body i32, depth int) ! { |
| 134 | template.add_repeat_levels(tree, pattern, body, depth, 1, false)! |
| 135 | } |
| 136 | |
| 137 | fn (mut template SyntaxTemplate) add_repeat_levels(tree &SyntaxTree, pattern &SyntaxPattern, body i32, depth int, levels int, consecutive bool) ! { |
| 138 | if levels > 512 - depth { return failure('syntax template nesting limit exceeded') } |
| 139 | for level in 0 .. levels { |
| 140 | template.add_repeat_driver(tree, pattern, body, depth + level, depth + levels, consecutive)! |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | fn (mut template SyntaxTemplate) add_repeat_driver(tree &SyntaxTree, pattern &SyntaxPattern, body i32, depth int, body_depth int, consecutive bool) ! { |
| 145 | key := template_repeat_key(body, depth, body_depth) |
| 146 | if key in template.repeats { return } |
| 147 | mut drivers := []SyntaxDriver{} |
| 148 | mut seen := map[u64]bool{} |
| 149 | mut pending := [TemplateJob{body, body_depth, 0, false}] |
| 150 | mut visits := map[u64]int{} |
| 151 | defer { unsafe { visits.free() } } |
| 152 | for pending.len > 0 { |
| 153 | job := pending.pop() |
| 154 | if job.depth > 512 { return failure('syntax template nesting limit exceeded') } |
| 155 | node := tree.nodes[job.id] |
| 156 | if !tree.unique_subtrees && node.kind in [.pair, .vector] |
| 157 | && syntax_grammar_seen(mut visits, job.id, job.depth, job.escaped, 0) { continue } |
| 158 | match node.kind { |
| 159 | .symbol { |
| 160 | if variable := pattern.indices[node.car] { |
| 161 | rank := pattern.variables[variable].depth |
| 162 | offset := job.depth - rank |
| 163 | if rank > 0 && offset >= 0 && offset <= depth { |
| 164 | coordinate := syntax_capture_key(variable, i32(offset)) |
| 165 | if coordinate !in seen { |
| 166 | seen[coordinate] = true |
| 167 | drivers << SyntaxDriver{i32(variable), i32(offset)} |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | .pair { |
| 173 | if !job.escaped && tree.ellipsis(node.car) { |
| 174 | pending << TemplateJob{tree.escaped_template(job.id)!, job.depth, job.nesting + 1, true} |
| 175 | continue |
| 176 | } |
| 177 | mut cursor := job.id |
| 178 | for tree.nodes[cursor].kind == .pair { |
| 179 | pair := tree.nodes[cursor] |
| 180 | tail := tree.nodes[pair.cdr] |
| 181 | repeat := !job.escaped && tail.kind == .pair && tree.ellipsis(tail.car) |
| 182 | if repeat { |
| 183 | nested, after := tree.template_list_repeat(tail.cdr, consecutive) |
| 184 | pending << TemplateJob{pair.car, job.depth + nested, 0, job.escaped} |
| 185 | cursor = after |
| 186 | } else { |
| 187 | pending << TemplateJob{pair.car, job.depth, 0, job.escaped} |
| 188 | cursor = pair.cdr |
| 189 | } |
| 190 | } |
| 191 | if tree.nodes[cursor].kind != .empty_list { |
| 192 | pending << TemplateJob{cursor, job.depth, 0, job.escaped} |
| 193 | } |
| 194 | } |
| 195 | .vector { |
| 196 | mut i := 0 |
| 197 | for i < int(node.cdr) { |
| 198 | body_id := tree.elements[node.car + i] |
| 199 | repeat := !job.escaped && i + 1 < int(node.cdr) && tree.ellipsis(tree.elements[node.car + i + 1]) |
| 200 | if repeat { |
| 201 | nested, after := tree.template_vector_repeat(node, i + 2, consecutive) |
| 202 | pending << TemplateJob{body_id, job.depth + nested, 0, job.escaped} |
| 203 | i = after |
| 204 | } else { |
| 205 | pending << TemplateJob{body_id, job.depth, 0, job.escaped} |
| 206 | i++ |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | else {} |
| 211 | } |
| 212 | } |
| 213 | if drivers.len == 0 { return failure('template ellipsis needs a repeated pattern variable') } |
| 214 | template.repeats[key] = drivers |
| 215 | } |
| 216 | |
| 217 | struct SyntaxExpansion { |
| 218 | origin reader.NodeId |
| 219 | mut: |
| 220 | indices []int |
| 221 | symbols map[i32]reader.NodeId |
| 222 | atoms map[i32]reader.NodeId |
| 223 | family string |
| 224 | family_context i32 |
| 225 | family_wrap &SyntaxWrap = unsafe { nil } |
| 226 | wrapped_families &RecordWrappedFamilies = unsafe { nil } |
| 227 | families &RecordExpansionGroups = unsafe { nil } |
| 228 | domain &SyntaxLexicalDomain = unsafe { nil } |
| 229 | wrap &SyntaxWrap = unsafe { nil } |
| 230 | other_wraps &SyntaxWrapApplications = unsafe { nil } |
| 231 | memo &SyntaxTemplateMemo[reader.NodeId] = unsafe { nil } |
| 232 | } |
| 233 | |
| 234 | fn (template &SyntaxTemplate) repeat_length(body i32, state &SyntaxExpansion, captures &SyntaxCaptures) !int { |
| 235 | return template.repeat_length_at(body, state.indices, captures) |
| 236 | } |
| 237 | |
| 238 | fn (template &SyntaxTemplate) repeat_length_at(body i32, indices []int, captures &SyntaxCaptures) !int { |
| 239 | return template.repeat_length_levels(body, indices, captures, 1) |
| 240 | } |
| 241 | |
| 242 | fn template_repeat_key(body i32, depth int, body_depth int) u64 { |
| 243 | return (u64(u32(body)) << 32) | (u64(depth) << 16) | u64(body_depth) |
| 244 | } |
| 245 | |
| 246 | fn (template &SyntaxTemplate) repeat_length_levels(body i32, indices []int, captures &SyntaxCaptures, levels int) !int { |
| 247 | depth := indices.len |
| 248 | drivers := template.repeats[template_repeat_key(body, depth, depth + levels)] or { |
| 249 | return failure('invalid compiled template repetition') |
| 250 | } |
| 251 | mut count := -1 |
| 252 | for driver in drivers { |
| 253 | path := captures.coordinate(indices, int(driver.offset))! |
| 254 | width := captures.items[syntax_capture_key(int(driver.variable), path)] or { |
| 255 | return failure('missing syntax repetition dimension') |
| 256 | } |
| 257 | if count >= 0 && count != int(width) { |
| 258 | return failure('template repetition lengths do not match') |
| 259 | } |
| 260 | count = int(width) |
| 261 | } |
| 262 | return count |
| 263 | } |
| 264 | |
| 265 | fn (captures &SyntaxCaptures) coordinate(indices []int, offset int) !i32 { |
| 266 | mut path := i32(0) |
| 267 | for i in offset .. indices.len { |
| 268 | key := (u64(u32(path)) << 32) | u64(u32(indices[i])) |
| 269 | path = captures.paths[key] or { return failure('missing syntax repetition coordinate') } |
| 270 | } |
| 271 | return path |
| 272 | } |
| 273 | |
| 274 | fn (mut b Builder) expand_syntax_template(tree &SyntaxTree, pattern &SyntaxPattern, template &SyntaxTemplate, captures &SyntaxCaptures, origin reader.NodeId) !reader.NodeId { |
| 275 | mut state := SyntaxExpansion{ origin: b.code.source_origin(origin) } |
| 276 | defer { |
| 277 | unsafe { |
| 278 | state.symbols.free() |
| 279 | state.atoms.free() |
| 280 | state.indices.free() |
| 281 | } |
| 282 | } |
| 283 | if !isnil(template.sharing) { |
| 284 | storage := alloc_arena.new(chunk_size: 1024) |
| 285 | defer { release_frozen_syntax_scratch(storage) } |
| 286 | state.memo = new_syntax_template_memo[reader.NodeId](storage, template.sharing) |
| 287 | return b.expand_template_node(tree, pattern, template, captures, template.root, false, mut state) |
| 288 | } |
| 289 | return b.expand_template_node(tree, pattern, template, captures, template.root, false, mut state) |
| 290 | } |
| 291 | |
| 292 | fn (mut b Builder) expand_template_node(tree &SyntaxTree, pattern &SyntaxPattern, template &SyntaxTemplate, captures &SyntaxCaptures, id i32, escaped bool, mut state SyntaxExpansion) !reader.NodeId { |
| 293 | node := tree.nodes[id] |
| 294 | cache_key := if !isnil(state.memo) && node.kind in [.pair, .vector] { |
| 295 | state.memo.key(id, escaped) |
| 296 | } else { no_template_memo_key } |
| 297 | if cache_key != no_template_memo_key { |
| 298 | if cached := state.memo.completed[cache_key] { return cached } |
| 299 | } |
| 300 | match node.kind { |
| 301 | .symbol { |
| 302 | if variable := pattern.indices[node.car] { |
| 303 | depth := pattern.variables[variable].depth |
| 304 | path := captures.coordinate(state.indices, state.indices.len - depth)! |
| 305 | key := syntax_capture_key(variable, path) |
| 306 | return reader.NodeId(captures.items[key] or { return failure('missing syntax pattern capture') }) |
| 307 | } |
| 308 | if cached := state.symbols[node.car] { return cached } |
| 309 | original := tree.identifiers[node.car] |
| 310 | info := SyntaxIdentifier{ |
| 311 | ...original |
| 312 | context: if !isnil(original.context) { |
| 313 | original.context |
| 314 | } else { |
| 315 | tree.context |
| 316 | } |
| 317 | } |
| 318 | name := b.template_record_identifier(info, tree.record_context(node.car), mut state)! |
| 319 | result := b.code.syntax_symbol(name, info.raw_name, state.origin)! |
| 320 | state.symbols[node.car] = result |
| 321 | return result |
| 322 | } |
| 323 | .pair { |
| 324 | if !escaped && tree.ellipsis(node.car) { |
| 325 | result := b.expand_template_node(tree, pattern, template, captures, tree.escaped_template(id)!, true, mut state)! |
| 326 | if cache_key != no_template_memo_key { state.memo.keep(cache_key, result) } |
| 327 | return result |
| 328 | } |
| 329 | // Build the private result forward. No temporary list of cars, no |
| 330 | // reverse copy, and no native recursion along a flat template cdr. |
| 331 | mut root := reader.nil_node |
| 332 | mut last := reader.no_node |
| 333 | mut cursor := id |
| 334 | mut spine := &SyntaxTemplateSpine[reader.NodeId](unsafe { nil }) |
| 335 | defer { if !isnil(spine) { state.memo.depth-- } } |
| 336 | mut end := reader.no_node |
| 337 | for tree.nodes[cursor].kind == .pair { |
| 338 | if cursor != id && !isnil(state.memo) { |
| 339 | tail_key := state.memo.key(cursor, escaped) |
| 340 | if tail_key != no_template_memo_key { |
| 341 | if cached := state.memo.completed[tail_key] { end = cached; break } |
| 342 | if isnil(spine) { spine = state.memo.acquire_spine() } |
| 343 | state.memo.note_spine(mut spine, tail_key, if last == reader.no_node { reader.nil_node } else { last }) |
| 344 | } |
| 345 | } |
| 346 | pair := tree.nodes[cursor] |
| 347 | tail := tree.nodes[pair.cdr] |
| 348 | repeat := !escaped && tail.kind == .pair && tree.ellipsis(tail.car) |
| 349 | count := if repeat { template.repeat_length(pair.car, state, captures)! } else { 1 } |
| 350 | for i in 0 .. count { |
| 351 | if repeat { state.indices << i } |
| 352 | previous := if repeat && !isnil(state.memo) { state.memo.enter(i)! } else { u32(0) } |
| 353 | car := b.expand_template_node(tree, pattern, template, captures, pair.car, escaped, mut state)! |
| 354 | if repeat && !isnil(state.memo) { state.memo.path = previous } |
| 355 | if repeat { state.indices.pop() } |
| 356 | cell := b.code.syntax_pair(car, reader.nil_node, state.origin)! |
| 357 | if last == reader.no_node { |
| 358 | root = cell |
| 359 | } else { |
| 360 | index := -int(last) - 2 |
| 361 | b.code.generated.nodes[index] = SyntaxNode{ ...b.code.generated.nodes[index], cdr: cell } |
| 362 | } |
| 363 | last = cell |
| 364 | } |
| 365 | cursor = if repeat { tail.cdr } else { pair.cdr } |
| 366 | } |
| 367 | tail := if end != reader.no_node { end } else { |
| 368 | b.expand_template_node(tree, pattern, template, captures, cursor, escaped, mut state)! |
| 369 | } |
| 370 | if last == reader.no_node { root = tail } else { |
| 371 | index := -int(last) - 2 |
| 372 | b.code.generated.nodes[index] = SyntaxNode{ ...b.code.generated.nodes[index], cdr: tail } |
| 373 | } |
| 374 | if !isnil(spine) { |
| 375 | for i, tail_key in spine.keys { |
| 376 | previous := spine.previous[i] |
| 377 | value := if previous == reader.nil_node { root } else { |
| 378 | b.code.generated.nodes[-int(previous) - 2].cdr |
| 379 | } |
| 380 | state.memo.keep(tail_key, value) |
| 381 | } |
| 382 | } |
| 383 | if cache_key != no_template_memo_key { state.memo.keep(cache_key, root) } |
| 384 | return root |
| 385 | } |
| 386 | .vector { |
| 387 | mut count := 0 |
| 388 | mut i := 0 |
| 389 | for i < int(node.cdr) { |
| 390 | body := tree.elements[node.car + i] |
| 391 | repeat := !escaped && i + 1 < int(node.cdr) && tree.ellipsis(tree.elements[node.car + i + 1]) |
| 392 | width := if repeat { template.repeat_length(body, state, captures)! } else { 1 } |
| 393 | if width > 2147483647 - count { |
| 394 | return failure('expanded syntax vector exceeds supported range') |
| 395 | } |
| 396 | count += width |
| 397 | i += if repeat { 2 } else { 1 } |
| 398 | } |
| 399 | mut arena := b.code.syntax_arena() |
| 400 | if count > 2147483647 - arena.elements.len { |
| 401 | return failure('expanded syntax vector exceeds supported range') |
| 402 | } |
| 403 | start := arena.elements.len |
| 404 | // Reserve once and fill directly; recursive vectors may grow the arena, |
| 405 | // so retain the integer offset rather than a pointer or borrowed slice. |
| 406 | unsafe { arena.elements.grow_len(count) } |
| 407 | i = 0 |
| 408 | mut output := start |
| 409 | for i < int(node.cdr) { |
| 410 | body := tree.elements[node.car + i] |
| 411 | repeat := !escaped && i + 1 < int(node.cdr) && tree.ellipsis(tree.elements[node.car + i + 1]) |
| 412 | width := if repeat { template.repeat_length(body, state, captures)! } else { 1 } |
| 413 | for index in 0 .. width { |
| 414 | if repeat { state.indices << index } |
| 415 | previous := if repeat && !isnil(state.memo) { state.memo.enter(index)! } else { u32(0) } |
| 416 | child := b.expand_template_node(tree, pattern, template, captures, body, escaped, mut state)! |
| 417 | if repeat && !isnil(state.memo) { state.memo.path = previous } |
| 418 | if repeat { state.indices.pop() } |
| 419 | arena.elements[output] = child |
| 420 | output++ |
| 421 | } |
| 422 | i += if repeat { 2 } else { 1 } |
| 423 | } |
| 424 | result := b.code.syntax_node(SyntaxNode{ kind: .vector, renamed: true, car: reader.NodeId(start), cdr: reader.NodeId(count), origin: state.origin })! |
| 425 | if cache_key != no_template_memo_key { state.memo.keep(cache_key, result) } |
| 426 | return result |
| 427 | } |
| 428 | .empty_list { return reader.nil_node } |
| 429 | else { |
| 430 | if cached := state.atoms[id] { return cached } |
| 431 | result := b.code.syntax_atom(tree.values[node.car], state.origin)! |
| 432 | state.atoms[id] = result |
| 433 | return result |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | fn (tree &SyntaxTree) escaped_template(id i32) !i32 { |
| 439 | wrapper := tree.nodes[tree.nodes[id].cdr] |
| 440 | if wrapper.kind != .pair || tree.nodes[wrapper.cdr].kind != .empty_list { |
| 441 | return failure('ellipsis escape needs exactly one template') |
| 442 | } |
| 443 | return wrapper.car |
| 444 | } |
| 445 | |