]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
4f4747ab5a0d41edfd1ebe050219bb9203e54df6
[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 NGRACE
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->remember_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->remember_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_primitive_eval (ly_symbol2scm ("repeat-name-to-ctor"));
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 #if 1
810         /*
811                 The other version is for easier debugging  of
812                 Sequential_music_iterator in combination with grace notes.
813         */
814
815                 SCM start = THIS->lexer_p_->lookup_identifier ("startGraceMusic");
816                 SCM stop = THIS->lexer_p_->lookup_identifier ("stopGraceMusic");
817                 Music *startm = unsmob_music (start);
818                 Music *stopm = unsmob_music (stop);
819
820                 SCM ms = SCM_EOL;
821                 if (stopm) {
822                         stopm = stopm->clone ();
823                         ms = gh_cons (stopm->self_scm (), ms);
824                         scm_gc_unprotect_object (stopm->self_scm ());
825                 }
826                 ms = gh_cons ($2->self_scm (), ms);
827                 scm_gc_unprotect_object ($2->self_scm());
828                 if (startm) {
829                         startm = startm->clone ();
830                         ms = gh_cons (startm->self_scm () , ms);
831                         scm_gc_unprotect_object (startm->self_scm ());
832                 }
833
834                 Music* seq = new Sequential_music (SCM_EOL);
835                 seq->set_mus_property ("elements", ms);
836
837                 $$ = new Grace_music (SCM_EOL);
838                 $$->set_mus_property ("element", seq->self_scm ());
839                 scm_gc_unprotect_object (seq->self_scm ());
840 #else
841                 $$ = new Grace_music (SCM_EOL);
842                 $$->set_mus_property ("element", $2->self_scm ());
843                 scm_gc_unprotect_object ($2->self_scm ());
844 #endif
845
846
847         }
848         | CONTEXT string '=' string Music {
849                 Context_specced_music *csm =  new Context_specced_music (SCM_EOL);
850                 csm->set_mus_property ("element", $5->self_scm ());
851                 scm_gc_unprotect_object ($5->self_scm ());
852
853                 csm->set_mus_property ("context-type", $2);
854                 csm->set_mus_property ("context-id", $4);
855
856                 $$ = csm;
857         }
858         | TIMES {
859                 THIS->remember_spot ();
860         }
861         /* CONTINUED */ 
862                 fraction Music  
863
864         {
865                 int n = gh_scm2int (gh_car ($3)); int d = gh_scm2int (gh_cdr ($3));
866                 Music *mp = $4;
867                 $$ = new Time_scaled_music (SCM_EOL);
868                 $$->set_spot (THIS->pop_spot ());
869
870
871                 $$->set_mus_property ("element", mp->self_scm ());
872                 scm_gc_unprotect_object (mp->self_scm ());
873                 $$->set_mus_property ("numerator", gh_int2scm (n));
874                 $$->set_mus_property ("denominator", gh_int2scm (d));
875                 $$->compress (Moment (n,d));
876
877         }
878         | Repeated_music                { $$ = $1; }
879         | Simultaneous_music            { $$ = $1; }
880         | Sequential_music              { $$ = $1; }
881         | TRANSPOSE pitch Music {
882                 $$ = new Transposed_music (SCM_EOL);
883                 Music *p = $3;
884                 Pitch pit = *unsmob_pitch ($2);
885
886                 p->transpose (pit);
887                 $$->set_mus_property ("element", p->self_scm ());
888                 scm_gc_unprotect_object (p->self_scm ());
889         }
890         | TRANSPOSE steno_tonic_pitch Music {
891                 $$ = new Transposed_music (SCM_EOL);
892                 Music *p = $3;
893                 Pitch pit = *unsmob_pitch ($2);
894
895                 p->transpose (pit);
896                 $$->set_mus_property ("element", p->self_scm ());
897                 scm_gc_unprotect_object (p->self_scm ());
898         
899         }
900         | APPLY embedded_scm Music  {
901                 SCM ret = gh_call1 ($2, $3->self_scm ());
902                 Music *m = unsmob_music (ret);
903                 if (!m) {
904                         THIS->parser_error ("\\apply must return a Music");
905                         m = new Music (SCM_EOL);
906                         }
907                 $$ = m;
908         }
909         | NOTES
910                 { THIS->lexer_p_->push_note_state (); }
911         Music
912                 { $$ = $3;
913                   THIS->lexer_p_->pop_state ();
914                 }
915         | CHORDS
916                 { THIS->lexer_p_->push_chord_state (); }
917         Music
918                 {
919                   Music * chm = new Un_relativable_music ;
920                   chm->set_mus_property ("element", $3->self_scm ());
921                   $$ = chm;
922
923                   THIS->lexer_p_->pop_state ();
924         }
925         | LYRICS
926                 { THIS->lexer_p_->push_lyric_state (); }
927         Music
928                 {
929                   $$ = $3;
930                   THIS->lexer_p_->pop_state ();
931         }
932         | relative_music        { $$ = $1; }
933         | re_rhythmed_music     { $$ = $1; } 
934         | part_combined_music   { $$ = $1; } 
935         ;
936
937 relative_music:
938         RELATIVE absolute_pitch Music {
939                 Music * p = $3;
940                 Pitch pit = *unsmob_pitch ($2);
941                 $$ = new Relative_octave_music (SCM_EOL);
942
943                 $$->set_mus_property ("element", p->self_scm ());
944                 scm_gc_unprotect_object (p->self_scm ());
945
946                 $$->set_mus_property ("last-pitch", p->to_relative_octave (pit).smobbed_copy ());
947
948         }
949         ;
950
951 re_rhythmed_music:
952         ADDLYRICS Music Music {
953           Lyric_combine_music * l = new Lyric_combine_music (SCM_EOL);
954           l->set_mus_property ("music", $2->self_scm ());
955           l->set_mus_property ("lyrics", $3->self_scm ());
956           scm_gc_unprotect_object ($3->self_scm ());
957           scm_gc_unprotect_object ($2->self_scm ());
958           $$ = l;
959         }
960         ;
961
962 part_combined_music:
963         PARTCOMBINE STRING Music Music {
964                 Part_combine_music * p = new Part_combine_music (SCM_EOL);
965
966                 p->set_mus_property ("what", $2);
967                 p->set_mus_property ("one", $3->self_scm ());
968                 p->set_mus_property ("two", $4->self_scm ());  
969
970                 scm_gc_unprotect_object ($3->self_scm ());
971                 scm_gc_unprotect_object ($4->self_scm ());  
972
973
974                 $$ = p;
975         }
976         ;
977
978 translator_change:
979         TRANSLATOR STRING '=' STRING  {
980                 Music * t = new Music (SCM_EOL);
981                 t->set_mus_property ("iterator-ctor",
982                         Change_iterator::constructor_cxx_function);
983                 t-> set_mus_property ("change-to-type", $2);
984                 t-> set_mus_property ("change-to-id", $4);
985
986                 $$ = t;
987                 $$->set_spot (THIS->here_input ());
988         }
989         ;
990
991 property_def:
992         PROPERTY STRING '.' STRING '='  scalar {
993                 
994                 Music *t = set_property_music (scm_string_to_symbol ($4), $6);
995                 Context_specced_music *csm = new Context_specced_music (SCM_EOL);
996
997                 csm->set_mus_property ("element", t->self_scm ());
998                 scm_gc_unprotect_object (t->self_scm ());
999
1000                 $$ = csm;
1001                 $$->set_spot (THIS->here_input ());
1002
1003                 csm-> set_mus_property ("context-type", $2);
1004         }
1005         | PROPERTY STRING '.' STRING UNSET {
1006                 Music *t = new Music (SCM_EOL);
1007
1008                 t->set_mus_property ("iterator-ctor",
1009                         Property_unset_iterator::constructor_cxx_function);
1010                 t->set_mus_property ("symbol", scm_string_to_symbol ($4));
1011
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 SET embedded_scm '=' embedded_scm {
1022                 Music *t = new Music (SCM_EOL);
1023                 t->set_mus_property ("iterator-ctor",
1024                         Push_property_iterator::constructor_cxx_function);
1025                 t->set_mus_property ("symbols", scm_string_to_symbol ($4));
1026                 t->set_mus_property ("pop-first", SCM_BOOL_T);
1027                 t->set_mus_property ("grob-property", $6);
1028                 t->set_mus_property ("grob-value", $8);
1029                 Context_specced_music *csm = new Context_specced_music (SCM_EOL);
1030                 csm->set_mus_property ("element", t->self_scm ());
1031                 scm_gc_unprotect_object (t->self_scm ());
1032                 $$ = csm;
1033                 $$->set_spot (THIS->here_input ());
1034
1035                 csm-> set_mus_property ("context-type", $2);
1036         }
1037         | PROPERTY STRING '.' STRING OVERRIDE embedded_scm '=' embedded_scm {
1038                 Music *t = new Music (SCM_EOL);
1039                 t->set_mus_property ("iterator-ctor",
1040                         Push_property_iterator::constructor_cxx_function);
1041                 t->set_mus_property ("symbols", scm_string_to_symbol ($4));
1042                 t->set_mus_property ("grob-property", $6);
1043                 t->set_mus_property ("grob-value", $8);
1044                 Context_specced_music *csm = new Context_specced_music (SCM_EOL);
1045                 csm->set_mus_property ("element", t->self_scm ());
1046                 scm_gc_unprotect_object (t->self_scm ());
1047
1048                 $$ = csm;
1049                 $$->set_spot (THIS->here_input ());
1050
1051                 csm-> set_mus_property ("context-type", $2);
1052         }
1053         | PROPERTY STRING '.' STRING REVERT embedded_scm {
1054                 Music *t = new Music (SCM_EOL);
1055                 t->set_mus_property ("iterator-ctor",
1056                         Pop_property_iterator::constructor_cxx_function);
1057                 t->set_mus_property ("symbols", scm_string_to_symbol ($4));
1058                 t->set_mus_property ("grob-property", $6);
1059
1060                 Context_specced_music *csm = new Context_specced_music (SCM_EOL);
1061                 csm->set_mus_property ("element", t->self_scm ());
1062                 scm_gc_unprotect_object (t->self_scm ());
1063
1064                 $$ = csm;
1065                 $$->set_spot (THIS->here_input ());
1066
1067                 csm-> set_mus_property ("context-type", $2);
1068         }
1069         ;
1070
1071 scalar:
1072         string          { $$ = $1; }
1073         | bare_int      { $$ = gh_int2scm ($1); }
1074         | embedded_scm  { $$ = $1; }
1075         ;
1076
1077
1078 request_chord:
1079         pre_requests simple_element post_requests       {
1080                 Music_sequence *l = dynamic_cast<Music_sequence*> ($2);
1081                 if (l) {
1082                         for (int i=0; i < $1->size (); i++)
1083                                 l->append_music ($1->elem (i));
1084                         for (int i=0; i < $3->size (); i++)
1085                                 l->append_music ($3->elem (i));
1086                         }
1087                 else
1088                         programming_error ("Need Sequence to add music to");
1089                 $$ = $2;
1090                 
1091         }
1092         | command_element
1093         ;
1094
1095 command_element:
1096         command_req {
1097                 $$ = new Request_chord (SCM_EOL);
1098                 $$->set_mus_property ("elements", gh_cons ($1->self_scm (), SCM_EOL));
1099                 $$-> set_spot (THIS->here_input ());
1100                 $1-> set_spot (THIS->here_input ());
1101         }
1102         | BAR STRING                    {
1103                 Music *t = set_property_music (ly_symbol2scm ("whichBar"), $2);
1104
1105                 Context_specced_music *csm = new Context_specced_music (SCM_EOL);
1106                 csm->set_mus_property ("element", t->self_scm ());
1107                 scm_gc_unprotect_object (t->self_scm ());
1108
1109                 $$ = csm;
1110                 $$->set_spot (THIS->here_input ());
1111
1112                 csm->set_mus_property ("context-type", ly_str02scm ("Score"));
1113         }
1114         | PARTIAL duration_length       {
1115                 Moment m = - unsmob_duration ($2)->length_mom ();
1116                 Music * p = set_property_music (ly_symbol2scm ( "measurePosition"),m.smobbed_copy ());
1117
1118                 Context_specced_music * sp = new Context_specced_music (SCM_EOL);
1119                 sp->set_mus_property ("element", p->self_scm ());
1120                 scm_gc_unprotect_object (p->self_scm ());
1121
1122                 $$ =sp ;
1123                 sp-> set_mus_property ("context-type", ly_str02scm ( "Score"));
1124         }
1125         | CLEF STRING  {
1126                 SCM func = scm_primitive_eval (ly_symbol2scm ("clef-name-to-properties"));
1127                 SCM result = gh_call1 (func, $2);
1128
1129                 SCM l = SCM_EOL;
1130                 for (SCM s = result ; gh_pair_p (s); s = gh_cdr (s)) {
1131                         Music * p = new Music (SCM_EOL);
1132                         set_music_properties (p, gh_car (s));
1133                         l = gh_cons (p->self_scm (), l);
1134                         scm_gc_unprotect_object (p->self_scm ());
1135                 }
1136                 Sequential_music * seq = new Sequential_music (SCM_EOL);
1137                 seq->set_mus_property ("elements", l);
1138
1139                 Context_specced_music * sp = new Context_specced_music (SCM_EOL);
1140                 sp->set_mus_property ("element", seq->self_scm ());
1141                 scm_gc_unprotect_object (seq->self_scm ());
1142
1143                 $$ =sp ;
1144                 sp-> set_mus_property ("context-type", ly_str02scm ("Staff"));
1145         }
1146         | TIME_T fraction  {
1147                 Music * p1 = set_property_music (ly_symbol2scm ( "timeSignatureFraction"), $2);
1148
1149   int l = gh_scm2int (gh_car ($2));
1150   int o = gh_scm2int (gh_cdr ($2));
1151   
1152   Moment one_beat = Moment (1)/Moment (o);
1153   Moment len = Moment (l) * one_beat;
1154
1155
1156                 Music *p2 = set_property_music (ly_symbol2scm ("measureLength"), len.smobbed_copy ());
1157                 Music *p3 = set_property_music (ly_symbol2scm ("beatLength"), one_beat.smobbed_copy ());
1158
1159                 SCM list = gh_list (p1->self_scm (), p2->self_scm (), p3->self_scm(), SCM_UNDEFINED);
1160                 Sequential_music *seq = new Sequential_music (SCM_EOL);
1161                 seq->set_mus_property ("elements", list);
1162                 
1163
1164                 Context_specced_music * sp = new Context_specced_music (SCM_EOL);
1165                 sp->set_mus_property ("element", seq->self_scm ());
1166
1167                 
1168
1169                 scm_gc_unprotect_object (p3->self_scm ());
1170                 scm_gc_unprotect_object (p2->self_scm ());
1171                 scm_gc_unprotect_object (p1->self_scm ());
1172                 scm_gc_unprotect_object (seq->self_scm ());
1173
1174                 $$ = sp;
1175
1176 /*
1177  TODO: should make alias TimingContext for Score
1178 */
1179
1180                 sp-> set_mus_property ("context-type", ly_str02scm ( "Score"));
1181         }
1182         ;
1183
1184 command_req:
1185         shorthand_command_req   { $$ = $1; }
1186         | verbose_command_req   { $$ = $1; }
1187         ;
1188
1189 shorthand_command_req:
1190         extender_req {
1191                 $$ = $1;
1192         }
1193         | hyphen_req {
1194                 $$ = $1;
1195         }
1196         | '|'                           {
1197                 $$ = new Barcheck_req;
1198         }
1199         | '~'   {
1200                 $$ = new Tie_req;
1201         }
1202         | '['           {
1203                 Span_req*b= new Span_req;
1204                 b->set_span_dir (START);
1205                 b->set_mus_property ("span-type", ly_str02scm ("beam"));
1206                 $$ =b;
1207         }
1208         | ']'           {
1209                 Span_req*b= new Span_req;
1210                 b->set_span_dir ( STOP);
1211                 b->set_mus_property ("span-type", ly_str02scm ("beam"));
1212                 $$ = b;
1213         }
1214         | BREATHE {
1215                 $$ = new Breathing_sign_req;
1216         }
1217         ;
1218
1219
1220 verbose_command_req:
1221         COMMANDSPANREQUEST bare_int STRING { /*TODO: junkme */
1222                 Span_req * sp_p = new Span_req;
1223                 sp_p-> set_span_dir ( Direction ($2));
1224                 sp_p->set_mus_property ("span-type",$3);
1225                 sp_p->set_spot (THIS->here_input ());
1226                 $$ = sp_p;
1227         }
1228         | MARK DEFAULT  {
1229                 Mark_req * m = new Mark_req;
1230                 $$ = m;
1231         }
1232         | MARK scalar {
1233                 Mark_req *m = new Mark_req;
1234                 m->set_mus_property ("label", $2);
1235                 $$ = m;
1236
1237         }
1238         | PENALTY SCM_T         {
1239
1240                 
1241                 Break_req * b = new Break_req;
1242                 SCM s = $2;
1243                 if (!gh_number_p (s))
1244                         s  =gh_int2scm (0);
1245
1246                 b->set_mus_property ("penalty", s);
1247                 b->set_spot (THIS->here_input ());
1248                 $$ = b;
1249         }
1250         | SKIP duration_length {
1251                 Skip_req * skip_p = new Skip_req;
1252                 skip_p->set_mus_property ("duration", $2);
1253
1254                 $$ = skip_p;
1255         }
1256         | tempo_request {
1257                 $$ = $1;
1258         }
1259         | KEY DEFAULT {
1260                 Key_change_req *key_p= new Key_change_req;
1261                 $$ = key_p;
1262         }
1263         | KEY NOTENAME_PITCH SCM_IDENTIFIER     {
1264                 Key_change_req *key_p= new Key_change_req;
1265                 
1266                 key_p->set_mus_property ("pitch-alist", $3);
1267  ((Music* )key_p)->transpose (* unsmob_pitch ($2));
1268                 $$ = key_p; 
1269         }
1270         ;
1271
1272 post_requests:
1273         {
1274                 $$ = new Link_array<Request>;
1275         }
1276         | post_requests post_request {
1277                 $2->set_spot (THIS->here_input ());
1278                 $$->push ($2);
1279         }
1280         ;
1281
1282 post_request:
1283         verbose_request
1284         | request_with_dir
1285         | close_request
1286         ;
1287
1288
1289 request_that_take_dir:
1290         gen_text_def
1291         | verbose_request
1292         | script_abbreviation {
1293                 SCM s = THIS->lexer_p_->lookup_identifier ("dash" + ly_scm2string ($1));
1294                 Articulation_req *a = new Articulation_req;
1295                 if (gh_string_p (s))
1296                         a->set_mus_property ("articulation-type", s);
1297                 else THIS->parser_error (_ ("Expecting string as script definition"));
1298                 $$ = a;
1299         }
1300         ;
1301
1302 request_with_dir:
1303         script_dir request_that_take_dir        {
1304                 if (Script_req * gs = dynamic_cast<Script_req*> ($2))
1305                         gs->set_direction (Direction ($1));
1306                 else if ($1)
1307                         $2->origin ()->warning (_ ("Can't specify direction for this request"));
1308                 $$ = $2;
1309         }
1310         ;
1311         
1312 verbose_request:
1313         REQUEST_IDENTIFIER      {
1314                 $$ = dynamic_cast<Request*> (unsmob_music ($1)->clone ());
1315                 $$->set_spot (THIS->here_input ());
1316         }
1317         | DYNAMICSCRIPT embedded_scm {
1318                 /*
1319                         TODO: junkme, use text-type == dynamic
1320                 */
1321                 Text_script_req *d = new Text_script_req;
1322                 d->set_mus_property ("text-type" , ly_symbol2scm ("dynamic"));
1323                 d->set_mus_property ("text", $2);
1324                 d->set_spot (THIS->here_input ());
1325                 $$ = d;
1326         }
1327         | SPANREQUEST bare_int STRING {
1328                 Span_req * sp_p = new Span_req;
1329                 sp_p->set_span_dir ( Direction ($2));
1330                 sp_p->set_mus_property ("span-type", $3);
1331                 sp_p->set_spot (THIS->here_input ());
1332                 $$ = sp_p;
1333         }
1334         | tremolo_type  {
1335                Tremolo_req* a = new Tremolo_req;
1336                a->set_spot (THIS->here_input ());
1337                a->set_mus_property ("tremolo-type", gh_int2scm ($1));
1338                $$ = a;
1339         }
1340         | SCRIPT STRING         { 
1341                 Articulation_req * a = new Articulation_req;
1342                 a->set_mus_property ("articulation-type", $2);
1343                 a->set_spot (THIS->here_input ());
1344                 $$ = a;
1345         }
1346         /*
1347 duh, junk this syntax from the parser, if possible. 
1348         */
1349         | ARPEGGIO {
1350                 Arpeggio_req *a = new Arpeggio_req;
1351                 a->set_spot (THIS->here_input ());
1352                 $$ = a;
1353         }
1354         | GLISSANDO {
1355                 Glissando_req *g = new Glissando_req;
1356                 g->set_spot /* No pun intended */ (THIS->here_input ());
1357                 $$ = g;
1358         }       
1359         ;
1360
1361 sup_quotes:
1362         '\'' {
1363                 $$ = 1;
1364         }
1365         | sup_quotes '\'' {
1366                 $$ ++;
1367         }
1368         ;
1369
1370 sub_quotes:
1371         ',' {
1372                 $$ = 1;
1373         }
1374         | sub_quotes ',' {
1375                 $$ ++ ;
1376         }
1377         ;
1378
1379 steno_pitch:
1380         NOTENAME_PITCH  {
1381                 $$ = $1;
1382         }
1383         | NOTENAME_PITCH sup_quotes     {
1384                 Pitch p = *unsmob_pitch ($1);
1385                 p.octave_i_ +=  $2;
1386                 $$ = p.smobbed_copy ();
1387         }
1388         | NOTENAME_PITCH sub_quotes      {
1389                 Pitch p =* unsmob_pitch ($1);
1390
1391                 p.octave_i_ +=  -$2;
1392                 $$ = p.smobbed_copy ();
1393
1394         }
1395         ;
1396
1397 /*
1398 ugh. duplication
1399 */
1400
1401 steno_tonic_pitch:
1402         TONICNAME_PITCH {
1403                 $$ = $1;
1404         }
1405         | TONICNAME_PITCH sup_quotes    {
1406                 Pitch p = *unsmob_pitch ($1);
1407                 p.octave_i_ +=  $2;
1408                 $$ = p.smobbed_copy ();
1409         }
1410         | TONICNAME_PITCH sub_quotes     {
1411                 Pitch p =* unsmob_pitch ($1);
1412
1413                 p.octave_i_ +=  -$2;
1414                 $$ = p.smobbed_copy ();
1415
1416         }
1417         ;
1418
1419 pitch:
1420         steno_pitch {
1421                 $$ = $1;
1422         }
1423         | explicit_pitch {
1424                 $$ = $1;
1425         }
1426         ;
1427
1428 explicit_pitch:
1429         PITCH embedded_scm {
1430                 $$ = $2;
1431                 if (!unsmob_pitch ($2)) {
1432                         THIS->parser_error (_f ("Expecting musical-pitch value", 3));
1433                          $$ = Pitch ().smobbed_copy ();
1434                 }
1435         }
1436         ;
1437
1438 explicit_duration:
1439         DURATION embedded_scm   {
1440                 $$ = $2;
1441                 if (!unsmob_duration ($2))
1442                 {
1443                         THIS->parser_error (_ ("Must have duration object"));
1444                         $$ = Duration ().smobbed_copy ();
1445                 }
1446         }
1447         ;
1448
1449 extender_req:
1450         EXTENDER {
1451                 if (!THIS->lexer_p_->lyric_state_b ())
1452                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
1453                 $$ = new Extender_req;
1454         }
1455         ;
1456
1457 hyphen_req:
1458         HYPHEN {
1459                 if (!THIS->lexer_p_->lyric_state_b ())
1460                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
1461                 $$ = new Hyphen_req;
1462         }
1463         ;
1464
1465 close_request:
1466         close_request_parens {
1467                 $$ = $1;
1468                 dynamic_cast<Span_req*> ($$)->set_span_dir ( START);
1469         }
1470         
1471 close_request_parens:
1472         '('     {
1473                 Span_req* s= new Span_req;
1474                 $$ = s;
1475                 s->set_mus_property ("span-type", ly_str02scm ( "slur"));
1476         }
1477         | E_OPEN        {
1478                 Span_req* s= new Span_req;
1479                 $$ = s;
1480                 s->set_mus_property ("span-type", ly_str02scm ( "phrasing-slur"));
1481         }
1482         | E_SMALLER {
1483                 Span_req*s =new Span_req;
1484                 $$ = s;
1485                 s->set_mus_property ("span-type", ly_str02scm ( "crescendo"));
1486         }
1487         | E_BIGGER {
1488                 Span_req*s =new Span_req;
1489                 $$ = s;
1490                 s->set_mus_property ("span-type", ly_str02scm ("decrescendo"));
1491         }
1492         ;
1493
1494
1495 open_request:
1496         open_request_parens {
1497                 $$ = $1;
1498                 dynamic_cast<Span_req*> ($$)->set_span_dir (STOP);
1499         }
1500         ;
1501
1502 open_request_parens:
1503         E_EXCLAMATION   {
1504                 Span_req *s =  new Span_req;
1505                 s->set_mus_property ("span-type", ly_str02scm ( "crescendo"));
1506
1507                 $$ = s;
1508         }
1509         | ')'   {
1510                 Span_req* s= new Span_req;
1511                 $$ = s;
1512                 s->set_mus_property ("span-type", ly_str02scm ( "slur"));
1513         }
1514         | E_CLOSE       {
1515                 Span_req* s= new Span_req;
1516                 $$ = s;
1517                 s->set_mus_property ("span-type", ly_str02scm ( "phrasing-slur"));
1518         }
1519         ;
1520
1521 gen_text_def:
1522         embedded_scm {
1523                 Text_script_req *t = new Text_script_req;
1524                 t->set_mus_property ("text", $1);
1525                 t->set_spot (THIS->here_input ());
1526                 $$ = t;
1527         }
1528         | string {
1529                 Text_script_req *t = new Text_script_req;
1530                 t->set_mus_property ("text", $1);
1531                 t->set_spot (THIS->here_input ());
1532                 $$ = t;
1533         }
1534         | DIGIT {
1535                 String ds = to_str ($1);
1536                 Text_script_req* t = new Text_script_req;
1537
1538                 t->set_mus_property ("text",  ly_str02scm (ds.ch_C ()));
1539                 t->set_mus_property ("text-type" , ly_symbol2scm ("finger"));
1540                 t->set_spot (THIS->here_input ());
1541                 $$ = t;
1542         }
1543         ;
1544
1545 script_abbreviation:
1546         '^'             {
1547                 $$ = gh_str02scm ("Hat");
1548         }
1549         | '+'           {
1550                 $$ = gh_str02scm ("Plus");
1551         }
1552         | '-'           {
1553                 $$ = gh_str02scm ("Dash");
1554         }
1555         | '|'           {
1556                 $$ = gh_str02scm ("Bar");
1557         }
1558         | '>'           {
1559                 $$ = gh_str02scm ("Larger");
1560         }
1561         | '.'           {
1562                 $$ = gh_str02scm ("Dot");
1563         }
1564         ;
1565
1566
1567 script_dir:
1568         '_'     { $$ = DOWN; }
1569         | '^'   { $$ = UP; }
1570         | '-'   { $$ = CENTER; }
1571         ;
1572
1573 pre_requests:
1574         {
1575                 $$ = new Link_array<Request>;
1576         }
1577         | pre_requests open_request {
1578                 $$->push ($2);
1579         }
1580         ;
1581
1582 absolute_pitch:
1583         steno_pitch     {
1584                 $$ = $1;
1585         }
1586         ;
1587
1588 duration_length:
1589         multiplied_duration {
1590                 $$ = $1;
1591         }
1592         | explicit_duration {
1593                 $$ = $1;
1594         }       
1595         ;
1596
1597 optional_notemode_duration:
1598         {
1599                 $$ = THIS->default_duration_.smobbed_copy ();
1600         }
1601         | multiplied_duration   {
1602                 $$ = $1;
1603         }
1604         | explicit_duration {
1605                 $$ = $1;
1606         }       
1607         ;
1608
1609 steno_duration:
1610         bare_unsigned dots              {
1611                 int l = 0;
1612                 if (!is_duration_b ($1))
1613                         THIS->parser_error (_f ("not a duration: %d", $1));
1614                 else
1615                         l =  intlog2 ($1);
1616
1617                 $$ = Duration (l, $2).smobbed_copy ();
1618
1619                 THIS->set_last_duration (unsmob_duration ($$));
1620         }
1621         | DURATION_IDENTIFIER dots      {
1622                 Duration *d =unsmob_duration ($1);
1623                 Duration k (d->duration_log (),d->dot_count () + $2);
1624                 $$ = k.smobbed_copy ();
1625
1626                 THIS->set_last_duration (unsmob_duration ($$));
1627         }
1628         ;
1629
1630
1631
1632
1633 multiplied_duration:
1634         steno_duration {
1635                 $$ = $1;
1636         }
1637         | multiplied_duration '*' bare_unsigned {
1638                 $$ = unsmob_duration ($$)->compressed ( $3) .smobbed_copy ();
1639         }
1640         | multiplied_duration '*' FRACTION {
1641                 Rational  m (gh_scm2int (gh_car ($3)), gh_scm2int (gh_cdr ($3)));
1642
1643                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
1644         }
1645         ;
1646
1647 fraction:
1648         FRACTION { $$ = $1; }
1649         | UNSIGNED '/' UNSIGNED {
1650                 $$ = gh_cons (gh_int2scm ($1), gh_int2scm ($3));
1651         }
1652         ;
1653
1654 dots:
1655         /* empty */     {
1656                 $$ = 0;
1657         }
1658         | dots '.' {
1659                 $$ ++;
1660         }
1661         ;
1662
1663
1664 tremolo_type: 
1665         ':'     {
1666                 $$ =0;
1667         }
1668         | ':' bare_unsigned {
1669                 if (!is_duration_b ($2))
1670                         THIS->parser_error (_f ("not a duration: %d", $2));
1671                 $$ = $2;
1672         }
1673         ;
1674
1675
1676 simple_element:
1677         pitch exclamations questions optional_notemode_duration {
1678                 if (!THIS->lexer_p_->note_state_b ())
1679                         THIS->parser_error (_ ("Have to be in Note mode for notes"));
1680
1681                 Note_req *n = new Note_req;
1682                 
1683                 n->set_mus_property ("pitch", $1);
1684                 n->set_mus_property ("duration", $4);
1685
1686                 if ($3 % 2)
1687                         n->set_mus_property ("cautionary", SCM_BOOL_T);
1688                 if ($2 % 2 || $3 % 2)
1689                         n->set_mus_property ("force-accidental", SCM_BOOL_T);
1690
1691                 Simultaneous_music*v = new Request_chord (SCM_EOL);
1692                 v->set_mus_property ("elements", gh_list (n->self_scm (), SCM_UNDEFINED));
1693                 
1694 /*
1695 FIXME: location is one off, since ptich & duration don't contain origin refs. 
1696 */
1697                 v->set_spot (THIS->here_input ());
1698                 n->set_spot (THIS->here_input ());
1699
1700                 $$ = v;
1701         }
1702         | RESTNAME optional_notemode_duration           {
1703
1704                 SCM e = SCM_UNDEFINED;
1705                   if (ly_scm2string ($1) =="s")
1706                     { /* Space */
1707                       Skip_req * skip_p = new Skip_req;
1708                       skip_p->set_mus_property ("duration" ,$2);
1709
1710                       skip_p->set_spot (THIS->here_input ());
1711                         e = skip_p->self_scm ();
1712                     }
1713                   else
1714                     {
1715                       Rest_req * rest_req_p = new Rest_req;
1716                       rest_req_p->set_mus_property ("duration", $2);
1717                       rest_req_p->set_spot (THIS->here_input ());
1718                         e = rest_req_p->self_scm ();
1719                     }
1720                   Simultaneous_music* velt_p = new Request_chord (SCM_EOL);
1721                 velt_p-> set_mus_property ("elements", gh_list (e,SCM_UNDEFINED));
1722                   velt_p->set_spot (THIS->here_input ());
1723
1724
1725                   $$ = velt_p;
1726         }
1727         | MULTI_MEASURE_REST optional_notemode_duration         {
1728                 Skip_req * sk = new Skip_req;
1729                 sk->set_mus_property ("duration", $2);
1730                 Span_req *sp1 = new Span_req;
1731                 Span_req *sp2 = new Span_req;
1732                 sp1-> set_span_dir ( START);
1733                 sp2-> set_span_dir ( STOP);
1734                 SCM r = ly_str02scm ("rest");
1735                 sp1->set_mus_property ("span-type", r);
1736                 sp2->set_mus_property ("span-type", r);
1737
1738                 Request_chord * rqc1 = new Request_chord (SCM_EOL);
1739                 rqc1->set_mus_property ("elements", gh_list (sp1->self_scm (), SCM_UNDEFINED));
1740                 Request_chord * rqc2 = new Request_chord (SCM_EOL);
1741                 rqc2->set_mus_property ("elements", gh_list (sk->self_scm (), SCM_UNDEFINED));;
1742                 Request_chord * rqc3 = new Request_chord (SCM_EOL);
1743                 rqc3->set_mus_property ("elements", gh_list (sp2->self_scm (), SCM_UNDEFINED));;
1744
1745                 SCM ms = gh_list (rqc1->self_scm (), rqc2->self_scm (), rqc3->self_scm (), SCM_UNDEFINED);
1746
1747                 $$ = new Sequential_music (SCM_EOL);
1748                 $$->set_mus_property ("elements", ms);
1749         }
1750         | STRING optional_notemode_duration     {
1751
1752                 Lyric_req* lreq_p = new Lyric_req;
1753                 lreq_p->set_mus_property ("text", $1);
1754                 lreq_p->set_mus_property ("duration",$2);
1755                 lreq_p->set_spot (THIS->here_input ());
1756                 Simultaneous_music* velt_p = new Request_chord (SCM_EOL);
1757                 velt_p->set_mus_property ("elements", gh_list (lreq_p->self_scm (), SCM_UNDEFINED));
1758
1759
1760                 $$= velt_p;
1761
1762         }
1763         | chord {
1764                 if (!THIS->lexer_p_->chord_state_b ())
1765                         THIS->parser_error (_ ("Have to be in Chord mode for chords"));
1766                 $$ = $1;
1767         }
1768         ;
1769
1770
1771 chord:
1772         steno_tonic_pitch optional_notemode_duration chord_additions chord_subtractions chord_inversion chord_bass {
1773                 $$ = Chord::get_chord ($1, $3, $4, $5, $6, $2);
1774                 $$->set_spot (THIS->here_input ());
1775         };
1776
1777 chord_additions: 
1778         {
1779                 $$ = SCM_EOL;
1780         } 
1781         | CHORD_COLON chord_notes {
1782                 $$ = $2;
1783         }
1784         ;
1785
1786 chord_notes:
1787         chord_step {
1788                 $$ = $1
1789         }
1790         | chord_notes '.' chord_step {
1791                 $$ = gh_append2 ($$, $3);
1792         }
1793         ;
1794
1795 chord_subtractions: 
1796         {
1797                 $$ = SCM_EOL;
1798         } 
1799         | CHORD_CARET chord_notes {
1800                 $$ = $2;
1801         }
1802         ;
1803
1804
1805 chord_inversion:
1806         {
1807                 $$ = SCM_EOL;
1808         }
1809         | '/' steno_tonic_pitch {
1810                 $$ = $2;
1811         }
1812         ;
1813
1814 chord_bass:
1815         {
1816                 $$ = SCM_EOL;
1817         }
1818         | CHORD_BASS steno_tonic_pitch {
1819                 $$ = $2;
1820         }
1821         ;
1822
1823 chord_step:
1824         chord_note {
1825                 $$ = gh_cons ($1, SCM_EOL);
1826         }
1827         | CHORDMODIFIER_PITCH {
1828                 $$ = gh_cons (unsmob_pitch ($1)->smobbed_copy (), SCM_EOL);
1829         }
1830         | CHORDMODIFIER_PITCH chord_note { /* Ugh. */
1831                 $$ = gh_list (unsmob_pitch ($1)->smobbed_copy (),
1832                         $2, SCM_UNDEFINED);
1833         }
1834         ;
1835
1836 chord_note:
1837         bare_unsigned {
1838                  Pitch m;
1839                 m.notename_i_ = ($1 - 1) % 7;
1840                 m.octave_i_ = $1 > 7 ? 1 : 0;
1841                 m.alteration_i_ = 0;
1842
1843                 $$ = m.smobbed_copy ();
1844         } 
1845         | bare_unsigned '+' {
1846                 Pitch m;
1847                 m.notename_i_ = ($1 - 1) % 7;
1848                 m.octave_i_ = $1 > 7 ? 1 : 0;
1849                 m.alteration_i_ = 1;
1850
1851
1852                 $$ = m.smobbed_copy ();
1853         }
1854         | bare_unsigned CHORD_MINUS {
1855                 Pitch m;
1856                 m.notename_i_ = ($1 - 1) % 7;
1857                 m.octave_i_ = $1 > 7 ? 1 : 0;
1858                 m.alteration_i_ = -1;
1859
1860                 $$ = m.smobbed_copy ();
1861         }
1862         ;
1863
1864 /*
1865         UTILITIES
1866  */
1867 number_expression:
1868         number_expression '+' number_term {
1869                 $$ = scm_sum ($1, $3);
1870         }
1871         | number_expression '-' number_term {
1872                 $$ = scm_difference ($1, $3);
1873         }
1874         | number_term 
1875         ;
1876
1877 number_term:
1878         number_factor {
1879                 $$ = $1;
1880         }
1881         | number_factor '*' number_factor {
1882                 $$ = scm_product ($1, $3);
1883         }
1884         | number_factor '/' number_factor {
1885                 $$ = scm_divide ($1, $3);
1886         }
1887         ;
1888
1889 number_factor:
1890         '(' number_expression ')'       {
1891                 $$ = $2;
1892         }
1893         | '-'  number_factor { /* %prec UNARY_MINUS */
1894                 $$ = scm_difference ($2, SCM_UNDEFINED);
1895         }
1896         | bare_number
1897         ;
1898
1899
1900 bare_number:
1901         UNSIGNED        {
1902                 $$ = gh_int2scm ($1);
1903         }
1904         | REAL          {
1905                 $$ = $1;
1906         }
1907         | NUMBER_IDENTIFIER             {
1908                 $$ = $1;
1909         }
1910         | REAL CM_T     {
1911                 $$ = gh_double2scm (gh_scm2double ($1) CM );
1912         }
1913         | REAL PT_T     {
1914                 $$ = gh_double2scm (gh_scm2double ($1) PT);
1915         }
1916         | REAL IN_T     {
1917                 $$ = gh_double2scm (gh_scm2double ($1) INCH);
1918         }
1919         | REAL MM_T     {
1920                 $$ = gh_double2scm (gh_scm2double ($1) MM);
1921         }
1922         | REAL CHAR_T   {
1923                 $$ = gh_double2scm (gh_scm2double ($1) CHAR);
1924         }
1925         ;
1926
1927
1928 bare_unsigned:
1929         UNSIGNED {
1930                         $$ = $1;
1931         }
1932         | DIGIT {
1933                 $$ = $1;
1934         }
1935         ;
1936
1937 bare_int:
1938         bare_number {
1939                 if (scm_integer_p ($1) == SCM_BOOL_T)
1940                 {
1941                         int k = gh_scm2int ($1);
1942                         $$ = k;
1943                 } else
1944                 {
1945                         THIS->parser_error (_ ("need integer number arg"));
1946                         $$ = 0;
1947                 }
1948         }
1949         | '-' bare_int {
1950                 $$ = -$2;
1951         }
1952         ;
1953
1954
1955 string:
1956         STRING          {
1957                 $$ = $1;
1958         }
1959         | STRING_IDENTIFIER     {
1960                 $$ = $1;
1961         }
1962         | string '+' string {
1963                 $$ = scm_string_append (scm_list_n ($1, $3, SCM_UNDEFINED));
1964         }
1965         ;
1966
1967
1968 exclamations:
1969                 { $$ = 0; }
1970         | exclamations '!'      { $$ ++; }
1971         ;
1972
1973 questions:
1974                 { $$ = 0; }
1975         | questions '?' { $$ ++; }
1976         ;
1977
1978
1979 %%
1980
1981 void
1982 My_lily_parser::set_yydebug (bool b)
1983 {
1984 #ifdef YYDEBUG
1985         yydebug = b;
1986 #endif
1987 }
1988
1989 extern My_lily_parser * current_parser;
1990
1991 void
1992 My_lily_parser::do_yyparse ()
1993 {
1994
1995         current_parser = this;;
1996         yyparse ((void*)this);
1997 }
1998
1999