]> git.donarmstrong.com Git - lilypond.git/blob - scm/define-event-classes.scm
* python/convertrules.py (conv): warning on \tempo{}
[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       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     (layout-instruction-event . (apply-output-event))
27     (script-event . (articulation-event text-script-event))
28     (part-combine-event . (solo1-event solo2-event unisono-event))
29     (break-event . (line-break-event page-break-event page-turn-event))
30     (dynamic-event . (absolute-dynamic-event))
31     (span-event . (span-dynamic-event beam-event ligature-event
32                          pedal-event phrasing-slur-event slur-event staff-span-event
33                          text-span-event trill-span-event tremolo-span-event 
34                          tuplet-span-event))
35     (span-dynamic-event . (decrescendo-event crescendo-event))
36     (pedal-event . (sostenuto-event sustain-event una-corda-event))
37     (rhythmic-event . (lyric-event melodic-event multi-measure-rest-event
38                                    rest-event skip-event bass-figure-event))
39     (melodic-event . (cluster-note-event note-event))
40     (() . (Announcement))
41     (Announcement . (AnnounceNewContext))
42     ))
43
44 ;; Maps event-class to a list of ancestors (inclusive)
45 (define ancestor-lookup (make-hash-table 11))
46
47 ;; Each class will be defined as
48 ;; (class parent grandparent .. )
49 ;; so that (eq? (cdr class) parent) holds.
50 (for-each
51  (lambda (rel)
52    (for-each
53     (lambda (type)
54       (hashq-set! ancestor-lookup type 
55                   (cons type (hashq-ref ancestor-lookup (car rel) '()))))
56     (cdr rel)))
57  event-classes)
58
59 ;; TODO: Allow entering more complex classes, by taking unions.
60 (define-public (ly:make-event-class leaf)
61  (hashq-ref ancestor-lookup leaf))
62
63 ;; does this exist in guile already?
64 (define (map-tree f t)
65   (cond
66    ((list? t)
67     (map (lambda (x) (map-tree f x)) t))
68    ((pair? t)
69     (cons (map-tree f (car t)) (map-tree f (cdr t))))
70    (else (f t))))
71
72 ;; expand each non-leaf subtree to (root . children), recursively
73 (define (expand-event-tree root)
74   (let ((children (assq root event-classes)))
75     (if children
76         (cons root (map expand-event-tree (cdr children)))
77         root)))
78
79 ;; All leaf event classes that no translator listens to
80 ;; directly. Avoids printing a warning.
81 (define unlistened-music-event-classes
82   '(harmonic-event line-break-event page-break-event page-turn-event
83                    solo1-event solo2-event skip-event unisono-event))
84
85 ;; produce neater representation of music event tree.
86 ;; TODO: switch to this representation for the event-classes list?
87 (define music-event-tree (expand-event-tree 'music-event))
88 (define (sort-tree t)
89   (define (stringify el)
90               (if (symbol? el)
91                   (symbol->string el)
92                   (symbol->string (first el))))
93   (if (list? t)
94       (sort (map (lambda (el)
95                    (if (list? el)
96                        (cons (car el) (sort-tree (cdr el)))
97                        el))
98                  t)
99             (lambda (a b) (string<? (stringify a) (stringify b))))
100       t))
101
102 ;;(use-modules (ice-9 pretty-print))
103 ;;(pretty-print (cons (car music-event-tree) (sort-tree (cdr music-event-tree))))
104
105 ;; check that the music event tree corresponds well with the set of
106 ;; available translators; print warnings otherwise.
107 (map-tree (lambda (sym) 
108             (if (and (symbol? sym)
109                      (not (ly:is-listened-event-class sym))
110                      (not (assq sym event-classes))
111                      (not (memq sym unlistened-music-event-classes)))
112                 (ly:programming-error (_ "event class ~A seems to be unused") sym)))      
113           music-event-tree)
114
115 (map (lambda (sym)
116        (if (not (pair? (ly:make-event-class sym)))
117            ;; should be programming-error
118            (ly:error (_ "translator listens to nonexisting event class ~A") sym)))
119      (ly:get-listened-event-classes))
120
121 (defmacro-public make-stream-event (expr)
122   (Stream_event::undump (primitive-eval (list 'quasiquote expr))))
123
124 (define* (simplify e)
125   (cond
126    ;; Special case for lists reduces stack consumption.
127    ((list? e) (map simplify e))
128    ((pair? e) (cons (simplify (car e))
129                     (simplify (cdr e))))
130    ((ly:stream-event? e)
131     (list 'unquote `(make-stream-event ,(simplify (Stream_event::dump e)))))
132    ((ly:music? e)
133     (list 'unquote (music->make-music e)))
134    ((ly:moment? e)
135     (list 'unquote `(ly:make-moment
136                      ,(ly:moment-main-numerator e)
137                      ,(ly:moment-main-denominator e)
138                      . ,(if (eq? 0 (ly:moment-grace-numerator e))
139                             '()
140                             (list (ly:moment-grace-numerator e)
141                                   (ly:moment-grace-denominator e))))))
142    ((ly:duration? e)
143     (list 'unquote `(ly:make-duration
144                      ,(ly:duration-log e)
145                      ,(ly:duration-dot-count e)
146                      ,(car (ly:duration-factor e))
147                      ,(cdr (ly:duration-factor e)))))
148    ((ly:pitch? e)
149     (list 'unquote `(ly:make-pitch
150                      ,(ly:pitch-octave e)
151                      ,(ly:pitch-notename e)
152                      ,(ly:pitch-alteration e))))
153    ((ly:input-location? e)
154     (list 'unquote '(ly:dummy-input-location)))
155    (#t e)))
156
157 (define-public (ly:simplify-scheme e)
158   (list 'quasiquote (simplify e)))
159