croma-core 1.3.0

Library-first ABC to MusicXML exporter
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
pub mod barline;
pub mod decoration;
pub mod directive;
pub mod field;
pub mod lyric;
pub mod music;
pub mod note;

use crate::diagnostic::{Diagnostic, RecoveryNote, Severity, Span, SpecReference};
use crate::error::{CromaError, Result};
use crate::lower::{ScoreModelInput, build_score_model, lower_tune_music};
use crate::model::{Score, TextLine, Tune};
use crate::options::ParseOptions;
use crate::parse::field::{InterpretationField, ParsedAbcFields, ParsedFieldKind, parse_fields};
use crate::parse::music::parse_music_document;
use crate::source::SourceText;
use crate::syntax::ParsedMusicDocument;
use crate::syntax::tune::{SurfaceMap, analyze_source};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseReport<T> {
    pub value: T,
    pub diagnostics: Vec<Diagnostic>,
}

impl<T> ParseReport<T> {
    pub fn new(value: T, diagnostics: Vec<Diagnostic>) -> Self {
        Self { value, diagnostics }
    }

    pub fn has_errors(&self) -> bool {
        self.diagnostics
            .iter()
            .any(|diagnostic| diagnostic.severity == Severity::Error)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AbcDocument {
    pub source: SourceText,
    pub options: ParseOptions,
    pub surface: SurfaceMap,
    pub fields: ParsedAbcFields,
    pub music: ParsedMusicDocument,
}

pub fn parse_document(source: &str, options: ParseOptions) -> ParseReport<AbcDocument> {
    parse_document_source(SourceText::new(source), options)
}

pub fn parse_document_source(
    source: SourceText,
    options: ParseOptions,
) -> ParseReport<AbcDocument> {
    let mut diagnostics = source_level_diagnostics(&source);
    let surface = analyze_source(&source);
    let (fields, field_diagnostics) = parse_fields(&source, &surface, options);
    diagnostics.extend(field_diagnostics);
    let ParseReport {
        value: music,
        diagnostics: music_diagnostics,
    } = parse_music_document(&source, &surface, &fields);
    diagnostics.extend(music_diagnostics);

    ParseReport::new(
        AbcDocument {
            source,
            options,
            surface,
            fields,
            music,
        },
        diagnostics,
    )
}

pub fn parse_tune(
    source: &SourceText,
    _surface: &SurfaceMap,
    _options: ParseOptions,
) -> Result<Tune> {
    let report = parse_tune_report(source, _surface, _options);
    report
        .value
        .ok_or_else(|| CromaError::from_diagnostics(report.diagnostics))
}

pub fn parse_tune_report(
    source: &SourceText,
    surface: &SurfaceMap,
    options: ParseOptions,
) -> ParseReport<Option<Tune>> {
    if source.content().trim().is_empty() {
        return ParseReport::new(None, source_level_diagnostics(source));
    }

    let fallback_surface;
    let surface = if surface.line_map.lines.is_empty() && source.line_count() > 0 {
        fallback_surface = analyze_source(source);
        &fallback_surface
    } else {
        surface
    };

    let (fields, mut diagnostics) = parse_fields(source, surface, options);
    let music_report = parse_music_document(source, surface, &fields);
    diagnostics.extend(music_report.diagnostics);
    parse_tune_report_with_fields(
        source,
        surface,
        &fields,
        Some(&music_report.value),
        diagnostics,
    )
}

pub fn parse_tune_report_from_document(document: &AbcDocument) -> ParseReport<Option<Tune>> {
    parse_tune_report_with_fields(
        &document.source,
        &document.surface,
        &document.fields,
        Some(&document.music),
        Vec::new(),
    )
}

fn header_time_symbol_from_fields(
    fields: &ParsedAbcFields,
    header_field_indices: &[usize],
) -> Option<String> {
    let mut pending = None;
    for field_index in header_field_indices {
        let Some(field) = fields.field(*field_index) else {
            continue;
        };
        match &field.kind {
            ParsedFieldKind::Interpretation(InterpretationField::CromaTimeSymbol { value }) => {
                pending = parse_time_symbol_value(&value.value);
            }
            ParsedFieldKind::Meter(_) => return pending,
            _ => {}
        }
    }
    None
}

fn parse_time_symbol_value(value: &str) -> Option<String> {
    for item in value.split_whitespace() {
        let Some(symbol) = item.strip_prefix("symbol=") else {
            continue;
        };
        if matches!(symbol, "common" | "cut") {
            return Some(symbol.to_owned());
        }
    }
    None
}

fn parse_tune_report_with_fields(
    source: &SourceText,
    surface: &SurfaceMap,
    fields: &ParsedAbcFields,
    music: Option<&ParsedMusicDocument>,
    mut diagnostics: Vec<Diagnostic>,
) -> ParseReport<Option<Tune>> {
    if source.content().trim().is_empty() {
        return ParseReport::new(None, source_level_diagnostics(source));
    }

    let mut reference = String::new();
    let mut reference_line = TextLine {
        text: String::new(),
        span: Span::new(0, 0),
    };
    let mut title = String::new();
    let mut title_line = None;
    let mut composers = Vec::new();
    let mut tempo_line = None;
    let mut meter = String::from("4/4");
    let mut key = String::new();
    let mut has_key = false;
    let mut events = Vec::new();
    let mut divisions = 8;
    let mut voices = Vec::new();
    let mut header_time_symbol = None;
    let mut score_directives = Vec::new();
    let mut preserved_directives = Vec::new();
    let mut post_tune_lyrics = Vec::new();
    let mut score = None;
    let mut body_start = None;
    let mut tune_field_state = None;

    let Some(tune) = surface.line_map.tunes.first() else {
        diagnostics.push(missing_key_diagnostic(source, surface.line_map.tunes.len()));
        return ParseReport::new(None, diagnostics);
    };

    if let Some(tune_fields) = fields.tune(tune.index) {
        if let Some(parsed_meter) = tune_fields.header.meter.as_ref() {
            meter = parsed_meter.value.raw.clone();
        }
        header_time_symbol =
            header_time_symbol_from_fields(fields, &tune_fields.header_field_indices);
        tune_field_state = Some(&tune_fields.header);

        for field_index in &tune_fields.header_field_indices {
            let Some(field) = fields.field(*field_index) else {
                continue;
            };
            match &field.kind {
                ParsedFieldKind::Reference(value) => {
                    reference = value.value.clone();
                    reference_line = TextLine {
                        text: value.value.clone(),
                        span: value.span,
                    };
                }
                ParsedFieldKind::Title(value) if title.is_empty() => {
                    title = value.value.clone();
                    title_line = Some(TextLine {
                        text: value.value.clone(),
                        span: value.span,
                    });
                }
                ParsedFieldKind::TextMetadata(metadata) if metadata.code == 'C' => {
                    composers.push(TextLine {
                        text: metadata.value.value.clone(),
                        span: metadata.value.span,
                    });
                }
                ParsedFieldKind::Tempo(value) => {
                    tempo_line = Some(TextLine {
                        text: value.value.clone(),
                        span: value.span,
                    });
                }
                ParsedFieldKind::Key(value) => {
                    has_key = true;
                    key = if value.value.raw.is_empty() {
                        String::from("none")
                    } else {
                        value.value.raw.clone()
                    };
                    body_start = Some(field.line_span.end);
                }
                _ => {}
            }
        }
    }

    if let (Some(tune_music), Some(field_state)) = (
        music.and_then(|music| music.tune(tune.index)),
        tune_field_state,
    ) {
        let lower_report = lower_tune_music(tune_music, field_state);
        diagnostics.extend(lower_report.diagnostics);
        events = lower_report.value.events;
        divisions = lower_report.value.divisions;
        voices = lower_report.value.voices;
        score_directives = lower_report.value.score_directives;
        preserved_directives = lower_report.value.preserved_directives;
        post_tune_lyrics = lower_report.value.post_tune_lyrics;
        score = Some(build_score_model(ScoreModelInput {
            reference: reference_line.clone(),
            title: title_line.clone(),
            composers: composers.clone(),
            tempo: tempo_line.clone(),
            source_span: tune.span,
            field_state,
            voices: &voices,
            header_time_symbol,
            score_directives: &score_directives,
            preserved_directives: &preserved_directives,
            post_tune_lyrics: &post_tune_lyrics,
            diagnostics: &diagnostics,
            divisions,
        }));
    }

    if !has_key {
        diagnostics.push(missing_key_diagnostic(source, surface.line_map.tunes.len()));
        return ParseReport::new(None, diagnostics);
    }
    if events.iter().all(|event| !event.is_time_bearing()) {
        diagnostics.push(no_music_diagnostic(source, body_start));
        return ParseReport::new(None, diagnostics);
    }

    ParseReport::new(
        Some(Tune {
            reference,
            title,
            meter,
            key,
            divisions,
            events,
            voices,
            score_directives,
            preserved_directives,
            post_tune_lyrics,
            score: score.unwrap_or_else(|| empty_score(tune.span, diagnostics.clone())),
        }),
        diagnostics,
    )
}

fn empty_score(span: Span, diagnostics: Vec<Diagnostic>) -> Score {
    Score {
        metadata: crate::model::ScoreMetadata {
            reference: TextLine {
                text: String::new(),
                span,
            },
            title: None,
            composers: Vec::new(),
            tempo: None,
            tempo_model: None,
            meter: None,
            key: None,
            directives: Vec::new(),
            preserved_directives: Vec::new(),
            post_tune_lyrics: Vec::new(),
            source_span: span,
        },
        parts: Vec::new(),
        diagnostics,
        divisions: 1,
        source_span: span,
        accidental_policy: crate::model::AccidentalPolicy {
            preserve_explicit_accidentals: true,
            reset_at_barlines: true,
            scope: crate::model::AccidentalScope::PitchAndOctave,
            source_span: span,
        },
    }
}

fn source_level_diagnostics(source: &SourceText) -> Vec<Diagnostic> {
    if source.content().trim().is_empty() {
        return vec![
            Diagnostic::new(
                Severity::Error,
                "abc.file.empty",
                "ABC source is empty",
                Span::new(source.content_start(), source.content_start()),
            )
            .with_spec_reference(abc_file_structure_reference()),
        ];
    }

    let has_non_comment_content = (0..source.line_count()).any(|index| {
        source
            .line_text(index)
            .map(|line| {
                let trimmed = line.trim_start();
                !trimmed.trim_end().is_empty() && !trimmed.starts_with('%')
            })
            .unwrap_or(false)
    });

    if has_non_comment_content {
        Vec::new()
    } else {
        vec![
            Diagnostic::new(
                Severity::Error,
                "abc.file.no_tune",
                "ABC source contains comments only and no tune content",
                source.content_span(),
            )
            .with_spec_reference(abc_file_structure_reference()),
        ]
    }
}

fn abc_file_structure_reference() -> SpecReference {
    SpecReference::new("ABC 2.1 file structure")
        .with_url("https://abcnotation.com/wiki/abc:standard:v2.1")
}

fn missing_key_diagnostic(source: &SourceText, tune_count: usize) -> Diagnostic {
    let diagnostic = Diagnostic::new(
        Severity::Error,
        "abc.file.missing_k",
        "ABC source is missing a K: field",
        Span::new(source.len(), source.len()),
    )
    .with_spec_reference(abc_file_structure_reference());
    if tune_count > 1 {
        // Clarify the otherwise-confusing message when K: is plainly present on a
        // LATER tune: croma processes a single tune per file, and the first one
        // (the one missing K:) was selected (ABC 2.1 §2.2 separates tunes by blank
        // lines — two adjacent `X:` with no blank line is malformed).
        diagnostic.with_recovery_note(RecoveryNote::new(
            "This file holds multiple tunes; croma processes only the first, which has no K: field. Separate tunes with a blank line.",
        ))
    } else {
        diagnostic
    }
}

fn no_music_diagnostic(source: &SourceText, body_start: Option<usize>) -> Diagnostic {
    Diagnostic::new(
        Severity::Error,
        "abc.file.no_music",
        "ABC source does not contain body music",
        Span::new(body_start.unwrap_or_else(|| source.len()), source.len()),
    )
    .with_spec_reference(abc_file_structure_reference())
}

#[cfg(test)]
#[path = "mod_tests.rs"]
mod tests;