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