]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
* lily/book-paper-def.cc (Book_paper_def): add copy ctor.
[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                 scm_gc_unprotect_object ($$->bookpaper_->self_scm ());
655         }
656         | book_body book_paper_block {
657                 $$->bookpaper_ = $2;
658                 scm_gc_unprotect_object ($2->self_scm ());
659         }
660         | book_body score_block {
661                 Score *score = $2;
662                 $$->scores_.push (score);
663                 scm_gc_unprotect_object (score->self_scm ());
664         }
665         | book_body Composite_music {
666                 Music *music = $2;
667                 Score *score
668                         = unsmob_score (ly_music_scorify (music->self_scm ()));
669                 $$->scores_.push (score);
670                 scm_gc_unprotect_object (music->self_scm ());
671         }
672         | lilypond_header {
673                 THIS->header_ = $1;
674         }
675         | book_body error {
676
677         }
678         ;
679
680 score_block:
681         SCORE {
682                 THIS->push_spot ();
683         }
684         /*cont*/ '{' score_body '}'     {
685                 THIS->pop_spot ();
686                 $$ = $4;
687         }
688         ;
689
690 score_body:
691         Music   {
692                 $$ = new Score;
693         
694                 $$->set_spot (THIS->here_input ());
695                 SCM m = $1->self_scm ();
696                 scm_gc_unprotect_object (m);
697
698                 /*
699                         guh.
700                 */
701                 SCM check_funcs = ly_scheme_function ("toplevel-music-functions");
702                 for (; ly_c_pair_p (check_funcs); check_funcs = ly_cdr (check_funcs))
703                         m = scm_call_1 (ly_car (check_funcs), m);
704                 $$->music_ = m;
705
706         }
707         | SCORE_IDENTIFIER {
708                 $$ = new Score ( *unsmob_score ($1));
709                 $$->set_spot (THIS->here_input ());
710         }
711         | score_body lilypond_header    {
712                 $$->header_ = $2;
713         }
714         | score_body output_def {
715                 $$->defs_.push ($2);
716                 scm_gc_unprotect_object ($2->self_scm ());
717         }
718         | score_body error {
719
720         }
721         ;
722
723
724 /*
725         MIDI
726 */
727 output_def:
728         music_output_def_body '}' {
729                 $$ = $1;
730                 THIS->lexer_->remove_scope ();
731                 THIS->lexer_->pop_state ();
732         }
733         ;
734
735 music_output_def_head:
736         MIDI    {
737                 Music_output_def *id = unsmob_music_output_def (THIS->lexer_->lookup_identifier ("$defaultmidi"));
738
739
740                 Midi_def *p = 0;
741                 if (id)
742                         p = dynamic_cast<Midi_def*> (id->clone ());
743                 else
744                         p = new Midi_def;
745
746                 $$ = p;
747                 THIS->lexer_->add_scope (p->scope_);
748         }
749         | PAPER         {
750                 Music_output_def *id = unsmob_music_output_def (THIS->lexer_->lookup_identifier ("$defaultpaper"));
751                   Paper_def *p = 0;
752                 if (id)
753                         p = dynamic_cast<Paper_def*> (id->clone ());
754                 else
755                         p = new Paper_def;
756
757                 THIS->lexer_->add_scope (p->scope_);
758                 $$ = p;
759         }
760         ;
761
762
763 music_output_def_body:
764         music_output_def_head '{' {
765                 $$ = $1;
766                 $$->input_origin_. set_spot (THIS->here_input ());
767                 THIS->lexer_->push_initial_state ();
768         }
769         | music_output_def_head '{' MUSIC_OUTPUT_DEF_IDENTIFIER         {
770                 scm_gc_unprotect_object ($1->self_scm ());
771                 Music_output_def *o = unsmob_music_output_def ($3);
772                 o->input_origin_.set_spot (THIS->here_input ());
773                 $$ = o;
774                 THIS->lexer_->remove_scope ();
775                 THIS->lexer_->add_scope (o->scope_);
776                 THIS->lexer_->push_initial_state ();
777         }
778         | music_output_def_body assignment  {
779
780         }
781         | music_output_def_body context_def_spec_block  {
782                 $$->assign_context_def ($2);
783         }
784         | music_output_def_body tempo_event  {
785                 /*
786                         junk this ? there already is tempo stuff in
787                         music.
788                 */
789                 int m = ly_scm2int ( $2->get_property ("metronome-count"));
790                 Duration *d = unsmob_duration ($2->get_property ("tempo-unit"));
791                 Midi_def *md = dynamic_cast<Midi_def*> ($$);
792                 if (md)
793                         md->set_tempo (d->get_length (), m);
794                 scm_gc_unprotect_object ($2->self_scm ());
795         }
796         | music_output_def_body error {
797
798         }
799         ;
800
801 tempo_event:
802         TEMPO steno_duration '=' bare_unsigned  {
803                 $$ = MY_MAKE_MUSIC ("MetronomeChangeEvent");
804                 $$->set_property ("tempo-unit", $2);
805                 $$->set_property ("metronome-count", scm_int2num ( $4));
806         }
807         ;
808
809 /*
810 The representation of a  list is the
811
812   (LIST . LAST-CONS)
813
814  to have  efficient append.
815 */
816 Music_list:
817         /* empty */ {
818                 $$ = scm_cons (SCM_EOL, SCM_EOL);
819         }
820         | Music_list Music {
821                 SCM s = $$;
822                 SCM c = scm_cons ($2->self_scm (), SCM_EOL);
823                 scm_gc_unprotect_object ($2->self_scm ()); /* UGH */
824                 if (ly_c_pair_p (ly_cdr (s)))
825                         scm_set_cdr_x (ly_cdr (s), c); /* append */
826                 else
827                         scm_set_car_x (s, c); /* set first cons */
828                 scm_set_cdr_x (s, c);  /* remember last cell */
829         }
830         | Music_list embedded_scm {
831         }
832         | Music_list error {
833         }
834         ;
835
836
837 Music:
838         Simple_music
839         | Composite_music
840         ;
841
842 Alternative_music:
843         /* empty */ {
844                 $$ = SCM_EOL;
845         }
846         | ALTERNATIVE '{' Music_list '}' {
847                 $$ = $3;
848         }
849         ;
850
851
852 Repeated_music:
853         REPEAT simple_string bare_unsigned Music Alternative_music
854         {
855                 Music *beg = $4;
856                 int times = $3;
857                 SCM alts = ly_c_pair_p ($5) ? ly_car ($5) : SCM_EOL;
858                 if (times < scm_ilength (alts)) {
859                   unsmob_music (ly_car (alts))
860                     ->origin ()->warning (
861                     _ ("More alternatives than repeats.  Junking excess alternatives."));
862                   alts = ly_truncate_list (times, alts);
863                 }
864
865
866                 SCM proc = ly_scheme_function ("make-repeated-music");
867
868                 SCM mus = scm_call_1 (proc, $2);
869                 scm_gc_protect_object (mus); // UGH.
870                 Music *r = unsmob_music (mus);
871                 if (beg)
872                         {
873                         r-> set_property ("element", beg->self_scm ());
874                         scm_gc_unprotect_object (beg->self_scm ());
875                         }
876                 r->set_property ("repeat-count", scm_int2num (times >? 1));
877
878                 r-> set_property ("elements",alts);
879                 if (ly_c_equal_p ($2, scm_makfrom0str ("tremolo"))) {
880                         /*
881                         TODO: move this code to Scheme.
882                         */
883
884                         /*
885                         we can not get durations and other stuff correct down the line, so we have to
886                         add to the duration log here.
887                         */
888                         SCM func = ly_scheme_function ("shift-duration-log");
889
890                         int dots = ($3 % 3) ? 0 : 1;
891                         int shift = -intlog2 ((dots) ? ($3*2/3) : $3);
892
893                         Sequential_music *seq = dynamic_cast<Sequential_music*> ($4);
894                         
895                         if (seq) {
896                                 int list_len = scm_ilength (seq->music_list ());
897                                 if (list_len != 2)
898                                         seq->origin ()->warning ("Chord tremolo must have 2 elements.");
899                                 shift -= 1;
900                                 r->compress (Moment (Rational (1, list_len)));
901                                 }
902                         scm_call_3 (func, r->self_scm (), scm_int2num (shift),scm_int2num (dots));
903
904                 }
905                 r->set_spot (*$4->origin ());
906
907                 $$ = r;
908         }
909         ;
910
911 Sequential_music:
912         SEQUENTIAL '{' Music_list '}'           {
913                 $$ = MY_MAKE_MUSIC ("SequentialMusic");
914                 $$->set_property ("elements", ly_car ($3));
915                 $$->set_spot (THIS->here_input ());
916         }
917         | '{' Music_list '}'            {
918                 $$ = MY_MAKE_MUSIC ("SequentialMusic");
919                 $$->set_property ("elements", ly_car ($2));
920                 $$->set_spot (THIS->here_input ());
921         }
922         ;
923
924 Simultaneous_music:
925         SIMULTANEOUS '{' Music_list '}'{
926                 $$ = MY_MAKE_MUSIC ("SimultaneousMusic");
927                 $$->set_property ("elements", ly_car ($3));
928                 $$->set_spot (THIS->here_input ());
929
930         }
931         | simul_open Music_list simul_close     {
932                 $$ = MY_MAKE_MUSIC ("SimultaneousMusic");
933                 $$->set_property ("elements", ly_car ($2));
934                 $$->set_spot (THIS->here_input ());
935         }
936         ;
937
938 Simple_music:
939         event_chord             { $$ = $1; }
940         | MUSIC_IDENTIFIER {
941                 $$ = unsmob_music ($1);
942         }
943         | music_property_def
944         | context_change
945         ;
946
947
948 optional_context_mod:
949         /**/ { $$ = SCM_EOL; }
950         | WITH { THIS->lexer_->push_initial_state (); }
951         '{' context_mod_list '}'
952         {
953                 THIS->lexer_->pop_state ();
954                 $$ = $4;
955         }
956         ;
957
958 context_mod_list:
959         /* */  { $$ = SCM_EOL; }
960         | context_mod_list context_mod  {
961                  $$ = scm_cons ($2, $1);
962         }
963         ;
964
965
966 Composite_music:
967         Prefix_composite_music { $$ = $1; }
968         | Grouped_music_list { $$ = $1; }
969         ;
970
971 Grouped_music_list:
972         Simultaneous_music              { $$ = $1; }
973         | Sequential_music              { $$ = $1; }
974         ;
975
976 Generic_prefix_music_scm:
977         MUSIC_FUNCTION {
978                 $$ = scm_list_2 ($1, make_input (THIS->here_input ()));
979         }
980         | MUSIC_FUNCTION_SCM {
981                 THIS->push_spot ();
982         } embedded_scm {
983                 $$ = scm_list_3 ($1, make_input (THIS->pop_spot ()), $3);
984         }
985         | MUSIC_FUNCTION_MUSIC {
986                 THIS->push_spot (); 
987         } Music {
988                 $$ = scm_list_3 ($1, make_input (THIS->pop_spot ()), $3->self_scm ());
989                 scm_gc_unprotect_object ($3->self_scm ());
990         }
991         | MUSIC_FUNCTION_SCM_MUSIC {
992                 THIS->push_spot (); 
993         }  embedded_scm Music {
994                 $$ = scm_list_4 ($1, make_input (THIS->pop_spot ()), $3, $4->self_scm ());
995                 scm_gc_unprotect_object ($4->self_scm ());
996         }
997         | MUSIC_FUNCTION_MUSIC_MUSIC {
998                 THIS->push_spot (); 
999         }  Music  Music {
1000                 $$ = scm_list_4 ($1, make_input (THIS->pop_spot ()), $3->self_scm (), $4->self_scm ());
1001                 scm_gc_unprotect_object ($3->self_scm ());
1002                 scm_gc_unprotect_object ($4->self_scm ());
1003         }
1004         | MUSIC_FUNCTION_SCM_MUSIC_MUSIC {
1005                 THIS->push_spot (); 
1006         } embedded_scm Music Music {
1007                 $$ = scm_list_5 ($1, make_input (THIS->pop_spot ()),
1008                         $3, $4->self_scm (), $5->self_scm ());
1009                 scm_gc_unprotect_object ($5->self_scm ());
1010                 scm_gc_unprotect_object ($4->self_scm ());
1011         }
1012         ;
1013
1014 Generic_prefix_music:
1015         Generic_prefix_music_scm {
1016                 SCM func = ly_car ($1);
1017                 Input *loc = unsmob_input (ly_cadr ($1));
1018                 SCM args = ly_cddr ($1);
1019                 SCM sig = scm_object_property (func, ly_symbol2scm ("music-function-signature"));
1020                 int k = 0;
1021                 bool ok  = true; 
1022                 for (SCM s = sig, t = args;
1023                         ok && ly_c_pair_p (s) && ly_c_pair_p (t);
1024                         s = ly_cdr (s), t = ly_cdr (t)) {
1025                         k++;
1026                         if (scm_call_1 (ly_car (s), ly_car (t)) != SCM_BOOL_T)
1027                         {
1028                                 loc->error (_f ("Argument %d failed typecheck", k));
1029                                 THIS->error_level_ = 1;
1030                                 ok = false;
1031                         }
1032                 }
1033                 SCM m = SCM_EOL;
1034                 if (ok)
1035                         m = scm_apply_0 (func, ly_cdr ($1));
1036                 if (unsmob_music (m))
1037                         {
1038                         $$ = unsmob_music (m);
1039                         scm_gc_protect_object (m);
1040                         }
1041                 else 
1042                         {
1043                         if (ok)
1044                                 loc->error (_ ("Music head function should return Music object.")); 
1045                         $$ = MY_MAKE_MUSIC ("Music");
1046                         }
1047                 $$->set_spot (*loc);
1048         }
1049         ;
1050
1051
1052 Prefix_composite_music:
1053         Generic_prefix_music {
1054                 $$ = $1;
1055         }
1056         | CONTEXT simple_string '=' simple_string optional_context_mod Music {
1057                 $$ = context_spec_music ($2, $4, $6, $5);
1058
1059         }
1060         | CONTEXT simple_string optional_context_mod Music {
1061                 $$ = context_spec_music ($2, SCM_UNDEFINED, $4, $3);
1062         }
1063         | NEWCONTEXT simple_string optional_context_mod Music {
1064                 $$ = context_spec_music ($2, get_next_unique_context (), $4,
1065                         $3);
1066         }
1067
1068         | TIMES {
1069                 THIS->push_spot ();
1070         }
1071         /* CONTINUED */
1072                 fraction Music  
1073
1074         {
1075                 int n = ly_scm2int (ly_car ($3)); int d = ly_scm2int (ly_cdr ($3));
1076                 Music *mp = $4;
1077
1078                 $$= MY_MAKE_MUSIC ("TimeScaledMusic");
1079                 $$->set_spot (THIS->pop_spot ());
1080
1081                 $$->set_property ("element", mp->self_scm ());
1082                 scm_gc_unprotect_object (mp->self_scm ());
1083                 $$->set_property ("numerator", scm_int2num (n));
1084                 $$->set_property ("denominator", scm_int2num (d));
1085                 $$->compress (Moment (Rational (n,d)));
1086
1087         }
1088         | Repeated_music                { $$ = $1; }
1089         | TRANSPOSE pitch_also_in_chords pitch_also_in_chords Music {
1090                 $$ = MY_MAKE_MUSIC ("TransposedMusic");
1091                 Music *p = $4;
1092                 Pitch from = *unsmob_pitch ($2);
1093                 Pitch to = *unsmob_pitch ($3);
1094
1095                 p->transpose (interval (from, to));
1096                 $$->set_property ("element", p->self_scm ());
1097                 scm_gc_unprotect_object (p->self_scm ());
1098         }
1099         | NOTES
1100                 {
1101                 SCM nn = THIS->lexer_->lookup_identifier ("pitchnames");
1102                 THIS->lexer_->push_note_state (alist_to_hashq (nn));
1103         }
1104         Music
1105                 { $$ = $3;
1106                   THIS->lexer_->pop_state ();
1107                 }
1108         | DRUMS
1109                 {
1110                 SCM nn = THIS->lexer_->lookup_identifier ("drumPitchNames");
1111                 THIS->lexer_->push_note_state (alist_to_hashq (nn));
1112         }
1113         /* FIXME: This used to be: */
1114         Music
1115 /*      Grouped_music_list */
1116                 { $$ = $3;
1117                   THIS->lexer_->pop_state ();
1118                 }
1119         | FIGURES
1120                 { THIS->lexer_->push_figuredbass_state (); }
1121         /* FIXME: This used to be:
1122         Music
1123         but that breaks web build
1124         */
1125         Grouped_music_list
1126                 {
1127                   Music *chm = MY_MAKE_MUSIC ("UntransposableMusic");
1128                   chm->set_property ("element", $3->self_scm ());
1129                   $$ = chm;
1130                   scm_gc_unprotect_object ($3->self_scm ());
1131
1132                   THIS->lexer_->pop_state ();
1133         }
1134         | CHORDS {
1135                 SCM nn = THIS->lexer_->lookup_identifier ("chordmodifiers");
1136                 THIS->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
1137                 nn = THIS->lexer_->lookup_identifier ("pitchnames");
1138                 THIS->lexer_->push_chord_state (alist_to_hashq (nn));
1139
1140         }
1141         /* FIXME:
1142         Music
1143 */
1144         Grouped_music_list
1145         {
1146                   Music *chm = MY_MAKE_MUSIC ("UnrelativableMusic");
1147                   chm->set_property ("element", $3->self_scm ());
1148                   scm_gc_unprotect_object ($3->self_scm ());
1149                   $$ = chm;
1150
1151                   THIS->lexer_->pop_state ();
1152         }
1153         | LYRICS
1154                 { THIS->lexer_->push_lyric_state (); }
1155         /* FIXME:
1156         Music
1157 */
1158         Grouped_music_list
1159                 {
1160                   $$ = $3;
1161                   THIS->lexer_->pop_state ();
1162         }
1163         | relative_music        { $$ = $1; }
1164         | re_rhythmed_music     { $$ = $1; }
1165         | TAG embedded_scm Music {
1166                 tag_music ($3, $2, THIS->here_input ());
1167                 $$ = $3;
1168         }
1169         ;
1170
1171 relative_music:
1172         RELATIVE absolute_pitch Music {
1173                 Music *m = $3;
1174                 Pitch start = *unsmob_pitch ($2);
1175                 $$ = make_music_relative (start, m);
1176                 scm_gc_unprotect_object (m->self_scm ());
1177         }
1178         | RELATIVE Composite_music {
1179                 Music *m = $2;
1180                 /* FIXME: why is octave==0 and default not middleC? */
1181                 Pitch middle_c (-1, 0, 0);
1182                 $$ = make_music_relative (middle_c, m);
1183                 scm_gc_unprotect_object (m->self_scm ());
1184         }
1185         ;
1186
1187 new_lyrics:
1188         NEWLYRICS { THIS->lexer_->push_lyric_state (); }
1189         /*cont */
1190         Grouped_music_list {
1191         /* Can also use Music at the expensive of two S/Rs similar to
1192            \repeat \alternative */
1193                 THIS->lexer_->pop_state ();
1194 #if 0
1195                 Music *music = MY_MAKE_MUSIC ("SimultaneousMusic");
1196                 music->set_property ("elements", scm_list_1 ($3->self_scm ()));
1197                 $$ = music;
1198 #else
1199                 $$ = scm_cons ($3->self_scm (), SCM_EOL);
1200 #endif
1201         }
1202         | new_lyrics NEWLYRICS { THIS->lexer_->push_lyric_state (); }
1203         Grouped_music_list {
1204                 THIS->lexer_->pop_state ();
1205                 $$ = scm_cons ($4->self_scm (), $1);
1206         }
1207         ;
1208
1209 re_rhythmed_music:
1210         Grouped_music_list new_lyrics {
1211
1212                 /* FIXME: should find out uniqueXXX name from music */
1213                 SCM name = $1->get_property ("context-id");
1214                 //if (name == SCM_EOL)
1215                 if (!ly_c_string_p (name))
1216                         name = scm_makfrom0str ("");
1217
1218                 SCM context = scm_makfrom0str ("Lyrics");
1219                 Music *all = MY_MAKE_MUSIC ("SimultaneousMusic");
1220
1221                 SCM lst = SCM_EOL;
1222                 for (SCM s = $2; ly_c_pair_p (s); s = ly_cdr (s))
1223                 {
1224                         Music *music = unsmob_music (ly_car (s));
1225                         Music *com = make_lyric_combine_music (name, music);
1226                         Music *csm = context_spec_music (context,
1227                                 get_next_unique_context (), com, SCM_EOL);
1228                         lst = scm_cons (csm->self_scm (), lst);
1229                 }
1230                 /* FIXME: only first lyric music is accepted,
1231                           the rest is junked */
1232                 all->set_property ("elements", scm_cons ($1->self_scm (),
1233                         lst));
1234                 $$ = all;
1235                 scm_gc_unprotect_object ($1->self_scm ());
1236         }
1237         | LYRICSTO string Music {
1238                 Music *music = $3;
1239                 SCM name = $2;
1240                 $$ = make_lyric_combine_music (name, music);
1241                 scm_gc_unprotect_object (music->self_scm ());
1242         }
1243         ;
1244
1245 context_change:
1246         CHANGE STRING '=' STRING  {
1247                 Music*t= MY_MAKE_MUSIC ("ContextChange");
1248                 t-> set_property ("change-to-type", scm_string_to_symbol ($2));
1249                 t-> set_property ("change-to-id", $4);
1250
1251                 $$ = t;
1252                 $$->set_spot (THIS->here_input ());
1253         }
1254         ;
1255
1256 property_operation:
1257         STRING '=' scalar {
1258                 $$ = scm_list_3 (ly_symbol2scm ("assign"),
1259                         scm_string_to_symbol ($1), $3);
1260         }
1261         | UNSET simple_string {
1262                 $$ = scm_list_2 (ly_symbol2scm ("unset"),
1263                         scm_string_to_symbol ($2));
1264         }
1265         | OVERRIDE simple_string embedded_scm '=' embedded_scm {
1266                 $$ = scm_list_4 (ly_symbol2scm ("push"),
1267                         scm_string_to_symbol ($2), $3, $5);
1268         }
1269         | REVERT simple_string embedded_scm {
1270                 $$ = scm_list_3 (ly_symbol2scm ("pop"),
1271                         scm_string_to_symbol ($2), $3);
1272         }
1273         ;
1274
1275 context_def_mod:
1276         CONSISTSEND { $$ = ly_symbol2scm ("consists-end"); }
1277         | CONSISTS { $$ = ly_symbol2scm ("consists"); }
1278         | REMOVE { $$ = ly_symbol2scm ("remove"); }
1279
1280         | ACCEPTS { $$ = ly_symbol2scm ("accepts"); }
1281         | DENIES { $$ = ly_symbol2scm ("denies"); }
1282
1283         | ALIAS { $$ = ly_symbol2scm ("alias"); }
1284         | TYPE { $$ = ly_symbol2scm ("translator-type"); }
1285         | DESCRIPTION { $$ = ly_symbol2scm ("description"); }
1286         | NAME { $$ = ly_symbol2scm ("context-name"); }
1287         ;
1288
1289 context_mod:
1290         property_operation { $$ = $1; }
1291         | context_def_mod STRING {
1292                 $$ = scm_list_2 ($1, $2);
1293         }
1294         ;
1295
1296 context_prop_spec:
1297         simple_string {
1298                 $$ = scm_list_2 (ly_symbol2scm ("Bottom"),
1299                         scm_string_to_symbol ($1));
1300         }
1301         | simple_string '.' simple_string {
1302                 $$ = scm_list_2 (scm_string_to_symbol ($1),
1303                         scm_string_to_symbol ($3));
1304         }
1305         ;
1306
1307 music_property_def:
1308         OVERRIDE context_prop_spec embedded_scm '=' scalar {
1309                 $$ = property_op_to_music (scm_list_4 (
1310                         ly_symbol2scm ("poppush"),
1311                         ly_cadr ($2),
1312                         $3, $5));
1313                 $$= context_spec_music (ly_car ($2), SCM_UNDEFINED, $$, SCM_EOL);
1314         }
1315         | REVERT context_prop_spec embedded_scm {
1316                 $$ = property_op_to_music (scm_list_3 (
1317                         ly_symbol2scm ("pop"),
1318                         ly_cadr ($2),
1319                         $3));
1320
1321                 $$= context_spec_music (ly_car ($2), SCM_UNDEFINED, $$, SCM_EOL);
1322         }
1323         | SET context_prop_spec '=' scalar {
1324                 $$ = property_op_to_music (scm_list_3 (
1325                         ly_symbol2scm ("assign"),
1326                         ly_cadr ($2),
1327                         $4));
1328                 $$= context_spec_music (ly_car ($2), SCM_UNDEFINED, $$, SCM_EOL);
1329         }
1330         | UNSET context_prop_spec {
1331                 $$ = property_op_to_music (scm_list_2 (
1332                         ly_symbol2scm ("unset"),
1333                         ly_cadr ($2)));
1334                 $$= context_spec_music (ly_car ($2), SCM_UNDEFINED, $$, SCM_EOL);
1335         }
1336         | ONCE music_property_def {
1337                 SCM e = $2->get_property ("element");
1338                 unsmob_music (e)->set_property ("once", SCM_BOOL_T);
1339                 $$ = $2;
1340
1341         }
1342         ;
1343
1344
1345 string:
1346         STRING {
1347                 $$ = $1;
1348         }
1349         | STRING_IDENTIFIER {
1350                 $$ = $1;
1351         }
1352         | string '+' string {
1353                 $$ = scm_string_append (scm_list_2 ($1, $3));
1354         }
1355         ;
1356
1357 simple_string: STRING {
1358         }
1359         | LYRICS_STRING {
1360         }
1361         ;
1362
1363 scalar: string {
1364         }
1365         | LYRICS_STRING {
1366         }
1367         | bare_int {
1368                 $$ = scm_int2num ($1);
1369         }
1370         | embedded_scm {
1371         }
1372         | full_markup {
1373         }
1374         | DIGIT {
1375                 $$ = scm_int2num ($1);
1376         }
1377         ;
1378
1379 /*
1380 FIXME: remove or fix this comment.  What is `This'?
1381
1382 This is a trick:
1383
1384 Adding pre_events to the simple_element
1385 makes the choice between
1386
1387   string:  STRING
1388
1389 and
1390
1391   simple_element: STRING
1392
1393 a single shift/reduction conflict.
1394
1395 nevertheless, this is not very clean, and we should find a different
1396 solution.
1397
1398 */
1399 pre_events: {
1400                 THIS->push_spot ();
1401         }
1402         ;
1403
1404 event_chord:
1405         pre_events simple_element post_events   {
1406                 SCM elts = $2-> get_property ("elements");
1407
1408                 elts = ly_append2 (elts, scm_reverse_x ($3, SCM_EOL));
1409
1410                 $2->set_property ("elements", elts);
1411                 $$ = $2;
1412         }
1413         | command_element
1414         | note_chord_element
1415         ;
1416
1417
1418 note_chord_element:
1419         chord_body optional_notemode_duration post_events
1420         {
1421                 SCM dur = unsmob_duration ($2)->smobbed_copy ();
1422                 SCM es = $1->get_property ("elements");
1423                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
1424
1425                 for (SCM s = es; ly_c_pair_p (s); s = ly_cdr (s))
1426                   unsmob_music (ly_car (s))->set_property ("duration", dur);
1427                 es = ly_append2 (es, postevs);
1428
1429                 $1-> set_property ("elements", es);
1430                 $$ = $1;
1431         }
1432         ;
1433
1434 chord_open: '<'
1435         ;
1436
1437 chord_close: '>'
1438         ;
1439
1440 simul_open: DOUBLE_ANGLE_OPEN
1441         ;
1442
1443 simul_close: DOUBLE_ANGLE_CLOSE
1444         ;
1445
1446 chord_body:
1447         chord_open chord_body_elements chord_close
1448         {
1449                 $$ = MY_MAKE_MUSIC ("EventChord");
1450                 $$->set_property ("elements",
1451                         scm_reverse_x ($2, SCM_EOL));
1452         }
1453         ;
1454
1455 chord_body_elements:
1456         /* empty */             { $$ = SCM_EOL; }
1457         | chord_body_elements chord_body_element {
1458                 $$ = scm_cons ($2->self_scm (), $1);
1459                 scm_gc_unprotect_object ($2->self_scm ());
1460         }
1461         ;
1462
1463 chord_body_element:
1464         pitch exclamations questions post_events
1465         {
1466                 Music *n = MY_MAKE_MUSIC ("NoteEvent");
1467                 n->set_property ("pitch", $1);
1468                 if ($3 % 2)
1469                         n->set_property ("cautionary", SCM_BOOL_T);
1470                 if ($2 % 2 || $3 % 2)
1471                         n->set_property ("force-accidental", SCM_BOOL_T);
1472
1473                 if (ly_c_pair_p ($4)) {
1474                         SCM arts = scm_reverse_x ($4, SCM_EOL);
1475                         n->set_property ("articulations", arts);
1476                 }
1477                 $$ = n;
1478         }
1479         | DRUM_PITCH post_events {
1480                 Music *n = MY_MAKE_MUSIC ("NoteEvent");
1481                 n->set_property ("duration", $2);
1482                 n->set_property ("drum-type", $1);
1483                 n->set_spot (THIS->here_input ());
1484
1485                 if (ly_c_pair_p ($2)) {
1486                         SCM arts = scm_reverse_x ($2, SCM_EOL);
1487                         n->set_property ("articulations", arts);
1488                 }
1489                 $$ = n;
1490         }
1491         ;
1492
1493 add_quote:
1494         ADDQUOTE string Music {
1495                 SCM adder = ly_scheme_function ("add-quotable");
1496                 
1497                 scm_call_2 (adder, $2, $3->self_scm ());
1498                 scm_gc_unprotect_object ($3->self_scm ());
1499         }
1500         ;
1501
1502 command_element:
1503         command_req {
1504                 $$ = MY_MAKE_MUSIC ("EventChord");
1505                 $$->set_property ("elements", scm_cons ($1->self_scm (), SCM_EOL));
1506                 scm_gc_unprotect_object ($1->self_scm ());
1507
1508                 $$-> set_spot (THIS->here_input ());
1509                 $1-> set_spot (THIS->here_input ());
1510         }
1511         | SKIP duration_length {
1512                 Music *skip = MY_MAKE_MUSIC ("SkipMusic");
1513                 skip->set_property ("duration", $2);
1514
1515                 $$ = skip;
1516         }
1517         | QUOTE STRING duration_length {
1518                 SCM tab = THIS->lexer_->lookup_identifier ("musicQuotes");
1519                 SCM evs = SCM_EOL;
1520                 if (scm_hash_table_p (tab) == SCM_BOOL_T)
1521                 {
1522                         SCM key = $2; // use symbol?
1523                         evs = scm_hash_ref (tab, key, SCM_BOOL_F);
1524                 }
1525                 Music *quote = 0;
1526                 if (ly_c_vector_p (evs))
1527                 {
1528                         quote = MY_MAKE_MUSIC ("QuoteMusic");
1529                         quote->set_property ("duration", $3);
1530                         quote->set_property ("quoted-events", evs);
1531                 } else {
1532                         THIS->here_input ().warning (_f ("Can\'t find music"));
1533                         quote = MY_MAKE_MUSIC ("Event");
1534                 }
1535                 quote->set_spot (THIS->here_input ());
1536                 $$ = quote;
1537         }
1538         | OCTAVE { THIS->push_spot (); }
1539           pitch {
1540                 Music *m = MY_MAKE_MUSIC ("RelativeOctaveCheck");
1541                 $$ = m;
1542                 $$->set_spot (THIS->pop_spot ());
1543                 $$->set_property ("pitch", $3);
1544         }
1545         | E_LEFTSQUARE {
1546                 Music *m = MY_MAKE_MUSIC ("LigatureEvent");
1547                 m->set_property ("span-direction", scm_int2num (START));
1548                 m->set_spot (THIS->here_input ());
1549
1550                 $$ = MY_MAKE_MUSIC ("EventChord");
1551                 $$->set_property ("elements", scm_cons (m->self_scm (), SCM_EOL));
1552                 scm_gc_unprotect_object (m->self_scm ());
1553                 $$->set_spot (THIS->here_input ());
1554         }
1555         | E_RIGHTSQUARE {
1556                 Music *m = MY_MAKE_MUSIC ("LigatureEvent");
1557                 m->set_property ("span-direction", scm_int2num (STOP));
1558                 m->set_spot (THIS->here_input ());
1559
1560                 $$ = MY_MAKE_MUSIC ("EventChord");
1561                 $$->set_property ("elements", scm_cons (m->self_scm (), SCM_EOL));
1562                 $$->set_spot (THIS->here_input ());
1563                 scm_gc_unprotect_object (m->self_scm ());
1564         }
1565         | E_BACKSLASH {
1566                 $$ = MY_MAKE_MUSIC ("VoiceSeparator");
1567                 $$->set_spot (THIS->here_input ());
1568         }
1569         | '|'      {
1570                 SCM pipe =THIS->lexer_->lookup_identifier ("pipeSymbol");
1571
1572                 if (Music * m = unsmob_music (pipe))
1573                         $$ = m->clone ();
1574                 else
1575                         $$ = MY_MAKE_MUSIC ("BarCheck");
1576
1577                 $$->set_spot (THIS->here_input ());
1578         }
1579         | TRANSPOSITION pitch {
1580                 $$ = set_property_music (ly_symbol2scm ("instrumentTransposition"),
1581                                         $2);
1582                 $$->set_spot (THIS-> here_input ());
1583                 $$ = context_spec_music (ly_symbol2scm ("Staff"), SCM_UNDEFINED,
1584                         $$, SCM_EOL);
1585         }
1586         | BAR STRING                    {
1587                 Music *t = set_property_music (ly_symbol2scm ("whichBar"), $2);
1588
1589                 Music *csm = context_spec_music (ly_symbol2scm ("Timing"), SCM_UNDEFINED,
1590                                         t, SCM_EOL);
1591                 $$ = context_spec_music (ly_symbol2scm ("Score"), SCM_UNDEFINED, csm, SCM_EOL);
1592                 $$->set_spot (THIS->here_input ());
1593                 t->set_spot (THIS->here_input ());
1594         }
1595         | PARTIAL duration_length       {
1596                 Moment m = - unsmob_duration ($2)->get_length ();
1597                 Music *p = set_property_music (ly_symbol2scm ( "measurePosition"),m.smobbed_copy ());
1598                 p->set_spot (THIS->here_input ());
1599                 p = context_spec_music (ly_symbol2scm ("Timing"), SCM_UNDEFINED,
1600                                         p, SCM_EOL);
1601                 p = context_spec_music (ly_symbol2scm ("Score"), SCM_UNDEFINED,
1602                                         p, SCM_EOL);
1603                 $$ = p;
1604         }
1605         | CLEF STRING  {
1606                 SCM proc = ly_scheme_function ("make-clef-set");
1607
1608                 SCM result = scm_call_1 (proc, $2);
1609                 scm_gc_protect_object (result);
1610                 $$ = unsmob_music (result);
1611         }
1612         | TIME_T fraction  {
1613                 SCM proc= ly_scheme_function ("make-time-signature-set");
1614
1615                 SCM result = scm_apply_2   (proc, ly_car ($2), ly_cdr ($2), SCM_EOL);
1616                 scm_gc_protect_object (result);
1617                 $$ = unsmob_music (result);
1618         }
1619         | MARK scalar {
1620                 SCM proc = ly_scheme_function ("make-mark-set");
1621
1622                 SCM result = scm_call_1 (proc, $2);
1623                 scm_gc_protect_object (result);
1624                 $$ = unsmob_music (result);
1625         }
1626         ;
1627
1628 command_req:
1629         E_TILDE {
1630                 $$ = MY_MAKE_MUSIC ("PesOrFlexaEvent");
1631         }
1632         | MARK DEFAULT  {
1633                 Music *m = MY_MAKE_MUSIC ("MarkEvent");
1634                 $$ = m;
1635         }
1636         | tempo_event {
1637                 $$ = $1;
1638         }
1639         | KEY DEFAULT {
1640                 Music *key= MY_MAKE_MUSIC ("KeyChangeEvent");
1641                 $$ = key;
1642         }
1643         | KEY NOTENAME_PITCH SCM_IDENTIFIER     {
1644
1645                 Music *key= MY_MAKE_MUSIC ("KeyChangeEvent");
1646                 if (scm_ilength ($3) > 0)
1647                 {               
1648                         key->set_property ("pitch-alist", $3);
1649                         key->set_property ("tonic", Pitch (0,0,0).smobbed_copy ());
1650                         ((Music*)key)->transpose (* unsmob_pitch ($2));
1651                 } else {
1652                         THIS->parser_error (_ ("Second argument must be pitch list."));
1653                 }
1654
1655                 $$ = key;
1656         }
1657         ;
1658
1659 post_events:
1660         /* empty */ {
1661                 $$ = SCM_EOL;
1662         }
1663         | post_events post_event {
1664                 $2->set_spot (THIS->here_input ());
1665                 $$ = scm_cons ($2->self_scm (), $$);
1666                 scm_gc_unprotect_object ($2->self_scm ());
1667         }
1668         | post_events tagged_post_event {
1669                 $2 -> set_spot (THIS->here_input ());
1670                 $$ = scm_cons ($2->self_scm (), $$);
1671                 scm_gc_unprotect_object ($2->self_scm ());
1672         }
1673         ;
1674
1675
1676 tagged_post_event:
1677         '-' TAG embedded_scm post_event {
1678                 tag_music ($4, $3, THIS->here_input ());
1679                 $$ = $4;
1680         }
1681         ;
1682
1683 post_event:
1684         direction_less_event {
1685                 $$ = $1;
1686         }
1687         | HYPHEN {
1688                 if (!THIS->lexer_->is_lyric_state ())
1689                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
1690                 $$ = MY_MAKE_MUSIC ("HyphenEvent");
1691         }
1692         | EXTENDER {
1693                 if (!THIS->lexer_->is_lyric_state ())
1694                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
1695                 $$ = MY_MAKE_MUSIC ("ExtenderEvent");
1696         }
1697         | script_dir direction_reqd_event {
1698                 if ($1)
1699                         $2->set_property ("direction", scm_int2num ($1));
1700                 $$ = $2;
1701         }
1702         | script_dir direction_less_event {
1703                 if ($1)
1704                         $2->set_property ("direction", scm_int2num ($1));
1705                 $$ = $2;
1706         }
1707         | string_number_event
1708         ;
1709
1710 string_number_event:
1711         E_UNSIGNED {
1712                 Music *s = MY_MAKE_MUSIC ("StringNumberEvent");
1713                 s->set_property ("string-number", scm_int2num ($1));
1714                 s->set_spot (THIS->here_input ());
1715                 $$ = s;
1716         }
1717         ;
1718
1719
1720 direction_less_event:
1721         '['  {
1722
1723
1724 /*
1725
1726 TODO: should take all these defs out of the parser, adn make use
1727 configurable, i.e.
1728
1729
1730 (set-articulation '~ "trill")
1731
1732 */
1733                 Music *m = MY_MAKE_MUSIC ("BeamEvent");
1734                 m->set_spot (THIS->here_input ());
1735                 m->set_property ("span-direction", scm_int2num (START));
1736                 $$ = m;
1737         }
1738         | ']'  {
1739                 Music *m = MY_MAKE_MUSIC ("BeamEvent");
1740                 m->set_spot (THIS->here_input ());
1741                 m->set_property ("span-direction", scm_int2num (STOP));
1742                 $$ = m;
1743         }
1744         | '~' {
1745                 Music *m = MY_MAKE_MUSIC ("TieEvent");
1746                 m->set_spot (THIS->here_input ());
1747                 $$ = m;
1748         }
1749         | close_event {
1750                 $$ = $1;
1751                 dynamic_cast<Music *> ($$)->set_property ("span-direction",
1752                         scm_int2num (START));
1753         }
1754         | open_event {
1755                 $$ = $1;
1756                 dynamic_cast<Music *> ($$)->set_property ("span-direction",
1757                         scm_int2num (STOP));
1758         }
1759         | EVENT_IDENTIFIER      {
1760                 $$ = unsmob_music ($1);
1761         }
1762         | tremolo_type  {
1763                Music *a = MY_MAKE_MUSIC ("TremoloEvent");
1764                a->set_spot (THIS->here_input ());
1765                a->set_property ("tremolo-type", scm_int2num ($1));
1766                $$ = a;
1767         }
1768         ;       
1769         
1770 direction_reqd_event:
1771         gen_text_def {
1772                 $$ = $1;
1773         }
1774         | script_abbreviation {
1775                 SCM s = THIS->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
1776                 Music *a = MY_MAKE_MUSIC ("ArticulationEvent");
1777                 if (ly_c_string_p (s))
1778                         a->set_property ("articulation-type", s);
1779                 else THIS->parser_error (_ ("Expecting string as script definition"));
1780                 $$ = a;
1781         }
1782         ;
1783
1784 oct_check:
1785         /**/ { $$ = SCM_EOL; }
1786         | '='  { $$ = scm_int2num (0); }
1787         | '=' sub_quotes { $$ = scm_int2num ($2); }
1788         | '=' sup_quotes { $$ = scm_int2num ($2); }
1789         ;
1790
1791 sup_quotes:
1792         '\'' {
1793                 $$ = 1;
1794         }
1795         | sup_quotes '\'' {
1796                 $$ ++;
1797         }
1798         ;
1799
1800 sub_quotes:
1801         ',' {
1802                 $$ = 1;
1803         }
1804         | sub_quotes ',' {
1805                 $$++;
1806         }
1807         ;
1808
1809 steno_pitch:
1810         NOTENAME_PITCH  {
1811                 $$ = $1;
1812         }
1813         | NOTENAME_PITCH sup_quotes     {
1814                 Pitch p = *unsmob_pitch ($1);
1815                 p = p.transposed (Pitch ($2,0,0));
1816                 $$ = p.smobbed_copy ();
1817         }
1818         | NOTENAME_PITCH sub_quotes      {
1819                 Pitch p =* unsmob_pitch ($1);
1820                 p = p.transposed (Pitch (-$2,0,0));
1821                 $$ = p.smobbed_copy ();
1822         }
1823         ;
1824
1825 /*
1826 ugh. duplication
1827 */
1828
1829 steno_tonic_pitch:
1830         TONICNAME_PITCH {
1831                 $$ = $1;
1832         }
1833         | TONICNAME_PITCH sup_quotes    {
1834                 Pitch p = *unsmob_pitch ($1);
1835                 p = p.transposed (Pitch ($2,0,0));
1836                 $$ = p.smobbed_copy ();
1837         }
1838         | TONICNAME_PITCH sub_quotes     {
1839                 Pitch p =* unsmob_pitch ($1);
1840
1841                 p = p.transposed (Pitch (-$2,0,0));
1842                 $$ = p.smobbed_copy ();
1843         }
1844         ;
1845
1846 pitch:
1847         steno_pitch {
1848                 $$ = $1;
1849         }
1850         ;
1851
1852 pitch_also_in_chords:
1853         pitch
1854         | steno_tonic_pitch
1855         ;
1856
1857 close_event:
1858         '('     {
1859                 Music *s = MY_MAKE_MUSIC ("SlurEvent");
1860                 $$ = s;
1861                 s->set_spot (THIS->here_input ());
1862         }
1863         | E_OPEN        {
1864                 Music *s = MY_MAKE_MUSIC ("PhrasingSlurEvent");
1865                 $$ = s;
1866                 s->set_spot (THIS->here_input ());
1867         }
1868         | E_SMALLER {
1869                 Music *s = MY_MAKE_MUSIC ("CrescendoEvent");
1870                 $$ = s;
1871                 s->set_spot (THIS->here_input ());
1872         }
1873         | E_BIGGER {
1874                 Music *s = MY_MAKE_MUSIC ("DecrescendoEvent");
1875                 $$ = s;
1876                 s->set_spot (THIS->here_input ());
1877         }
1878         ;
1879
1880
1881 open_event:
1882         E_EXCLAMATION   {
1883                 Music *s = MY_MAKE_MUSIC ("CrescendoEvent");
1884                 s->set_spot (THIS->here_input ());
1885
1886                 $$ = s;
1887         }
1888         | ')'   {
1889                 Music *s= MY_MAKE_MUSIC ("SlurEvent");
1890                 $$ = s;
1891                 s->set_spot (THIS->here_input ());
1892
1893         }
1894         | E_CLOSE       {
1895                 Music *s= MY_MAKE_MUSIC ("PhrasingSlurEvent");
1896                 $$ = s;
1897                 s->set_property ("span-type",
1898                         scm_makfrom0str ("phrasing-slur"));
1899                 s->set_spot (THIS->here_input ());
1900         }
1901         ;
1902
1903 gen_text_def:
1904         full_markup {
1905                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent");
1906                 t->set_property ("text", $1);
1907                 t->set_spot (THIS->here_input ());
1908                 $$ = t; 
1909         }
1910         | string {
1911                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent");
1912                 t->set_property ("text",
1913                         make_simple_markup (THIS->lexer_->encoding (), $1));
1914                 t->set_spot (THIS->here_input ());
1915                 $$ = t;
1916         
1917         }
1918         | DIGIT {
1919                 Music *t = MY_MAKE_MUSIC ("FingerEvent");
1920                 t->set_property ("digit", scm_int2num ($1));
1921                 t->set_spot (THIS->here_input ());
1922                 $$ = t;
1923         }
1924         ;
1925
1926 script_abbreviation:
1927         '^'             {
1928                 $$ = scm_makfrom0str ("Hat");
1929         }
1930         | '+'           {
1931                 $$ = scm_makfrom0str ("Plus");
1932         }
1933         | '-'           {
1934                 $$ = scm_makfrom0str ("Dash");
1935         }
1936         | '|'           {
1937                 $$ = scm_makfrom0str ("Bar");
1938         }
1939         | '>'           {
1940                 $$ = scm_makfrom0str ("Larger");
1941         }
1942         | '.'           {
1943                 $$ = scm_makfrom0str ("Dot");
1944         }
1945         | '_' {
1946                 $$ = scm_makfrom0str ("Underscore");
1947         }
1948         ;
1949
1950 script_dir:
1951         '_'     { $$ = DOWN; }
1952         | '^'   { $$ = UP; }
1953         | '-'   { $$ = CENTER; }
1954         ;
1955
1956
1957 absolute_pitch:
1958         steno_pitch     {
1959                 $$ = $1;
1960         }
1961         ;
1962
1963 duration_length:
1964         multiplied_duration {
1965                 $$ = $1;
1966         }
1967         ;
1968
1969 optional_notemode_duration:
1970         {
1971                 Duration dd = THIS->default_duration_;
1972                 $$ = dd.smobbed_copy ();
1973
1974                 THIS->beam_check ($$);
1975         }
1976         | multiplied_duration   {
1977                 $$ = $1;
1978                 THIS->default_duration_ = *unsmob_duration ($$);
1979
1980                 THIS->beam_check ($$);
1981         }
1982         ;
1983
1984 steno_duration:
1985         bare_unsigned dots              {
1986                 int len = 0;
1987                 if (!is_duration ($1))
1988                         THIS->parser_error (_f ("not a duration: %d", $1));
1989                 else
1990                         len = intlog2 ($1);
1991
1992                 $$ = Duration (len, $2).smobbed_copy ();
1993         }
1994         | DURATION_IDENTIFIER dots      {
1995                 Duration *d = unsmob_duration ($1);
1996                 Duration k (d->duration_log (), d->dot_count () + $2);
1997                 *d = k;
1998                 $$ = $1;
1999         }
2000         ;
2001
2002 multiplied_duration:
2003         steno_duration {
2004                 $$ = $1;
2005         }
2006         | multiplied_duration '*' bare_unsigned {
2007                 $$ = unsmob_duration ($$)->compressed ( $3) .smobbed_copy ();
2008         }
2009         | multiplied_duration '*' FRACTION {
2010                 Rational  m (ly_scm2int (ly_car ($3)), ly_scm2int (ly_cdr ($3)));
2011
2012                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
2013         }
2014         ;
2015
2016 fraction:
2017         FRACTION { $$ = $1; }
2018         | UNSIGNED '/' UNSIGNED {
2019                 $$ = scm_cons (scm_int2num ($1), scm_int2num ($3));
2020         }
2021         ;
2022
2023 dots:
2024         /* empty */     {
2025                 $$ = 0;
2026         }
2027         | dots '.' {
2028                 $$ ++;
2029         }
2030         ;
2031
2032 tremolo_type:
2033         ':'     {
2034                 $$ = 0;
2035         }
2036         | ':' bare_unsigned {
2037                 if (!is_duration ($2))
2038                         THIS->parser_error (_f ("not a duration: %d", $2));
2039                 $$ = $2;
2040         }
2041         ;
2042
2043 bass_number:
2044         DIGIT   {
2045                 $$ = scm_number_to_string (scm_int2num ($1), scm_int2num (10));
2046                 $$ = scm_list_2 (ly_scheme_function ("number-markup"),
2047                                 $$);
2048         }
2049         | UNSIGNED {
2050                 $$ = scm_number_to_string (scm_int2num ($1), scm_int2num (10));
2051                 $$ = scm_list_2 (ly_scheme_function ("number-markup"),
2052                                 $$);
2053         }
2054         | STRING { $$ = $1; }
2055         ;
2056
2057 bass_mod:
2058         '-'     { $$ = -2; }
2059         | '+'   { $$ = 2; }
2060         | '!'   { $$ = 0; }
2061         ;
2062
2063 bass_figure:
2064         FIGURE_SPACE {
2065                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent");
2066                 $$ = bfr->self_scm ();
2067                 scm_gc_unprotect_object ($$);
2068         }
2069         | bass_number  {
2070                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent");
2071                 $$ = bfr->self_scm ();
2072
2073                 bfr->set_property ("figure", $1);
2074
2075                 scm_gc_unprotect_object ($$);
2076         }
2077         | bass_figure bass_mod {
2078                 Music *m = unsmob_music ($1);
2079                 if ($2) {
2080                         SCM salter = m->get_property ("alteration");
2081                         int alter = ly_c_number_p (salter) ? ly_scm2int (salter) : 0;
2082                         m->set_property ("alteration",
2083                                 scm_int2num (alter + $2));
2084                 } else {
2085                         m->set_property ("alteration", scm_int2num (0));
2086                 }
2087         }
2088         ;
2089
2090 br_bass_figure:
2091         '[' bass_figure {
2092                 $$ = $2;
2093                 unsmob_music ($$)->set_property ("bracket-start", SCM_BOOL_T);
2094         }
2095         | bass_figure   {
2096                 $$ = $1;
2097         }
2098         | br_bass_figure ']' {
2099                 $$ = $1;
2100                 unsmob_music ($1)->set_property ("bracket-stop", SCM_BOOL_T);
2101         }
2102         ;
2103
2104 figure_list:
2105         /**/            {
2106                 $$ = SCM_EOL;
2107         }
2108         | figure_list br_bass_figure {
2109                 $$ = scm_cons ($2, $1);
2110         }
2111         ;
2112
2113 figure_spec:
2114         FIGURE_OPEN figure_list FIGURE_CLOSE {
2115                 Music *m = MY_MAKE_MUSIC ("EventChord");
2116                 $2 = scm_reverse_x ($2, SCM_EOL);
2117                 m->set_property ("elements", $2);
2118                 $$ = m->self_scm ();
2119         }
2120         ;
2121
2122
2123 optional_rest:
2124         /**/   { $$ = 0; }
2125         | REST { $$ = 1; }
2126         ;
2127
2128 simple_element:
2129         pitch exclamations questions oct_check optional_notemode_duration optional_rest {
2130
2131                 Input i = THIS->pop_spot ();
2132                 if (!THIS->lexer_->is_note_state ())
2133                         THIS->parser_error (_ ("Have to be in Note mode for notes"));
2134
2135                 Music *n = 0;
2136                 if ($6)
2137                         n = MY_MAKE_MUSIC ("RestEvent");
2138                 else
2139                         n = MY_MAKE_MUSIC ("NoteEvent");
2140                 
2141                 n->set_property ("pitch", $1);
2142                 n->set_property ("duration", $5);
2143
2144                 if (ly_c_number_p ($4))
2145                 {
2146                         int q = ly_scm2int ($4);
2147                         n->set_property ("absolute-octave", scm_int2num (q-1));
2148                 }
2149
2150                 if ($3 % 2)
2151                         n->set_property ("cautionary", SCM_BOOL_T);
2152                 if ($2 % 2 || $3 % 2)
2153                         n->set_property ("force-accidental", SCM_BOOL_T);
2154
2155                 Music *v = MY_MAKE_MUSIC ("EventChord");
2156                 v->set_property ("elements", scm_list_1 (n->self_scm ()));
2157                 scm_gc_unprotect_object (n->self_scm ());
2158
2159                 v->set_spot (i);
2160                 n->set_spot (i);
2161                 $$ = v;
2162         }
2163         | DRUM_PITCH optional_notemode_duration {
2164                 Input i = THIS->pop_spot ();
2165
2166                 Music *n = MY_MAKE_MUSIC ("NoteEvent");
2167                 n->set_property ("duration", $2);
2168                 n->set_property ("drum-type", $1);
2169
2170                 Music *v = MY_MAKE_MUSIC ("EventChord");
2171                 v->set_property ("elements", scm_list_1 (n->self_scm ()));
2172                 scm_gc_unprotect_object (n->self_scm ());
2173                 v->set_spot (i);
2174                 n->set_spot (i);
2175                 $$ = v;
2176                 
2177         }
2178         | figure_spec optional_notemode_duration {
2179                 Music *m = unsmob_music ($1);
2180                 Input i = THIS->pop_spot ();
2181                 m->set_spot (i);
2182                 for (SCM s = m->get_property ("elements"); ly_c_pair_p (s); s = ly_cdr (s))
2183                 {
2184                         unsmob_music (ly_car (s))->set_property ("duration", $2);
2185                 }
2186                 $$ = m;
2187         }       
2188         | RESTNAME optional_notemode_duration           {
2189
2190                 Input i = THIS->pop_spot ();
2191                 Music *ev = 0;
2192                 if (ly_scm2string ($1) == "s") {
2193                         /* Space */
2194                         ev = MY_MAKE_MUSIC ("SkipEvent");
2195                   }
2196                 else {
2197                         ev = MY_MAKE_MUSIC ("RestEvent");
2198                 
2199                     }
2200                 ev->set_property ("duration", $2);
2201                 ev->set_spot (i);
2202                 Music *velt = MY_MAKE_MUSIC ("EventChord");
2203                 velt->set_property ("elements", scm_list_1 (ev->self_scm ()));
2204                 velt->set_spot (i);
2205
2206                 scm_gc_unprotect_object (ev->self_scm ());
2207
2208                 $$ = velt;
2209         }
2210         | MULTI_MEASURE_REST optional_notemode_duration         {
2211                 THIS->pop_spot ();
2212
2213                 SCM proc = ly_scheme_function ("make-multi-measure-rest");
2214                 SCM mus = scm_call_2 (proc, $2,
2215                         make_input (THIS->here_input ()));      
2216                 scm_gc_protect_object (mus);
2217                 $$ = unsmob_music (mus);
2218         }
2219         
2220         | lyric_element optional_notemode_duration      {
2221                 Input i = THIS->pop_spot ();
2222                 if (!THIS->lexer_->is_lyric_state ())
2223                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
2224
2225                 Music *lreq = MY_MAKE_MUSIC ("LyricEvent");
2226                 lreq->set_property ("text", $1);
2227                 lreq->set_property ("duration",$2);
2228                 lreq->set_spot (i);
2229                 Music *velt = MY_MAKE_MUSIC ("EventChord");
2230                 velt->set_property ("elements", scm_list_1 (lreq->self_scm ()));
2231
2232                 $$= velt;
2233         }
2234         | new_chord {
2235                 THIS->pop_spot ();
2236
2237                 if (!THIS->lexer_->is_chord_state ())
2238                         THIS->parser_error (_ ("Have to be in Chord mode for chords"));
2239                 $$ = unsmob_music ($1);
2240         }
2241         ;
2242
2243 lyric_element:
2244         /* FIXME: lyric flavoured markup would be better */
2245         full_markup {
2246         }
2247         | LYRICS_STRING {
2248         }
2249         ;
2250
2251 new_chord:
2252         steno_tonic_pitch optional_notemode_duration   {
2253                 $$ = make_chord ($1, $2, SCM_EOL);
2254         }
2255         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
2256                 SCM its = scm_reverse_x ($4, SCM_EOL);
2257                 $$ = make_chord ($1, $2, scm_cons ($3, its));
2258         }
2259         ;
2260
2261 chord_items:
2262         /**/ {
2263                 $$ = SCM_EOL;           
2264         }
2265         | chord_items chord_item {
2266                 $$ = scm_cons ($2, $$);
2267         }
2268         ;
2269
2270 chord_separator:
2271         CHORD_COLON {
2272                 $$ = ly_symbol2scm ("chord-colon");
2273         }
2274         | CHORD_CARET {
2275                 $$ = ly_symbol2scm ("chord-caret");
2276         }
2277         | CHORD_SLASH steno_tonic_pitch {
2278                 $$ = scm_list_2 (ly_symbol2scm ("chord-slash"), $2);
2279         }
2280         | CHORD_BASS steno_tonic_pitch {
2281                 $$ = scm_list_2 (ly_symbol2scm ("chord-bass"), $2);
2282         }
2283         ;
2284
2285 chord_item:
2286         chord_separator {
2287                 $$ = $1;
2288         }
2289         | step_numbers {
2290                 $$ = scm_reverse_x ($1, SCM_EOL);
2291         }
2292         | CHORD_MODIFIER  {
2293                 $$ = $1;
2294         }
2295         ;
2296
2297 step_numbers:
2298         step_number { $$ = scm_cons ($1, SCM_EOL); }
2299         | step_numbers '.' step_number {
2300                 $$ = scm_cons ($3, $$);
2301         }
2302         ;
2303
2304 step_number:
2305         bare_unsigned {
2306                 $$ = make_chord_step ($1, 0);
2307         }
2308         | bare_unsigned '+' {
2309                 $$ = make_chord_step ($1, SHARP);
2310         }
2311         | bare_unsigned CHORD_MINUS {
2312                 $$ = make_chord_step ($1, FLAT);
2313         }
2314         ;       
2315
2316 /*
2317         UTILITIES
2318
2319 TODO: should deprecate in favor of Scheme?
2320
2321  */
2322 number_expression:
2323         number_expression '+' number_term {
2324                 $$ = scm_sum ($1, $3);
2325         }
2326         | number_expression '-' number_term {
2327                 $$ = scm_difference ($1, $3);
2328         }
2329         | number_term
2330         ;
2331
2332 number_term:
2333         number_factor {
2334                 $$ = $1;
2335         }
2336         | number_factor '*' number_factor {
2337                 $$ = scm_product ($1, $3);
2338         }
2339         | number_factor '/' number_factor {
2340                 $$ = scm_divide ($1, $3);
2341         }
2342         ;
2343
2344 number_factor:
2345         '-'  number_factor { /* %prec UNARY_MINUS */
2346                 $$ = scm_difference ($2, SCM_UNDEFINED);
2347         }
2348         | bare_number
2349         ;
2350
2351
2352 bare_number:
2353         UNSIGNED        {
2354                 $$ = scm_int2num ($1);
2355         }
2356         | REAL          {
2357                 $$ = $1;
2358         }
2359         | NUMBER_IDENTIFIER             {
2360                 $$ = $1;
2361         }
2362         | REAL NUMBER_IDENTIFIER        {
2363                 $$ = scm_make_real (ly_scm2double ($1) *ly_scm2double ($2));
2364         }
2365         | UNSIGNED NUMBER_IDENTIFIER    {
2366                 $$ = scm_make_real ($1 *ly_scm2double ($2));
2367         }
2368         ;
2369
2370
2371 bare_unsigned:
2372         UNSIGNED {
2373                         $$ = $1;
2374         }
2375         | DIGIT {
2376                 $$ = $1;
2377         }
2378         ;
2379
2380 bare_int:
2381         bare_number {
2382                 if (scm_integer_p ($1) == SCM_BOOL_T)
2383                 {
2384                         int k = ly_scm2int ($1);
2385                         $$ = k;
2386                 } else
2387                 {
2388                         THIS->parser_error (_ ("need integer number arg"));
2389                         $$ = 0;
2390                 }
2391         }
2392         | '-' bare_int {
2393                 $$ = -$2;
2394         }
2395         ;
2396
2397 exclamations:
2398                 { $$ = 0; }
2399         | exclamations '!'      { $$ ++; }
2400         ;
2401
2402 questions:
2403                 { $$ = 0; }
2404         | questions '?' { $$ ++; }
2405         ;
2406
2407
2408
2409 full_markup:
2410         MARKUP_IDENTIFIER {
2411                 $$ = $1;
2412         }
2413         | MARKUP
2414                 { THIS->lexer_->push_markup_state (); }
2415         markup
2416                 { $$ = $3;
2417                   THIS->lexer_->pop_state ();
2418                 }
2419         ;
2420
2421
2422 /*
2423 This should be done more dynamically if possible.
2424 */
2425 markup:
2426         STRING {
2427                 $$ = make_simple_markup (THIS->lexer_->encoding (), $1);
2428         }
2429         | MARKUP_HEAD_EMPTY {
2430                 $$ = scm_list_1 ($1);
2431         }
2432         | MARKUP_HEAD_MARKUP0 markup {
2433                 $$ = scm_list_2 ($1, $2);
2434         }
2435         | MARKUP_HEAD_MARKUP0_MARKUP1 markup markup {
2436                 $$ = scm_list_3 ($1, $2, $3);
2437         }
2438         | MARKUP_HEAD_SCM0_MARKUP1 SCM_T markup {
2439                 $$ = scm_list_3 ($1, $2, $3);
2440         }
2441         | markup_line {
2442                 $$ = $1;
2443         }
2444         | MARKUP_HEAD_LIST0 markup_list {
2445                 $$ = scm_list_2 ($1,$2);
2446         }
2447         | MARKUP_HEAD_SCM0 embedded_scm {
2448                 $$ = scm_list_2 ($1, $2);
2449         }
2450         | MARKUP_HEAD_SCM0_SCM1_MARKUP2 embedded_scm embedded_scm markup {
2451                 $$ = scm_list_4 ($1, $2, $3, $4);
2452         }
2453         | MARKUP_HEAD_SCM0_SCM1_SCM2 embedded_scm embedded_scm embedded_scm {
2454                 $$ = scm_list_4 ($1, $2, $3, $4);
2455         }
2456         | MARKUP_HEAD_SCM0_SCM1 embedded_scm embedded_scm {
2457                 $$ = scm_list_3 ($1, $2, $3);
2458         }
2459         | MARKUP_IDENTIFIER {
2460                 $$ = $1;
2461         }
2462         | STRING_IDENTIFIER {
2463                 $$ = $1;
2464         }
2465         | {
2466                 SCM nn = THIS->lexer_->lookup_identifier ("pitchnames");
2467                 THIS->lexer_->push_note_state (alist_to_hashq (nn));
2468         }
2469         /* cont */ score_block {
2470                 /* WIP this is a bit arbitrary,
2471                    we should also allow \book or Composite_music.
2472                    However, you'd typically want to change paper
2473                    settings, and need a \score block anyway.  */
2474
2475                 THIS->lexer_->pop_state ();
2476                 Score *score = $2;
2477                 Book *book = new Book;
2478                 book->scores_.push (score);
2479
2480                 Music_output_def *paper = get_paper (THIS);
2481                 SCM s = book->to_stencil (paper, THIS->header_);
2482                 scm_gc_unprotect_object (score->self_scm ());
2483                 scm_gc_unprotect_object (book->self_scm ());
2484                 $$ = scm_list_2 (ly_scheme_function ("stencil-markup"), s);
2485         }
2486         ;
2487
2488 markup_list:
2489         chord_open markup_list_body chord_close { $$ = scm_reverse_x ($2, SCM_EOL); }
2490         ;
2491
2492 markup_line:
2493         '{' markup_list_body '}' {
2494                 SCM line = ly_scheme_function ("line-markup");
2495         
2496                 $$ = scm_list_2 (line, scm_reverse_x ($2, SCM_EOL));
2497         }
2498         ;
2499
2500 markup_list_body:
2501         /**/ {  $$ = SCM_EOL; }
2502         | markup_list_body markup {
2503                 $$ = scm_cons ($2, $1);
2504         }
2505         ;
2506
2507
2508 %%
2509
2510 void
2511 My_lily_parser::set_yydebug (bool )
2512 {
2513 #if 0
2514         yydebug = 1;
2515 #endif
2516 }
2517
2518 void
2519 My_lily_parser::do_yyparse ()
2520 {
2521         yyparse ((void*)this);
2522 }
2523
2524
2525 /*
2526 Should make this optional?    It will also complain when you do
2527
2528         [s4]
2529
2530 which is entirely legitimate.
2531
2532 Or we can scrap it. Barchecks should detect wrong durations, and
2533 skipTypesetting speeds it up a lot.
2534 */
2535
2536 void
2537 My_lily_parser::beam_check (SCM dur)
2538 {
2539   Duration *d = unsmob_duration (dur);
2540   if (unsmob_music (last_beam_start_) && d->duration_log () <= 2)
2541     {
2542       Music *m = unsmob_music (last_beam_start_);
2543       m->origin ()->warning (_ ("Suspect duration found following this beam"));
2544     }
2545   last_beam_start_ = SCM_EOL;
2546 }
2547
2548
2549
2550
2551 /*
2552
2553 It is a little strange to have this function in this file, but
2554 otherwise, we have to import music classes into the lexer.
2555
2556 */
2557 int
2558 My_lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
2559 {
2560         if (ly_c_string_p (sid)) {
2561                 *destination = sid;
2562                 return STRING_IDENTIFIER;
2563         } else if (ly_c_number_p (sid)) {
2564                 *destination = sid;
2565                 return NUMBER_IDENTIFIER;
2566         } else if (unsmob_context_def (sid)) {
2567                 *destination = unsmob_context_def (sid)->clone_scm ();
2568                 return CONTEXT_DEF_IDENTIFIER;
2569         } else if (unsmob_score (sid)) {
2570                 Score *score = new Score (*unsmob_score (sid));
2571                 *destination = score->self_scm ();
2572                 return SCORE_IDENTIFIER;
2573         } else if (Music *mus = unsmob_music (sid)) {
2574                 mus = mus->clone ();
2575                 *destination = mus->self_scm ();
2576                 unsmob_music (*destination)->
2577                         set_property ("origin", make_input (last_input_));
2578                 return dynamic_cast<Event*> (mus)
2579                         ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
2580         } else if (unsmob_duration (sid)) {
2581                 *destination = unsmob_duration (sid)->smobbed_copy ();
2582                 return DURATION_IDENTIFIER;
2583         } else if (unsmob_music_output_def (sid)) {
2584                 Music_output_def *p = unsmob_music_output_def (sid);
2585                 p = p->clone ();
2586
2587                 *destination = p->self_scm ();
2588                 return MUSIC_OUTPUT_DEF_IDENTIFIER;
2589         } else if (Text_item::markup_p (sid)) {
2590                 *destination = sid;
2591                 return MARKUP_IDENTIFIER;
2592         }
2593
2594         return -1;      
2595 }
2596
2597 Music *
2598 property_op_to_music (SCM op)
2599 {
2600         Music *m = 0;
2601         SCM tag = ly_car (op);
2602         SCM symbol = ly_cadr (op);
2603         SCM args = ly_cddr (op);
2604         SCM grob_val = SCM_UNDEFINED;
2605         SCM grob_sym = SCM_UNDEFINED;
2606         SCM val = SCM_UNDEFINED;
2607         
2608         if (tag == ly_symbol2scm ("assign"))
2609                 {
2610                 m = MY_MAKE_MUSIC ("PropertySet");
2611                 val = ly_car (args);
2612                 }
2613         else if (tag == ly_symbol2scm ("unset"))
2614                 m = MY_MAKE_MUSIC ("PropertyUnset");
2615         else if (tag == ly_symbol2scm ("poppush")
2616                  || tag == ly_symbol2scm ("push"))
2617                 {
2618                 m = MY_MAKE_MUSIC ("OverrideProperty");
2619                 grob_sym = ly_car (args);
2620                 grob_val = ly_cadr (args);
2621                 }
2622         else if (tag == ly_symbol2scm ("pop")) {
2623                 m = MY_MAKE_MUSIC ("RevertProperty");
2624                 grob_sym = ly_car (args);
2625                 }
2626
2627         m->set_property ("symbol", symbol);
2628
2629         if (val != SCM_UNDEFINED)
2630                 m->set_property ("value", val);
2631         if (grob_val != SCM_UNDEFINED)
2632                 m->set_property ("grob-value", grob_val);
2633
2634         if (grob_sym != SCM_UNDEFINED)
2635                 {
2636                 bool itc = internal_type_checking_global_b;
2637                 /* UGH.
2638                 */
2639                 bool autobeam = ly_c_equal_p (symbol, ly_symbol2scm ("autoBeamSettings"));
2640                 if (autobeam)
2641                         internal_type_checking_global_b = false;
2642                 m->set_property ("grob-property", grob_sym);
2643                 if (autobeam)
2644                         internal_type_checking_global_b = itc;
2645                 }
2646
2647         if (tag == ly_symbol2scm ("poppush"))
2648                 m->set_property ("pop-first", SCM_BOOL_T);
2649
2650
2651         return m;
2652 }
2653
2654 Music*
2655 context_spec_music (SCM type, SCM id, Music *m, SCM ops)
2656 {
2657         Music *csm = MY_MAKE_MUSIC ("ContextSpeccedMusic");
2658
2659         csm->set_property ("element", m->self_scm ());
2660         scm_gc_unprotect_object (m->self_scm ());
2661
2662         csm->set_property ("context-type",
2663                 ly_c_symbol_p (type) ? type : scm_string_to_symbol (type));
2664         csm->set_property ("property-operations", ops);
2665
2666         if (ly_c_string_p (id))
2667                 csm->set_property ("context-id", id);
2668         return csm;
2669 }
2670
2671 SCM
2672 get_next_unique_context ()
2673 {
2674         static int new_context_count;
2675         char s[1024];
2676         snprintf (s, 1024, "uniqueContext%d", new_context_count++);
2677         return scm_makfrom0str (s);
2678 }
2679