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