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