forked from LastOfTheCarelessMen/ABC
/
Actions.pm
151 lines (127 loc) · 3.68 KB
/
Actions.pm
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
use v6;
use ABC::Header;
use ABC::Tune;
use ABC::Duration;
use ABC::Note;
use ABC::Rest;
use ABC::Tuplet;
use ABC::BrokenRhythm;
use ABC::Chord;
class ABC::Actions {
method header_field($/) {
make ~$<header_field_name> => ~$<header_field_data>;
}
method header($/) {
my $header = ABC::Header.new;
for @( $<header_field> ) -> $field {
$header.add-line($field.ast.key, $field.ast.value);
}
make $header;
}
method note_length($/) {
if $<note_length_denominator> {
make duration-from-parse($<top>[0], $<note_length_denominator>[0]<bottom>[0]);
} else {
make duration-from-parse($<top>[0]);
}
}
method mnote($/) {
make ABC::Note.new(~$<pitch>,
$<note_length>.ast,
$<tie> eq '-');
}
method stem($/) {
if @( $<mnote> ) == 1 {
make $<mnote>[0].ast;
} else {
make ABC::Stem.new(@( $<mnote> )>>.ast);
}
}
method rest($/) {
make ABC::Rest.new(~$<rest_type>, $<note_length>.ast);
}
method tuplet($/) {
make ABC::Tuplet.new(3, @( $<stem> )>>.ast);
}
method broken_rhythm($/) {
make ABC::BrokenRhythm.new($<stem>[0].ast,
~$<g1>,
~$<broken_rhythm_bracket>,
~$<g2>,
$<stem>[1].ast);
}
method inline_field($/) {
make ~$/<alpha> => ~$/<value>;
}
method long_gracing($/) {
make ~$/<long_gracing_text>;
}
method gracing($/) {
make $/<long_gracing> ?? $/<long_gracing>.ast !! ~$/;
}
method slur_begin($/) {
make ~$/<slur_begin>;
}
method slur_end($/) {
make ~$/<slur_end>;
}
method chord($/) {
# say "hello?";
# say $/<chord_accidental>[0].WHAT;
# say $/<chord_accidental>[0].perl;
make ABC::Chord.new(~$/<mainnote>, ~$/<mainaccidental>[0] // "", ~$/<maintype>,
~$/<bassnote>, ~$/<bass_accidental>);
}
method chord_or_text($/) {
make $/<chord>.map({ $_.ast });
}
method element($/) {
my $type;
for <broken_rhythm stem rest slur_begin slur_end gracing grace_notes nth_repeat end_nth_repeat spacing tuplet inline_field chord_or_text> {
$type = $_ if $/{$_};
}
# say $type ~ " => " ~ $/{$type}.ast.WHAT;
my $ast = $type => ~$/{$type};
# say :$ast.perl;
# say $/{$type}.ast.perl;
# say $/{$type}.ast.WHAT;
if $/{$type}.ast ~~ ABC::Duration || $/{$type}.ast ~~ Pair | Str | List {
$ast = $type => $/{$type}.ast;
}
make $ast;
}
method barline($/) {
make "barline" => ~$/;
}
method bar($/) {
my @bar = @( $<element> )>>.ast;
@bar.push($<barline>>>.ast);
make @bar;
}
method line_of_music($/) {
my @line = $<barline>>>.ast;
my @bars = @( $<bar> )>>.ast;
for @bars -> $bar {
for $bar.list {
@line.push($_);
}
}
@line.push("endline" => "");
make @line;
}
method music($/) {
my @music;
for @( $<line_of_music> )>>.ast -> $line {
for $line.list {
@music.push($_);
}
}
make @music;
}
method tune($/) {
make ABC::Tune.new($<header>.ast, $<music>.ast);
}
method tune_file($/) {
make @( $<tune> )>>.ast;
}
}