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