]> git.donarmstrong.com Git - lilypond.git/blob - scm/ly-syntax-constructors.scm
Issue 4440: Establish %parser in Lily_parser::do_yyparse
[lilypond.git] / scm / ly-syntax-constructors.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2006--2015 Erik Sandberg <mandolaerik@gmail.com>
4 ;;;;
5 ;;;; LilyPond is free software: you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation, either version 3 of the License, or
8 ;;;; (at your option) any later version.
9 ;;;;
10 ;;;; LilyPond is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ;;;; GNU General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
17
18 ;; TODO: use separate module for syntax
19 ;; constructors. Also create wrapper around the constructor?
20 (defmacro define-ly-syntax (args . body)
21   `(define-public ,args ,@body))
22
23 ;; A ly-syntax constructor takes one extra parameter,
24 ;; location. This is mainly used for reporting errors and
25 ;; warnings. This function is a syntactic sugar which uses the
26 ;; location arg to set the origin of the returned music object; this
27 ;; behaviour is usually desired
28 (defmacro define-ly-syntax-loc (args . body)
29   `(define-public ,args
30      (let ((m ,(cons 'begin body)))
31        (set! (ly:music-property m 'origin) ,(second args))
32        m)))
33 ;; Like define-ly-syntax-loc, but adds location
34 ;; parameter. Useful for simple constructors that don't need to
35 ;; report errors.
36 (defmacro define-ly-syntax-simple (args . body)
37   `(define-public ,(cons* (car args)
38                           'location
39                           (cdr args))
40      (let ((m ,(cons 'begin body)))
41        (set! (ly:music-property m 'origin) location)
42        m)))
43
44 (define (music-function-call-error loc fun m)
45   (let* ((sig (ly:music-function-signature fun))
46          (pred (if (pair? (car sig)) (caar sig) (car sig))))
47     (ly:parser-error (*parser*)
48                      (format #f (_ "~a function cannot return ~a")
49                              (type-name pred)
50                              (value->lily-string m (*parser*)))
51                      loc)
52     (and (pair? (car sig)) (cdar sig))))
53
54 ;; Music function: Apply function and check return value.
55 ;; args are in reverse order, rest may specify additional ones
56 ;;
57 ;; If args is not a proper list, an error has been flagged earlier
58 ;; and no fallback value had been available.  In this case,
59 ;; we don't call the function but rather return the general
60 ;; fallback.
61 (define-ly-syntax (music-function loc fun args . rest)
62   (let* ((sig (ly:music-function-signature fun))
63          (pred (if (pair? (car sig)) (caar sig) (car sig)))
64          (good (proper-list? args))
65          (m (and good (with-fluids ((%location loc))
66                                    (apply (ly:music-function-extract fun)
67                                           (reverse! args rest))))))
68     (if (and good (pred m))
69         (begin
70           (if (ly:music? m)
71               (set! (ly:music-property m 'origin) loc))
72           m)
73         (if good
74             (music-function-call-error loc fun m)
75             (and (pair? (car sig)) (cdar sig))))))
76
77 (define-ly-syntax (argument-error location n pred arg)
78   (ly:parser-error
79    (*parser*)
80    (format #f
81            (_ "wrong type for argument ~a.  Expecting ~a, found ~s")
82            n (type-name pred) (music->make-music arg))
83    location))
84
85 (define-ly-syntax-simple (void-music)
86   (make-music 'Music))
87
88 (define-ly-syntax-simple (sequential-music mlist)
89   (make-sequential-music mlist))
90
91 (define-ly-syntax-simple (simultaneous-music mlist)
92   (make-simultaneous-music mlist))
93
94 (define-ly-syntax-simple (event-chord mlist)
95   (make-music 'EventChord
96               'elements mlist))
97
98 (define-ly-syntax-simple (unrelativable-music mus)
99   (make-music 'UnrelativableMusic
100               'element mus))
101
102 (define-ly-syntax-simple (context-change type id)
103   (make-music 'ContextChange
104               'change-to-type type
105               'change-to-id id))
106
107 (define-ly-syntax (tempo location text . rest)
108   (let* ((unit (and (pair? rest)
109                     (car rest)))
110          (count (and unit
111                      (cadr rest)))
112          (range-tempo? (pair? count))
113          (tempo-change (make-music 'TempoChangeEvent
114                                    'origin location
115                                    'text text
116                                    'tempo-unit unit
117                                    'metronome-count count))
118          (tempo-set
119           (and unit
120                (context-spec-music
121                 (make-property-set 'tempoWholesPerMinute
122                                    (ly:moment-mul
123                                     (ly:make-moment
124                                      (if range-tempo?
125                                          (round (/ (+ (car count) (cdr count))
126                                                    2))
127                                          count)
128                                      1)
129                                     (ly:duration-length unit)))
130                 'Score))))
131
132     (if tempo-set
133         (make-sequential-music (list tempo-change tempo-set))
134         tempo-change)))
135
136 (define-ly-syntax-simple (repeat type num body alts)
137   (make-repeat type num body alts))
138
139 (define (script-to-mmrest-text music)
140   "Extract @code{'direction} and @code{'text} from @var{music}, and transform
141 into a @code{MultiMeasureTextEvent}."
142
143   (if (music-is-of-type? music 'script-event)
144       (make-music 'MultiMeasureTextEvent music)
145       music))
146
147 (define-ly-syntax (multi-measure-rest location duration articulations)
148   (make-music 'MultiMeasureRestMusic
149               'articulations (map script-to-mmrest-text articulations)
150               'duration duration
151               'origin location))
152
153 (define-ly-syntax (repetition-chord location duration articulations)
154   (make-music 'EventChord
155               'duration duration
156               'elements articulations
157               'origin location))
158
159 (define-ly-syntax-simple (context-specification type id ops create-new mus)
160   (let ((csm (context-spec-music mus type id)))
161     (set! (ly:music-property csm 'property-operations) ops)
162     (if create-new (set! (ly:music-property csm 'create-new) #t))
163     csm))
164
165 (define-ly-syntax (composed-markup-list location commands markups)
166   ;; `markups' being a list of markups, eg (markup1 markup2 markup3),
167   ;; and `commands' a list of commands with their scheme arguments, in reverse order,
168   ;; eg: ((italic) (raise 4) (bold)), maps the commands on each markup argument, eg:
169   ;;  ((bold (raise 4 (italic markup1)))
170   ;;   (bold (raise 4 (italic markup2)))
171   ;;   (bold (raise 4 (italic markup3))))
172
173   (define (compose arg)
174     (fold
175      (lambda (cmd prev) (append cmd (list prev)))
176      arg
177      commands))
178   (let loop ((markups markups) (completed '()))
179     (cond ((null? markups) (reverse! completed))
180           ((markup? (car markups))
181            (loop (cdr markups)
182                  (cons (compose (car markups)) completed)))
183           (else
184            (call-with-values
185                (lambda () (break! markup? markups))
186              (lambda (complex rest)
187                (loop rest
188                      (reverse!
189                       (make-map-markup-commands-markup-list
190                        compose complex) completed))))))))
191
192 (define-ly-syntax (property-operation location ctx music-type symbol . args)
193   (let* ((props (case music-type
194                   ((PropertySet) (list 'value (car args)))
195                   ((PropertyUnset) '())
196                   ((OverrideProperty) (list 'grob-value (car args)
197                                             'grob-property-path (if (list? (cadr args))
198                                                                     (cadr args)
199                                                                     (cdr args))
200                                             'pop-first #t))
201                   ((RevertProperty)
202                    (if (list? (car args))
203                        (list 'grob-property-path (car args))
204                        (list 'grob-property-path args)))
205                   (else (ly:error (_ "Invalid property operation ~a") music-type))))
206          (m (apply make-music music-type
207                    'symbol symbol
208                    'origin location
209                    props)))
210     (make-music 'ContextSpeccedMusic
211                 'element m
212                 'context-type ctx
213                 'origin location)))
214
215 (define (get-first-context-id! mus)
216   "Find the name of a ContextSpeccedMusic, possibly naming it"
217   (let ((id (ly:music-property mus 'context-id)))
218     (if (eq? (ly:music-property mus 'name) 'ContextSpeccedMusic)
219         (if (and (string? id)
220                  (not (string-null? id)))
221             id
222             ;; We may reliably give a new context a unique name, but
223             ;; not an existing one
224             (if (ly:music-property mus 'create-new #f)
225                 (let ((id (get-next-unique-voice-name)))
226                   (set! (ly:music-property mus 'context-id) id)
227                   id)
228                 '()))
229         '())))
230
231 (define unique-counter -1)
232 (define (get-next-unique-voice-name)
233   (set! unique-counter (1+ unique-counter))
234   (call-with-output-string (lambda (p) (format p "uniqueContext~s" unique-counter))))
235
236 (define-ly-syntax-simple (lyric-event text duration)
237   (make-lyric-event text duration))
238
239 (define (lyric-combine-music sync sync-type music loc)
240   ;; CompletizeExtenderEvent is added following the last lyric in MUSIC
241   ;; to signal to the Extender_engraver that any pending extender should
242   ;; be completed if the lyrics end before the associated voice.
243   (append! (ly:music-property music 'elements)
244            (list (make-music 'CompletizeExtenderEvent)))
245   (make-music 'LyricCombineMusic
246               'element music
247               'associated-context sync
248               'associated-context-type sync-type
249               'origin loc))
250
251 (define-ly-syntax (lyric-combine location voice typ music)
252   (lyric-combine-music voice typ music location))
253
254 (define-ly-syntax (add-lyrics location music addlyrics-list)
255   (let* ((existing-voice-name (get-first-context-id! music))
256          (voice-name (if (string? existing-voice-name)
257                          existing-voice-name
258                          (get-next-unique-voice-name)))
259          (voice (if (string? existing-voice-name)
260                     music
261                     (make-music 'ContextSpeccedMusic
262                                 'element music
263                                 'context-type 'Voice
264                                 'context-id voice-name
265                                 'origin (ly:music-property music 'origin))))
266          (voice-type (ly:music-property voice 'context-type))
267          (lyricstos (map (lambda (mus)
268                            (let* ((loc (ly:music-property mus 'origin))
269                                   (lyr (lyric-combine-music
270                                         voice-name voice-type mus loc)))
271                              (make-music 'ContextSpeccedMusic
272                                          'create-new #t
273                                          'context-type 'Lyrics
274                                          'element lyr
275                                          'origin loc)))
276                          addlyrics-list)))
277     (make-simultaneous-music (cons voice lyricstos))))