* The constructs a site is written inside, outermost first: its guards and its * loops merged by where each one STARTS. Both were read by the same climb up * the same ancestors, and on one ancestor chain an outer construct always * begins before an inner one — so the positions alone rebuild the n
(site: ProgramSite)
| 221 | * without either reading having to know about the other. |
| 222 | */ |
| 223 | function scopesOf(site: ProgramSite): Scope[] { |
| 224 | const guards: Scope[] = site.guards.map((guard) => ({ kind: 'guard' as const, branch: guard.branch, guard })); |
| 225 | const loops: Scope[] = (site.loops ?? []).map((loop) => ({ kind: 'loop' as const, branch: loop.branch, loop })); |
| 226 | if (loops.length === 0) return guards; |
| 227 | return [...guards, ...loops].sort((a, b) => at(a.branch) - at(b.branch) || a.branch.localeCompare(b.branch)); |
| 228 | } |
| 229 | |
| 230 | /** A `line:column` branch as one number, for ordering constructs by where they start. */ |
| 231 | function at(branch: string): number { |