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