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