| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | |
| 5 | // Shared grammar nodes can have different ellipsis contexts. A validation |
| 6 | // from a shallower structural nesting cannot establish the 512-level limit |
| 7 | // for a deeper occurrence of the same grammar state. |
| 8 | fn syntax_grammar_seen(mut deepest map[u64]int, id i32, depth int, escaped bool, nesting int) bool { |
| 9 | key := (u64(u32(id)) << 32) | (u64(depth) << 1) | u64(escaped) |
| 10 | if previous := deepest[key] { |
| 11 | if previous >= nesting { return true } |
| 12 | } |
| 13 | deepest[key] = nesting |
| 14 | return false |
| 15 | } |
| 16 | |
| 17 | fn frozen_syntax_job(mut pending []i32, storage arena.Arena, id i32) { |
| 18 | if pending.len == pending.cap { |
| 19 | reserve_frozen_syntax_jobs(mut pending, storage) |
| 20 | } |
| 21 | pending << id |
| 22 | } |
| 23 | |
| 24 | fn reserve_frozen_syntax_jobs(mut pending []i32, storage arena.Arena) { |
| 25 | storage.push() |
| 26 | defer { storage.pop() } |
| 27 | pending.ensure_cap(pending.len + 1) |
| 28 | } |
| 29 | |
| 30 | fn new_frozen_syntax_work(storage arena.Arena, count int) ([]u8, []i32) { |
| 31 | storage.push() |
| 32 | defer { storage.pop() } |
| 33 | marks := []u8{len: count} |
| 34 | mut pending := []i32{cap: 32} |
| 35 | unsafe { pending.flags.set(.noslices) } |
| 36 | return marks, pending |
| 37 | } |
| 38 | |
| 39 | fn release_frozen_syntax_scratch(storage arena.Arena) { |
| 40 | mut owner := storage |
| 41 | owner.free() |
| 42 | } |
| 43 | |
| 44 | // syntax-rules patterns/templates have finite recursive grammar (R7RS §7.1.5). |
| 45 | // A macro can capture and quote a cyclic datum, but a transformer definition |
| 46 | // cannot itself contain a circular pattern/template. Check once after freezing, |
| 47 | // before the existing tree-oriented pattern and ellipsis walks can run. |
| 48 | // Dense marks cover just this transformer's compact frozen graph, not its source. |
| 49 | fn (tree &SyntaxTree) require_finite_transformer() ! { |
| 50 | storage := arena.new(chunk_size: 1024) |
| 51 | defer { release_frozen_syntax_scratch(storage) } |
| 52 | mut marks, mut pending := new_frozen_syntax_work(storage, tree.nodes.len) |
| 53 | for root in 0 .. tree.nodes.len { |
| 54 | if marks[root] != 0 { continue } |
| 55 | frozen_syntax_job(mut pending, storage, i32(root)) |
| 56 | for pending.len > 0 { |
| 57 | visit := pending.pop() |
| 58 | id := if visit < 0 { ~visit } else { visit } |
| 59 | if visit < 0 { |
| 60 | marks[id] = 2 |
| 61 | continue |
| 62 | } |
| 63 | if marks[id] == 1 { return failure('circular syntax-rules transformer') } |
| 64 | if marks[id] == 2 { continue } |
| 65 | node := tree.nodes[id] |
| 66 | if node.kind !in [.pair, .vector] { |
| 67 | marks[id] = 2 |
| 68 | continue |
| 69 | } |
| 70 | marks[id] = 1 |
| 71 | frozen_syntax_job(mut pending, storage, ~id) |
| 72 | if node.kind == .pair { |
| 73 | frozen_syntax_job(mut pending, storage, node.cdr) |
| 74 | frozen_syntax_job(mut pending, storage, node.car) |
| 75 | } else { |
| 76 | for i in 0 .. node.cdr { |
| 77 | frozen_syntax_job(mut pending, storage, tree.elements[node.car + i]) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |