sva-ast 0.10.0

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
// Concern: reads a score's JSON Lines and lowers it to the sum of each line's sound shifted to its time | Non-concern: checking a line against its sound (tuning.rs) | IO: (&str) -> Score -> Expr

use std::fmt;

use serde::de::{self, Deserialize, Deserializer, MapAccess, Visitor};

use crate::diag::{ByteSpan, Diag, DiagCode};
use crate::expr::{Address, BinOp, Expr, Literal};
use crate::filename::SpanUnit;
use crate::lexer::{TokenKind, tokenize};

#[derive(Clone, Debug, PartialEq)]
pub struct Score {
    pub events: Vec<Event>,
}

/// One line: at `at`, `sound` plays with `args` bound, `len` among them where written.
#[derive(Clone, Debug, PartialEq)]
pub struct Event {
    pub line: usize,
    pub span: ByteSpan,
    pub at: Time,
    pub sound: String,
    pub deg: Option<i64>,
    pub args: Vec<(String, Expr)>,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Time {
    pub amount: f64,
    pub unit: SpanUnit,
}

impl Time {
    /// As a score writes it, `0.25b` or `0.5s`.
    pub fn spelled(self) -> String {
        match self.unit {
            SpanUnit::Bars => format!("{}b", self.amount),
            SpanUnit::Seconds => format!("{}s", self.amount),
        }
    }

    fn literal(self) -> Expr {
        Expr::Lit(match self.unit {
            SpanUnit::Bars => Literal::Bars(self.amount),
            SpanUnit::Seconds => Literal::Num(self.amount),
        })
    }
}

/// No equation opens with `{`, so the first line a score writes says what the file is.
pub fn is_score(content: &str) -> bool {
    content
        .lines()
        .find(|line| !line.trim().is_empty())
        .is_some_and(|line| line.trim_start().starts_with('{'))
}

pub fn parse_score(content: &str) -> Result<Score, Diag> {
    let mut events: Vec<Event> = Vec::new();
    let mut start = 0usize;
    for (index, line) in content.split('\n').enumerate() {
        let span = ByteSpan::new(start, start + line.len());
        start += line.len() + 1;
        if line.trim().is_empty() {
            continue;
        }
        let number = index + 1;
        let written: Line = serde_json::from_str(line).map_err(|e| {
            let column = e.column().max(1);
            let message = e.to_string();
            let message = message
                .rsplit_once(" at line ")
                .map_or(&*message, |(m, _)| m);
            refused(span.start + column - 1, (number, column), message)
        })?;
        if let Some(before) = events.last() {
            sorted(before, &written, (number, span.start))?;
        }
        events.push(Event {
            line: number,
            span,
            at: written.at,
            sound: written.sound,
            deg: written.deg,
            args: written.args,
        });
    }
    Ok(Score { events })
}

/// Where a refusal on a score line lands: its byte, and the line and column it names.
pub fn refused(at: usize, (line, column): (usize, usize), message: &str) -> Diag {
    Diag::new(
        DiagCode::BadScore,
        ByteSpan::new(at, at),
        format!("line {line}, column {column}: {message}"),
    )
}

fn sorted(before: &Event, next: &Line, (line, at): (usize, usize)) -> Result<(), Diag> {
    if before.at.unit != next.at.unit {
        return Err(refused(
            at,
            (line, 1),
            &format!(
                "`at` is {} here and {} on line {}; one score counts its time in bars or in \
                 seconds, not both",
                next.at.spelled(),
                before.at.spelled(),
                before.line
            ),
        ));
    }
    if next.at.amount < before.at.amount {
        return Err(refused(
            at,
            (line, 1),
            &format!(
                "`at` is {} here, before line {}'s {}; a score's lines are sorted by `at`",
                next.at.spelled(),
                before.line,
                before.at.spelled()
            ),
        ));
    }
    Ok(())
}

impl Score {
    /// Σ soundᵢ(t − atᵢ; argsᵢ), summed as a balanced tree so its depth is log₂ of its lines.
    pub fn lowered(&self, pitch: &dyn Fn(&Event, i64) -> Expr) -> Expr {
        let terms: Vec<Expr> = self.events.iter().map(|e| e.lowered(pitch)).collect();
        balanced(terms)
    }
}

impl Event {
    fn lowered(&self, pitch: &dyn Fn(&Event, i64) -> Expr) -> Expr {
        let mut binds = self.args.clone();
        if let Some(deg) = self.deg {
            binds.push(("f0".to_string(), pitch(self, deg)));
        }
        Expr::Ref {
            path: self.sound.clone(),
            arg: Box::new(shifted(self.at)),
            binds,
            address: Address::Time,
            span: self.span,
        }
    }
}

fn shifted(at: Time) -> Expr {
    let t = Box::new(Expr::Var("t".to_string()));
    let by = |amount: f64| Box::new(Time { amount, ..at }.literal());
    match at.amount {
        0.0 => *t,
        a if a < 0.0 => Expr::Bin(BinOp::Add, t, by(-a)),
        a => Expr::Bin(BinOp::Sub, t, by(a)),
    }
}

fn balanced(mut terms: Vec<Expr>) -> Expr {
    match terms.len() {
        0 => Expr::Lit(Literal::Num(0.0)),
        1 => terms.pop().expect("one term"),
        n => {
            let right = terms.split_off(n / 2);
            Expr::Bin(
                BinOp::Add,
                Box::new(balanced(terms)),
                Box::new(balanced(right)),
            )
        }
    }
}

/// The fields a line names, read in written order so a key written twice is refused.
struct Line {
    at: Time,
    sound: String,
    deg: Option<i64>,
    args: Vec<(String, Expr)>,
}

impl<'de> Deserialize<'de> for Line {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Line, D::Error> {
        d.deserialize_map(LineVisitor)
    }
}

struct LineVisitor;

impl<'de> Visitor<'de> for LineVisitor {
    type Value = Line;

    fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("one event, an object such as {\"at\": \"0b\", \"sound\": \"@../voice/note\"}")
    }

    fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Line, A::Error> {
        let (mut at, mut sound, mut deg) = (None, None, None);
        let mut args: Vec<(String, Expr)> = Vec::new();
        while let Some(key) = map.next_key::<String>()? {
            let twice = match key.as_str() {
                "at" => at.is_some(),
                "sound" => sound.is_some(),
                "deg" => deg.is_some(),
                other => args.iter().any(|(held, _)| held == other),
            };
            if twice {
                return Err(de::Error::custom(format!("`{key}` is written twice")));
            }
            match key.as_str() {
                "at" => at = Some(map.next_value::<When<true>>()?.0),
                "sound" => sound = Some(map.next_value::<Sound>()?.0),
                "deg" => deg = Some(map.next_value::<i64>()?),
                "len" => {
                    let len = map.next_value::<When<false>>()?.0;
                    args.push((key, len.literal()));
                }
                _ => {
                    let value = map.next_value::<Argument>()?.0;
                    args.push((key, value));
                }
            }
        }
        let at = at.ok_or_else(|| de::Error::missing_field("at"))?;
        let sound = sound.ok_or_else(|| de::Error::missing_field("sound"))?;
        if deg.is_some() && args.iter().any(|(key, _)| key == "f0") {
            return Err(de::Error::custom(
                "`f0` and `deg` both set the pitch; write one of them",
            ));
        }
        Ok(Line {
            at,
            sound,
            deg,
            args,
        })
    }
}

/// A time in a string with its unit, `"0.25b"` or `"0.5s"`; `SIGNED` admits a leading `-`.
struct When<const SIGNED: bool>(Time);

impl<'de, const SIGNED: bool> Deserialize<'de> for When<SIGNED> {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        d.deserialize_str(WhenVisitor::<SIGNED>)
    }
}

struct WhenVisitor<const SIGNED: bool>;

impl<'de, const SIGNED: bool> Visitor<'de> for WhenVisitor<SIGNED> {
    type Value = When<SIGNED>;

    fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(
            "a time literal string such as \"0.25b\" or \"0.5s\"; a bare number has no unit",
        )
    }

    fn visit_str<E: de::Error>(self, s: &str) -> Result<When<SIGNED>, E> {
        let tokens = tokenize(s).map_err(|d| E::custom(d.message))?;
        let (sign, rest) = match tokens.as_slice() {
            [first, rest @ ..] if SIGNED && first.kind == TokenKind::Minus => (-1.0, rest),
            all => (1.0, all),
        };
        match rest {
            [only] => match only.kind {
                TokenKind::Time(amount, unit) => Ok(When(Time {
                    amount: sign * amount,
                    unit,
                })),
                _ => Err(E::custom(format!(
                    "`{s}` is no time literal: write a number with a unit, as \"0.25b\" or \
                     \"0.5s\""
                ))),
            },
            _ => Err(E::custom(format!(
                "`{s}` is no time literal: write one number with a unit, as \"0.25b\" or \
                 \"0.5s\""
            ))),
        }
    }
}

struct Sound(String);

impl<'de> Deserialize<'de> for Sound {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        d.deserialize_str(SoundVisitor)
    }
}

struct SoundVisitor;

impl<'de> Visitor<'de> for SoundVisitor {
    type Value = Sound;

    fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("an `@ref` string such as \"@../voice/note\"")
    }

    fn visit_str<E: de::Error>(self, s: &str) -> Result<Sound, E> {
        match s.strip_prefix('@') {
            Some(path) if !path.is_empty() && crate::lexer::whole_ref_path(path) => {
                Ok(Sound(path.to_string()))
            }
            _ => Err(E::custom(format!(
                "`sound` is an `@ref` string such as \"@../voice/note\", not `{s}`"
            ))),
        }
    }
}

/// A number, or one literal in a string: `"0.03s"`, `"900hz"`, `"-6db"`, `"A1"`.
struct Argument(Expr);

impl<'de> Deserialize<'de> for Argument {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        d.deserialize_any(ArgumentVisitor)
    }
}

struct ArgumentVisitor;

impl<'de> Visitor<'de> for ArgumentVisitor {
    type Value = Argument;

    fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("a number, or a literal string such as \"0.03s\", \"-6db\" or \"A1\"")
    }

    fn visit_u64<E: de::Error>(self, v: u64) -> Result<Argument, E> {
        Ok(Argument(Expr::Lit(Literal::Num(v as f64))))
    }

    fn visit_i64<E: de::Error>(self, v: i64) -> Result<Argument, E> {
        Ok(Argument(Expr::Lit(Literal::Num(v as f64))))
    }

    fn visit_f64<E: de::Error>(self, v: f64) -> Result<Argument, E> {
        Ok(Argument(Expr::Lit(Literal::Num(v))))
    }

    fn visit_str<E: de::Error>(self, s: &str) -> Result<Argument, E> {
        let tokens = tokenize(s).map_err(|d| E::custom(d.message))?;
        let unsigned = match tokens.as_slice() {
            [first, rest @ ..] if first.kind == TokenKind::Minus => rest,
            all => all,
        };
        let literal = match unsigned {
            [only] => match &only.kind {
                TokenKind::Num(_)
                | TokenKind::Time(..)
                | TokenKind::Samples(_)
                | TokenKind::Log(..) => true,
                TokenKind::Ident(name) => {
                    unsigned.len() == tokens.len() && crate::vocabulary::note_midi(name).is_some()
                }
                _ => false,
            },
            _ => false,
        };
        if !literal {
            return Err(E::custom(format!(
                "`{s}` is not one literal; a score binds a number, a number with a unit such \
                 as \"0.03s\" or \"-6db\", or a note name such as \"A1\""
            )));
        }
        crate::parser::parse(s)
            .map(Argument)
            .map_err(|d| E::custom(d.message))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn refusal(text: &str) -> String {
        parse_score(text).unwrap_err().message
    }

    #[test]
    fn each_line_is_an_event_in_written_order() {
        let score = parse_score(
            "{\"at\": \"0b\", \"sound\": \"@bass\", \"deg\": 1, \"vel\": 0.9}\n\n\
             {\"at\": \"0.5b\", \"sound\": \"@bass\", \"len\": \"0.25b\", \"cutoff\": \"900hz\"}\n",
        )
        .unwrap();
        assert_eq!(score.events.len(), 2);
        assert_eq!(score.events[1].line, 3);
        assert_eq!(score.events[1].args[0].0, "len");
        assert_eq!(score.events[1].args[1].1, Expr::Lit(Literal::Num(900.0)));
    }

    #[test]
    fn a_refusal_names_the_line_and_column() {
        let text = "{\"at\": \"0b\", \"sound\": \"@bass\"}\n{\"at\": 0.5, \"sound\": \"@bass\"}\n";
        let refused = refusal(text);
        assert!(refused.starts_with("line 2, column "), "{refused}");
        assert!(refused.contains("bare number"), "{refused}");
        assert!(refusal("{\"at\": \"0b\"}").contains("missing field `sound`"));
        assert!(refusal("{\"at\": \"0b\", \"sound\": \"@b\", \"at\": \"1b\"}").contains("twice"));
        assert!(
            refusal("{\"at\": \"0b\", \"sound\": \"@b\", \"deg\": 1, \"f0\": 440}")
                .contains("`deg`")
        );
        assert!(
            refusal("{\"at\": \"0b\", \"sound\": \"@b\", \"vel\": \"x+1\"}").contains("literal")
        );
    }

    #[test]
    fn lines_out_of_order_or_mixing_units_refuse() {
        let late = "{\"at\": \"1b\", \"sound\": \"@b\"}\n{\"at\": \"0.5b\", \"sound\": \"@b\"}\n";
        assert!(refusal(late).contains("sorted"));
        let mixed = "{\"at\": \"0b\", \"sound\": \"@b\"}\n{\"at\": \"1s\", \"sound\": \"@b\"}\n";
        assert!(refusal(mixed).contains("not both"));
    }

    #[test]
    fn the_sum_is_balanced() {
        let line = "{\"at\": \"0.5s\", \"sound\": \"@b\"}\n";
        let score = parse_score(&line.repeat(1024)).unwrap();
        fn depth(e: &Expr) -> usize {
            match e {
                Expr::Bin(BinOp::Add, l, r) => 1 + depth(l).max(depth(r)),
                _ => 0,
            }
        }
        assert_eq!(
            depth(&score.lowered(&|_, _| Expr::Lit(Literal::Num(1.0)))),
            10
        );
    }
}