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