]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
* lily/parser.yy (book_body): set default bookpaper.
[lilypond.git] / lily / parser.yy
1 %{ // -*-Fundamental-*-
2
3 /*
4   parser.yy -- Bison/C++ parser for LilyPond
5
6   source file of the GNU LilyPond music typesetter
7
8   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9                  Jan Nieuwenhuizen <janneke@gnu.org>
10 */
11
12 /*
13
14 TODO:
15
16 * The rules for who is protecting what are very shady. Uniformise
17   this.
18
19 * There are too many lexical modes?
20
21 */
22
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26
27
28 #include "book.hh"
29 #include "book-paper-def.hh"
30 #include "context-def.hh"
31 #include "dimensions.hh"
32 #include "event.hh"
33 #include "file-path.hh"
34 #include "input-smob.hh"
35 #include "input.hh"
36 #include "lily-guile.hh"
37 #include "lilypond-input-version.hh"
38 #include "ly-module.hh"
39 #include "main.hh"
40 #include "midi-def.hh"
41 #include "misc.hh"
42 #include "music-list.hh"
43 #include "music-sequence.hh"
44 #include "my-lily-lexer.hh"
45 #include "my-lily-parser.hh"
46 #include "paper-book.hh"
47 #include "paper-def.hh"
48 #include "scm-hash.hh"
49 #include "scm-option.hh"
50 #include "score.hh"
51 #include "text-item.hh"
52 #include "warn.hh"
53
54 #define MY_MAKE_MUSIC(x)  make_music_by_name (ly_symbol2scm (x))
55
56 Music *property_op_to_music (SCM op);
57 Music *context_spec_music (SCM type, SCM id, Music *m, SCM ops_);
58 SCM get_next_unique_context ();
59
60 #define YYERROR_VERBOSE 1
61
62 #define YYPARSE_PARAM my_lily_parser
63 #define YYLEX_PARAM my_lily_parser
64 #define THIS\
65         ((My_lily_parser *) my_lily_parser)
66
67 #define yyerror THIS->parser_error
68
69 /* Add symbols to the TAGS field of a music object.  */
70
71 void
72 tag_music (Music *m, SCM tag, Input ip)
73 {
74         SCM tags = m->get_property ("tags");
75         if (ly_c_symbol_p (tag))
76                 tags = scm_cons (tag, tags);
77         else if (ly_c_list_p (tag))
78                 tags = ly_append2 (tag, tags);
79         else
80                 ip.warning (_ ("Tag must be symbol or list of symbols."));
81
82         m->set_property ("tags", tags);
83 }
84
85 bool
86 is_regular_identifier (SCM id)
87 {
88   String str = ly_scm2string (id);
89   char const *s = str.to_str0 ();
90
91   bool v = true;
92 #if 0
93   isalpha (*s);
94   s++;
95 #endif
96   while (*s && v)
97    {
98         v = v && isalnum (*s);
99         s++;
100    }
101   return v;
102 }
103
104 SCM
105 make_simple_markup (SCM encoding, SCM a)
106 {
107         SCM simple = ly_scheme_function ("simple-markup");
108         if (ly_c_symbol_p (encoding))
109                 return scm_list_3 (ly_scheme_function ("encoded-simple-markup"),
110                            encoding, a);
111         return scm_list_2 (simple, a);
112 }
113
114 bool
115 is_duration (int t)
116 {
117   return t && t == 1 << intlog2 (t);
118 }
119
120 void
121 set_music_properties (Music *p, SCM a)
122 {
123   for (SCM k = a; ly_c_pair_p (k); k = ly_cdr (k))
124         p->internal_set_property (ly_caar (k), ly_cdar (k));
125 }
126
127 SCM
128 make_chord_step (int step, int alter)
129 {
130         if (step == 7)
131                 alter += FLAT;
132
133         while (step < 0)
134                 step += 7;
135         Pitch m ((step -1) / 7, (step - 1) % 7, alter);
136         return m.smobbed_copy ();
137 }
138
139
140 SCM
141 make_chord (SCM pitch, SCM dur, SCM modification_list)
142 {
143         SCM chord_ctor = ly_scheme_function ("construct-chord");
144         SCM ch = scm_call_3 (chord_ctor, pitch, dur, modification_list);
145         scm_gc_protect_object (ch);
146         return ch;
147 }
148
149 /* Todo: actually also use apply iso. call too ...  */
150 bool
151 ly_input_procedure_p (SCM x)
152 {
153         return ly_c_procedure_p (x)
154                 || (ly_c_pair_p (x) && ly_c_procedure_p (ly_car (x)));
155 }
156
157 Music*
158 set_property_music (SCM sym, SCM value)
159 {
160         Music *p = MY_MAKE_MUSIC ("PropertySet");
161         p->set_property ("symbol", sym);
162         p->set_property ("value", value);
163         return p;
164 }
165
166 Music*
167 make_music_relative (Pitch start, Music *music)
168 {
169         Music *relative = MY_MAKE_MUSIC ("RelativeOctaveMusic");
170         relative->set_property ("element", music->self_scm ());
171         
172         Pitch last = music->to_relative_octave (start);
173         if (lily_1_8_relative)
174                 music->set_property ("last-pitch", last.smobbed_copy ());
175         return relative;
176 }
177
178 Music*
179 make_lyric_combine_music (SCM name, Music *music)
180 {
181         Music *combine = MY_MAKE_MUSIC ("NewLyricCombineMusic");
182         combine->set_property ("element", music->self_scm ());
183         combine->set_property ("associated-context", name);
184         return combine;
185 }
186
187 %}
188
189 /* We use SCMs to do strings, because it saves us the trouble of
190 deleting them.  Let's hope that a stack overflow doesnt trigger a move
191 of the parse stack onto the heap. */
192
193 %left NEWLYRICS
194
195 %union {
196         Book *book;
197         Book_paper_def *bookpaper;
198         Music_output_def *outputdef;
199         SCM scm;
200         String *string;
201         Music *music;
202         Score *score;
203         int i;
204 }
205 %{
206
207 int
208 yylex (YYSTYPE *s, void *v)
209 {
210         My_lily_parser *pars = (My_lily_parser*) v;
211         My_lily_lexer *lex = pars->lexer_;
212
213         lex->lexval = (void*) s;
214         lex->prepare_for_next_token ();
215         return lex->yylex ();
216 }
217
218
219 %}
220
221 %expect 3
222
223 /*
224   Three shift/reduce problems:
225
226 2. \markup identifier.
227
228 3. \markup { }
229
230 4.  \repeat
231         \repeat .. \alternative
232
233
234     \repeat { \repeat .. \alternative }
235
236 or
237
238     \repeat { \repeat } \alternative 
239
240 )
241  */
242
243
244 %pure_parser
245
246 %token ACCEPTS
247 %token ADDQUOTE
248 %token LYRICSTO
249 %token ALIAS
250 %token ALTERNATIVE
251 %token BAR
252 %token BOOK
253 %token BOOKPAPER
254 %token CHANGE
255 %token CHORDMODIFIERS
256 %token CHORDS
257 %token DOUBLE_ANGLE_OPEN
258 %token DOUBLE_ANGLE_CLOSE
259 %token CLEF
260 %token COMMANDSPANREQUEST
261 %token CONSISTS
262 %token CONSISTSEND
263 %token CONTEXT
264 %token DEFAULT
265 %token DENIES
266 %token DESCRIPTION
267 %token EXTENDER
268 %token FIGURES FIGURE_OPEN FIGURE_CLOSE
269 %token FIGURE_BRACKET_CLOSE FIGURE_BRACKET_OPEN
270 %token GROBDESCRIPTIONS
271 %token HEADER
272 %token HYPHEN
273 %token INVALID
274 %token KEY
275 %token LYRICS
276 %token LYRICS_STRING
277 %token MARK
278 %token MIDI
279 %token MULTI_MEASURE_REST
280 %token NAME
281 %token NEWCONTEXT
282 %token NOTES
283 %token OCTAVE
284 %token ONCE
285 %token OVERRIDE SET REVERT
286 %token PAPER
287 %token PARTIAL
288 %token QUOTE
289 %token RELATIVE
290 %token REMOVE
291 %token REPEAT
292 %token REST
293 %token SCM_T
294 %token SCORE
295 %token SEQUENTIAL
296 %token NEWLYRICS
297 %token SIMULTANEOUS
298 %token SKIP
299 %token SPANREQUEST
300 %token TAG
301 %token TEMPO
302 %token TIMES
303 %token TIME_T
304 %token TRANSPOSE
305 %token TRANSPOSITION
306 %token TYPE
307 %token UNSET
308 %token WITH
309
310 /* escaped */
311 /* FIXME: this sucks.  The user will get to see these names:
312     syntax error, unexpected E_CHAR:
313                 \
314                  %%
315   */
316 %token E_CHAR E_EXCLAMATION E_SMALLER E_BIGGER E_OPEN E_CLOSE
317 %token E_LEFTSQUARE E_RIGHTSQUARE E_TILDE
318 %token E_BACKSLASH
319 %token <i> E_UNSIGNED
320 %token CHORD_BASS CHORD_COLON CHORD_MINUS CHORD_CARET  CHORD_SLASH
321 %token FIGURE_SPACE
322
323 %token <i>      DIGIT
324 %token <scm>    NOTENAME_PITCH
325 %token <scm>    TONICNAME_PITCH
326 %token <scm>    CHORDMODIFIER_PITCH
327 %token <scm>    DURATION_IDENTIFIER
328 %token <scm>    FRACTION
329 %token <id>     IDENTIFIER
330 %token DRUMS
331 %token <scm>    DRUM_PITCH
332 %token <scm>    CHORD_MODIFIER
333 %token <scm>    SCORE_IDENTIFIER
334 %token <scm>    MUSIC_OUTPUT_DEF_IDENTIFIER
335 %token <scm>    NUMBER_IDENTIFIER
336 %token <scm>    EVENT_IDENTIFIER
337 %token <scm>    MUSIC_IDENTIFIER CONTEXT_DEF_IDENTIFIER
338 %token <scm>    STRING_IDENTIFIER SCM_IDENTIFIER
339 %token <scm>    RESTNAME
340 %token <scm>    STRING
341 %token <scm>    SCM_T
342 %token <i>      UNSIGNED
343 %token <scm>   REAL
344
345 %token MARKUP
346 %token <scm> MARKUP_HEAD_MARKUP0
347 %token <scm> MARKUP_HEAD_EMPTY
348 %token <scm> MARKUP_HEAD_MARKUP0_MARKUP1
349 %token <scm> MARKUP_HEAD_SCM0
350 %token <scm> MARKUP_HEAD_SCM0_MARKUP1
351 %token <scm> MARKUP_HEAD_SCM0_SCM1
352 %token <scm> MARKUP_HEAD_SCM0_SCM1_SCM2
353 %token <scm> MARKUP_HEAD_SCM0_SCM1_MARKUP2
354
355 %token <scm> MUSIC_FUNCTION
356 %token <scm> MUSIC_FUNCTION_SCM 
357 %token <scm> MUSIC_FUNCTION_MUSIC 
358 %token <scm> MUSIC_FUNCTION_SCM_MUSIC 
359 %token <scm> MUSIC_FUNCTION_MUSIC_MUSIC 
360 %token <scm> MUSIC_FUNCTION_SCM_SCM_MUSIC 
361 %token <scm> MUSIC_FUNCTION_SCM_MUSIC_MUSIC 
362
363 %token <scm> MARKUP_IDENTIFIER MARKUP_HEAD_LIST0
364 %type <scm> markup markup_line markup_list  markup_list_body full_markup
365
366 %type <book>    book_block book_body
367 %type <bookpaper> book_paper_head book_paper_block book_paper_body
368
369 %type <i>       exclamations questions dots optional_rest
370 %type <i>       bass_mod
371 %type <scm>     oct_check
372 %type <scm>     context_mod_list
373 %type <scm>     lyric_element
374 %type <scm>     bass_number br_bass_figure bass_figure figure_list figure_spec
375 %type <scm>     new_lyrics
376 %type <outputdef> output_def
377 %type <scm>     lilypond_header lilypond_header_body
378 %type <music>   open_event close_event
379 %type <i>       sub_quotes sup_quotes
380 %type <music>   toplevel_music
381 %type <music>   simple_element event_chord command_element
382 %type <music>   Composite_music Simple_music Prefix_composite_music Generic_prefix_music
383 %type <scm>     Generic_prefix_music_scm 
384 %type <music>   Grouped_music_list
385 %type <music>   Repeated_music
386 %type <scm>     Alternative_music
387 %type <i>       tremolo_type
388 %type <i>       bare_int  bare_unsigned
389 %type <i>       script_dir
390 %type <scm>     identifier_init
391 %type <scm>     simple_string
392
393 %type <music>   note_chord_element chord_body chord_body_element
394 %type <scm>     chord_body_elements
395 %type <scm>     steno_duration optional_notemode_duration multiplied_duration
396         
397 %type <scm>     post_events
398 %type <music>   gen_text_def direction_less_event direction_reqd_event
399 %type <scm>     steno_pitch pitch absolute_pitch pitch_also_in_chords
400 %type <scm>     steno_tonic_pitch
401 %type <scm>     duration_length fraction
402
403 %type <scm>     chord_item chord_items chord_separator new_chord
404 %type <scm>     step_number step_numbers 
405
406 %type <scm>     embedded_scm scalar
407 %type <music>   Music Sequential_music Simultaneous_music
408 %type <music>   relative_music re_rhythmed_music
409 %type <music>   music_property_def context_change
410 %type <scm>     context_prop_spec
411 %type <scm>     Music_list
412 %type <scm>     property_operation
413 %type <scm>     context_mod context_def_mod optional_context_mod
414 %type <outputdef>       music_output_def_body music_output_def_head
415 %type <music>   post_event tagged_post_event
416 %type <music>   command_req
417 %type <music>   string_number_event
418 %type <scm>     string bare_number number_expression number_term number_factor
419 %type <score>   score_block score_body
420
421 %type <scm>     context_def_spec_block context_def_spec_body
422 %type <music>   tempo_event
423 %type <scm>     script_abbreviation
424
425
426
427 %left '-' '+'
428
429 /* We don't assign precedence to / and *, because we might need varied
430 prec levels in different prods */
431
432 %left UNARY_MINUS
433
434 %%
435
436 lilypond:       /* empty */
437         | lilypond toplevel_expression {
438         }
439         | lilypond assignment {
440         }
441         | lilypond error {
442                 THIS->error_level_ = 1;
443         }
444         | lilypond INVALID      {
445                 THIS->error_level_ = 1;
446         }
447         ;
448
449 toplevel_expression:
450         lilypond_header {
451                 THIS->header_ = $1;
452         }
453         | add_quote {
454         
455         }
456         | book_block {
457                 Book *book = $1;
458                 SCM proc = THIS->lexer_->lookup_identifier ("toplevel-book-handler");
459                 scm_call_2 (proc, THIS->self_scm (), book->self_scm ());
460                 scm_gc_unprotect_object (book->self_scm ());
461         }
462         | score_block {
463                 Score *score = $1;
464                 
465                 SCM proc = THIS->lexer_->lookup_identifier ("toplevel-score-handler");
466                 scm_call_2 (proc, THIS->self_scm (), score->self_scm ());
467                 scm_gc_unprotect_object (score->self_scm ());
468         }
469         | toplevel_music {
470                 Music *music = $1;
471                 SCM proc = THIS->lexer_->lookup_identifier ("toplevel-music-handler");
472                 scm_call_2 (proc, THIS->self_scm (), music->self_scm ());
473                 scm_gc_unprotect_object (music->self_scm ());
474         }
475         | output_def {
476                 SCM id = SCM_EOL;
477                 if (dynamic_cast<Paper_def*> ($1))
478                         id = scm_makfrom0str ("$defaultpaper");
479                 else if (dynamic_cast<Midi_def*> ($1))
480                         id = scm_makfrom0str ("$defaultmidi");
481                 THIS->lexer_->set_identifier (id, $1->self_scm ());
482                 scm_gc_unprotect_object ($1->self_scm ());
483         }
484         | book_paper_block {
485                 THIS->lexer_->set_identifier (scm_makfrom0str ("$defaultbookpaper"), $1->self_scm ());
486                 scm_gc_unprotect_object ($1->self_scm ());
487         }
488         ;
489
490 toplevel_music:
491         Composite_music {
492         }
493         ;
494
495 embedded_scm:
496         SCM_T
497         | SCM_IDENTIFIER
498         ;
499
500
501 lilypond_header_body:
502         {
503                 $$ = ly_make_anonymous_module (safe_global_b);
504                 THIS->lexer_->add_scope ($$);
505         }
506         | lilypond_header_body assignment  {
507                 
508         }
509         ;
510
511 lilypond_header:
512         HEADER '{' lilypond_header_body '}'     {
513                 $$ = THIS->lexer_->remove_scope ();
514         }
515         ;
516
517
518 /*
519         DECLARATIONS
520 */
521 assignment:
522         STRING {
523                 THIS->push_spot ();
524         }
525         /* cont */ '=' identifier_init  {
526
527         /*
528                 Should find generic way of associating input with objects.
529         */
530                 Input ip = THIS->pop_spot ();
531
532                 if (! is_regular_identifier ($1))
533                 {
534                         ip.warning (_ ("Identifier should have alphabetic characters only"));
535                 }
536
537                 THIS->lexer_->set_identifier ($1, $4);
538
539 /*
540  TODO: devise standard for protection in parser.
541
542   The parser stack lives on the C-stack, which means that
543 all objects can be unprotected as soon as they're here.
544
545 */
546         }
547         | embedded_scm { }
548         ;
549
550
551 identifier_init:
552         score_block {
553                 $$ = $1->self_scm ();
554                 scm_gc_unprotect_object ($$);
555         }
556         | full_markup {
557                 $$ = $1;
558         }
559         | output_def {
560                 $$ = $1->self_scm ();
561                 scm_gc_unprotect_object ($$);
562         }
563         | context_def_spec_block {
564                 $$ = $1;
565         }
566         | Music  {
567                 $$ = $1->self_scm ();
568                 scm_gc_unprotect_object ($$);
569         }
570         | post_event {
571                 $$ = $1->self_scm ();
572                 scm_gc_unprotect_object ($$);
573         }
574         | number_expression {
575                 $$ = $1;
576         }
577         | string {
578                 $$ = $1;
579         }
580         | embedded_scm  {
581                 $$ = $1;
582         }
583         ;
584
585 context_def_spec_block:
586         CONTEXT '{' context_def_spec_body '}'
587                 {
588                 $$ = $3;
589         }
590         ;
591
592 context_def_spec_body:
593         /**/ {
594                 $$ = Context_def::make_scm ();
595                 unsmob_context_def ($$)->set_spot (THIS->here_input ());
596         }
597         | CONTEXT_DEF_IDENTIFIER {
598                 $$ = $1;
599                 unsmob_context_def ($$)->set_spot (THIS->here_input ());
600         }
601         | context_def_spec_body GROBDESCRIPTIONS embedded_scm {
602                 Context_def*td = unsmob_context_def ($$);
603
604                 for (SCM p = $3; ly_c_pair_p (p); p = ly_cdr (p)) {
605                         SCM tag = ly_caar (p);
606
607                         /* TODO: should make new tag "grob-definition" ? */
608                         td->add_context_mod (scm_list_3 (ly_symbol2scm ("assign"),
609                                                         tag, scm_cons (ly_cdar (p), SCM_EOL)));
610                 }
611         }
612         | context_def_spec_body context_mod {
613                 unsmob_context_def ($$)->add_context_mod ($2);          
614         }
615         ;
616
617
618 book_paper_block:
619         book_paper_body '}' {
620                 $$ = $1;
621         }
622         ;
623 book_paper_head:
624         BOOKPAPER '{' {
625                 $$ = unsmob_book_paper_def (THIS->lexer_->lookup_identifier ("$defaultbookpaper"))->clone ();
626                 THIS->lexer_->add_scope ($$->scope_);
627         }
628         ;
629
630 book_paper_body:
631         book_paper_head
632         | book_paper_body assignment { }
633         ;
634         
635
636
637 book_block:
638         BOOK {
639                 THIS->push_spot ();
640         }
641         /*cont*/ '{' book_body '}'      {
642                 THIS->pop_spot ();
643                 $$ = $4;
644         }
645         ;
646
647 /* FIXME:
648    * Use 'handlers' like for toplevel-* stuff?
649    * grok \paper and \midi?  */
650 book_body:
651         {
652                 $$ = new Book;
653                 $$->set_spot (THIS->here_input ());
654                 $$->bookpaper_ = unsmob_book_paper_def (THIS->lexer_->lookup_identifier ("$defaultbookpaper"))->clone ();
655                 scm_gc_unprotect_object ($$->bookpaper_->self_scm ());
656         }
657         | book_body book_paper_block {
658                 $$->bookpaper_ = $2;
659                 scm_gc_unprotect_object ($2->self_scm ());
660         }
661         | book_body score_block {
662                 Score *score = $2;
663                 $$->scores_.push (score);
664                 scm_gc_unprotect_object (score->self_scm ());
665         }
666         | book_body Composite_music {
667                 Music *music = $2;
668                 Score *score
669                         = unsmob_score (ly_music_scorify (music->self_scm ()));
670                 $$->scores_.push (score);
671                 scm_gc_unprotect_object (music->self_scm ());
672         }
673         | lilypond_header {
674                 THIS->header_ = $1;
675         }
676         | book_body error {
677
678         }
679         ;
680
681 score_block:
682         SCORE {
683                 THIS->push_spot ();
684         }
685         /*cont*/ '{' score_body '}'     {
686                 THIS->pop_spot ();
687                 $$ = $4;
688         }
689         ;
690
691 score_body:
692         Music   {
693                 $$ = new Score;
694         
695                 $$->set_spot (THIS->here_input ());
696                 SCM m = $1->self_scm ();
697                 scm_gc_unprotect_object (m);
698
699                 /*
700                         guh.
701                 */
702                 SCM check_funcs = ly_scheme_function ("toplevel-music-functions");
703                 for (; ly_c_pair_p (check_funcs); check_funcs = ly_cdr (check_funcs))
704                         m = scm_call_1 (ly_car (check_funcs), m);
705                 $$->music_ = m;
706
707         }
708         | SCORE_IDENTIFIER {
709                 $$ = new Score ( *unsmob_score ($1));
710                 $$->set_spot (THIS->here_input ());
711         }
712         | score_body lilypond_header    {
713                 $$->header_ = $2;
714         }
715         | score_body output_def {
716                 $$->defs_.push ($2);
717                 scm_gc_unprotect_object ($2->self_scm ());
718         }
719         | score_body error {
720
721         }
722         ;
723
724
725 /*
726         MIDI
727 */
728 output_def:
729         music_output_def_body '}' {
730                 $$ = $1;
731                 THIS->lexer_->remove_scope ();
732                 THIS->lexer_->pop_state ();
733         }
734         ;
735
736 music_output_def_head:
737         MIDI    {
738                 Music_output_def *id = unsmob_music_output_def (THIS->lexer_->lookup_identifier ("$defaultmidi"));
739
740
741                 Midi_def *p = 0;
742                 if (id)
743                         p = dynamic_cast<Midi_def*> (id->clone ());
744                 else
745                         p = new Midi_def;
746
747                 $$ = p;
748                 THIS->lexer_->add_scope (p->scope_);
749         }
750         | PAPER         {
751                 Music_output_def *id = unsmob_music_output_def (THIS->lexer_->lookup_identifier ("$defaultpaper"));
752                   Paper_def *p = 0;
753                 if (id)
754                         p = dynamic_cast<Paper_def*> (id->clone ());
755                 else
756                         p = new Paper_def;
757
758                 THIS->lexer_->add_scope (p->scope_);
759                 $$ = p;
760         }
761         ;
762
763
764 music_output_def_body:
765         music_output_def_head '{' {
766                 $$ = $1;
767                 $$->input_origin_. set_spot (THIS->here_input ());
768                 THIS->lexer_->push_initial_state ();
769         }
770         | music_output_def_head '{' MUSIC_OUTPUT_DEF_IDENTIFIER         {
771                 scm_gc_unprotect_object ($1->self_scm ());
772                 Music_output_def *o = unsmob_music_output_def ($3);
773                 o->input_origin_.set_spot (THIS->here_input ());
774                 $$ = o;
775                 THIS->lexer_->remove_scope ();
776                 THIS->lexer_->add_scope (o->scope_);
777                 THIS->lexer_->push_initial_state ();
778         }
779         | music_output_def_body assignment  {
780
781         }
782         | music_output_def_body context_def_spec_block  {
783                 $$->assign_context_def ($2);
784         }
785         | music_output_def_body tempo_event  {
786                 /*
787                         junk this ? there already is tempo stuff in
788                         music.
789                 */
790                 int m = ly_scm2int ( $2->get_property ("metronome-count"));
791                 Duration *d = unsmob_duration ($2->get_property ("tempo-unit"));
792                 Midi_def *md = dynamic_cast<Midi_def*> ($$);
793                 if (md)
794                         md->set_tempo (d->get_length (), m);
795                 scm_gc_unprotect_object ($2->self_scm ());
796         }
797         | music_output_def_body error {
798
799         }
800         ;
801
802 tempo_event:
803         TEMPO steno_duration '=' bare_unsigned  {
804                 $$ = MY_MAKE_MUSIC ("MetronomeChangeEvent");
805                 $$->set_property ("tempo-unit", $2);
806                 $$->set_property ("metronome-count", scm_int2num ( $4));
807         }
808         ;
809
810 /*
811 The representation of a  list is the
812
813   (LIST . LAST-CONS)
814
815  to have  efficient append.
816 */
817 Music_list:
818         /* empty */ {
819                 $$ = scm_cons (SCM_EOL, SCM_EOL);
820         }
821         | Music_list Music {
822                 SCM s = $$;
823                 SCM c = scm_cons ($2->self_scm (), SCM_EOL);
824                 scm_gc_unprotect_object ($2->self_scm ()); /* UGH */
825                 if (ly_c_pair_p (ly_cdr (s)))
826                         scm_set_cdr_x (ly_cdr (s), c); /* append */
827                 else
828                         scm_set_car_x (s, c); /* set first cons */
829                 scm_set_cdr_x (s, c);  /* remember last cell */
830         }
831         | Music_list embedded_scm {
832         }
833         | Music_list error {
834         }
835         ;
836
837
838 Music:
839         Simple_music
840         | Composite_music
841         ;
842
843 Alternative_music:
844         /* empty */ {
845                 $$ = SCM_EOL;
846         }
847         | ALTERNATIVE '{' Music_list '}' {
848                 $$ = $3;
849         }
850         ;
851
852
853 Repeated_music:
854         REPEAT simple_string bare_unsigned Music Alternative_music
855         {
856                 Music *beg = $4;
857                 int times = $3;
858                 SCM alts = ly_c_pair_p ($5) ? ly_car ($5) : SCM_EOL;
859                 if (times < scm_ilength (alts)) {
860                   unsmob_music (ly_car (alts))
861                     ->origin ()->warning (
862                     _ ("More alternatives than repeats.  Junking excess alternatives."));
863                   alts = ly_truncate_list (times, alts);
864                 }
865
866
867                 SCM proc = ly_scheme_function ("make-repeated-music");
868
869                 SCM mus = scm_call_1 (proc, $2);
870                 scm_gc_protect_object (mus); // UGH.
871                 Music *r = unsmob_music (mus);
872                 if (beg)
873                         {
874                         r-> set_property ("element", beg->self_scm ());
875                         scm_gc_unprotect_object (beg->self_scm ());
876                         }
877                 r->set_property ("repeat-count", scm_int2num (times >? 1));
878
879                 r-> set_property ("elements",alts);
880                 if (ly_c_equal_p ($2, scm_makfrom0str ("tremolo"))) {
881                         /*
882                         TODO: move this code to Scheme.
883                         */
884
885                         /*
886                         we can not get durations and other stuff correct down the line, so we have to
887                         add to the duration log here.
888                         */
889                         SCM func = ly_scheme_function ("shift-duration-log");
890
891                         int dots = ($3 % 3) ? 0 : 1;
892                         int shift = -intlog2 ((dots) ? ($3*2/3) : $3);
893
894                         Sequential_music *seq = dynamic_cast<Sequential_music*> ($4);
895                         
896                         if (seq) {
897                                 int list_len = scm_ilength (seq->music_list ());
898                                 if (list_len != 2)
899                                         seq->origin ()->warning ("Chord tremolo must have 2 elements.");
900                                 shift -= 1;
901                                 r->compress (Moment (Rational (1, list_len)));
902                                 }
903                         scm_call_3 (func, r->self_scm (), scm_int2num (shift),scm_int2num (dots));
904
905                 }
906                 r->set_spot (*$4->origin ());
907
908                 $$ = r;
909         }
910         ;
911
912 Sequential_music:
913         SEQUENTIAL '{' Music_list '}'           {
914                 $$ = MY_MAKE_MUSIC ("SequentialMusic");
915                 $$->set_property ("elements", ly_car ($3));
916                 $$->set_spot (THIS->here_input ());
917         }
918         | '{' Music_list '}'            {
919                 $$ = MY_MAKE_MUSIC ("SequentialMusic");
920                 $$->set_property ("elements", ly_car ($2));
921                 $$->set_spot (THIS->here_input ());
922         }
923         ;
924
925 Simultaneous_music:
926         SIMULTANEOUS '{' Music_list '}'{
927                 $$ = MY_MAKE_MUSIC ("SimultaneousMusic");
928                 $$->set_property ("elements", ly_car ($3));
929                 $$->set_spot (THIS->here_input ());
930
931         }
932         | simul_open Music_list simul_close     {
933                 $$ = MY_MAKE_MUSIC ("SimultaneousMusic");
934                 $$->set_property ("elements", ly_car ($2));
935                 $$->set_spot (THIS->here_input ());
936         }
937         ;
938
939 Simple_music:
940         event_chord             { $$ = $1; }
941         | MUSIC_IDENTIFIER {
942                 $$ = unsmob_music ($1);
943         }
944         | music_property_def
945         | context_change
946         ;
947
948
949 optional_context_mod:
950         /**/ { $$ = SCM_EOL; }
951         | WITH { THIS->lexer_->push_initial_state (); }
952         '{' context_mod_list '}'
953         {
954                 THIS->lexer_->pop_state ();
955                 $$ = $4;
956         }
957         ;
958
959 context_mod_list:
960         /* */  { $$ = SCM_EOL; }
961         | context_mod_list context_mod  {
962                  $$ = scm_cons ($2, $1);
963         }
964         ;
965
966
967 Composite_music:
968         Prefix_composite_music { $$ = $1; }
969         | Grouped_music_list { $$ = $1; }
970         ;
971
972 Grouped_music_list:
973         Simultaneous_music              { $$ = $1; }
974         | Sequential_music              { $$ = $1; }
975         ;
976
977 Generic_prefix_music_scm:
978         MUSIC_FUNCTION {
979                 $$ = scm_list_2 ($1, make_input (THIS->here_input ()));
980         }
981         | MUSIC_FUNCTION_SCM {
982                 THIS->push_spot ();
983         } embedded_scm {
984                 $$ = scm_list_3 ($1, make_input (THIS->pop_spot ()), $3);
985         }
986         | MUSIC_FUNCTION_MUSIC {
987                 THIS->push_spot (); 
988         } Music {
989                 $$ = scm_list_3 ($1, make_input (THIS->pop_spot ()), $3->self_scm ());
990                 scm_gc_unprotect_object ($3->self_scm ());
991         }
992         | MUSIC_FUNCTION_SCM_MUSIC {
993                 THIS->push_spot (); 
994         }  embedded_scm Music {
995                 $$ = scm_list_4 ($1, make_input (THIS->pop_spot ()), $3, $4->self_scm ());
996                 scm_gc_unprotect_object ($4->self_scm ());
997         }
998         | MUSIC_FUNCTION_MUSIC_MUSIC {
999                 THIS->push_spot (); 
1000         }  Music  Music {
1001                 $$ = scm_list_4 ($1, make_input (THIS->pop_spot ()), $3->self_scm (), $4->self_scm ());
1002                 scm_gc_unprotect_object ($3->self_scm ());
1003                 scm_gc_unprotect_object ($4->self_scm ());
1004         }
1005         | MUSIC_FUNCTION_SCM_MUSIC_MUSIC {
1006                 THIS->push_spot (); 
1007         } embedded_scm Music Music {
1008                 $$ = scm_list_5 ($1, make_input (THIS->pop_spot ()),
1009                         $3, $4->self_scm (), $5->self_scm ());
1010                 scm_gc_unprotect_object ($5->self_scm ());
1011                 scm_gc_unprotect_object ($4->self_scm ());
1012         }
1013         ;
1014
1015 Generic_prefix_music:
1016         Generic_prefix_music_scm {
1017                 SCM func = ly_car ($1);
1018                 Input *loc = unsmob_input (ly_cadr ($1));
1019                 SCM args = ly_cddr ($1);
1020                 SCM sig = scm_object_property (func, ly_symbol2scm ("music-function-signature"));
1021                 int k = 0;
1022                 bool ok  = true; 
1023                 for (SCM s = sig, t = args;
1024                         ok && ly_c_pair_p (s) && ly_c_pair_p (t);
1025                         s = ly_cdr (s), t = ly_cdr (t)) {
1026                         k++;
1027                         if (scm_call_1 (ly_car (s), ly_car (t)) != SCM_BOOL_T)
1028                         {
1029                                 loc->error (_f ("Argument %d failed typecheck", k));
1030                                 THIS->error_level_ = 1;
1031                                 ok = false;
1032                         }
1033                 }
1034                 SCM m = SCM_EOL;
1035                 if (ok)
1036                         m = scm_apply_0 (func, ly_cdr ($1));
1037                 if (unsmob_music (m))
1038                         {
1039                         $$ = unsmob_music (m);
1040                         scm_gc_protect_object (m);
1041                         }
1042                 else 
1043                         {
1044                         if (ok)
1045                                 loc->error (_ ("Music head function should return Music object.")); 
1046                         $$ = MY_MAKE_MUSIC ("Music");
1047                         }
1048                 $$->set_spot (*loc);
1049         }
1050         ;
1051
1052
1053 Prefix_composite_music:
1054         Generic_prefix_music {
1055                 $$ = $1;
1056         }
1057         | CONTEXT simple_string '=' simple_string optional_context_mod Music {
1058                 $$ = context_spec_music ($2, $4, $6, $5);
1059
1060         }
1061         | CONTEXT simple_string optional_context_mod Music {
1062                 $$ = context_spec_music ($2, SCM_UNDEFINED, $4, $3);
1063         }
1064         | NEWCONTEXT simple_string optional_context_mod Music {
1065                 $$ = context_spec_music ($2, get_next_unique_context (), $4,
1066                         $3);
1067         }
1068
1069         | TIMES {
1070                 THIS->push_spot ();
1071         }
1072         /* CONTINUED */
1073                 fraction Music  
1074
1075         {
1076                 int n = ly_scm2int (ly_car ($3)); int d = ly_scm2int (ly_cdr ($3));
1077                 Music *mp = $4;
1078
1079                 $$= MY_MAKE_MUSIC ("TimeScaledMusic");
1080                 $$->set_spot (THIS->pop_spot ());
1081
1082                 $$->set_property ("element", mp->self_scm ());
1083                 scm_gc_unprotect_object (mp->self_scm ());
1084                 $$->set_property ("numerator", scm_int2num (n));
1085                 $$->set_property ("denominator", scm_int2num (d));
1086                 $$->compress (Moment (Rational (n,d)));
1087
1088         }
1089         | Repeated_music                { $$ = $1; }
1090         | TRANSPOSE pitch_also_in_chords pitch_also_in_chords Music {
1091                 $$ = MY_MAKE_MUSIC ("TransposedMusic");
1092                 Music *p = $4;
1093                 Pitch from = *unsmob_pitch ($2);
1094                 Pitch to = *unsmob_pitch ($3);
1095
1096                 p->transpose (interval (from, to));
1097                 $$->set_property ("element", p->self_scm ());
1098                 scm_gc_unprotect_object (p->self_scm ());
1099         }
1100         | NOTES
1101                 {
1102                 SCM nn = THIS->lexer_->lookup_identifier ("pitchnames");
1103                 THIS->lexer_->push_note_state (alist_to_hashq (nn));
1104         }
1105         Music
1106                 { $$ = $3;
1107                   THIS->lexer_->pop_state ();
1108                 }
1109         | DRUMS
1110                 {
1111                 SCM nn = THIS->lexer_->lookup_identifier ("drumPitchNames");
1112                 THIS->lexer_->push_note_state (alist_to_hashq (nn));
1113         }
1114         /* FIXME: This used to be: */
1115         Music
1116 /*      Grouped_music_list */
1117                 { $$ = $3;
1118                   THIS->lexer_->pop_state ();
1119                 }
1120         | FIGURES
1121                 { THIS->lexer_->push_figuredbass_state (); }
1122         /* FIXME: This used to be:
1123         Music
1124         but that breaks web build
1125         */
1126         Grouped_music_list
1127                 {
1128                   Music *chm = MY_MAKE_MUSIC ("UntransposableMusic");
1129                   chm->set_property ("element", $3->self_scm ());
1130                   $$ = chm;
1131                   scm_gc_unprotect_object ($3->self_scm ());
1132
1133                   THIS->lexer_->pop_state ();
1134         }
1135         | CHORDS {
1136                 SCM nn = THIS->lexer_->lookup_identifier ("chordmodifiers");
1137                 THIS->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
1138                 nn = THIS->lexer_->lookup_identifier ("pitchnames");
1139                 THIS->lexer_->push_chord_state (alist_to_hashq (nn));
1140
1141         }
1142         /* FIXME:
1143         Music
1144 */
1145         Grouped_music_list
1146         {
1147                   Music *chm = MY_MAKE_MUSIC ("UnrelativableMusic");
1148                   chm->set_property ("element", $3->self_scm ());
1149                   scm_gc_unprotect_object ($3->self_scm ());
1150                   $$ = chm;
1151
1152                   THIS->lexer_->pop_state ();
1153         }
1154         | LYRICS
1155                 { THIS->lexer_->push_lyric_state (); }
1156         /* FIXME:
1157         Music
1158 */
1159         Grouped_music_list
1160                 {
1161                   $$ = $3;
1162                   THIS->lexer_->pop_state ();
1163         }
1164         | relative_music        { $$ = $1; }
1165         | re_rhythmed_music     { $$ = $1; }
1166         | TAG embedded_scm Music {
1167                 tag_music ($3, $2, THIS->here_input ());
1168                 $$ = $3;
1169         }
1170         ;
1171
1172 relative_music:
1173         RELATIVE absolute_pitch Music {
1174                 Music *m = $3;
1175                 Pitch start = *unsmob_pitch ($2);
1176                 $$ = make_music_relative (start, m);
1177                 scm_gc_unprotect_object (m->self_scm ());
1178         }
1179         | RELATIVE Composite_music {
1180                 Music *m = $2;
1181                 /* FIXME: why is octave==0 and default not middleC? */
1182                 Pitch middle_c (-1, 0, 0);
1183                 $$ = make_music_relative (middle_c, m);
1184                 scm_gc_unprotect_object (m->self_scm ());
1185         }
1186         ;
1187
1188 new_lyrics:
1189         NEWLYRICS { THIS->lexer_->push_lyric_state (); }
1190         /*cont */
1191         Grouped_music_list {
1192         /* Can also use Music at the expensive of two S/Rs similar to
1193            \repeat \alternative */
1194                 THIS->lexer_->pop_state ();
1195 #if 0
1196                 Music *music = MY_MAKE_MUSIC ("SimultaneousMusic");
1197                 music->set_property ("elements", scm_list_1 ($3->self_scm ()));
1198                 $$ = music;
1199 #else
1200                 $$ = scm_cons ($3->self_scm (), SCM_EOL);
1201 #endif
1202         }
1203         | new_lyrics NEWLYRICS { THIS->lexer_->push_lyric_state (); }
1204         Grouped_music_list {
1205                 THIS->lexer_->pop_state ();
1206                 $$ = scm_cons ($4->self_scm (), $1);
1207         }
1208         ;
1209
1210 re_rhythmed_music:
1211         Grouped_music_list new_lyrics {
1212
1213                 /* FIXME: should find out uniqueXXX name from music */
1214                 SCM name = $1->get_property ("context-id");
1215                 //if (name == SCM_EOL)
1216                 if (!ly_c_string_p (name))
1217                         name = scm_makfrom0str ("");
1218
1219                 SCM context = scm_makfrom0str ("Lyrics");
1220                 Music *all = MY_MAKE_MUSIC ("SimultaneousMusic");
1221
1222                 SCM lst = SCM_EOL;
1223                 for (SCM s = $2; ly_c_pair_p (s); s = ly_cdr (s))
1224                 {
1225                         Music *music = unsmob_music (ly_car (s));
1226                         Music *com = make_lyric_combine_music (name, music);
1227                         Music *csm = context_spec_music (context,
1228                                 get_next_unique_context (), com, SCM_EOL);
1229                         lst = scm_cons (csm->self_scm (), lst);
1230                 }
1231                 /* FIXME: only first lyric music is accepted,
1232                           the rest is junked */
1233                 all->set_property ("elements", scm_cons ($1->self_scm (),
1234                         lst));
1235                 $$ = all;
1236                 scm_gc_unprotect_object ($1->self_scm ());
1237         }
1238         | LYRICSTO string Music {
1239                 Music *music = $3;
1240                 SCM name = $2;
1241                 $$ = make_lyric_combine_music (name, music);
1242                 scm_gc_unprotect_object (music->self_scm ());
1243         }
1244         ;
1245
1246 context_change:
1247         CHANGE STRING '=' STRING  {
1248                 Music*t= MY_MAKE_MUSIC ("ContextChange");
1249                 t-> set_property ("change-to-type", scm_string_to_symbol ($2));
1250                 t-> set_property ("change-to-id", $4);
1251
1252                 $$ = t;
1253                 $$->set_spot (THIS->here_input ());
1254         }
1255         ;
1256
1257 property_operation:
1258         STRING '=' scalar {
1259                 $$ = scm_list_3 (ly_symbol2scm ("assign"),
1260                         scm_string_to_symbol ($1), $3);
1261         }
1262         | UNSET simple_string {
1263                 $$ = scm_list_2 (ly_symbol2scm ("unset"),
1264                         scm_string_to_symbol ($2));
1265         }
1266         | OVERRIDE simple_string embedded_scm '=' embedded_scm {
1267                 $$ = scm_list_4 (ly_symbol2scm ("push"),
1268                         scm_string_to_symbol ($2), $3, $5);
1269         }
1270         | REVERT simple_string embedded_scm {
1271                 $$ = scm_list_3 (ly_symbol2scm ("pop"),
1272                         scm_string_to_symbol ($2), $3);
1273         }
1274         ;
1275
1276 context_def_mod:
1277         CONSISTSEND { $$ = ly_symbol2scm ("consists-end"); }
1278         | CONSISTS { $$ = ly_symbol2scm ("consists"); }
1279         | REMOVE { $$ = ly_symbol2scm ("remove"); }
1280
1281         | ACCEPTS { $$ = ly_symbol2scm ("accepts"); }
1282         | DENIES { $$ = ly_symbol2scm ("denies"); }
1283
1284         | ALIAS { $$ = ly_symbol2scm ("alias"); }
1285         | TYPE { $$ = ly_symbol2scm ("translator-type"); }
1286         | DESCRIPTION { $$ = ly_symbol2scm ("description"); }
1287         | NAME { $$ = ly_symbol2scm ("context-name"); }
1288         ;
1289
1290 context_mod:
1291         property_operation { $$ = $1; }
1292         | context_def_mod STRING {
1293                 $$ = scm_list_2 ($1, $2);
1294         }
1295         ;
1296
1297 context_prop_spec:
1298         simple_string {
1299                 $$ = scm_list_2 (ly_symbol2scm ("Bottom"),
1300                         scm_string_to_symbol ($1));
1301         }
1302         | simple_string '.' simple_string {
1303                 $$ = scm_list_2 (scm_string_to_symbol ($1),
1304                         scm_string_to_symbol ($3));
1305         }
1306         ;
1307
1308 music_property_def:
1309         OVERRIDE context_prop_spec embedded_scm '=' scalar {
1310                 $$ = property_op_to_music (scm_list_4 (
1311                         ly_symbol2scm ("poppush"),
1312                         ly_cadr ($2),
1313                         $3, $5));
1314                 $$= context_spec_music (ly_car ($2), SCM_UNDEFINED, $$, SCM_EOL);
1315         }
1316         | REVERT context_prop_spec embedded_scm {
1317                 $$ = property_op_to_music (scm_list_3 (
1318                         ly_symbol2scm ("pop"),
1319                         ly_cadr ($2),
1320                         $3));
1321
1322                 $$= context_spec_music (ly_car ($2), SCM_UNDEFINED, $$, SCM_EOL);
1323         }
1324         | SET context_prop_spec '=' scalar {
1325                 $$ = property_op_to_music (scm_list_3 (
1326                         ly_symbol2scm ("assign"),
1327                         ly_cadr ($2),
1328                         $4));
1329                 $$= context_spec_music (ly_car ($2), SCM_UNDEFINED, $$, SCM_EOL);
1330         }
1331         | UNSET context_prop_spec {
1332                 $$ = property_op_to_music (scm_list_2 (
1333                         ly_symbol2scm ("unset"),
1334                         ly_cadr ($2)));
1335                 $$= context_spec_music (ly_car ($2), SCM_UNDEFINED, $$, SCM_EOL);
1336         }
1337         | ONCE music_property_def {
1338                 SCM e = $2->get_property ("element");
1339                 unsmob_music (e)->set_property ("once", SCM_BOOL_T);
1340                 $$ = $2;
1341
1342         }
1343         ;
1344
1345
1346 string:
1347         STRING {
1348                 $$ = $1;
1349         }
1350         | STRING_IDENTIFIER {
1351                 $$ = $1;
1352         }
1353         | string '+' string {
1354                 $$ = scm_string_append (scm_list_2 ($1, $3));
1355         }
1356         ;
1357
1358 simple_string: STRING {
1359         }
1360         | LYRICS_STRING {
1361         }
1362         ;
1363
1364 scalar: string {
1365         }
1366         | LYRICS_STRING {
1367         }
1368         | bare_int {
1369                 $$ = scm_int2num ($1);
1370         }
1371         | embedded_scm {
1372         }
1373         | full_markup {
1374         }
1375         | DIGIT {
1376                 $$ = scm_int2num ($1);
1377         }
1378         ;
1379
1380 /*
1381 FIXME: remove or fix this comment.  What is `This'?
1382
1383 This is a trick:
1384
1385 Adding pre_events to the simple_element
1386 makes the choice between
1387
1388   string:  STRING
1389
1390 and
1391
1392   simple_element: STRING
1393
1394 a single shift/reduction conflict.
1395
1396 nevertheless, this is not very clean, and we should find a different
1397 solution.
1398
1399 */
1400 pre_events: {
1401                 THIS->push_spot ();
1402         }
1403         ;
1404
1405 event_chord:
1406         pre_events simple_element post_events   {
1407                 SCM elts = $2-> get_property ("elements");
1408
1409                 elts = ly_append2 (elts, scm_reverse_x ($3, SCM_EOL));
1410
1411                 $2->set_property ("elements", elts);
1412                 $$ = $2;
1413         }
1414         | command_element
1415         | note_chord_element
1416         ;
1417
1418
1419 note_chord_element:
1420         chord_body optional_notemode_duration post_events
1421         {
1422                 SCM dur = unsmob_duration ($2)->smobbed_copy ();
1423                 SCM es = $1->get_property ("elements");
1424                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
1425
1426                 for (SCM s = es; ly_c_pair_p (s); s = ly_cdr (s))
1427                   unsmob_music (ly_car (s))->set_property ("duration", dur);
1428                 es = ly_append2 (es, postevs);
1429
1430                 $1-> set_property ("elements", es);
1431                 $$ = $1;
1432         }
1433         ;
1434
1435 chord_open: '<'
1436         ;
1437
1438 chord_close: '>'
1439         ;
1440
1441 simul_open: DOUBLE_ANGLE_OPEN
1442         ;
1443
1444 simul_close: DOUBLE_ANGLE_CLOSE
1445         ;
1446
1447 chord_body:
1448         chord_open chord_body_elements chord_close
1449         {
1450                 $$ = MY_MAKE_MUSIC ("EventChord");
1451                 $$->set_property ("elements",
1452                         scm_reverse_x ($2, SCM_EOL));
1453         }
1454         ;
1455
1456 chord_body_elements:
1457         /* empty */             { $$ = SCM_EOL; }
1458         | chord_body_elements chord_body_element {
1459                 $$ = scm_cons ($2->self_scm (), $1);
1460                 scm_gc_unprotect_object ($2->self_scm ());
1461         }
1462         ;
1463
1464 chord_body_element:
1465         pitch exclamations questions post_events
1466         {
1467                 Music *n = MY_MAKE_MUSIC ("NoteEvent");
1468                 n->set_property ("pitch", $1);
1469                 if ($3 % 2)
1470                         n->set_property ("cautionary", SCM_BOOL_T);
1471                 if ($2 % 2 || $3 % 2)
1472                         n->set_property ("force-accidental", SCM_BOOL_T);
1473
1474                 if (ly_c_pair_p ($4)) {
1475                         SCM arts = scm_reverse_x ($4, SCM_EOL);
1476                         n->set_property ("articulations", arts);
1477                 }
1478                 $$ = n;
1479         }
1480         | DRUM_PITCH post_events {
1481                 Music *n = MY_MAKE_MUSIC ("NoteEvent");
1482                 n->set_property ("duration", $2);
1483                 n->set_property ("drum-type", $1);
1484                 n->set_spot (THIS->here_input ());
1485
1486                 if (ly_c_pair_p ($2)) {
1487                         SCM arts = scm_reverse_x ($2, SCM_EOL);
1488                         n->set_property ("articulations", arts);
1489                 }
1490                 $$ = n;
1491         }
1492         ;
1493
1494 add_quote:
1495         ADDQUOTE string Music {
1496                 SCM adder = ly_scheme_function ("add-quotable");
1497                 
1498                 scm_call_2 (adder, $2, $3->self_scm ());
1499                 scm_gc_unprotect_object ($3->self_scm ());
1500         }
1501         ;
1502
1503 command_element:
1504         command_req {
1505                 $$ = MY_MAKE_MUSIC ("EventChord");
1506                 $$->set_property ("elements", scm_cons ($1->self_scm (), SCM_EOL));
1507                 scm_gc_unprotect_object ($1->self_scm ());
1508
1509                 $$-> set_spot (THIS->here_input ());
1510                 $1-> set_spot (THIS->here_input ());
1511         }
1512         | SKIP duration_length {
1513                 Music *skip = MY_MAKE_MUSIC ("SkipMusic");
1514                 skip->set_property ("duration", $2);
1515
1516                 $$ = skip;
1517         }
1518         | QUOTE STRING duration_length {
1519                 SCM tab = THIS->lexer_->lookup_identifier ("musicQuotes");
1520                 SCM evs = SCM_EOL;
1521                 if (scm_hash_table_p (tab) == SCM_BOOL_T)
1522                 {
1523                         SCM key = $2; // use symbol?
1524                         evs = scm_hash_ref (tab, key, SCM_BOOL_F);
1525                 }
1526                 Music *quote = 0;
1527                 if (ly_c_vector_p (evs))
1528                 {
1529                         quote = MY_MAKE_MUSIC ("QuoteMusic");
1530                         quote->set_property ("duration", $3);
1531                         quote->set_property ("quoted-events", evs);
1532                 } else {
1533                         THIS->here_input ().warning (_f ("Can\'t find music"));
1534                         quote = MY_MAKE_MUSIC ("Event");
1535                 }
1536                 quote->set_spot (THIS->here_input ());
1537                 $$ = quote;
1538         }
1539         | OCTAVE { THIS->push_spot (); }
1540           pitch {
1541                 Music *m = MY_MAKE_MUSIC ("RelativeOctaveCheck");
1542                 $$ = m;
1543                 $$->set_spot (THIS->pop_spot ());
1544                 $$->set_property ("pitch", $3);
1545         }
1546         | E_LEFTSQUARE {
1547                 Music *m = MY_MAKE_MUSIC ("LigatureEvent");
1548                 m->set_property ("span-direction", scm_int2num (START));
1549                 m->set_spot (THIS->here_input ());
1550
1551                 $$ = MY_MAKE_MUSIC ("EventChord");
1552                 $$->set_property ("elements", scm_cons (m->self_scm (), SCM_EOL));
1553                 scm_gc_unprotect_object (m->self_scm ());
1554                 $$->set_spot (THIS->here_input ());
1555         }
1556         | E_RIGHTSQUARE {
1557                 Music *m = MY_MAKE_MUSIC ("LigatureEvent");
1558                 m->set_property ("span-direction", scm_int2num (STOP));
1559                 m->set_spot (THIS->here_input ());
1560
1561                 $$ = MY_MAKE_MUSIC ("EventChord");
1562                 $$->set_property ("elements", scm_cons (m->self_scm (), SCM_EOL));
1563                 $$->set_spot (THIS->here_input ());
1564                 scm_gc_unprotect_object (m->self_scm ());
1565         }
1566         | E_BACKSLASH {
1567                 $$ = MY_MAKE_MUSIC ("VoiceSeparator");
1568                 $$->set_spot (THIS->here_input ());
1569         }
1570         | '|'      {
1571                 SCM pipe =THIS->lexer_->lookup_identifier ("pipeSymbol");
1572
1573                 if (Music * m = unsmob_music (pipe))
1574                         $$ = m->clone ();
1575                 else
1576                         $$ = MY_MAKE_MUSIC ("BarCheck");
1577
1578                 $$->set_spot (THIS->here_input ());
1579         }
1580         | TRANSPOSITION pitch {
1581                 $$ = set_property_music (ly_symbol2scm ("instrumentTransposition"),
1582                                         $2);
1583                 $$->set_spot (THIS-> here_input ());
1584                 $$ = context_spec_music (ly_symbol2scm ("Staff"), SCM_UNDEFINED,
1585                         $$, SCM_EOL);
1586         }
1587         | BAR STRING                    {
1588                 Music *t = set_property_music (ly_symbol2scm ("whichBar"), $2);
1589
1590                 Music *csm = context_spec_music (ly_symbol2scm ("Timing"), SCM_UNDEFINED,
1591                                         t, SCM_EOL);
1592                 $$ = context_spec_music (ly_symbol2scm ("Score"), SCM_UNDEFINED, csm, SCM_EOL);
1593                 $$->set_spot (THIS->here_input ());
1594                 t->set_spot (THIS->here_input ());
1595         }
1596         | PARTIAL duration_length       {
1597                 Moment m = - unsmob_duration ($2)->get_length ();
1598                 Music *p = set_property_music (ly_symbol2scm ( "measurePosition"),m.smobbed_copy ());
1599                 p->set_spot (THIS->here_input ());
1600                 p = context_spec_music (ly_symbol2scm ("Timing"), SCM_UNDEFINED,
1601                                         p, SCM_EOL);
1602                 p = context_spec_music (ly_symbol2scm ("Score"), SCM_UNDEFINED,
1603                                         p, SCM_EOL);
1604                 $$ = p;
1605         }
1606         | CLEF STRING  {
1607                 SCM proc = ly_scheme_function ("make-clef-set");
1608
1609                 SCM result = scm_call_1 (proc, $2);
1610                 scm_gc_protect_object (result);
1611                 $$ = unsmob_music (result);
1612         }
1613         | TIME_T fraction  {
1614                 SCM proc= ly_scheme_function ("make-time-signature-set");
1615
1616                 SCM result = scm_apply_2   (proc, ly_car ($2), ly_cdr ($2), SCM_EOL);
1617                 scm_gc_protect_object (result);
1618                 $$ = unsmob_music (result);
1619         }
1620         | MARK scalar {
1621                 SCM proc = ly_scheme_function ("make-mark-set");
1622
1623                 SCM result = scm_call_1 (proc, $2);
1624                 scm_gc_protect_object (result);
1625                 $$ = unsmob_music (result);
1626         }
1627         ;
1628
1629 command_req:
1630         E_TILDE {
1631                 $$ = MY_MAKE_MUSIC ("PesOrFlexaEvent");
1632         }
1633         | MARK DEFAULT  {
1634                 Music *m = MY_MAKE_MUSIC ("MarkEvent");
1635                 $$ = m;
1636         }
1637         | tempo_event {
1638                 $$ = $1;
1639         }
1640         | KEY DEFAULT {
1641                 Music *key= MY_MAKE_MUSIC ("KeyChangeEvent");
1642                 $$ = key;
1643         }
1644         | KEY NOTENAME_PITCH SCM_IDENTIFIER     {
1645
1646                 Music *key= MY_MAKE_MUSIC ("KeyChangeEvent");
1647                 if (scm_ilength ($3) > 0)
1648                 {               
1649                         key->set_property ("pitch-alist", $3);
1650                         key->set_property ("tonic", Pitch (0,0,0).smobbed_copy ());
1651                         ((Music*)key)->transpose (* unsmob_pitch ($2));
1652                 } else {
1653                         THIS->parser_error (_ ("Second argument must be pitch list."));
1654                 }
1655
1656                 $$ = key;
1657         }
1658         ;
1659
1660 post_events:
1661         /* empty */ {
1662                 $$ = SCM_EOL;
1663         }
1664         | post_events post_event {
1665                 $2->set_spot (THIS->here_input ());
1666                 $$ = scm_cons ($2->self_scm (), $$);
1667                 scm_gc_unprotect_object ($2->self_scm ());
1668         }
1669         | post_events tagged_post_event {
1670                 $2 -> set_spot (THIS->here_input ());
1671                 $$ = scm_cons ($2->self_scm (), $$);
1672                 scm_gc_unprotect_object ($2->self_scm ());
1673         }
1674         ;
1675
1676
1677 tagged_post_event:
1678         '-' TAG embedded_scm post_event {
1679                 tag_music ($4, $3, THIS->here_input ());
1680                 $$ = $4;
1681         }
1682         ;
1683
1684 post_event:
1685         direction_less_event {
1686                 $$ = $1;
1687         }
1688         | HYPHEN {
1689                 if (!THIS->lexer_->is_lyric_state ())
1690                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
1691                 $$ = MY_MAKE_MUSIC ("HyphenEvent");
1692         }
1693         | EXTENDER {
1694                 if (!THIS->lexer_->is_lyric_state ())
1695                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
1696                 $$ = MY_MAKE_MUSIC ("ExtenderEvent");
1697         }
1698         | script_dir direction_reqd_event {
1699                 if ($1)
1700                         $2->set_property ("direction", scm_int2num ($1));
1701                 $$ = $2;
1702         }
1703         | script_dir direction_less_event {
1704                 if ($1)
1705                         $2->set_property ("direction", scm_int2num ($1));
1706                 $$ = $2;
1707         }
1708         | string_number_event
1709         ;
1710
1711 string_number_event:
1712         E_UNSIGNED {
1713                 Music *s = MY_MAKE_MUSIC ("StringNumberEvent");
1714                 s->set_property ("string-number", scm_int2num ($1));
1715                 s->set_spot (THIS->here_input ());
1716                 $$ = s;
1717         }
1718         ;
1719
1720
1721 direction_less_event:
1722         '['  {
1723
1724
1725 /*
1726
1727 TODO: should take all these defs out of the parser, adn make use
1728 configurable, i.e.
1729
1730
1731 (set-articulation '~ "trill")
1732
1733 */
1734                 Music *m = MY_MAKE_MUSIC ("BeamEvent");
1735                 m->set_spot (THIS->here_input ());
1736                 m->set_property ("span-direction", scm_int2num (START));
1737                 $$ = m;
1738         }
1739         | ']'  {
1740                 Music *m = MY_MAKE_MUSIC ("BeamEvent");
1741                 m->set_spot (THIS->here_input ());
1742                 m->set_property ("span-direction", scm_int2num (STOP));
1743                 $$ = m;
1744         }
1745         | '~' {
1746                 Music *m = MY_MAKE_MUSIC ("TieEvent");
1747                 m->set_spot (THIS->here_input ());
1748                 $$ = m;
1749         }
1750         | close_event {
1751                 $$ = $1;
1752                 dynamic_cast<Music *> ($$)->set_property ("span-direction",
1753                         scm_int2num (START));
1754         }
1755         | open_event {
1756                 $$ = $1;
1757                 dynamic_cast<Music *> ($$)->set_property ("span-direction",
1758                         scm_int2num (STOP));
1759         }
1760         | EVENT_IDENTIFIER      {
1761                 $$ = unsmob_music ($1);
1762         }
1763         | tremolo_type  {
1764                Music *a = MY_MAKE_MUSIC ("TremoloEvent");
1765                a->set_spot (THIS->here_input ());
1766                a->set_property ("tremolo-type", scm_int2num ($1));
1767                $$ = a;
1768         }
1769         ;       
1770         
1771 direction_reqd_event:
1772         gen_text_def {
1773                 $$ = $1;
1774         }
1775         | script_abbreviation {
1776                 SCM s = THIS->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
1777                 Music *a = MY_MAKE_MUSIC ("ArticulationEvent");
1778                 if (ly_c_string_p (s))
1779                         a->set_property ("articulation-type", s);
1780                 else THIS->parser_error (_ ("Expecting string as script definition"));
1781                 $$ = a;
1782         }
1783         ;
1784
1785 oct_check:
1786         /**/ { $$ = SCM_EOL; }
1787         | '='  { $$ = scm_int2num (0); }
1788         | '=' sub_quotes { $$ = scm_int2num ($2); }
1789         | '=' sup_quotes { $$ = scm_int2num ($2); }
1790         ;
1791
1792 sup_quotes:
1793         '\'' {
1794                 $$ = 1;
1795         }
1796         | sup_quotes '\'' {
1797                 $$ ++;
1798         }
1799         ;
1800
1801 sub_quotes:
1802         ',' {
1803                 $$ = 1;
1804         }
1805         | sub_quotes ',' {
1806                 $$++;
1807         }
1808         ;
1809
1810 steno_pitch:
1811         NOTENAME_PITCH  {
1812                 $$ = $1;
1813         }
1814         | NOTENAME_PITCH sup_quotes     {
1815                 Pitch p = *unsmob_pitch ($1);
1816                 p = p.transposed (Pitch ($2,0,0));
1817                 $$ = p.smobbed_copy ();
1818         }
1819         | NOTENAME_PITCH sub_quotes      {
1820                 Pitch p =* unsmob_pitch ($1);
1821                 p = p.transposed (Pitch (-$2,0,0));
1822                 $$ = p.smobbed_copy ();
1823         }
1824         ;
1825
1826 /*
1827 ugh. duplication
1828 */
1829
1830 steno_tonic_pitch:
1831         TONICNAME_PITCH {
1832                 $$ = $1;
1833         }
1834         | TONICNAME_PITCH sup_quotes    {
1835                 Pitch p = *unsmob_pitch ($1);
1836                 p = p.transposed (Pitch ($2,0,0));
1837                 $$ = p.smobbed_copy ();
1838         }
1839         | TONICNAME_PITCH sub_quotes     {
1840                 Pitch p =* unsmob_pitch ($1);
1841
1842                 p = p.transposed (Pitch (-$2,0,0));
1843                 $$ = p.smobbed_copy ();
1844         }
1845         ;
1846
1847 pitch:
1848         steno_pitch {
1849                 $$ = $1;
1850         }
1851         ;
1852
1853 pitch_also_in_chords:
1854         pitch
1855         | steno_tonic_pitch
1856         ;
1857
1858 close_event:
1859         '('     {
1860                 Music *s = MY_MAKE_MUSIC ("SlurEvent");
1861                 $$ = s;
1862                 s->set_spot (THIS->here_input ());
1863         }
1864         | E_OPEN        {
1865                 Music *s = MY_MAKE_MUSIC ("PhrasingSlurEvent");
1866                 $$ = s;
1867                 s->set_spot (THIS->here_input ());
1868         }
1869         | E_SMALLER {
1870                 Music *s = MY_MAKE_MUSIC ("CrescendoEvent");
1871                 $$ = s;
1872                 s->set_spot (THIS->here_input ());
1873         }
1874         | E_BIGGER {
1875                 Music *s = MY_MAKE_MUSIC ("DecrescendoEvent");
1876                 $$ = s;
1877                 s->set_spot (THIS->here_input ());
1878         }
1879         ;
1880
1881
1882 open_event:
1883         E_EXCLAMATION   {
1884                 Music *s = MY_MAKE_MUSIC ("CrescendoEvent");
1885                 s->set_spot (THIS->here_input ());
1886
1887                 $$ = s;
1888         }
1889         | ')'   {
1890                 Music *s= MY_MAKE_MUSIC ("SlurEvent");
1891                 $$ = s;
1892                 s->set_spot (THIS->here_input ());
1893
1894         }
1895         | E_CLOSE       {
1896                 Music *s= MY_MAKE_MUSIC ("PhrasingSlurEvent");
1897                 $$ = s;
1898                 s->set_property ("span-type",
1899                         scm_makfrom0str ("phrasing-slur"));
1900                 s->set_spot (THIS->here_input ());
1901         }
1902         ;
1903
1904 gen_text_def:
1905         full_markup {
1906                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent");
1907                 t->set_property ("text", $1);
1908                 t->set_spot (THIS->here_input ());
1909                 $$ = t; 
1910         }
1911         | string {
1912                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent");
1913                 t->set_property ("text",
1914                         make_simple_markup (THIS->lexer_->encoding (), $1));
1915                 t->set_spot (THIS->here_input ());
1916                 $$ = t;
1917         
1918         }
1919         | DIGIT {
1920                 Music *t = MY_MAKE_MUSIC ("FingerEvent");
1921                 t->set_property ("digit", scm_int2num ($1));
1922                 t->set_spot (THIS->here_input ());
1923                 $$ = t;
1924         }
1925         ;
1926
1927 script_abbreviation:
1928         '^'             {
1929                 $$ = scm_makfrom0str ("Hat");
1930         }
1931         | '+'           {
1932                 $$ = scm_makfrom0str ("Plus");
1933         }
1934         | '-'           {
1935                 $$ = scm_makfrom0str ("Dash");
1936         }
1937         | '|'           {
1938                 $$ = scm_makfrom0str ("Bar");
1939         }
1940         | '>'           {
1941                 $$ = scm_makfrom0str ("Larger");
1942         }
1943         | '.'           {
1944                 $$ = scm_makfrom0str ("Dot");
1945         }
1946         | '_' {
1947                 $$ = scm_makfrom0str ("Underscore");
1948         }
1949         ;
1950
1951 script_dir:
1952         '_'     { $$ = DOWN; }
1953         | '^'   { $$ = UP; }
1954         | '-'   { $$ = CENTER; }
1955         ;
1956
1957
1958 absolute_pitch:
1959         steno_pitch     {
1960                 $$ = $1;
1961         }
1962         ;
1963
1964 duration_length:
1965         multiplied_duration {
1966                 $$ = $1;
1967         }
1968         ;
1969
1970 optional_notemode_duration:
1971         {
1972                 Duration dd = THIS->default_duration_;
1973                 $$ = dd.smobbed_copy ();
1974
1975                 THIS->beam_check ($$);
1976         }
1977         | multiplied_duration   {
1978                 $$ = $1;
1979                 THIS->default_duration_ = *unsmob_duration ($$);
1980
1981                 THIS->beam_check ($$);
1982         }
1983         ;
1984
1985 steno_duration:
1986         bare_unsigned dots              {
1987                 int len = 0;
1988                 if (!is_duration ($1))
1989                         THIS->parser_error (_f ("not a duration: %d", $1));
1990                 else
1991                         len = intlog2 ($1);
1992
1993                 $$ = Duration (len, $2).smobbed_copy ();
1994         }
1995         | DURATION_IDENTIFIER dots      {
1996                 Duration *d = unsmob_duration ($1);
1997                 Duration k (d->duration_log (), d->dot_count () + $2);
1998                 *d = k;
1999                 $$ = $1;
2000         }
2001         ;
2002
2003 multiplied_duration:
2004         steno_duration {
2005                 $$ = $1;
2006         }
2007         | multiplied_duration '*' bare_unsigned {
2008                 $$ = unsmob_duration ($$)->compressed ( $3) .smobbed_copy ();
2009         }
2010         | multiplied_duration '*' FRACTION {
2011                 Rational  m (ly_scm2int (ly_car ($3)), ly_scm2int (ly_cdr ($3)));
2012
2013                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
2014         }
2015         ;
2016
2017 fraction:
2018         FRACTION { $$ = $1; }
2019         | UNSIGNED '/' UNSIGNED {
2020                 $$ = scm_cons (scm_int2num ($1), scm_int2num ($3));
2021         }
2022         ;
2023
2024 dots:
2025         /* empty */     {
2026                 $$ = 0;
2027         }
2028         | dots '.' {
2029                 $$ ++;
2030         }
2031         ;
2032
2033 tremolo_type:
2034         ':'     {
2035                 $$ = 0;
2036         }
2037         | ':' bare_unsigned {
2038                 if (!is_duration ($2))
2039                         THIS->parser_error (_f ("not a duration: %d", $2));
2040                 $$ = $2;
2041         }
2042         ;
2043
2044 bass_number:
2045         DIGIT   {
2046                 $$ = scm_number_to_string (scm_int2num ($1), scm_int2num (10));
2047                 $$ = scm_list_2 (ly_scheme_function ("number-markup"),
2048                                 $$);
2049         }
2050         | UNSIGNED {
2051                 $$ = scm_number_to_string (scm_int2num ($1), scm_int2num (10));
2052                 $$ = scm_list_2 (ly_scheme_function ("number-markup"),
2053                                 $$);
2054         }
2055         | STRING { $$ = $1; }
2056         ;
2057
2058 bass_mod:
2059         '-'     { $$ = -2; }
2060         | '+'   { $$ = 2; }
2061         | '!'   { $$ = 0; }
2062         ;
2063
2064 bass_figure:
2065         FIGURE_SPACE {
2066                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent");
2067                 $$ = bfr->self_scm ();
2068                 scm_gc_unprotect_object ($$);
2069         }
2070         | bass_number  {
2071                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent");
2072                 $$ = bfr->self_scm ();
2073
2074                 bfr->set_property ("figure", $1);
2075
2076                 scm_gc_unprotect_object ($$);
2077         }
2078         | bass_figure bass_mod {
2079                 Music *m = unsmob_music ($1);
2080                 if ($2) {
2081                         SCM salter = m->get_property ("alteration");
2082                         int alter = ly_c_number_p (salter) ? ly_scm2int (salter) : 0;
2083                         m->set_property ("alteration",
2084                                 scm_int2num (alter + $2));
2085                 } else {
2086                         m->set_property ("alteration", scm_int2num (0));
2087                 }
2088         }
2089         ;
2090
2091 br_bass_figure:
2092         '[' bass_figure {
2093                 $$ = $2;
2094                 unsmob_music ($$)->set_property ("bracket-start", SCM_BOOL_T);
2095         }
2096         | bass_figure   {
2097                 $$ = $1;
2098         }
2099         | br_bass_figure ']' {
2100                 $$ = $1;
2101                 unsmob_music ($1)->set_property ("bracket-stop", SCM_BOOL_T);
2102         }
2103         ;
2104
2105 figure_list:
2106         /**/            {
2107                 $$ = SCM_EOL;
2108         }
2109         | figure_list br_bass_figure {
2110                 $$ = scm_cons ($2, $1);
2111         }
2112         ;
2113
2114 figure_spec:
2115         FIGURE_OPEN figure_list FIGURE_CLOSE {
2116                 Music *m = MY_MAKE_MUSIC ("EventChord");
2117                 $2 = scm_reverse_x ($2, SCM_EOL);
2118                 m->set_property ("elements", $2);
2119                 $$ = m->self_scm ();
2120         }
2121         ;
2122
2123
2124 optional_rest:
2125         /**/   { $$ = 0; }
2126         | REST { $$ = 1; }
2127         ;
2128
2129 simple_element:
2130         pitch exclamations questions oct_check optional_notemode_duration optional_rest {
2131
2132                 Input i = THIS->pop_spot ();
2133                 if (!THIS->lexer_->is_note_state ())
2134                         THIS->parser_error (_ ("Have to be in Note mode for notes"));
2135
2136                 Music *n = 0;
2137                 if ($6)
2138                         n = MY_MAKE_MUSIC ("RestEvent");
2139                 else
2140                         n = MY_MAKE_MUSIC ("NoteEvent");
2141                 
2142                 n->set_property ("pitch", $1);
2143                 n->set_property ("duration", $5);
2144
2145                 if (ly_c_number_p ($4))
2146                 {
2147                         int q = ly_scm2int ($4);
2148                         n->set_property ("absolute-octave", scm_int2num (q-1));
2149                 }
2150
2151                 if ($3 % 2)
2152                         n->set_property ("cautionary", SCM_BOOL_T);
2153                 if ($2 % 2 || $3 % 2)
2154                         n->set_property ("force-accidental", SCM_BOOL_T);
2155
2156                 Music *v = MY_MAKE_MUSIC ("EventChord");
2157                 v->set_property ("elements", scm_list_1 (n->self_scm ()));
2158                 scm_gc_unprotect_object (n->self_scm ());
2159
2160                 v->set_spot (i);
2161                 n->set_spot (i);
2162                 $$ = v;
2163         }
2164         | DRUM_PITCH optional_notemode_duration {
2165                 Input i = THIS->pop_spot ();
2166
2167                 Music *n = MY_MAKE_MUSIC ("NoteEvent");
2168                 n->set_property ("duration", $2);
2169                 n->set_property ("drum-type", $1);
2170
2171                 Music *v = MY_MAKE_MUSIC ("EventChord");
2172                 v->set_property ("elements", scm_list_1 (n->self_scm ()));
2173                 scm_gc_unprotect_object (n->self_scm ());
2174                 v->set_spot (i);
2175                 n->set_spot (i);
2176                 $$ = v;
2177                 
2178         }
2179         | figure_spec optional_notemode_duration {
2180                 Music *m = unsmob_music ($1);
2181                 Input i = THIS->pop_spot ();
2182                 m->set_spot (i);
2183                 for (SCM s = m->get_property ("elements"); ly_c_pair_p (s); s = ly_cdr (s))
2184                 {
2185                         unsmob_music (ly_car (s))->set_property ("duration", $2);
2186                 }
2187                 $$ = m;
2188         }       
2189         | RESTNAME optional_notemode_duration           {
2190
2191                 Input i = THIS->pop_spot ();
2192                 Music *ev = 0;
2193                 if (ly_scm2string ($1) == "s") {
2194                         /* Space */
2195                         ev = MY_MAKE_MUSIC ("SkipEvent");
2196                   }
2197                 else {
2198                         ev = MY_MAKE_MUSIC ("RestEvent");
2199                 
2200                     }
2201                 ev->set_property ("duration", $2);
2202                 ev->set_spot (i);
2203                 Music *velt = MY_MAKE_MUSIC ("EventChord");
2204                 velt->set_property ("elements", scm_list_1 (ev->self_scm ()));
2205                 velt->set_spot (i);
2206
2207                 scm_gc_unprotect_object (ev->self_scm ());
2208
2209                 $$ = velt;
2210         }
2211         | MULTI_MEASURE_REST optional_notemode_duration         {
2212                 THIS->pop_spot ();
2213
2214                 SCM proc = ly_scheme_function ("make-multi-measure-rest");
2215                 SCM mus = scm_call_2 (proc, $2,
2216                         make_input (THIS->here_input ()));      
2217                 scm_gc_protect_object (mus);
2218                 $$ = unsmob_music (mus);
2219         }
2220         
2221         | lyric_element optional_notemode_duration      {
2222                 Input i = THIS->pop_spot ();
2223                 if (!THIS->lexer_->is_lyric_state ())
2224                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
2225
2226                 Music *lreq = MY_MAKE_MUSIC ("LyricEvent");
2227                 lreq->set_property ("text", $1);
2228                 lreq->set_property ("duration",$2);
2229                 lreq->set_spot (i);
2230                 Music *velt = MY_MAKE_MUSIC ("EventChord");
2231                 velt->set_property ("elements", scm_list_1 (lreq->self_scm ()));
2232
2233                 $$= velt;
2234         }
2235         | new_chord {
2236                 THIS->pop_spot ();
2237
2238                 if (!THIS->lexer_->is_chord_state ())
2239                         THIS->parser_error (_ ("Have to be in Chord mode for chords"));
2240                 $$ = unsmob_music ($1);
2241         }
2242         ;
2243
2244 lyric_element:
2245         /* FIXME: lyric flavoured markup would be better */
2246         full_markup {
2247         }
2248         | LYRICS_STRING {
2249         }
2250         ;
2251
2252 new_chord:
2253         steno_tonic_pitch optional_notemode_duration   {
2254                 $$ = make_chord ($1, $2, SCM_EOL);
2255         }
2256         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
2257                 SCM its = scm_reverse_x ($4, SCM_EOL);
2258                 $$ = make_chord ($1, $2, scm_cons ($3, its));
2259         }
2260         ;
2261
2262 chord_items:
2263         /**/ {
2264                 $$ = SCM_EOL;           
2265         }
2266         | chord_items chord_item {
2267                 $$ = scm_cons ($2, $$);
2268         }
2269         ;
2270
2271 chord_separator:
2272         CHORD_COLON {
2273                 $$ = ly_symbol2scm ("chord-colon");
2274         }
2275         | CHORD_CARET {
2276                 $$ = ly_symbol2scm ("chord-caret");
2277         }
2278         | CHORD_SLASH steno_tonic_pitch {
2279                 $$ = scm_list_2 (ly_symbol2scm ("chord-slash"), $2);
2280         }
2281         | CHORD_BASS steno_tonic_pitch {
2282                 $$ = scm_list_2 (ly_symbol2scm ("chord-bass"), $2);
2283         }
2284         ;
2285
2286 chord_item:
2287         chord_separator {
2288                 $$ = $1;
2289         }
2290         | step_numbers {
2291                 $$ = scm_reverse_x ($1, SCM_EOL);
2292         }
2293         | CHORD_MODIFIER  {
2294                 $$ = $1;
2295         }
2296         ;
2297
2298 step_numbers:
2299         step_number { $$ = scm_cons ($1, SCM_EOL); }
2300         | step_numbers '.' step_number {
2301                 $$ = scm_cons ($3, $$);
2302         }
2303         ;
2304
2305 step_number:
2306         bare_unsigned {
2307                 $$ = make_chord_step ($1, 0);
2308         }
2309         | bare_unsigned '+' {
2310                 $$ = make_chord_step ($1, SHARP);
2311         }
2312         | bare_unsigned CHORD_MINUS {
2313                 $$ = make_chord_step ($1, FLAT);
2314         }
2315         ;       
2316
2317 /*
2318         UTILITIES
2319
2320 TODO: should deprecate in favor of Scheme?
2321
2322  */
2323 number_expression:
2324         number_expression '+' number_term {
2325                 $$ = scm_sum ($1, $3);
2326         }
2327         | number_expression '-' number_term {
2328                 $$ = scm_difference ($1, $3);
2329         }
2330         | number_term
2331         ;
2332
2333 number_term:
2334         number_factor {
2335                 $$ = $1;
2336         }
2337         | number_factor '*' number_factor {
2338                 $$ = scm_product ($1, $3);
2339         }
2340         | number_factor '/' number_factor {
2341                 $$ = scm_divide ($1, $3);
2342         }
2343         ;
2344
2345 number_factor:
2346         '-'  number_factor { /* %prec UNARY_MINUS */
2347                 $$ = scm_difference ($2, SCM_UNDEFINED);
2348         }
2349         | bare_number
2350         ;
2351
2352
2353 bare_number:
2354         UNSIGNED        {
2355                 $$ = scm_int2num ($1);
2356         }
2357         | REAL          {
2358                 $$ = $1;
2359         }
2360         | NUMBER_IDENTIFIER             {
2361                 $$ = $1;
2362         }
2363         | REAL NUMBER_IDENTIFIER        {
2364                 $$ = scm_make_real (ly_scm2double ($1) *ly_scm2double ($2));
2365         }
2366         | UNSIGNED NUMBER_IDENTIFIER    {
2367                 $$ = scm_make_real ($1 *ly_scm2double ($2));
2368         }
2369         ;
2370
2371
2372 bare_unsigned:
2373         UNSIGNED {
2374                         $$ = $1;
2375         }
2376         | DIGIT {
2377                 $$ = $1;
2378         }
2379         ;
2380
2381 bare_int:
2382         bare_number {
2383                 if (scm_integer_p ($1) == SCM_BOOL_T)
2384                 {
2385                         int k = ly_scm2int ($1);
2386                         $$ = k;
2387                 } else
2388                 {
2389                         THIS->parser_error (_ ("need integer number arg"));
2390                         $$ = 0;
2391                 }
2392         }
2393         | '-' bare_int {
2394                 $$ = -$2;
2395         }
2396         ;
2397
2398 exclamations:
2399                 { $$ = 0; }
2400         | exclamations '!'      { $$ ++; }
2401         ;
2402
2403 questions:
2404                 { $$ = 0; }
2405         | questions '?' { $$ ++; }
2406         ;
2407
2408
2409
2410 full_markup:
2411         MARKUP_IDENTIFIER {
2412                 $$ = $1;
2413         }
2414         | MARKUP
2415                 { THIS->lexer_->push_markup_state (); }
2416         markup
2417                 { $$ = $3;
2418                   THIS->lexer_->pop_state ();
2419                 }
2420         ;
2421
2422
2423 /*
2424 This should be done more dynamically if possible.
2425 */
2426 markup:
2427         STRING {
2428                 $$ = make_simple_markup (THIS->lexer_->encoding (), $1);
2429         }
2430         | MARKUP_HEAD_EMPTY {
2431                 $$ = scm_list_1 ($1);
2432         }
2433         | MARKUP_HEAD_MARKUP0 markup {
2434                 $$ = scm_list_2 ($1, $2);
2435         }
2436         | MARKUP_HEAD_MARKUP0_MARKUP1 markup markup {
2437                 $$ = scm_list_3 ($1, $2, $3);
2438         }
2439         | MARKUP_HEAD_SCM0_MARKUP1 SCM_T markup {
2440                 $$ = scm_list_3 ($1, $2, $3);
2441         }
2442         | markup_line {
2443                 $$ = $1;
2444         }
2445         | MARKUP_HEAD_LIST0 markup_list {
2446                 $$ = scm_list_2 ($1,$2);
2447         }
2448         | MARKUP_HEAD_SCM0 embedded_scm {
2449                 $$ = scm_list_2 ($1, $2);
2450         }
2451         | MARKUP_HEAD_SCM0_SCM1_MARKUP2 embedded_scm embedded_scm markup {
2452                 $$ = scm_list_4 ($1, $2, $3, $4);
2453         }
2454         | MARKUP_HEAD_SCM0_SCM1_SCM2 embedded_scm embedded_scm embedded_scm {
2455                 $$ = scm_list_4 ($1, $2, $3, $4);
2456         }
2457         | MARKUP_HEAD_SCM0_SCM1 embedded_scm embedded_scm {
2458                 $$ = scm_list_3 ($1, $2, $3);
2459         }
2460         | MARKUP_IDENTIFIER {
2461                 $$ = $1;
2462         }
2463         | STRING_IDENTIFIER {
2464                 $$ = $1;
2465         }
2466         | {
2467                 SCM nn = THIS->lexer_->lookup_identifier ("pitchnames");
2468                 THIS->lexer_->push_note_state (alist_to_hashq (nn));
2469         }
2470         /* cont */ score_block {
2471                 /* WIP this is a bit arbitrary,
2472                    we should also allow \book or Composite_music.
2473                    However, you'd typically want to change paper
2474                    settings, and need a \score block anyway.  */
2475
2476                 THIS->lexer_->pop_state ();
2477                 Score *score = $2;
2478                 Book *book = new Book;
2479                 book->scores_.push (score);
2480                         
2481                 Music_output_def *paper = get_paper (THIS);
2482                 book->bookpaper_ = get_bookpaper (THIS);
2483
2484                 SCM s = book->to_stencil (paper, THIS->header_);
2485                 scm_gc_unprotect_object (score->self_scm ());
2486                 scm_gc_unprotect_object (book->self_scm ());
2487                 $$ = scm_list_2 (ly_scheme_function ("stencil-markup"), s);
2488         }
2489         ;
2490
2491 markup_list:
2492         chord_open markup_list_body chord_close { $$ = scm_reverse_x ($2, SCM_EOL); }
2493         ;
2494
2495 markup_line:
2496         '{' markup_list_body '}' {
2497                 SCM line = ly_scheme_function ("line-markup");
2498         
2499                 $$ = scm_list_2 (line, scm_reverse_x ($2, SCM_EOL));
2500         }
2501         ;
2502
2503 markup_list_body:
2504         /**/ {  $$ = SCM_EOL; }
2505         | markup_list_body markup {
2506                 $$ = scm_cons ($2, $1);
2507         }
2508         ;
2509
2510
2511 %%
2512
2513 void
2514 My_lily_parser::set_yydebug (bool )
2515 {
2516 #if 0
2517         yydebug = 1;
2518 #endif
2519 }
2520
2521 void
2522 My_lily_parser::do_yyparse ()
2523 {
2524         yyparse ((void*)this);
2525 }
2526
2527
2528 /*
2529 Should make this optional?    It will also complain when you do
2530
2531         [s4]
2532
2533 which is entirely legitimate.
2534
2535 Or we can scrap it. Barchecks should detect wrong durations, and
2536 skipTypesetting speeds it up a lot.
2537 */
2538
2539 void
2540 My_lily_parser::beam_check (SCM dur)
2541 {
2542   Duration *d = unsmob_duration (dur);
2543   if (unsmob_music (last_beam_start_) && d->duration_log () <= 2)
2544     {
2545       Music *m = unsmob_music (last_beam_start_);
2546       m->origin ()->warning (_ ("Suspect duration found following this beam"));
2547     }
2548   last_beam_start_ = SCM_EOL;
2549 }
2550
2551
2552
2553
2554 /*
2555
2556 It is a little strange to have this function in this file, but
2557 otherwise, we have to import music classes into the lexer.
2558
2559 */
2560 int
2561 My_lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
2562 {
2563         if (ly_c_string_p (sid)) {
2564                 *destination = sid;
2565                 return STRING_IDENTIFIER;
2566         } else if (ly_c_number_p (sid)) {
2567                 *destination = sid;
2568                 return NUMBER_IDENTIFIER;
2569         } else if (unsmob_context_def (sid)) {
2570                 *destination = unsmob_context_def (sid)->clone_scm ();
2571                 return CONTEXT_DEF_IDENTIFIER;
2572         } else if (unsmob_score (sid)) {
2573                 Score *score = new Score (*unsmob_score (sid));
2574                 *destination = score->self_scm ();
2575                 return SCORE_IDENTIFIER;
2576         } else if (Music *mus = unsmob_music (sid)) {
2577                 mus = mus->clone ();
2578                 *destination = mus->self_scm ();
2579                 unsmob_music (*destination)->
2580                         set_property ("origin", make_input (last_input_));
2581                 return dynamic_cast<Event*> (mus)
2582                         ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
2583         } else if (unsmob_duration (sid)) {
2584                 *destination = unsmob_duration (sid)->smobbed_copy ();
2585                 return DURATION_IDENTIFIER;
2586         } else if (unsmob_music_output_def (sid)) {
2587                 Music_output_def *p = unsmob_music_output_def (sid);
2588                 p = p->clone ();
2589
2590                 *destination = p->self_scm ();
2591                 return MUSIC_OUTPUT_DEF_IDENTIFIER;
2592         } else if (Text_item::markup_p (sid)) {
2593                 *destination = sid;
2594                 return MARKUP_IDENTIFIER;
2595         }
2596
2597         return -1;      
2598 }
2599
2600 Music *
2601 property_op_to_music (SCM op)
2602 {
2603         Music *m = 0;
2604         SCM tag = ly_car (op);
2605         SCM symbol = ly_cadr (op);
2606         SCM args = ly_cddr (op);
2607         SCM grob_val = SCM_UNDEFINED;
2608         SCM grob_sym = SCM_UNDEFINED;
2609         SCM val = SCM_UNDEFINED;
2610         
2611         if (tag == ly_symbol2scm ("assign"))
2612                 {
2613                 m = MY_MAKE_MUSIC ("PropertySet");
2614                 val = ly_car (args);
2615                 }
2616         else if (tag == ly_symbol2scm ("unset"))
2617                 m = MY_MAKE_MUSIC ("PropertyUnset");
2618         else if (tag == ly_symbol2scm ("poppush")
2619                  || tag == ly_symbol2scm ("push"))
2620                 {
2621                 m = MY_MAKE_MUSIC ("OverrideProperty");
2622                 grob_sym = ly_car (args);
2623                 grob_val = ly_cadr (args);
2624                 }
2625         else if (tag == ly_symbol2scm ("pop")) {
2626                 m = MY_MAKE_MUSIC ("RevertProperty");
2627                 grob_sym = ly_car (args);
2628                 }
2629
2630         m->set_property ("symbol", symbol);
2631
2632         if (val != SCM_UNDEFINED)
2633                 m->set_property ("value", val);
2634         if (grob_val != SCM_UNDEFINED)
2635                 m->set_property ("grob-value", grob_val);
2636
2637         if (grob_sym != SCM_UNDEFINED)
2638                 {
2639                 bool itc = internal_type_checking_global_b;
2640                 /* UGH.
2641                 */
2642                 bool autobeam = ly_c_equal_p (symbol, ly_symbol2scm ("autoBeamSettings"));
2643                 if (autobeam)
2644                         internal_type_checking_global_b = false;
2645                 m->set_property ("grob-property", grob_sym);
2646                 if (autobeam)
2647                         internal_type_checking_global_b = itc;
2648                 }
2649
2650         if (tag == ly_symbol2scm ("poppush"))
2651                 m->set_property ("pop-first", SCM_BOOL_T);
2652
2653
2654         return m;
2655 }
2656
2657 Music*
2658 context_spec_music (SCM type, SCM id, Music *m, SCM ops)
2659 {
2660         Music *csm = MY_MAKE_MUSIC ("ContextSpeccedMusic");
2661
2662         csm->set_property ("element", m->self_scm ());
2663         scm_gc_unprotect_object (m->self_scm ());
2664
2665         csm->set_property ("context-type",
2666                 ly_c_symbol_p (type) ? type : scm_string_to_symbol (type));
2667         csm->set_property ("property-operations", ops);
2668
2669         if (ly_c_string_p (id))
2670                 csm->set_property ("context-id", id);
2671         return csm;
2672 }
2673
2674 SCM
2675 get_next_unique_context ()
2676 {
2677         static int new_context_count;
2678         char s[1024];
2679         snprintf (s, 1024, "uniqueContext%d", new_context_count++);
2680         return scm_makfrom0str (s);
2681 }
2682