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