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