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