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