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