This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
/
tokenize.clj
715 lines (591 loc) · 20.6 KB
/
tokenize.clj
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
(ns alda.parser.tokenize
(:require [clojure.core.async :refer (>!!)]))
(def initial-parser-state
{:state :parsing ; parsing, done, or error
:line 1
:column 1
:stack []}) ; context for nesting tokens
(defn parser
[tokens-ch]
(assoc initial-parser-state :tokens-ch tokens-ch))
(def token-names
{:accidentals "accidentals"
:clj-char "Clojure character"
:clj-sexp "Clojure S-expression"
:clj-expr "Clojure expression"
:clj-string "Clojure string"
:colon "':'"
:comment "comment"
:duration "duration"
:equals "'='"
:event-seq "event sequence"
:name "name"
:nickname "nickname"
:note "note"
:note-length "note length"
:note-rest-or-name "note, rest, or name"
:octave-change "octave change"
:repeat "repeat"
:rest "rest"
:slash "'/'"})
(defn current-token-type
[{:keys [stack] :as parser}]
(-> stack peek first))
(defn starting-line-and-column
[{:keys [stack] :as parser}]
(-> stack peek second))
(defn current-token-content
[{:keys [stack] :as parser}]
(-> stack peek (nth 2)))
(defn last-token-type
[{:keys [stack] :as parser}]
(-> stack pop peek first))
(defn last-token-content
[{:keys [stack] :as parser}]
(-> stack pop peek (nth 2)))
(defn currently-parsing?
[parser token]
(= (current-token-type parser) token))
(defn pop-stack
[parser]
(-> parser (update :stack #(if (empty? %) % (pop %)))))
(defn emit!
[{:keys [tokens-ch] :as parser} x]
(>!! tokens-ch x)
parser)
(defn emit-token!
[parser & {:keys [token content pop-stack?]}]
(let [maybe-pop-stack #(if pop-stack? (pop-stack %) %)]
(-> parser
(emit! [(or token (current-token-type parser))
(starting-line-and-column parser)
(or content (current-token-content parser))])
maybe-pop-stack)))
(defn emit-error!
[parser e-or-msg]
(let [error (if (instance? Throwable e-or-msg)
e-or-msg
(Exception. e-or-msg))]
(-> parser (emit! error) (assoc :state :error))))
(defn unexpected-char-error
[{:keys [line column] :as parser} character]
(let [parsing (current-token-type parser)
error-msg (format "Unexpected %s%s at line %s, column %s."
(if (= :EOF character)
"EOF"
(format "'%s'" character))
(if parsing
(str " in " (get token-names parsing parsing))
"")
line
column)]
(-> parser (emit-error! error-msg))))
(defn reject-chars
[parser character blacklist]
(when (contains? blacklist character)
(unexpected-char-error parser character)))
(defn next-line
[parser]
(-> parser (update :line inc) (assoc :column 1)))
(defn next-column
[parser]
(-> parser (update :column inc)))
(defn advance
[parser x & [size]]
(cond
(= :EOF x)
parser
(#{\newline "\n"} x)
(-> parser next-line)
:else
(-> parser (update :column + (or size 1)))))
(defn append-to-current-buffer
[{:keys [stack] :as parser} x]
(if-let [[token [line col] buffer] (peek stack)]
(update parser :stack #(-> % pop (conj [token [line col] (str buffer x)])))
parser))
(defn add-current-buffer-to-last
[{:keys [stack] :as parser}]
(let [[current-token current-line-col current-buffer] (peek stack)
popped-stack (pop stack)
[last-token last-line-col last-buffer] (peek popped-stack)
last-buffer+ (str last-buffer
current-buffer)]
(-> parser
(assoc :stack (-> popped-stack pop (conj [last-token
last-line-col
last-buffer+]))))))
(defn new-buffer
[{:keys [line column] :as parser} token]
(-> parser (update :stack conj [token [line column] ""])))
(defn read-to-buffer
[parser x & [size]]
(-> parser (append-to-current-buffer x) (advance x size)))
(defn read-to-new-buffer
[parser token x & [size]]
(-> parser (new-buffer token) (read-to-buffer x size)))
(defn rename-current-token
[{:keys [stack] :as parser} token]
(let [renamed-token (-> stack peek (assoc 0 token))]
(-> parser (update :stack #(-> % pop (conj renamed-token))))))
(defn read-chars
[parser character whitelist]
(when (contains? whitelist character)
(-> parser (read-to-buffer character))))
(defn discard-buffer
[parser]
(-> parser (update :stack pop)))
(defn ensure-parsing
"If the parser's state is not :parsing, short-circuits the parser so that the
current state is passed through until the end.
Otherwise returns nil so that parsing will continue."
[{:keys [state] :as parser}]
(when (not= :parsing state)
parser))
(defn finish-parsing
[parser character]
(when (= :EOF character)
(-> parser (assoc :state :done))))
(defn ignore-carriage-return
[parser character]
(when (= \return character)
parser))
(defn skip-whitespace
[parser character]
(when (#{\newline \space} character)
(advance parser character)))
(declare read-character!)
(defn start-parsing
[parser character token
& [{:keys [start-char ignore-first-char buffer-first-char]}]]
(when (or (nil? start-char)
(= start-char character)
(and (set? start-char) (contains? start-char character)))
(let [maybe-advance #(if ignore-first-char
(-> % (advance character))
%)
maybe-buffer #(if buffer-first-char
(-> % (read-to-buffer character))
%)
maybe-read #(if (or ignore-first-char buffer-first-char)
%
(-> % (read-character! character)))]
(-> parser (new-buffer token) maybe-advance maybe-buffer maybe-read))))
(defn start-parsing-accidentals
[p c]
(start-parsing p c :accidentals))
(defn start-parsing-at-marker
[p c]
(start-parsing p c :at-marker {:start-char \@ :ignore-first-char true}))
(defn start-parsing-clj-char
[p c]
(start-parsing p c :clj-char {:start-char \\}))
(defn start-parsing-clj-sexp
[p c]
(start-parsing p c :clj-sexp {:start-char \( :buffer-first-char true}))
(defn start-parsing-clj-string
[p c]
(start-parsing p c :clj-string {:start-char \" :buffer-first-char true}))
(defn start-parsing-comment
[p c]
(start-parsing p c :comment {:start-char \# :ignore-first-char true}))
(defn start-parsing-duration
[p c]
(start-parsing p c :duration))
(defn start-parsing-marker
[p c]
(start-parsing p c :marker {:start-char \% :ignore-first-char true}))
(defn start-parsing-nickname
[p c]
(start-parsing p c :nickname {:start-char \" :ignore-first-char true}))
(defn start-parsing-note-length
[p c]
(start-parsing p c :note-length))
(defn start-parsing-note-rest-or-name
[p c]
(start-parsing p c :note-rest-or-name
{:start-char (set (str "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
:buffer-first-char true}))
(defn start-parsing-octave-change
[p c]
(start-parsing p c :octave-change {:start-char #{\o \< \>}}))
(defn start-parsing-repeat
[p c]
(start-parsing p c :repeat {:start-char \* :ignore-first-char true}))
(defn start-parsing-voice
[p c]
(start-parsing p c :voice {:start-char \V}))
(declare parse-tie)
(defn parse-accidentals
[parser character]
(when (currently-parsing? parser :accidentals)
(condp contains? character
#{\+ \- \_}
(-> parser (read-to-buffer character))
#{\0 \1 \2 \3 \4 \5 \6 \7 \8 \9}
(-> parser (emit-token! :pop-stack? true)
(start-parsing-duration character))
#{\~}
(-> parser (emit-token! :pop-stack? true)
(parse-tie character))
; else
(-> parser (emit-token! :pop-stack? true)
(read-character! character)))))
(defn parse-at-marker
[parser character]
(when (currently-parsing? parser :at-marker)
(if ((set (str "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789_-"))
character)
(-> parser (read-to-buffer character))
(-> parser (emit-token! :pop-stack? true)
(read-character! character)))))
(defn parse-barline
[parser character]
(when (= \| character)
(-> parser (read-to-new-buffer :barline character)
(emit-token! :pop-stack? true))))
(defn parse-clj-char
[parser character]
(when (currently-parsing? parser :clj-char)
(cond
(empty? (current-token-content parser))
(-> parser (read-to-buffer character))
((set "0123456789abcdefghijklmnopqrstuvwxyz") character)
(-> parser (read-to-buffer character))
:else
(-> parser (read-to-buffer character) add-current-buffer-to-last))))
(declare parse-clj-string finish-parsing-clj-sexp)
(defn parse-clj-sexp
[p c]
(when (currently-parsing? p :clj-sexp)
(or (reject-chars p c #{:EOF})
(read-chars p c #{\newline \space \,})
(parse-clj-string p c)
(parse-clj-char p c)
(start-parsing-clj-sexp p c)
(start-parsing-clj-string p c)
(start-parsing-clj-char p c)
(finish-parsing-clj-sexp p c)
(read-to-buffer p c))))
(defn finish-parsing-clj-sexp
[parser character]
(when (= \) character)
(let [emit-or-continue-parsing-parent
(if (= :clj-sexp (last-token-type parser))
add-current-buffer-to-last
#(-> % (emit-token! :token :clj-expr :pop-stack? true)))]
(-> parser (read-to-buffer \)) emit-or-continue-parsing-parent))))
(defn parse-clj-string
[parser character]
(when (currently-parsing? parser :clj-string)
(cond
(= \\ (last (current-token-content parser)))
(-> parser (read-to-buffer character))
(= \" character)
(-> parser (read-to-buffer character) add-current-buffer-to-last)
:else
(-> parser (read-to-buffer character)))))
(defn parse-colon
[parser character]
(when (= \: character)
(-> parser (read-to-new-buffer :colon character)
(emit-token! :pop-stack? true))))
(declare parse-newline)
(defn parse-comment
[parser character]
(when (currently-parsing? parser :comment)
(if (= \newline character)
(-> parser (emit-token! :pop-stack? true) (parse-newline character))
(-> parser (read-to-buffer character)))))
(defn parse-cram-open
[parser character]
(when (= \{ character)
(-> parser (read-to-new-buffer :cram-open character)
(emit-token! :pop-stack? true))))
(defn parse-cram-close
[parser character]
(when (= \} character)
(-> parser (read-to-new-buffer :cram-close character)
(emit-token! :pop-stack? true)
(new-buffer :duration))))
(defn parse-duration
[p c]
(when (currently-parsing? p :duration)
(condp contains? c
#{\space}
(-> p (advance c))
#{\newline}
(-> p (parse-newline c))
#{\0 \1 \2 \3 \4 \5 \6 \7 \8 \9}
(-> p (start-parsing-note-length c))
#{\|}
(-> p (parse-barline c))
#{\~}
(-> p (parse-tie c))
; else
(-> p discard-buffer (read-character! c)))))
(defn parse-equals
[parser character]
(when (= \= character)
(-> parser (read-to-new-buffer :equals character)
(emit-token! :pop-stack? true))))
(defn parse-event-seq-open
[parser character]
(when (= \[ character)
(-> parser (read-to-new-buffer :event-seq-open character)
(emit-token! :pop-stack? true))))
(defn parse-event-seq-close
[parser character]
(when (= \] character)
(-> parser (read-to-new-buffer :event-seq-close character)
(emit-token! :pop-stack? true))))
(defn parse-marker
[parser character]
(when (currently-parsing? parser :marker)
(if ((set (str "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789_-"))
character)
(-> parser (read-to-buffer character))
(-> parser (emit-token! :pop-stack? true)
(read-character! character)))))
(defn parse-name
[parser character]
(when (currently-parsing? parser :name)
(if ((set (str "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789_-."))
character)
(-> parser (read-to-buffer character))
(-> parser (emit-token! :pop-stack? true)
(read-character! character)))))
(defn parse-newline
[parser character]
(when (= \newline character)
(-> parser (read-to-new-buffer :newline character)
(emit-token! :pop-stack? true))))
(defn parse-nickname
[parser character]
(when (currently-parsing? parser :nickname)
(let [buffer (current-token-content parser)]
(cond
(= \" character)
(-> parser (emit-token! :pop-stack? true)
(advance character))
((set (str "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789_-"))
character)
(-> parser (read-to-buffer character))
:else
(unexpected-char-error parser character)))))
(defn parse-note
[parser character]
(when (currently-parsing? parser :note)
(condp contains? character
#{\+ \- \_}
(-> parser (emit-token! :pop-stack? true)
(start-parsing-accidentals character))
#{\0 \1 \2 \3 \4 \5 \6 \7 \8 \9}
(-> parser (emit-token! :pop-stack? true)
(start-parsing-duration character))
#{\~}
(-> parser (emit-token! :pop-stack? true)
(parse-tie character))
; else
(-> parser (emit-token! :pop-stack? true)
(read-character! character)))))
(defn parse-note-length
[parser character]
(when (currently-parsing? parser :note-length)
(let [buffer (current-token-content parser)]
(condp contains? character
#{\0 \1 \2 \3 \4 \5 \6 \7 \8 \9}
(-> parser (read-to-buffer character))
#{\.}
(if (re-matches #"(\d+\.)?\d+\.*" buffer)
(-> parser (read-to-buffer character))
(-> parser (unexpected-char-error character)))
#{\m}
(if (re-matches #"\d+" buffer)
(-> parser (read-to-buffer character))
(-> parser (unexpected-char-error character)))
#{\s}
(if (re-matches #"\d+m?" buffer)
(-> parser (read-to-buffer character))
(-> parser (unexpected-char-error character)))
; else
(-> parser (emit-token! :pop-stack? true)
(start-parsing-duration character))))))
(defn parse-note-rest-or-name
"Parse a character that could be part of:
- a variable name
- a note
- a rest
- an instrument call"
[parser character]
(when (currently-parsing? parser :note-rest-or-name)
(let [buffer (current-token-content parser)
char1 (first buffer)
parse #(-> %1 (rename-current-token %2)
(read-character! character))]
(cond
(and ((set "abcdefg") char1)
((conj (set "# \n+-_/~*}]<>0123456789") :EOF) character))
(-> parser (parse :note))
(and (= \r char1)
((conj (set "# \n/~*}]<>0123456789") :EOF) character))
(-> parser (parse :rest))
((set "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") character)
(-> parser (parse :name))
:else
(-> parser (unexpected-char-error character))))))
(defn parse-repeat
[parser character]
(let [buffer (current-token-content parser)]
(when (currently-parsing? parser :repeat)
(cond
(#{\0 \1 \2 \3 \4 \5 \6 \7 \8 \9} character)
(-> parser (read-to-buffer character))
(re-matches #"\d+" buffer)
(-> parser (emit-token! :pop-stack? true)
(read-character! character))
(= \space character)
(-> parser (advance character))
(= \newline character)
(-> parser (parse-newline character))
:else
(-> parser (unexpected-char-error character))))))
(defn parse-rest
[parser character]
(when (currently-parsing? parser :rest)
(condp contains? character
#{\space :EOF}
(-> parser
(advance character)
(emit-token! :pop-stack? true))
#{\0 \1 \2 \3 \4 \5 \6 \7 \8 \9}
(-> parser
(emit-token! :pop-stack? true)
(start-parsing-duration character))
; else
(-> parser
(emit-token! :pop-stack? true)
(read-character! character)))))
(defn parse-slash
[parser character]
(when (= \/ character)
(-> parser (read-to-new-buffer :slash character)
(emit-token! :pop-stack? true))))
(defn parse-tie
[parser character]
(when (= \~ character)
(-> parser (read-to-new-buffer :tie character)
(emit-token! :pop-stack? true))))
(defn parse-voice
[parser character]
(when (currently-parsing? parser :voice)
(let [buffer (current-token-content parser)]
(condp re-matches buffer
#""
(if (= \V character)
(-> parser (read-to-buffer character))
(-> parser (unexpected-char-error character)))
#"V"
(condp contains? character
#{\0 \1 \2 \3 \4 \5 \6 \7 \8 \9}
(-> parser (read-to-buffer character))
(set "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
(-> parser (rename-current-token :name)
(read-character! character))
; else
(-> parser (unexpected-char-error character)))
#"V\d+"
(condp contains? character
#{\0 \1 \2 \3 \4 \5 \6 \7 \8 \9}
(-> parser (read-to-buffer character))
#{\:}
(-> parser (emit-token! :pop-stack? true)
(advance character)))))))
(defn parse-octave-change
[parser character]
(when (currently-parsing? parser :octave-change)
(let [buffer (current-token-content parser)]
(condp re-matches buffer
#""
(condp contains? character
#{\o}
(-> parser (read-to-buffer character))
#{\< \>}
(-> parser (read-to-buffer character) (emit-token! :pop-stack? true))
; else
(-> parser (unexpected-char-error character)))
#"o"
(condp contains? character
#{\- \0 \1 \2 \3 \4 \5 \6 \7 \8 \9}
(-> parser (read-to-buffer character))
(set "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
(-> parser (rename-current-token :name)
(read-character! character))
; else
(-> parser (unexpected-char-error character)))
#"o-?\d+"
(condp contains? character
#{\0 \1 \2 \3 \4 \5 \6 \7 \8 \9}
(-> parser (read-to-buffer character))
#{\space :EOF}
(-> parser (emit-token! :pop-stack? true) (advance character))
#{\newline}
(-> parser (emit-token! :pop-stack? true) (parse-newline character))
; else
(-> parser (unexpected-char-error character)))))))
(defn read-character!
"Reads one character `c` and updates parser `p`.
Puts tokens on (:tokens-ch p) as they are parsed."
[p c]
(try
(or (ensure-parsing p)
(ignore-carriage-return p c)
(parse-comment p c)
(parse-clj-sexp p c)
(parse-clj-string p c)
(parse-clj-char p c)
(parse-note p c)
(parse-rest p c)
(parse-name p c)
(parse-nickname p c)
(parse-voice p c)
(parse-octave-change p c)
(parse-marker p c)
(parse-at-marker p c)
(parse-note-rest-or-name p c)
(parse-duration p c)
(parse-note-length p c)
(parse-accidentals p c)
(parse-repeat p c)
(parse-slash p c)
(parse-colon p c)
(parse-barline p c)
(parse-equals p c)
(parse-newline p c)
(parse-cram-open p c)
(parse-cram-close p c)
(parse-event-seq-open p c)
(parse-event-seq-close p c)
(start-parsing-clj-sexp p c)
(start-parsing-comment p c)
(start-parsing-voice p c)
(start-parsing-octave-change p c)
(start-parsing-marker p c)
(start-parsing-at-marker p c)
(start-parsing-note-rest-or-name p c)
(start-parsing-nickname p c)
(start-parsing-repeat p c)
(finish-parsing p c)
(skip-whitespace p c)
(unexpected-char-error p c))
(catch Throwable e
(emit-error! p e))))