-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.py
362 lines (311 loc) · 8.22 KB
/
parser.py
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
import io
class ParserError(Exception):
def __init__(self, input: io.StringIO, message: str):
told = input.tell()
input.seek(0)
prev = input.read(told)
prev = prev.split("\n")
last = prev.pop()
self.lineno = len(prev) + 1
self.colno = len(last) + 1
self.message = message
def __str__(self):
return f"{self.message} at line {self.lineno} column {self.colno}"
def parse_music(input):
groups = [parse_group(input)]
while True:
told = input.tell()
try:
parse_ch(input, ";")
groups.append(parse_group(input))
except ParserError:
input.seek(told)
break
order = parse_order(input)
parse_eof(input)
return {"groups": groups, "order": order}
def parse_order(input):
told = input.tell()
try:
parse_ch(input, "|")
return None
except ParserError:
input.seek(told)
try:
parse_ch(input, ":")
nums = []
while True:
told = input.tell()
try:
nums.append(parse_positive(input))
except ParserError:
input.seek(told)
break
return nums
except ParserError:
input.seek(told)
raise ParserError(input, "Expected order")
def parse_group(input):
mod = parse_mod(input)
mtr = parse_mtr(input)
bmp = parse_bmp(input)
passages = [parse_passage(input)]
while True:
told = input.tell()
try:
parse_ch(input, ",")
passages.append(parse_passage(input))
except ParserError:
input.seek(told)
break
return {"mod": mod, "mtr": mtr, "bmp": bmp, "passages": passages}
def parse_passage(input):
measures = [parse_measure(input)]
while True:
told = input.tell()
try:
measures.append(parse_measure(input))
except ParserError:
input.seek(told)
break
return {"measures": measures}
def parse_measure(input):
elements = [parse_element(input)]
while True:
told = input.tell()
try:
elements.append(parse_element(input))
except ParserError:
input.seek(told)
break
parse_ch(input, "|")
return {"elements": elements}
def parse_element(input):
told = input.tell()
try:
note = parse_note(input)
time = parse_time(input)
return {"note": note, "time": time}
except ParserError:
input.seek(told)
try:
angled = parse_angled(input)
return {"angled": angled}
except ParserError:
input.seek(told)
try:
rat = parse_rat(input)
braced = parse_braced(input)
return {"rat": rat, "braced": braced}
except ParserError:
input.seek(told)
try:
rat = parse_rat(input)
angled = parse_angled(input)
return {"rat": rat, "angled": angled}
except ParserError:
input.seek(told)
raise ParserError(input, "Expected element")
def parse_rat(input):
parse_ch(input, "[")
n = parse_positive(input)
told = input.tell()
try:
parse_ch(input, ":")
d = parse_positive(input)
except ParserError:
input.seek(told)
d = None
parse_ch(input, "]")
return {"n": n, "d": d}
def parse_angled(input):
parse_ch(input, "<")
elements = []
while True:
told = input.tell()
try:
elements.append(parse_element(input))
except ParserError:
input.seek(told)
break
parse_ch(input, ">")
return {"elements": elements}
def parse_braced(input):
parse_ch(input, "{")
elements = []
while True:
told = input.tell()
try:
elements.append(parse_element(input))
except ParserError:
input.seek(told)
break
parse_ch(input, "}")
return {"elements": elements}
def parse_note(input):
told = input.tell()
try:
return parse_sao(input)
except ParserError:
input.seek(told)
try:
parse_ch(input, "0")
return {"rest": "0"}
except ParserError:
input.seek(told)
try:
parse_ch(input, "-")
return {"tied": "-"}
except ParserError:
input.seek(told)
raise ParserError(input, "Expected note")
def parse_sao(input):
accid = parse_accid(input)
solfa = parse_solfa(input)
octav = parse_octav(input)
return {"accid": accid, "solfa": solfa, "octav": octav}
def parse_aao(input):
alpha = parse_alpha(input)
accid = parse_accid(input)
octav = parse_octav(input)
return {"alpha": alpha, "accid": accid, "octav": octav}
def parse_mod(input):
lft = parse_sao(input)
parse_ch(input, "=")
rgt = parse_aao(input)
return {"lft": lft, "rgt": rgt}
def parse_mtr(input):
n = parse_positive(input)
parse_ch(input, "/")
d = parse_positive(input)
return {"n": n, "d": d}
def parse_bmp(input):
return parse_positive(input)
def parse_accid(input):
told = input.tell()
try:
parse_ch(input, "@")
return 0
except ParserError:
input.seek(told)
try:
parse_ch(input, "#")
i = +1
while True:
told = input.tell()
try:
parse_ch(input, "#")
i += 1
except ParserError:
input.seek(told)
break
return i
except ParserError:
input.seek(told)
try:
parse_ch(input, "b")
i = -1
while True:
told = input.tell()
try:
parse_ch(input, "b")
i -= 1
except ParserError:
input.seek(told)
break
return i
except ParserError:
input.seek(told)
return None
def parse_octav(input):
told = input.tell()
try:
parse_ch(input, "'")
i = +1
while True:
told = input.tell()
try:
parse_ch(input, "'")
i += 1
except ParserError:
input.seek(told)
break
return i
except ParserError:
input.seek(told)
try:
parse_ch(input, ",")
i = -1
while True:
told = input.tell()
try:
parse_ch(input, ",")
i -= 1
except ParserError:
input.seek(told)
break
return i
except ParserError:
input.seek(told)
return 0
def parse_time(input):
und = 0
while True:
told = input.tell()
try:
parse_ch(input, "/")
und += 1
except ParserError:
input.seek(told)
break
dot = 0
while True:
told = input.tell()
try:
parse_ch(input, ".")
dot += 1
except ParserError:
input.seek(told)
break
return {"und": und, "dot": dot}
def parse_positive(input):
parse_ws(input)
char = input.read(1)
if char not in {"1", "2", "3", "4", "5", "6", "7", "8", "9"}:
raise ParserError(input, "Expected a positive integer")
num = char
while True:
told = input.tell()
char = input.read(1)
if char not in {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}:
input.seek(told)
break
num += char
return num
def parse_alpha(input):
parse_ws(input)
char = input.read(1)
if char not in {"C", "D", "E", "F", "G", "A", "B"}:
raise ParserError(input, "Expected A-G")
return char
def parse_solfa(input):
parse_ws(input)
char = input.read(1)
if char not in {"1", "2", "3", "4", "5", "6", "7"}:
raise ParserError(input, "Expected 1-7")
return char
def parse_eof(input):
parse_ws(input)
if input.read(1):
raise ParserError(input, "Expected end of file")
def parse_ch(input, char):
parse_ws(input)
if input.read(1) != char:
raise ParserError(input, f"Expected {char}")
def parse_ws(input):
while True:
told = input.tell()
char = input.read(1)
if not char.isspace():
input.seek(told)
break