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