-
Notifications
You must be signed in to change notification settings - Fork 11
/
parser.pegjs
392 lines (317 loc) · 9.86 KB
/
parser.pegjs
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
{
var defaultTime = undefined;
var defaultMeter = undefined;
var WHOLE = 256,
HALF = 128,
QUARTER = 64,
_4TH = 32,
_16TH = 16,
_32TH = 8,
_64TH = 4,
_128TH = 2;
var durations = [_128TH, _64TH, _32TH, _16TH, _4TH, QUARTER, HALF, WHOLE];
var isDotted = function(duration) {
return durations.indexOf(duration) === -1;
};
var getDots = function(duration) {
if (duration == 0 || !isDotted(duration))
return 0;
var baseNote = 0;
var l = durations.length - 1;
for (var i=l; i>=0; i--) {
if (duration > durations[i]) {
baseNote = durations[i];
break;
}
}
if (baseNote == 0)
throw new Error("Duration out of range")
return {
duration: baseNote,
dots: 1 + getDots(duration - baseNote)
}
}
var createTimeSignature = function(result) {
if (isDotted(result)) {
return getDots(result);
} else {
return {
duration: result,
dots: 0
}
}
};
}
start
= header:header song:song EOF { return { header: header, song:song } }
// Header definition and fields
header
= refnum:reference_number? title:title+ pairs:(other_fields _)* k:(key _) {
var p = {
refnum: refnum || 1, // Fallback to 1 for songs that don't include one
title: title[0],
key: k[0]
}
for (i = 0; i < pairs.length; i++) {
p[pairs[i][0][0]] = pairs[i][0][1];
}
return p;
}
reference_number
= 'X:' _? value:integer _ { return value }
title
= 'T:' _? value:string _ { return value }
other_fields
= f:("A:" / "B:" / "C:" / "D:" / "G:" / "H:" / "N:" / "O:" / "R:" / "S:" / "Z:") _ value:string {
var fields = {
"A:": "area",
"B:": "book",
"C:": "composer",
"D:": "discography",
"G:": "group",
"H:": "history",
"N:": "notes",
"O:": "origin",
"R:": "rythm",
"S:": "source",
"Z:": "t_note"
};
return [fields[f] || "", value];
}
/
"L:" _ n:note_length_strict {
defaultTime = WHOLE * eval(n);
return ["note_length", defaultTime];
}
/
"M:" _ m:meter {
defaultMeter = WHOLE * (eval(m) < 0.75 ? 0.0625 : 0.125);
return ["meter", defaultMeter];
}
/
"P:" _ p:parts {
return p;
}
/
"Q:" _ t:tempo {
return t;
}
tempo = integer+ / ("C" note_length? "=" integer+) / (note_length_strict "=" integer+)
meter = "C" / "C|" / meter_fraction
meter_fraction = l:(integer+ "/" integer+) { return l.join("") }
note_length_strict = l:(integer+ "/" integer+) { return l.join("") }
note_length = (integer+)? ("/" (integer+))?
parts = part_spec+
part_spec = (part / ( "(" part_spec+ ")" ) ) integer+
part = "A" / "B" / "C" / "D" / "E" / "F" / "G" / "H" / "I" / "J" / "K" / "L" / "M" / "N" / "O" / "P" / "Q" / "R" / "S" / "T" / "U" / "V" / "X" / "Y" / "Z"
/* ---------------------------- Key Signature -------------------------------*/
key = "K:" _ k:key_def _ { return k }
key_def = key_spec / "HP" / "Hp"
key_spec = k:keynote m:mode_spec? g:(" " global_accidental)* {
if (m)
k.mode = m;
return k;
}
keynote = bn:basenote k:key_accidental? {
return {
baseNote: bn,
accidental: k
}
}
key_accidental = "#" / "b"
mode_spec = " "? m:mode extratext? { return m }
mode = mode_minor / mode_major / mode_lydian / mode_ionian / mode_mixolydian / mode_dorian / mode_aeolian / mode_phrygian / mode_locrian
extratext = alpha*
global_accidental = accidental basenote
mode_minor = chars:(("m"/"M") (("i"/"I") ("n"/"N"))? ) { return chars.join("") }
mode_major = chars:(("m"/"M") ("a"/"A") ("j"/"J")) { return chars.join("") }
mode_lydian = chars:(("l"/"L") ("y"/"Y") ("d"/"D")) { return chars.join("") }
mode_ionian = chars:(("i"/"I") ("o"/"O") ("n"/"N")) { return chars.join("") }
mode_mixolydian = chars:(("m"/"M") ("i"/"I") ("x"/"X")) { return chars.join("") }
mode_dorian = chars:(("d"/"D") ("o"/"O") ("r"/"R")) { return chars.join("") }
mode_aeolian = chars:(("a"/"A") ("e"/"E") ("o"/"O")) { return chars.join("") }
mode_phrygian = chars:(("p"/"P") ("h"/"H") ("r"/"R")) { return chars.join("") }
mode_locrian = chars:(("l"/"L") ("o"/"O") ("c"/"C")) { return chars.join("") }
/*-------------------------------- Song -------------------------------------*/
song
= stave+ _
stave
= measures:measure+ _ { return measures; }
measure
= _ bar? notes:(note_element / tuplet)+ _ bar:(bar / nth_repeat) nth_repeat? ("\\" nl)? {
var finalNotes = [];
var counter = 0;
var beams = [[]];
var len = notes.length;
for (var n = 0; n<len; n++) {
var note = notes[n];
var lastBeam = beams[beams.length - 1];
var lastBeamLen = lastBeam.length;
if (note.note === "rest") {
// If the last beam contains only one note there is really no need
// for beaming, so I delete the 'beam' property from that lone note
// and then lastBeam is emptied.
// In case the last beam contains more notes, an empty array is
// pushed in beams to break the beaming. The counter is increased
// so the next generated beam is a new one.
if (lastBeamLen === 1) {
lastBeam[0].beam = null;
lastBeam.pop();
}
else if (lastBeamLen > 1) { beams.push([]); }
counter = counter + 1;
finalNotes.push([note]);
continue;
}
if (len > 1) {
if (note.duration < QUARTER &&
!((n === len-1) && !lastBeamLen)) {
lastBeam.push(note);
note.beam = counter;
} else if (note.duration >= QUARTER) {
if (lastBeamLen === 1) {
lastBeam[0].beam = null;
lastBeam.pop();
} else if (lastBeamLen > 1) {
counter++;
beams.push([]);
}
}
}
if (!Array.isArray(note))
note = [note];
finalNotes.push(note);
}
var mObj = { bar: bar[0], chords: [] };
// For each note/chord we create a chord object that contains a notes array
// with the proper note/chord, and attach it to the chords that the measure
// will contain.
finalNotes.forEach(function(n) { mObj.chords.push({ notes: n }); });
return mObj;
}
note_element = n:note_stem broken_rhythm? _? { return n }
note_stem
= gc:guitar_chord? gn:grace_notes? gracings* n:(note / chord) {
if (gc)
n.guitar_chord = gc;
if (gn)
n.grace_notes = gn;
return n;
}
chord = "[" n:note+ "]" { return n }
note = n:note_or_rest time:time_signature? _? tie:tie? {
if (time) {
n.duration = time.duration;
n.dots = time.dots
}
else {
n.duration = defaultTime || defaultMeter;
}
if (tie)
n.tie = true;
return n;
}
note_or_rest = n:(pitch / rest) { return n }
pitch = acc:accidental? bn:basenote o:octave? {
var obj = {
accidental: acc,
note: bn + o
}
if (!acc)
delete obj.accidental;
return obj;
}
octave = "'" / ","
basenote = [A-G] / [a-g]
rest = "z" { return { note: "rest" } }
tie = "-"
gracings = "~" / "." / "v" / "u"
grace_notes = "{" p:pitch+ "}" { return p }
broken_rhythm = "<"+ / ">"+
tuplet = tuplet_spec n:note_element+ {
return {
type: "tuple",
notes: n
}
}
tuplet_spec = "(" integer (":" (integer) ( ":" integer? )? )?
bar
= bars !(stringNum)
bars
= "|]" / "||" / "[|" / "|]" / "|:" / "|" / ":|" / "::"
nth_repeat
= "[1" / "[2" / "|1" / ":|2"
// TODO: Validate the chord with /^[A-G](b|#)?((m(aj)?|M|aug|dim|sus)([2-7]|9|13)?)?(\/[A-G](b|#)?)?$/
guitar_chord
= '"' chord:string_no_quotes '"' { return chord }
/* ---------------- Accidentals ----------------- */
sharp
= "^" !("^") { return "sharp" }
natural
= "="
flat
= "_" !("_") { return "flat" }
double_sharp
= "^^" { return "dsharp" }
double_flat
= "__" { return "dflat" }
accidental
= sharp / flat / natural / double_sharp / double_flat
/* --------------------------------------------- */
middle_pairs
=[^XKa-z]
time_signature
=
ts:(stringNum "/" stringNum) {
var num = parseInt(ts[0]) * (defaultTime || defaultMeter);
var denom = parseInt(ts[2]);
var result = parseInt(num/denom);
return createTimeSignature(result);
}
/
ts:("/" cd:stringNum) {
var result = parseInt((defaultTime || defaultMeter) / parseInt(ts[1]));
return createTimeSignature(result);
}
/
ts:stringNum {
return createTimeSignature(parseFloat((defaultTime || defaultMeter) * eval(ts)))
}
/
ts:("/") {
var result = parseInt((defaultTime || defaultMeter) / 2);
return createTimeSignature(result);
}
// Basic types
stringNum /* integers in string format */
= digits:[0-9]+ { return digits.join(""); }
integer "integer"
= digits:stringNum { return parseInt(digits, 10); }
string
= chars:[A-Za-z0-9,/'"#&.=()\-\[\]: ]+ { return chars.join ? chars.join("") : chars; }
string_no_quotes
= chars:[A-Za-z0-9,/#&.=()\-\[\]: ]+ { return chars.join ? chars.join("") : chars; }
alpha
= chars:[a-zA-Z] {
if (chars.join)
return chars.join("")
else
return chars
}
_
= (whitespace / comment)*
whitespace
= [\t\v\f \u00A0\uFEFF] / Zs / nl
LineTerminator
= [\n\r\u2028\u2029]
nl
= "\n"
/ "\r\n"
/ "\r"
/ "\u2028" // line separator
/ "\u2029" // paragraph separator
comment
= "%" (!nl .)*
Zs = [\u0020\u00A0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000]
EOF
= !.