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