]> git.donarmstrong.com Git - lilypond.git/blob - scm/define-event-classes.scm
c7f28846dde79b59e3e29eb0f6cef005c8145751
[lilypond.git] / scm / define-event-classes.scm
1 ;;;; stream-event-classes.scm -- define the tree of stream-event classes.
2 ;;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;;
5 ;;;; (c) 2005-2006 Erik Sandberg <mandolaerik@gmail.com>
6
7
8 (use-modules (srfi srfi-1))
9
10 ;; Event class hierarchy. Each line is on the form (Parent . (List of children))
11 (define event-classes
12   '((() . (StreamEvent))
13     (StreamEvent .
14                  (RemoveContext ChangeParent Override Revert UnsetProperty
15                                 SetProperty music-event OldMusicEvent CreateContext Prepare
16                                 OneTimeStep Finish))
17     (music-event . (arpeggio-event breathing-event extender-event span-event
18       rhythmic-event dynamic-event break-event percent-event
19       key-change-event string-number-event tie-event part-combine-event
20       metronome-change-event beam-forbid-event script-event
21       tremolo-event bend-after-event fingering-event glissando-event
22       harmonic-event hyphen-event laissez-vibrer-event mark-event
23       multi-measure-text-event note-grouping-event
24       pes-or-flexa-event repeat-tie-event spacing-section-event
25       layout-instruction-event))
26     (script-event . (articulation-event text-script-event))
27     (part-combine-event . (solo1-event solo2-event unisono-event))
28     (break-event . (line-break-event page-break-event page-turn-event))
29     (dynamic-event . (absolute-dynamic-event))
30     (span-event . (span-dynamic-event beam-event ligature-event
31                          pedal-event phrasing-slur-event slur-event staff-span-event
32                          text-span-event trill-span-event tremolo-span-event 
33                          tuplet-span-event))
34     (span-dynamic-event . (decrescendo-event crescendo-event))
35     (pedal-event . (sostenuto-event sustain-event una-corda-event))
36     (rhythmic-event . (lyric-event melodic-event multi-measure-rest-event
37                                    rest-event skip-event bass-figure-event))
38     (melodic-event . (cluster-note-event note-event))
39     (() . (Announcement))
40     (Announcement . (AnnounceNewContext))
41     ))
42
43 ;; Maps event-class to a list of ancestors (inclusive)
44 (define ancestor-lookup (make-hash-table))
45
46 ;; Each class will be defined as
47 ;; (class parent grandparent .. )
48 ;; so that (eq? (cdr class) parent) holds.
49 (for-each
50  (lambda (rel)
51    (for-each
52     (lambda (type)
53       (hashq-set! ancestor-lookup type 
54                   (cons type (hashq-ref ancestor-lookup (car rel) '()))))
55     (cdr rel)))
56  event-classes)
57
58 ;; TODO: Allow entering more complex classes, by taking unions.
59 (define-public (ly:make-event-class leaf)
60  (hashq-ref ancestor-lookup leaf))
61
62 ;; does this exist in guile already?
63 (define (map-tree f t)
64   (cond
65    ((list? t)
66     (map (lambda (x) (map-tree f x)) t))
67    ((pair? t)
68     (cons (map-tree f (car t)) (map-tree f (cdr t))))
69    (else (f t))))
70
71 ;; expand each non-leaf subtree to (root . children), recursively
72 (define (expand-event-tree root)
73   (let ((children (assq root event-classes)))
74     (if children
75         (cons root (map expand-event-tree (cdr children)))
76         root)))
77
78 ;; All leaf event classes that no translator listens to
79 ;; directly. Avoids printing a warning.
80 (define unlistened-music-event-classes
81   '(harmonic-event line-break-event page-break-event page-turn-event
82                    solo1-event solo2-event skip-event unisono-event))
83
84 ;; produce neater representation of music event tree.
85 ;; TODO: switch to this representation for the event-classes list?
86 (define music-event-tree (expand-event-tree 'music-event))
87 (define (sort-tree t)
88   (define (stringify el)
89               (if (symbol? el)
90                   (symbol->string el)
91                   (symbol->string (first el))))
92   (if (list? t)
93       (sort (map (lambda (el)
94                    (if (list? el)
95                        (cons (car el) (sort-tree (cdr el)))
96                        el))
97                  t)
98             (lambda (a b) (string<? (stringify a) (stringify b))))
99       t))
100
101 ;;(use-modules (ice-9 pretty-print))
102 ;;(pretty-print (cons (car music-event-tree) (sort-tree (cdr music-event-tree))))
103
104 ;; check that the music event tree corresponds well with the set of
105 ;; available translators; print warnings otherwise.
106 (map-tree (lambda (sym) 
107             (if (and (symbol? sym)
108                      (not (ly:is-listened-event-class sym))
109                      (not (assq sym event-classes))
110                      (not (memq sym unlistened-music-event-classes)))
111                 (ly:programming-error (_ "event class ~A seems to be unused") sym)))      
112           music-event-tree)
113
114 (map (lambda (sym)
115        (if (not (pair? (ly:make-event-class sym)))
116            ;; should be programming-error
117            (ly:error (_ "translator listens to nonexisting event class ~A") sym)))
118      (ly:get-listened-event-classes))
119
120 (defmacro-public make-stream-event (expr)
121   (Stream_event::undump (primitive-eval (list 'quasiquote expr))))
122
123 (define* (simplify e)
124   (cond
125    ;; Special case for lists reduces stack consumption.
126    ((list? e) (map simplify e))
127    ((pair? e) (cons (simplify (car e))
128                     (simplify (cdr e))))
129    ((ly:stream-event? e)
130     (list 'unquote `(make-stream-event ,(simplify (Stream_event::dump e)))))
131    ((ly:music? e)
132     (list 'unquote (music->make-music e)))
133    ((ly:moment? e)
134     (list 'unquote `(ly:make-moment
135                      ,(ly:moment-main-numerator e)
136                      ,(ly:moment-main-denominator e)
137                      . ,(if (eq? 0 (ly:moment-grace-numerator e))
138                             '()
139                             (list (ly:moment-grace-numerator e)
140                                   (ly:moment-grace-denominator e))))))
141    ((ly:duration? e)
142     (list 'unquote `(ly:make-duration
143                      ,(ly:duration-log e)
144                      ,(ly:duration-dot-count e)
145                      ,(car (ly:duration-factor e))
146                      ,(cdr (ly:duration-factor e)))))
147    ((ly:pitch? e)
148     (list 'unquote `(ly:make-pitch
149                      ,(ly:pitch-octave e)
150                      ,(ly:pitch-notename e)
151                      ,(ly:pitch-alteration e))))
152    ((ly:input-location? e)
153     (list 'unquote '(ly:dummy-input-location)))
154    (#t e)))
155
156 (define-public (ly:simplify-scheme e)
157   (list 'quasiquote (simplify e)))
158