sva-ast 0.14.13

The document model for a composition
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
// Concern: derives a song from its root: placements on the tempo curve, lanes, the equations it lowers to | Non-concern: an instance's span, which the caller asks the engine | IO: (&Graph, root) -> Song

mod lanes;
mod lower;
mod place;
mod reach;
mod tempo;

use std::collections::BTreeSet;
use std::rc::Rc;

pub use lanes::{Held, Hold, Home, Lane, LaneId, Swept};
pub use place::parameters;
pub use place::{Chain, Instance, PlacedMark, Pos, Reached};
pub use tempo::Curve;

use crate::diag::{ByteSpan, DiagCode};
use crate::expr::Expr;
use crate::graph::{Defined, Graph, VARIABLES, variable_text};
use crate::refusal::Refusal;
use crate::scale::Scale;
use crate::score::{EXTENSION, Item, Ratio, Shape, located};

use lanes::Sweeping;
use place::{Container, Offset, Placing, at_line, shifted};
use reach::Reach;
use tempo::TempoMove;

/// One instance's span from its onset: the instants of its first and last samples, `None` an
/// end the engine leaves open.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Span {
    pub start: Option<f64>,
    pub end: Option<f64>,
}

/// A warning a song's files earn: it prints, and the render goes on.
#[derive(Clone, Debug, PartialEq)]
pub struct Warning {
    pub code: &'static str,
    pub file: String,
    pub span: ByteSpan,
    pub start: (usize, usize),
    pub message: String,
}

pub struct Song {
    root: String,
    curve: Curve,
    tree: Rc<Container>,
    instances: Vec<Instance>,
    lanes: Vec<Lane>,
    marks: Vec<PlacedMark>,
    instruments: Vec<(String, Defined)>,
    asked: Vec<(usize, Expr)>,
    warnings: Vec<Warning>,
    key: Option<f64>,
    scale: Option<Scale>,
    passed: Vec<(usize, LaneId)>,
    containers: BTreeSet<String>,
}

/// What a song lowers to: each node's new body, the root's included.
pub struct Lowered {
    pub nodes: Vec<(String, Defined)>,
}

fn refused(refusals: Vec<Refusal>) -> Result<(), Vec<Refusal>> {
    match refusals.is_empty() {
        true => Ok(()),
        false => Err(refusals),
    }
}

impl Graph {
    /// Steps 3 to 7 short of the spans: every line `root` reaches placed on the tempo curve, every
    /// lane swept, and each node no score sits under lowered with the lanes reaching it.
    pub fn song(&self, root: &str) -> Result<Song, Vec<Refusal>> {
        let values = self.values(None)?;
        let (bpm, beats) = match &values.tempo {
            Some(tempo) => (tempo.bpm, tempo.beats()),
            None => (60.0, 1.0),
        };
        let steady = Curve::new(bpm, beats, &[]);
        let tempo = {
            let mut first = Placing::new(self, &steady);
            first.song(root);
            refused(std::mem::take(&mut first.refusals))?;
            let reached: Vec<Reached> = first
                .moves
                .into_iter()
                .filter(|m| m.lane.is_variable("bpm"))
                .collect();
            let mut refusals = Vec::new();
            for m in reached
                .iter()
                .filter(|m| matches!(m.start, Pos::Seconds(_)))
            {
                refusals.push(self.through_seconds(m, &steady, "a `bpm` move"));
            }
            refused(refusals)?;
            let sweeping = Sweeping {
                graph: self,
                curve: &steady,
                per_bar: values.tempo.as_ref().map(|t| t.seconds_per_bar()),
                degree: &|_, _| Err("a `bpm` move reads no degree".to_string()),
            };
            let lanes = sweeping.sweep(reached)?;
            lanes
                .first()
                .map(|lane| {
                    lane.moves
                        .iter()
                        .map(|m| {
                            let Pos::Bars(start) = m.reached.start else {
                                unreachable!("refused above")
                            };
                            let len = match m.reached.end {
                                Some(Pos::Bars(end)) => end.minus(start).expect("a bar span"),
                                _ => Ratio::ZERO,
                            };
                            TempoMove {
                                start,
                                len,
                                shape: m.shape.unwrap_or(Shape::Lin),
                                from: m.from.as_ref().and_then(Held::number).unwrap_or(bpm),
                                to: m.to.number().expect("bpm is a number"),
                            }
                        })
                        .collect::<Vec<_>>()
                })
                .unwrap_or_default()
        };
        let curve = Curve::new(bpm, beats, &tempo);
        let mut placing = Placing::new(self, &curve);
        let tree = placing.song(root);
        let mut refusals = std::mem::take(&mut placing.refusals);
        if curve.moves() {
            for m in placing
                .moves
                .iter()
                .filter(|m| matches!(m.start, Pos::Seconds(_)))
            {
                refusals.push(self.through_seconds(m, &curve, "a move"));
            }
            for (score, line, item, lost) in &placing.bars_lost {
                let at = self.score(score).expect("a score").lines[*line].items[*item].at();
                refusals.push(at_line(
                    self,
                    score,
                    at.at,
                    DiagCode::BadLane,
                    &format!(
                        "this line counts in bars, and the tempo moves, so a bar is a place on \
                         the tempo curve; `{}` reads what places it at a time that is no \
                         constant shift of `t`, so it has no song time: place it at `t - d`",
                        lost.path
                    ),
                ));
            }
        }
        refused(refusals)?;
        let key = self.key_hz();
        let scale = values.scale.clone();
        let degree = |at: f64, n: i64| -> Result<f64, String> {
            let _ = at;
            let ratio = scale.as_ref().and_then(|s| s.ratio(n)).ok_or_else(|| {
                "`deg` counts in `variables/scale`, and there is none".to_string()
            })?;
            key.map(|k| k * ratio)
                .ok_or_else(|| "`deg` counts from `variables/key`, a constant note".to_string())
        };
        let sweeping = Sweeping {
            graph: self,
            curve: &curve,
            per_bar: values.tempo.as_ref().map(|t| t.seconds_per_bar()),
            degree: &degree,
        };
        let placed_moves = std::mem::take(&mut placing.moves);
        let lanes = sweeping.sweep(placed_moves)?;
        let marks = std::mem::take(&mut placing.marks);
        let containers = std::mem::take(&mut placing.placed);
        let instances_lost = std::mem::take(&mut placing.instances_lost);
        let mut instances = std::mem::take(&mut placing.instances);
        let moved: BTreeSet<LaneId> = lanes.iter().map(|l| l.id.clone()).collect();
        let mut reach = Reach::new(self, &moved, &values);
        for instance in &mut instances {
            let bound: BTreeSet<&str> = instance.bound.iter().map(String::as_str).collect();
            let mut held = reach.sound(&instance.node, &bound);
            let read = instance.node.strip_prefix(&format!("{VARIABLES}/"));
            if !read.is_some_and(|name| moved.contains(&LaneId::Variable(name.to_string()))) {
                held.extend(reach.of(&instance.node, &bound));
            }
            held.retain(|l| !l.is_variable("scale"));
            held.sort();
            held.dedup();
            instance.lanes = held;
        }
        let mut lanes = lanes;
        if let Some(scale) = lanes.iter().find(|l| l.id.is_variable("scale")) {
            let read: BTreeSet<i64> = instances
                .iter()
                .flat_map(|i| &i.lanes)
                .filter_map(|l| match l {
                    LaneId::Degree(n) => Some(*n),
                    _ => None,
                })
                .collect();
            let derived: Vec<Lane> = read
                .into_iter()
                .filter_map(|n| lanes::degree_lane(scale, n))
                .collect();
            lanes.extend(derived);
        }
        let mut song = Song {
            root: root.to_string(),
            curve,
            tree,
            instances,
            lanes,
            marks,
            instruments: Vec::new(),
            asked: Vec::new(),
            warnings: Vec::new(),
            key,
            scale: values.scale.clone(),
            passed: Vec::new(),
            containers,
        };
        let mut refusals = song.unbound(self);
        for (k, lost) in instances_lost {
            let instance = &song.instances[k];
            if instance.lanes.is_empty() {
                continue;
            }
            let lanes: Vec<String> = instance.lanes.iter().map(LaneId::name).collect();
            let (file, span) = lost.unwrap_or((song.root.clone(), ByteSpan::at(0)));
            refusals.push(Refusal::new(
                file.clone(),
                span,
                DiagCode::BadLane,
                format!(
                    "`@{}` has no song time: `{file}` reads it at a time that is no constant \
                     shift of `t`, and the lanes `{}` move it; a lane reaches an instance at its \
                     song position, so read it at `t - d`",
                    instance.node,
                    lanes.join("`, `")
                ),
            ));
        }
        refused(refusals)?;
        let mut lowered = BTreeSet::new();
        for instance in &song.instances {
            let mut open: Vec<String> = vec![instance.node.clone()];
            while let Some(node) = open.pop() {
                if reach.of(&node, &BTreeSet::new()).is_empty() || !lowered.insert(node.clone()) {
                    continue;
                }
                let mut sites = Vec::new();
                if let Some(defined) = self.source(&node) {
                    for e in std::iter::once(&defined.body)
                        .chain(defined.defaults.iter().map(|(_, v)| v))
                    {
                        place::sites(self, &node, e, &mut sites);
                    }
                }
                open.extend(
                    sites
                        .iter()
                        .filter_map(|e| reach::invoked(self, &node, e).map(|(n, ..)| n)),
                );
            }
        }
        let mut refusals = Vec::new();
        for node in lowered {
            match reach.lowered(&node) {
                Ok(defined) => song.instruments.push((node, defined)),
                Err(refusal) => refusals.push(refusal),
            }
        }
        refused(refusals)?;
        song.asked = song.asked_spans(self)?;
        song.warnings = song.warned(self);
        Ok(song)
    }

    fn through_seconds(&self, m: &Reached, curve: &Curve, what: &str) -> Refusal {
        let at = match &self.score(&m.score).expect("a score").lines[m.line].items[m.item] {
            Item::Move(mv) => mv.at.at,
            _ => 0,
        };
        at_line(
            self,
            &m.score,
            at,
            DiagCode::BadLane,
            &format!(
                "{what} on `{}` is reached through seconds ({}), and while the tempo moves a \
                 move sits on a bar, so a lane compares exact positions; place it from a file \
                 counting in bars",
                m.lane.name(),
                m.chain.text(curve)
            ),
        )
    }

    /// The tonic `variables/key` holds, in hertz, where it is a constant.
    fn key_hz(&self) -> Option<f64> {
        let text = variable_text(self, "key")?;
        let reading = crate::score::Reading::<f64> {
            field: crate::score::Field::Value,
            mark: &|_| None,
            degree: &|_| Err(String::new()),
            own: None,
        };
        crate::score::evaluate(text.trim(), &reading)
            .ok()
            .map(|v| v.amount)
    }
}

impl Song {
    pub fn curve(&self) -> &Curve {
        &self.curve
    }

    pub fn lanes(&self) -> &[Lane] {
        &self.lanes
    }

    pub fn marks(&self) -> &[PlacedMark] {
        &self.marks
    }

    pub fn instances(&self) -> &[Instance] {
        &self.instances
    }

    pub fn warnings(&self) -> &[Warning] {
        &self.warnings
    }

    /// Each node no score sits under, lowered with the lanes reaching it as parameters.
    pub fn instruments(&self) -> &[(String, Defined)] {
        &self.instruments
    }

    /// Each instance whose span decides a fold, as the node a probe at the root reads:
    /// `@node(t, ...)` with every lane at its onset value.
    pub fn asked(&self) -> Vec<Expr> {
        self.asked.iter().map(|(_, e)| e.clone()).collect()
    }

    pub fn lane(&self, id: &LaneId) -> Option<&Lane> {
        self.lanes.iter().find(|l| &l.id == id)
    }

    /// The moved lanes of `node`'s own parameters: a container equation's are passed on as
    /// moving values, never folded, since no one span bounds what it places.
    pub(crate) fn own_lanes(&self, graph: &Graph, node: &str) -> Vec<LaneId> {
        parameters(graph, node)
            .into_iter()
            .map(|p| LaneId::Sound(node.to_string(), p))
            .filter(|l| self.lane(l).is_some())
            .collect()
    }

    /// Where instance `i`'s line's `len` ends in song time; `None` where it writes none.
    pub fn ends(&self, graph: &Graph, i: &Instance) -> Option<Pos> {
        let (score, li, ii) = i.line.as_ref()?;
        let file = graph.score(score)?;
        let Item::Event(event) = &file.lines[*li].items[*ii] else {
            return None;
        };
        let len = event.len?.amount;
        let by = match file.unit {
            crate::filename::SpanUnit::Bars => Offset::Bars(len),
            crate::filename::SpanUnit::Seconds => Offset::Seconds(len.to_f64()),
        };
        shifted(i.at, by, &self.curve)
    }

    /// The key at second `at`, in hertz.
    pub fn key_at(&self, at: f64) -> Option<f64> {
        match self.lane(&LaneId::Variable("key".to_string())) {
            Some(lane) => lane.at(at).and_then(|h| h.number()),
            None => self.key,
        }
    }

    pub fn scale_at(&self, at: f64) -> Option<Scale> {
        match self.lane(&LaneId::Variable("scale".to_string())) {
            Some(lane) => match lane.at(at) {
                Some(Held::Scale(scale)) => Some(scale),
                _ => None,
            },
            None => self.scale.clone(),
        }
    }

    /// Every parameter a score line's instance reads that nothing binds: not the line, not a
    /// lane set by its onset, not a default.
    fn unbound(&self, graph: &Graph) -> Vec<Refusal> {
        let mut out = Vec::new();
        let mut seen = BTreeSet::new();
        for instance in &self.instances {
            let Some((score, li, ii)) = &instance.line else {
                continue;
            };
            let Item::Event(event) = &graph.score(score).expect("a score").lines[*li].items[*ii]
            else {
                continue;
            };
            let params = parameters(graph, &instance.node);
            let read: Vec<&str> = params.iter().map(String::as_str).collect();
            for bound in &instance.bound {
                if read.contains(&bound.as_str()) {
                    continue;
                }
                let (key, at) = match bound.as_str() {
                    "f0" if event.deg.is_some() => ("deg", event.deg.expect("a deg").1),
                    "len" => ("len", event.len.expect("a len").at),
                    name => (
                        name,
                        event
                            .binds
                            .iter()
                            .find(|b| b.name == name)
                            .map_or(event.node.at, |b| b.key),
                    ),
                };
                if !seen.insert((score.clone(), *li, key.to_string())) {
                    continue;
                }
                let set = match key == bound {
                    true => format!("`{key}`"),
                    false => format!("`{key}`, which sets `{bound}`,"),
                };
                let chord = match key {
                    "chord" => "; a chord is a `deg` list, one event per voice",
                    _ => "",
                };
                out.push(at_line(
                    graph,
                    score,
                    at,
                    DiagCode::BadScore,
                    &format!(
                        "{set} is no parameter `@{}` reads; it reads `{}`{chord}",
                        event.node.name,
                        read.join("`, `")
                    ),
                ));
            }
            let defaults: Vec<&str> = graph
                .source(&instance.node)
                .map(|d| d.defaults.iter().map(|(k, _)| k.as_str()).collect())
                .unwrap_or_default();
            let at = instance.at.map(|p| p.seconds(&self.curve));
            for p in &params {
                if defaults.contains(&p.as_str()) || instance.bound.contains(p) {
                    continue;
                }
                let lane = LaneId::Sound(instance.node.clone(), p.clone());
                let set = self
                    .lane(&lane)
                    .is_some_and(|l| at.is_some_and(|at| l.at(at).is_some()));
                if set || !seen.insert((score.clone(), *li, format!("unbound {p}"))) {
                    continue;
                }
                out.push(at_line(
                    graph,
                    score,
                    event.node.at,
                    DiagCode::BadScore,
                    &format!(
                        "`@{}` reads `{p}`, which nothing binds here: bind it on this line, set \
                         the lane `{}` at or before this line, or give it a default, `{p} = ...`",
                        event.node.name,
                        lane.name()
                    ),
                ));
            }
        }
        out
    }

    /// The probe expressions for every instance a lane may move under.
    fn asked_spans(&self, graph: &Graph) -> Result<Vec<(usize, Expr)>, Vec<Refusal>> {
        let mut out = Vec::new();
        for (k, instance) in self.instances.iter().enumerate() {
            let Some(at) = instance.at else { continue };
            let at = at.seconds(&self.curve);
            let moving = instance
                .lanes
                .iter()
                .filter_map(|l| self.lane(l))
                .any(|l| l.changes_after(at));
            if moving {
                out.push((k, self.probe(graph, k, at)?));
            }
        }
        Ok(out)
    }

    fn warned(&self, graph: &Graph) -> Vec<Warning> {
        let mut out = Vec::new();
        let curve = &self.curve;
        for instance in &self.instances {
            let (Some((score, li, ii)), Some(at)) = (&instance.line, instance.at) else {
                continue;
            };
            let Item::Event(event) = &graph.score(score).expect("a score").lines[*li].items[*ii]
            else {
                continue;
            };
            let start = at.seconds(curve);
            let end = start + self.len_seconds(graph, score, at, event.len.map(|l| l.amount));
            for bound in &instance.bound {
                let lane = LaneId::Sound(instance.node.clone(), bound.clone());
                let Some(l) = self.lane(&lane) else { continue };
                let ramp = l.moves.iter().find(|m| {
                    m.end > m.start && m.end > start && (m.start < end || m.start <= start)
                });
                if let Some(ramp) = ramp {
                    let key_at = event
                        .binds
                        .iter()
                        .find(|b| &b.name == bound)
                        .map_or(event.node.at, |b| b.key);
                    out.push(warn(
                        graph,
                        score,
                        key_at,
                        "line-overrides-lane",
                        &format!(
                            "this line binds `{bound}`, and the lane `{}` ramps during its event \
                             ({}); the line wins over the lane",
                            lane.name(),
                            self.line_of(graph, &ramp.reached)
                        ),
                    ));
                }
            }
        }
        for lane in &self.lanes {
            let reached = lane.id.is_variable("bpm")
                || self
                    .instances
                    .iter()
                    .any(|i| i.lanes.contains(&lane.id) || i.node == lane.id.name())
                || matches!(&lane.id, LaneId::Sound(node, _) if self.containers.contains(node))
                || self.degrees_read(graph, &lane.id);
            if !reached {
                let first = &lane.moves[0].reached;
                let at = match &graph.score(&first.score).expect("a score").lines[first.line].items
                    [first.item]
                {
                    Item::Move(m) => m.lane.at,
                    _ => 0,
                };
                out.push(warn(
                    graph,
                    &first.score,
                    at,
                    "lane-reaches-nothing",
                    &format!(
                        "the lane `{}` has moves, and no instance in the song reads it: its \
                         node is never placed, or every invoker passes the parameter",
                        lane.id.name()
                    ),
                ));
            }
        }
        out
    }

    /// Whether a degree reads `key` or `scale` here: a score line's, or an equation's own.
    fn degrees_read(&self, graph: &Graph, id: &LaneId) -> bool {
        let own = |i: &Instance| i.lanes.iter().any(|l| matches!(l, LaneId::Degree(_)));
        if id.is_variable("scale") && self.instances.iter().any(own) {
            return true;
        }
        (id.is_variable("key") || id.is_variable("scale"))
            && self.instances.iter().any(|i| {
                i.line.as_ref().is_some_and(|(score, li, ii)| {
                    match &graph.score(score).expect("a score").lines[*li].items[*ii] {
                        Item::Event(e) => {
                            e.deg.is_some()
                                || e.binds
                                    .iter()
                                    .any(|b| crate::render_expr(&b.value).contains("deg("))
                        }
                        _ => false,
                    }
                })
            })
    }

    fn line_of(&self, graph: &Graph, r: &Reached) -> String {
        let number = graph.score(&r.score).expect("a score").lines[r.line].number;
        format!("{}{EXTENSION}:{number}", r.score)
    }

    /// The seconds a line's `len` lasts from `at`, on the curve where it counts in bars.
    fn len_seconds(&self, graph: &Graph, score: &str, at: Pos, len: Option<Ratio>) -> f64 {
        let Some(len) = len else { return 0.0 };
        match graph.score(score).expect("a score").unit {
            crate::filename::SpanUnit::Seconds => len.to_f64(),
            crate::filename::SpanUnit::Bars => match at {
                Pos::Bars(b) => self.curve.duration(b, len),
                Pos::Seconds(s) => self
                    .curve
                    .after(s, len)
                    .unwrap_or(len.to_f64() * self.curve.seconds_per_bar()),
            },
        }
    }

    /// Steps 7 and 8: each instance's lanes folded over its span or passed on, and every
    /// container lowered per placement; what is passed is kept for a refusal to name.
    pub fn lowered(&mut self, graph: &Graph, spans: &[Span]) -> Result<Lowered, Vec<Refusal>> {
        let (lowered, passed) = lower::Lowering::new(graph, self, spans).lowered()?;
        self.passed = passed;
        Ok(lowered)
    }

    /// Each instance and lane a lowering passed on as a moving value.
    pub fn passed(&self) -> &[(usize, LaneId)] {
        &self.passed
    }

    /// Where the first move of `lane` under instance `k` is written, its `to`: the file, the
    /// byte and the file's text.
    pub fn moved_under<'g>(
        &self,
        graph: &'g Graph,
        k: usize,
        lane: &LaneId,
    ) -> Option<(String, usize, &'g str)> {
        let at = self.instances[k].at?.seconds(&self.curve);
        let l = self.lane(lane)?;
        let m = l
            .moves
            .iter()
            .find(|m| m.end > at || m.start >= at)
            .or(l.moves.last())?;
        let r = &m.reached;
        let Item::Move(mv) = &graph.score(&r.score)?.lines[r.line].items[r.item] else {
            return None;
        };
        Some((
            format!("{}{EXTENSION}", r.score),
            mv.to.at,
            graph.text(&r.score)?,
        ))
    }
}

fn warn(graph: &Graph, score: &str, at: usize, code: &'static str, message: &str) -> Warning {
    let text = graph.text(score).unwrap_or("");
    let diag = located(text, at, message);
    Warning {
        code,
        file: format!("{score}{EXTENSION}"),
        span: diag.span,
        start: crate::score::line_column(text, at),
        message: diag.message,
    }
}

impl Graph {
    /// Each node's body a song lowered, kept beside its source.
    pub fn apply(&mut self, lowered: Lowered) {
        for (path, defined) in lowered.nodes {
            self.derive(&path, defined);
        }
    }
}