abc-parser 0.4.0

An ABC music notation parser. Turns ABC text into Rust data structures and back.
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
use crate::datatypes::*;

peg::parser! {
    /// Module generated by [rust_peg](https://crates.io/crates/peg)
    ///
    /// Each function corresponds to a rule in the grammar and can be called with an input string
    /// to begin parsing that type of ABC object.
    ///
    /// # Examples
    /// Usually you will want to parse whole ABC files in which case you will want to use
    /// `abc::tune_book`.
    /// ```
    /// use abc_parser::datatypes::*;
    /// use abc_parser::abc;
    ///
    /// let parsed = abc::tune_book("X:1\nT:Example\nK:D\n").unwrap();
    /// assert_eq!(
    ///     parsed,
    ///     TuneBook::new(vec![], None, vec![
    ///         Tune::new(
    ///             TuneHeader::new(vec![
    ///                 HeaderLine::Field(InfoField::new('X', "1".to_string()), None),
    ///                 HeaderLine::Field(InfoField::new('T', "Example".to_string()), None),
    ///                 HeaderLine::Field(InfoField::new('K', "D".to_string()), None),
    ///             ]),
    ///             None
    ///         )
    ///     ])
    /// )
    /// ```
    /// If you know that you will only be parsing one tune you can use `abc::tune`.
    /// ```
    /// use abc_parser::datatypes::*;
    /// use abc_parser::abc;
    ///
    /// let parsed = abc::tune("X:1\nT:Example\nK:D\n").unwrap();
    /// assert_eq!(
    ///     parsed,
    ///     Tune::new(
    ///         TuneHeader::new(vec![
    ///             HeaderLine::Field(InfoField::new('X', "1".to_string()), None),
    ///             HeaderLine::Field(InfoField::new('T', "Example".to_string()), None),
    ///             HeaderLine::Field(InfoField::new('K', "D".to_string()), None),
    ///         ]),
    ///         None
    ///     )
    /// )
    /// ```
    pub grammar abc() for str {
        pub rule tune_book() -> TuneBook =
            c:ignored_line()* h:file_header()? ts:tune()* { TuneBook::new(c, h, ts) } /
            ![_] { TuneBook::new(vec![], None, vec![]) }

        pub rule ignored_line() -> IgnoredLine =
            "\n" { IgnoredLine::EmptyLine } /
            c:comment_line() { IgnoredLine::Comment(c) }

        pub rule file_header() -> FileHeader =
            l:header_line()* "\n" { FileHeader::new(l) }

        pub rule tune() -> Tune =
            h:tune_header() b:tune_body()? { Tune::new(h, b) }

        pub rule tune_header() -> TuneHeader =
            c1:comment_line()*
            "X:" optional_space() n:number() optional_space() cx:inline_comment()? music_line_end()
            c2:comment_line()*
            t:info_field_header(<$("T")>) ct:inline_comment()? music_line_end()
            extra:info_field_header_non_special()*
            k:info_field_header(<$("K")>) ck:inline_comment()? music_line_end()
            c3:comment_line()* {
                let mut info = c1.into_iter().map(HeaderLine::Comment).collect::<Vec<_>>();
                info.push(
                    HeaderLine::Field(
                        InfoField::new('X', n.to_string()),
                        cx.map(|s| Comment::Comment(s.to_string())),
                    )
                );
                info.extend(c2.into_iter().map(HeaderLine::Comment));
                info.push(HeaderLine::Field(t, ct.map(|s| Comment::Comment(s.to_string()))));
                info.extend(extra);
                info.push(HeaderLine::Field(k, ck.map(|s| Comment::Comment(s.to_string()))));
                info.extend(c3.into_iter().map(HeaderLine::Comment));
                TuneHeader::new(info)
            }

        rule info_field_header<'a>(n: rule<&'a str>) -> InfoField =
            name:n() ":" optional_space() t:$(([^ '\n' | '%'])*) {
                InfoField::new(
                    name.chars().next().unwrap(),
                    t.to_string()
                )
            }

        rule info_field_inline() -> InfoField =
            name:$(['H'..='Y' | 'h'..='y' | '+']) ":" optional_space() t:$((!"]"!"\n"[_])*) {
                InfoField::new(
                    name.chars().next().unwrap(),
                    t.to_string()
                )
            }

        rule header_line() -> HeaderLine =
            f:info_field_header(<$(['A'..='Z' | 'a'..='z' | '+'])>)
            c:inline_comment()?
            music_line_end() {
                HeaderLine::Field(f, c.map(|s| Comment::Comment(s.to_string())))
            } /
            c:comment_line() { HeaderLine::Comment(c) }

        rule info_field_header_non_special() -> HeaderLine =
            f:info_field_header(<$(!['K' | 'X']['A'..='Z' | 'a'..='z' | '+'])>)
            c:inline_comment()?
            music_line_end() {
                HeaderLine::Field(f, c.map(|s| Comment::Comment(s.to_string())))
            } /
            c:comment_line() { HeaderLine::Comment(c) }

        pub rule tune_body() -> TuneBody =
            l:tune_line()+ { TuneBody::new(l) } /
            "\n" { TuneBody::new(vec![]) }

        pub rule tune_line() -> TuneLine =
            c:comment_line() { TuneLine::Comment(c) } /
            m:music_line() { TuneLine::Music(m) } /
            s:symbol_line() { TuneLine::Symbol(s) } /
            l:lyric_line() { TuneLine::Lyric(l) } /
            i:inline_field_line() { TuneLine::Music(i) }

        pub rule music_line() -> MusicLine =
            s:music_symbol()+ c:inline_comment()? music_line_end() {
                let mut symbols = s;
                if let Some(c) = c {
                    symbols.push(
                        MusicSymbol::Comment(Comment::Comment(c.to_string()))
                    );
                }
                MusicLine::new(symbols)
            }

        pub rule symbol_line() -> SymbolLine =
            "s:" s:symbol_line_symbol()* music_line_end() {
                SymbolLine::new(s)
            }

        pub rule lyric_line() -> LyricLine =
            "w:" s:lyric_line_symbol()* music_line_end() {
                LyricLine::new(s)
            }

        pub rule inline_field_line() -> MusicLine =
            i:info_field_inline() music_line_end() {
                MusicLine::new(vec![MusicSymbol::InlineField(i, false)])
            }

        pub rule music_symbol() -> MusicSymbol =
            broken_rhythm() /
            note() /
            rest() /
            spacer() /
            chord() /
            "[" i:info_field_inline() "]" { MusicSymbol::InlineField(i, true) } /
            bar() /
            beam() /
            ending() /
            grace_notes() /
            tuplet() /
            slur() /
            d:decoration() { MusicSymbol::Decoration(d) } /
            a:annotation() { MusicSymbol::Annotation(a) } /
            music_space() /
            reserved()

        pub rule symbol_line_symbol() -> SymbolLineSymbol =
            s:symbol_alignment() { SymbolLineSymbol::SymbolAlignment(s) } /
            a:annotation() { SymbolLineSymbol::Annotation(a) } /
            d:decoration() { SymbolLineSymbol::Decoration(d) } /
            s:space() { SymbolLineSymbol::Space(s.to_string()) }

        pub rule lyric_line_symbol() -> LyricSymbol =
            s:symbol_alignment() { LyricSymbol::SymbolAlignment(s) } /
            l:$([^ '-' | '_' | '*' | '~' | '\\' | '|' | ' ' | '\n']+) {
                LyricSymbol::Syllable(l.to_string())
            } /
            s:space() { LyricSymbol::Space(s.to_string()) }

        //
        // Music Symbols
        //

        pub rule broken_rhythm() -> MusicSymbol =
            b:(t:broken_rhythm_target() s:music_space()? {
                match s {
                    Some(s) => vec![t, s],
                    None => vec![t],
                }
            })
            r:$("<"*<1,3> / ">"*<1,3>)
                a:(s:music_space()? t:(broken_rhythm() / broken_rhythm_target()) {
                match s {
                    Some(s) => vec![s, t],
                    None => vec![t],
                }
            }) {
                MusicSymbol::BrokenRhythm {
                    rhythm: r.to_string(),
                    before: b,
                    after: a,
                }
            }

        pub rule broken_rhythm_target() -> MusicSymbol =
            note() /
            rest() /
            chord() /
            grace_notes()

        pub rule note() -> MusicSymbol =
            a:accidental()? n:note_uppercase() o:octave()? l:length()? t:tie()? {
                MusicSymbol::new_note(
                    a, n, o.unwrap_or(1), l, t
                )
            } /
            a:accidental()? n:note_lowercase() o:octave()? l:length()? t:tie()? {
                MusicSymbol::new_note(
                    a, n, o.unwrap_or(1) + 1, l, t
                )
            }

        pub rule bar() -> MusicSymbol =
            b:$(
                "."?
                (
                    [':' | '|' | '\\' | ']'] /
                    "[" !['H'..='Y' | 'h'..='y' | '1'..='9']
                )+
            )
            e:ending_number()?
            { MusicSymbol::Bar(b.to_string(), e) }

        pub rule beam() -> MusicSymbol =
             b:$("`"+) { MusicSymbol::Beam(b.to_string()) }

        pub rule slur() -> MusicSymbol =
            "(" { MusicSymbol::Slur(Slur::Begin) } /
            ".(" { MusicSymbol::Slur(Slur::BeginDotted) } /
            ")" { MusicSymbol::Slur(Slur::End) }

        pub rule rest() -> MusicSymbol =
            "z" l:length()? { MusicSymbol::Rest(Rest::Note(l)) } /
            "Z" l:length()? { MusicSymbol::Rest(Rest::Measure(l)) } /
            "x" l:length()? { MusicSymbol::Rest(Rest::NoteHidden(l)) } /
            "X" l:length()? { MusicSymbol::Rest(Rest::MeasureHidden(l)) }

        pub rule spacer() -> MusicSymbol =
            "y" { MusicSymbol::Spacer }

        pub rule ending() -> MusicSymbol =
            "[" i:ending_identifier() { MusicSymbol::Ending(i.to_string()) }

        pub rule ending_identifier() -> String =
            n1:ending_number() n2:("," n:ending_number() { n })* {
                let mut s = n1;
                for n in n2 {
                    s.push(',');
                    s.push_str(&n);
                }
                s
            }

        pub rule ending_number() -> String =
            d1:number_digits() d2:("-" d:number_digits() { d } )? {
                let mut s = d1.to_string();
                if let Some(d2) = d2 {
                    s.push('-');
                    s.push_str(d2);
                }
                s
            }

        pub rule chord() -> MusicSymbol =
            "[" n:chord_inner_symbol()+ "]" l:length()? t:tie()? {
                MusicSymbol::Chord {
                    notes: n,
                    length: l,
                    tie: t,
                }
            }

        pub rule chord_inner_symbol() -> MusicSymbol =
            note() /
            d:decoration() { MusicSymbol::Decoration(d) }

        pub rule grace_notes() -> MusicSymbol =
            "{" a:"/"? n:grace_notes_inner_symbol()+ "}" {
                MusicSymbol::GraceNotes {
                    acciaccatura: a,
                    notes: n
                }
            }

        pub rule grace_notes_inner_symbol() -> MusicSymbol =
            broken_rhythm() /
            note() /
            music_space()

        pub rule tuplet() -> MusicSymbol =
            "("
            p:digit_at_least_2()
            q:(":" x:digit_at_least_1()? { x })?
            r:(":" x:digit_at_least_1()? { x })? {
                MusicSymbol::new_tuplet(p, q.flatten(), r.flatten())
            }

        pub rule music_space() -> MusicSymbol =
            s:space() { MusicSymbol::Space(s.to_string()) }

        pub rule reserved() -> MusicSymbol =
            r:['#' | '*' | ';' | '?' | '@'] {
                MusicSymbol::Reserved(r.to_string())
            }

        // Music symbol components

        rule accidental() -> Accidental =
            "^^" { Accidental::DoubleSharp } /
            "__" { Accidental::DoubleFlat } /
            "^" { Accidental::Sharp } /
            "_" { Accidental::Flat } /
            "=" { Accidental::Natural }

        pub rule note_uppercase() -> Note =
            "A" { Note::A } /
            "B" { Note::B } /
            "C" { Note::C } /
            "D" { Note::D } /
            "E" { Note::E } /
            "F" { Note::F } /
            "G" { Note::G }

        pub rule note_lowercase() -> Note =
            "a" { Note::A } /
            "b" { Note::B } /
            "c" { Note::C } /
            "d" { Note::D } /
            "e" { Note::E } /
            "f" { Note::F } /
            "g" { Note::G }

        rule octave() -> i8 =
            p:$([',' | '\'']+) {
                let mut octave = 1;
                for c in p.chars() {
                    match c {
                        ',' => octave -= 1,
                        '\'' => octave += 1,
                        _ => panic!("Parser malfunctioned. Unexpected octave identifier")
                    }
                }
                octave
            }

        rule tie() -> Tie =
            ".-" { Tie::Dotted } /
            "-" { Tie::Solid }

        //
        // Shared Symbols
        //

        rule inline_comment() -> &'input str =
            "%" c:$([^'\n']*) { c }

        rule comment_line() -> Comment =
            "%" c:inline_comment() music_line_end() {
                Comment::StylesheetDirective(c.to_string())
            } /
            s:space() c:inline_comment() music_line_end() {
                Comment::CommentLine(s.to_string(), c.to_string())
            } /
            c:inline_comment() music_line_end() {
                Comment::CommentLine("".to_string(), c.to_string())
            }

        rule length() -> Length =
            "/" n:number() {
                Length::new(1.0 / (n as f32))
            } /
            s:$("/"+) {
                Length::new(1.0 / (s.len() as f32).exp2())
            } /
            n1:number() "/" n2:number() {
                Length::new((n1 as f32) / (n2 as f32))
            } /
            n:number() { Length::new(n as f32) }

        pub rule annotation() -> Annotation =
            "\"" p:placement()? s:$((!"\""[_])*) "\"" {
                Annotation::new(p, s.to_string())
            }

        pub rule placement() -> Placement =
            "^" { Placement::Above } /
            "_" { Placement::Below } /
            "<" { Placement::Left } /
            ">" { Placement::Right } /
            "@" { Placement::Auto }

        rule decoration() -> Decoration =
            "." { Decoration::Staccato } /
            "~" { Decoration::Roll } /
            "H" { Decoration::Fermata } /
            "L" { Decoration::Accent } /
            "M" { Decoration::LowerMordent } /
            "O" { Decoration::Coda } /
            "P" { Decoration::UpperMordent } /
            "S" { Decoration::Segno } /
            "T" { Decoration::Trill } /
            "u" { Decoration::UpBow } /
            "v" { Decoration::DownBow } /
            "!" d:$((!['\n' | '!'][_])+) "!" {
                Decoration::Unresolved(d.to_string())
            }

        pub rule symbol_alignment() -> SymbolAlignment =
            "-" { SymbolAlignment::Break } /
            "_" { SymbolAlignment::Extend } /
            "*" { SymbolAlignment::Skip } /
            "~" { SymbolAlignment::Space } /
            "\\-" { SymbolAlignment::Hyphen } /
            "|" { SymbolAlignment::Bar }

        //
        // Primitives
        //

        rule number() -> u32 =
            n:number_digits() { n.parse().unwrap() }

        rule number_digits() -> &'input str =
            $(['1'..='9']['0'..='9']*)

        rule digit_at_least_1() -> u32 =
            n:$(['1'..='9']) { n.parse().unwrap() }

        rule digit_at_least_2() -> u32 =
            n:$(['2'..='9']) { n.parse().unwrap() }

        rule space() -> &'input str =
            s:$([' ' | '\t']+) { s }

        rule optional_space() =
            [' ' | '\t']*

        rule music_line_end() =
            "\n" / ![_]
    }
}