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