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