This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
RealParser.ts
281 lines (256 loc) · 9.74 KB
/
RealParser.ts
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
import { Measure } from './Measure';
import { Measures } from './Sheet';
// extension of https://github.com/daumling/ireal-renderer/blob/master/src/ireal-renderer.js
export class RealParser {
/**
* The RegExp for a complete chord. The match array contains:
* 1 - the base note
* 2 - the modifiers (+-ohd0123456789 and su for sus)
* 3 - any comments (may be e.g. add, sub, or private stuff)
* 4 - the "over" part starting with a slash
* 5 - the top chord as (chord)
* @type RegExp
*/
static chordRegex = /^([ A-GW][b#]?)((?:sus|[\+\-\^\dhob#])*)(\*.+?\*)*(\/[A-G][#b]?)?(\(.*?\))?/;
static regExps = [
/^\*[a-zA-Z]/, // section
/^T\d\d/, // time measurement
/^N./, // repeat marker
/^<.*?>/, // comments
/^ \(.*?\)/, // blank and (note)
RealParser.chordRegex, // chords
/^LZ/, // 1 cell + right bar
/^XyQ/, // 3 empty cells
/^Kcl/ // repeat last bar
];
static replacements = {
"LZ": [" ", "|"],
"XyQ": [" ", " ", " "],
"Kcl": ["|", "x", " "]
};
static getChord(iRealChord) {
return iRealChord.note + iRealChord.modifiers + (iRealChord.over ? '/' + this.getChord(iRealChord.over) : '');
}
static parseSheet(raw): Measures {
const tokens = RealParser.parse(raw);
return RealParser.getSheet(tokens);
}
static getSheet(tokens): Measures {
const parsed = tokens
.reduce((current, token, index, array) => {
const lastBarEnded = ['{', '|', '[', '||' /* 'Z', *//* , ']' */]
.includes(token.bars || token.token);
let signs = token.annots || [];
const repeatStart = (token.bars || token.token) === '{';
const repeatEnd = (token.bars || token.token) === '}';
if (repeatStart) {
signs.push('{');
}
if (repeatEnd) {
signs.push('}');
}
// current.measure ends
if (lastBarEnded) {
if (current.measure) {
// simplify measure if no signs
/* if (Object.keys(current.measure).find(k=>k)) {
current.measure = current.measure.chords;
} */
current.measures.push(current.measure);
}
current.measure = { chords: [] };
}
const sectionStart = signs.find(a => a.match(/^\*[a-zA-Z]/));
if (sectionStart) {
signs = signs.filter(s => s !== sectionStart);
current.measure.section = sectionStart.replace('*', '');
}
const hasCodaSign = signs.includes('Q');
if (hasCodaSign) {
signs = signs.filter(s => s !== 'Q');
let codaSign = 'ToCoda';
if (!!current.measures.find(m => Measure.hasSign('ToCoda', m))) {
codaSign = 'Coda';
}
current.measure.signs = (current.measure.signs || []).concat([codaSign]);
}
const houseStart = signs.find(s => !!s.match(/^N./));
if (houseStart) {
signs = signs.filter(s => s !== houseStart);
current.measure.house = parseInt(houseStart.replace('N', ''));
}
const time = signs.find(a => a.match(/^T\d\d/));
if (time) {
signs = signs.filter(s => s !== time);
current.measure.time = time.replace('T', '');
}
if (token.chord) {
current.measure.chords.push(this.getChord(token.chord));
} else if (token.token === 'n') {
current.measure.chords.push(0);
}
const last = current.measures[current.measures.length - 1];
if (last && last.chords[0] === 'r') {
last.chords = current.measures[current.measures.length - 3].chords;
current.measure.chords = current.measures[current.measures.length - 2].chords;
}
if (last && current.measure.chords[0] === 'x') {
current.measure.chords = [].concat(last.chords);
current.measure.idle = true;
}
if (signs.length) {
current.measure.signs = (current.measure.signs || [])
.concat(signs);
}
if (token.comments.length) {
current.measure.comments = (current.measure.comments || [])
.concat(token.comments.map(c => c.trim()));
}
return current;
}, { measure: null, signs: null, measures: [] });
if (parsed.measure.chords.length) {
parsed.measures.push(parsed.measure);
}
return parsed.measures;
}
static parse(raw: string): any {
var text = raw;
var arr = [], headers = [], comments = [];
var i;
text = text.trim();
// text = text.trimRight();
while (text) {
var found = false;
for (i = 0; i < RealParser.regExps.length; i++) {
var match = RealParser.regExps[i].exec(text);
if (match) {
found = true;
if (match.length <= 2) {
const replacement = match[0];
var repl = RealParser.replacements[replacement];
arr = arr.concat(repl ? repl : [replacement]);
text = text.substr(replacement.length);
}
else { // a chord
arr.push(match);
text = text.substr(match[0].length);
}
break;
}
}
if (!found) {
// ignore the comma separator
if (text[0] !== ',')
arr.push(text[0]);
text = text.substr(1);
}
}
// console.log(arr);
// pass 2: extract prefixes, suffixes, annotations and comments
var out = [];
var obj = RealParser.newToken(out);
for (i = 0; i < arr.length; i++) {
var token = arr[i];
if (token instanceof Array) {
obj.chord = RealParser.parseChord(token);
token = " ";
}
switch (token[0]) {
case ',': token = null; break; // separator
case 'S': // segno
case 'T': // time measurement
case 'Q': // coda
case 'N': // repeat
case 'U': // END
case 's': // small
case 'l': // normal
case 'f': // fermata
case '*': obj.annots.push(token); token = null; break;
case 'Y': obj.spacer++; token = null; break;
case 'r':
case 'x':
case 'W':
obj.chord = new iRealChord(token, "", null, null);
break;
case '<':
token = token.substr(1, token.length - 2);
token = token.replace(/XyQ/g, " "); // weird; needs to be done
obj.comments.push(token);
token = null; break;
default:
}
if (token) {
if ("]}Z".indexOf(arr[i + 1]) >= 0)
obj.bars += arr[++i];
if ("{[|".indexOf(token) >= 0) {
obj.bars += token; token = null;
}
}
if (token && i < arr.length - 1) {
obj.token = token;
obj = RealParser.newToken(out);
}
}
return out;
}
static parseChord(match) {
var note = match[1] || " ";
var modifiers = match[2] || "";
var comment = match[3] || "";
if (comment)
modifiers += comment.substr(1, comment.length - 2).replace("XyQ", " ");
var over = match[4] || "";
if (over[0] === '/')
over = over.substr(1);
var alternate = match[5] || null;
if (alternate) {
match = RealParser.chordRegex.exec(alternate.substr(1, alternate.length - 2));
if (!match)
alternate = null;
else
alternate = this.parseChord(match);
}
// empty cell?
if (note === " " && !alternate && !over)
return null;
if (over) {
var offset = (over[1] === '#' || over[1] === 'b') ? 2 : 1;
over = new iRealChord(over.substr(0, offset), over.substr(offset), null, null);
}
else
over = null;
return new iRealChord(note, modifiers, over, alternate);
}
static newToken(arr) {
var obj = new iRealToken;
arr.push(obj);
return obj;
}
}
class iRealChord {
note: any;
modifiers: any;
over: any;
alternate: any;
constructor(note, modifiers, over, alternate) {
this.note = note;
this.modifiers = modifiers;
this.over = over;
this.alternate = alternate;
}
}
class iRealToken {
chord: any;
spacer: number;
bars: string;
comments: any[];
token: any;
annots: any[];
constructor() {
this.annots = [];
this.comments = [];
this.bars = "";
this.spacer = 0;
this.chord = null;
}
}