]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
release: 1.3.139
[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                 sp-> set_mus_property ("context-type", ly_str02scm ( "Score"));
1126         }
1127         ;
1128
1129 command_req:
1130         shorthand_command_req   { $$ = $1; }
1131         | verbose_command_req semicolon { $$ = $1; }
1132         ;
1133
1134 shorthand_command_req:
1135         extender_req {
1136                 $$ = $1;
1137         }
1138         | hyphen_req {
1139                 $$ = $1;
1140         }
1141         | '|'                           {
1142                 $$ = new Barcheck_req;
1143         }
1144         | '~'   {
1145                 $$ = new Tie_req;
1146         }
1147         | '['           {
1148                 Span_req*b= new Span_req;
1149                 b->set_span_dir (START);
1150                 b->set_mus_property ("span-type", ly_str02scm ("beam"));
1151                 $$ =b;
1152         }
1153         | ']'           {
1154                 Span_req*b= new Span_req;
1155                 b->set_span_dir ( STOP);
1156                 b->set_mus_property ("span-type", ly_str02scm ("beam"));
1157                 $$ = b;
1158         }
1159         | BREATHE {
1160                 $$ = new Breathing_sign_req;
1161         }
1162         ;
1163
1164
1165 verbose_command_req:
1166         COMMANDSPANREQUEST bare_int STRING { /*TODO: junkme */
1167                 Span_req * sp_p = new Span_req;
1168                 sp_p-> set_span_dir ( Direction ($2));
1169                 sp_p->set_mus_property ("span-type",$3);
1170                 sp_p->set_spot (THIS->here_input ());
1171                 $$ = sp_p;
1172         }
1173         | MARK  {
1174                 Mark_req * m = new Mark_req;
1175                 $$ = m;
1176         }
1177         | MARK scalar {
1178                 Mark_req *m = new Mark_req;
1179                 m->set_mus_property ("label", $2);
1180                 $$ = m;
1181
1182         }
1183         | PENALTY bare_int      {
1184                 Break_req * b = new Break_req;
1185                 b->set_mus_property ("penalty", gh_double2scm ( $2 / 100.0));
1186                 b->set_spot (THIS->here_input ());
1187                 $$ = b;
1188         }
1189         | SKIP duration_length {
1190                 Skip_req * skip_p = new Skip_req;
1191                 skip_p->set_mus_property ("duration", $2);
1192
1193                 $$ = skip_p;
1194         }
1195         | tempo_request {
1196                 $$ = $1;
1197         }
1198         | KEY {
1199                 Key_change_req *key_p= new Key_change_req;
1200                 $$ = key_p;
1201         }
1202         | KEY NOTENAME_PITCH SCM_IDENTIFIER     {
1203                 Key_change_req *key_p= new Key_change_req;
1204                 
1205                 key_p->set_mus_property ("pitch-alist", $3);
1206  ((Music* )key_p)->transpose (* unsmob_pitch ($2));
1207                 $$ = key_p; 
1208         }
1209         ;
1210
1211 post_requests:
1212         {
1213                 $$ = new Link_array<Request>;
1214         }
1215         | post_requests post_request {
1216                 $2->set_spot (THIS->here_input ());
1217                 $$->push ($2);
1218         }
1219         ;
1220
1221 post_request:
1222         verbose_request
1223         | request_with_dir
1224         | close_request
1225         ;
1226
1227
1228 request_that_take_dir:
1229         gen_text_def
1230         | verbose_request
1231         | script_abbreviation {
1232                 SCM s = THIS->lexer_p_->lookup_identifier ("dash-" + ly_scm2string ($1));
1233                 Articulation_req *a = new Articulation_req;
1234                 if (gh_string_p (s))
1235                         a->set_mus_property ("articulation-type", s);
1236                 else THIS->parser_error (_ ("Expecting string as script definition"));
1237                 $$ = a;
1238         }
1239         ;
1240
1241 request_with_dir:
1242         script_dir request_that_take_dir        {
1243                 if (Script_req * gs = dynamic_cast<Script_req*> ($2))
1244                         gs->set_direction (Direction ($1));
1245                 else if ($1)
1246                         $2->origin ()->warning (_ ("Can't specify direction for this request"));
1247                 $$ = $2;
1248         }
1249         ;
1250         
1251 verbose_request:
1252         REQUEST_IDENTIFIER      {
1253                 $$ = dynamic_cast<Request*> (unsmob_music ($1)->clone ());
1254                 $$->set_spot (THIS->here_input ());
1255         }
1256         | DYNAMICSCRIPT embedded_scm {
1257                 /*
1258                         TODO: junkme, use text-type == dynamic
1259                 */
1260                 Text_script_req *d = new Text_script_req;
1261                 d->set_mus_property ("text-type" , ly_symbol2scm ("dynamic"));
1262                 d->set_mus_property ("text", $2);
1263                 d->set_spot (THIS->here_input ());
1264                 $$ = d;
1265         }
1266         | SPANREQUEST bare_int STRING {
1267                 Span_req * sp_p = new Span_req;
1268                 sp_p->set_span_dir ( Direction ($2));
1269                 sp_p->set_mus_property ("span-type", $3);
1270                 sp_p->set_spot (THIS->here_input ());
1271                 $$ = sp_p;
1272         }
1273         | tremolo_type  {
1274                Tremolo_req* a = new Tremolo_req;
1275                a->set_spot (THIS->here_input ());
1276                a->set_mus_property ("tremolo-type", gh_int2scm ($1));
1277                $$ = a;
1278         }
1279         | SCRIPT STRING         { 
1280                 Articulation_req * a = new Articulation_req;
1281                 a->set_mus_property ("articulation-type", $2);
1282                 a->set_spot (THIS->here_input ());
1283                 $$ = a;
1284         }
1285         /*
1286 duh, junk this syntax from the parser, if possible. 
1287         */
1288         | ARPEGGIO {
1289                 Arpeggio_req *a = new Arpeggio_req;
1290                 a->set_spot (THIS->here_input ());
1291                 $$ = a;
1292         }
1293         | GLISSANDO {
1294                 Glissando_req *g = new Glissando_req;
1295                 g->set_spot /* No pun intended */ (THIS->here_input ());
1296                 $$ = g;
1297         }       
1298         ;
1299
1300 sup_quotes:
1301         '\'' {
1302                 $$ = 1;
1303         }
1304         | sup_quotes '\'' {
1305                 $$ ++;
1306         }
1307         ;
1308
1309 sub_quotes:
1310         ',' {
1311                 $$ = 1;
1312         }
1313         | sub_quotes ',' {
1314                 $$ ++ ;
1315         }
1316         ;
1317
1318 steno_pitch:
1319         NOTENAME_PITCH  {
1320                 $$ = $1;
1321         }
1322         | NOTENAME_PITCH sup_quotes     {
1323                 Pitch p = *unsmob_pitch ($1);
1324                 p.octave_i_ +=  $2;
1325                 $$ = p.smobbed_copy ();
1326         }
1327         | NOTENAME_PITCH sub_quotes      {
1328                 Pitch p =* unsmob_pitch ($1);
1329
1330                 p.octave_i_ +=  -$2;
1331                 $$ = p.smobbed_copy ();
1332
1333         }
1334         ;
1335
1336 /*
1337 ugh. duplication
1338 */
1339
1340 steno_tonic_pitch:
1341         TONICNAME_PITCH {
1342                 $$ = $1;
1343         }
1344         | TONICNAME_PITCH sup_quotes    {
1345                 Pitch p = *unsmob_pitch ($1);
1346                 p.octave_i_ +=  $2;
1347                 $$ = p.smobbed_copy ();
1348         }
1349         | TONICNAME_PITCH sub_quotes     {
1350                 Pitch p =* unsmob_pitch ($1);
1351
1352                 p.octave_i_ +=  -$2;
1353                 $$ = p.smobbed_copy ();
1354
1355         }
1356         ;
1357
1358 pitch:
1359         steno_pitch {
1360                 $$ = $1;
1361         }
1362         | explicit_pitch {
1363                 $$ = $1;
1364         }
1365         ;
1366
1367 explicit_pitch:
1368         PITCH embedded_scm {
1369                 $$ = $2;
1370                 if (!unsmob_pitch ($2)) {
1371                         THIS->parser_error (_f ("Expecting musical-pitch value", 3));
1372                          $$ = Pitch ().smobbed_copy ();
1373                 }
1374         }
1375         ;
1376
1377 explicit_duration:
1378         DURATION embedded_scm   {
1379                 $$ = $2;
1380                 if (!unsmob_duration ($2))
1381                 {
1382                         THIS->parser_error (_ ("Must have duration object"));
1383                         $$ = Duration ().smobbed_copy ();
1384                 }
1385         }
1386         ;
1387
1388 extender_req:
1389         EXTENDER {
1390                 if (!THIS->lexer_p_->lyric_state_b ())
1391                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
1392                 $$ = new Extender_req;
1393         }
1394         ;
1395
1396 hyphen_req:
1397         HYPHEN {
1398                 if (!THIS->lexer_p_->lyric_state_b ())
1399                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
1400                 $$ = new Hyphen_req;
1401         }
1402         ;
1403
1404 close_request:
1405         close_request_parens {
1406                 $$ = $1;
1407                 dynamic_cast<Span_req*> ($$)->set_span_dir ( START);
1408         }
1409         
1410 close_request_parens:
1411         '('     {
1412                 Span_req* s= new Span_req;
1413                 $$ = s;
1414                 s->set_mus_property ("span-type", ly_str02scm ( "slur"));
1415         }
1416         | E_OPEN        {
1417                 Span_req* s= new Span_req;
1418                 $$ = s;
1419                 s->set_mus_property ("span-type", ly_str02scm ( "phrasing-slur"));
1420         }
1421         | E_SMALLER {
1422                 Span_req*s =new Span_req;
1423                 $$ = s;
1424                 s->set_mus_property ("span-type", ly_str02scm ( "crescendo"));
1425         }
1426         | E_BIGGER {
1427                 Span_req*s =new Span_req;
1428                 $$ = s;
1429                 s->set_mus_property ("span-type", ly_str02scm ("decrescendo"));
1430         }
1431         ;
1432
1433
1434 open_request:
1435         open_request_parens {
1436                 $$ = $1;
1437                 dynamic_cast<Span_req*> ($$)->set_span_dir (STOP);
1438         }
1439         ;
1440
1441 open_request_parens:
1442         E_EXCLAMATION   {
1443                 Span_req *s =  new Span_req;
1444                 s->set_mus_property ("span-type", ly_str02scm ( "crescendo"));
1445
1446                 $$ = s;
1447         }
1448         | ')'   {
1449                 Span_req* s= new Span_req;
1450                 $$ = s;
1451                 s->set_mus_property ("span-type", ly_str02scm ( "slur"));
1452         }
1453         | E_CLOSE       {
1454                 Span_req* s= new Span_req;
1455                 $$ = s;
1456                 s->set_mus_property ("span-type", ly_str02scm ( "phrasing-slur"));
1457         }
1458         ;
1459
1460 gen_text_def:
1461         embedded_scm {
1462                 Text_script_req *t = new Text_script_req;
1463                 t->set_mus_property ("text", $1);
1464                 t->set_spot (THIS->here_input ());
1465                 $$ = t;
1466         }
1467         | string {
1468                 Text_script_req *t = new Text_script_req;
1469                 t->set_mus_property ("text", $1);
1470                 t->set_spot (THIS->here_input ());
1471                 $$ = t;
1472         }
1473         | DIGIT {
1474                 String ds = to_str ($1);
1475                 Text_script_req* t = new Text_script_req;
1476
1477                 t->set_mus_property ("text",  ly_str02scm (ds.ch_C ()));
1478                 t->set_mus_property ("text-type" , ly_symbol2scm ("finger"));
1479                 t->set_spot (THIS->here_input ());
1480                 $$ = t;
1481         }
1482         ;
1483
1484 script_abbreviation:
1485         '^'             {
1486                 $$ = gh_str02scm ("hat");
1487         }
1488         | '+'           {
1489                 $$ = gh_str02scm ("plus");
1490         }
1491         | '-'           {
1492                 $$ = gh_str02scm ("dash");
1493         }
1494         | '|'           {
1495                 $$ = gh_str02scm ("bar");
1496         }
1497         | '>'           {
1498                 $$ = gh_str02scm ("larger");
1499         }
1500         | '.'           {
1501                 $$ = gh_str02scm ("dot");
1502         }
1503         ;
1504
1505
1506 script_dir:
1507         '_'     { $$ = DOWN; }
1508         | '^'   { $$ = UP; }
1509         | '-'   { $$ = CENTER; }
1510         ;
1511
1512 pre_requests:
1513         {
1514                 $$ = new Link_array<Request>;
1515         }
1516         | pre_requests open_request {
1517                 $$->push ($2);
1518         }
1519         ;
1520
1521 absolute_pitch:
1522         steno_pitch     {
1523                 $$ = $1;
1524         }
1525         ;
1526
1527 duration_length:
1528         steno_duration {
1529                 $$ = $1;
1530         }
1531         | explicit_duration {
1532                 $$ = $1;
1533         }       
1534         | duration_length '*' bare_unsigned {
1535                 $$ = unsmob_duration ($$)->compressed ( $3) .smobbed_copy ();
1536         }
1537         | duration_length '/' bare_unsigned {
1538                 $$ = unsmob_duration ($$)->compressed (Moment (1,$3)).smobbed_copy ();
1539         }
1540         ;
1541
1542 entered_notemode_duration:
1543         steno_duration  {
1544                 THIS->set_last_duration (unsmob_duration ($1));
1545         }
1546         ;
1547
1548 optional_notemode_duration:
1549         {
1550                 $$ = THIS->default_duration_.smobbed_copy ();
1551         }
1552         | entered_notemode_duration {
1553                 $$ = $1;
1554         }
1555         | explicit_duration {
1556                 $$ = $1;
1557         }       
1558         ;
1559
1560 steno_duration:
1561         bare_unsigned dots              {
1562                 int l = 0;
1563                 if (!is_duration_b ($1))
1564                         THIS->parser_error (_f ("not a duration: %d", $1));
1565                 else
1566                         l =  intlog2 ($1);
1567
1568                 $$ = Duration (l, $2).smobbed_copy ();
1569         }
1570         | DURATION_IDENTIFIER dots      {
1571                 Duration *d =unsmob_duration ($1);
1572                 Duration k (d->duration_log (),d->dot_count () + $2);
1573                 $$ = k.smobbed_copy ();         
1574         }
1575         ;
1576
1577 dots:
1578         /* empty */     {
1579                 $$ = 0;
1580         }
1581         | dots '.' {
1582                 $$ ++;
1583         }
1584         ;
1585
1586
1587 tremolo_type: 
1588         ':'     {
1589                 $$ =0;
1590         }
1591         | ':' bare_unsigned {
1592                 if (!is_duration_b ($2))
1593                         THIS->parser_error (_f ("not a duration: %d", $2));
1594                 $$ = $2;
1595         }
1596         ;
1597
1598
1599 simple_element:
1600         pitch exclamations questions optional_notemode_duration {
1601                 if (!THIS->lexer_p_->note_state_b ())
1602                         THIS->parser_error (_ ("Have to be in Note mode for notes"));
1603
1604                 Note_req *n = new Note_req;
1605                 
1606                 n->set_mus_property ("pitch", $1);
1607                 n->set_mus_property ("duration", $4);
1608
1609                 if ($3 % 2)
1610                         n->set_mus_property ("cautionary", SCM_BOOL_T);
1611                 if ($2 % 2 || $3 % 2)
1612                         n->set_mus_property ("force-accidental", SCM_BOOL_T);
1613
1614                 Simultaneous_music*v = new Request_chord (SCM_EOL);
1615                 v->set_mus_property ("elements", gh_list (n->self_scm (), SCM_UNDEFINED));
1616                 
1617 /*
1618 FIXME: location is one off, since ptich & duration don't contain origin refs. 
1619 */
1620                 v->set_spot (THIS->here_input ());
1621                 n->set_spot (THIS->here_input ());
1622
1623                 $$ = v;
1624         }
1625         | RESTNAME optional_notemode_duration           {
1626
1627                 SCM e = SCM_UNDEFINED;
1628                   if (ly_scm2string ($1) =="s")
1629                     { /* Space */
1630                       Skip_req * skip_p = new Skip_req;
1631                       skip_p->set_mus_property ("duration" ,$2);
1632
1633                       skip_p->set_spot (THIS->here_input ());
1634                         e = skip_p->self_scm ();
1635                     }
1636                   else
1637                     {
1638                       Rest_req * rest_req_p = new Rest_req;
1639                       rest_req_p->set_mus_property ("duration", $2);
1640                       rest_req_p->set_spot (THIS->here_input ());
1641                         e = rest_req_p->self_scm ();
1642                     }
1643                   Simultaneous_music* velt_p = new Request_chord (SCM_EOL);
1644                 velt_p-> set_mus_property ("elements", gh_list (e,SCM_UNDEFINED));
1645                   velt_p->set_spot (THIS->here_input ());
1646
1647
1648                   $$ = velt_p;
1649         }
1650         | MULTI_MEASURE_REST optional_notemode_duration         {
1651                 Skip_req * sk = new Skip_req;
1652                 sk->set_mus_property ("duration", $2);
1653                 Span_req *sp1 = new Span_req;
1654                 Span_req *sp2 = new Span_req;
1655                 sp1-> set_span_dir ( START);
1656                 sp2-> set_span_dir ( STOP);
1657                 SCM r = ly_str02scm ("rest");
1658                 sp1->set_mus_property ("span-type", r);
1659                 sp2->set_mus_property ("span-type", r);
1660
1661                 Request_chord * rqc1 = new Request_chord (SCM_EOL);
1662                 rqc1->set_mus_property ("elements", gh_list (sp1->self_scm (), SCM_UNDEFINED));
1663                 Request_chord * rqc2 = new Request_chord (SCM_EOL);
1664                 rqc2->set_mus_property ("elements", gh_list (sk->self_scm (), SCM_UNDEFINED));;
1665                 Request_chord * rqc3 = new Request_chord (SCM_EOL);
1666                 rqc3->set_mus_property ("elements", gh_list (sp2->self_scm (), SCM_UNDEFINED));;
1667
1668                 SCM ms = gh_list (rqc1->self_scm (), rqc2->self_scm (), rqc3->self_scm (), SCM_UNDEFINED);
1669
1670                 $$ = new Sequential_music (SCM_EOL);
1671                 $$->set_mus_property ("elements", ms);
1672         }
1673         | STRING { 
1674                 THIS->remember_spot ();
1675         } 
1676         /* cont */
1677         optional_notemode_duration      {
1678                 if (!THIS->lexer_p_->lyric_state_b ()) {
1679                         THIS->pop_spot ().error (_ ("Have to be in Lyric mode for lyrics"));
1680                         THIS->error_level_i_  = 1;
1681                         THIS->parser_error (_ ("Giving up"));
1682                 } 
1683                 else
1684                         THIS->pop_spot ();
1685                 Lyric_req* lreq_p = new Lyric_req;
1686                 lreq_p->set_mus_property ("text", $1);
1687                 lreq_p->set_mus_property ("duration",$3);
1688                 lreq_p->set_spot (THIS->here_input ());
1689                 Simultaneous_music* velt_p = new Request_chord (SCM_EOL);
1690                 velt_p->set_mus_property ("elements", gh_list (lreq_p->self_scm (), SCM_UNDEFINED));
1691
1692
1693                 $$= velt_p;
1694
1695         }
1696         | chord {
1697                 if (!THIS->lexer_p_->chord_state_b ())
1698                         THIS->parser_error (_ ("Have to be in Chord mode for chords"));
1699                 $$ = $1;
1700         }
1701         ;
1702
1703
1704 chord:
1705         steno_tonic_pitch optional_notemode_duration chord_additions chord_subtractions chord_inversion chord_bass {
1706                 $$ = Chord::get_chord ($1, $3, $4, $5, $6, $2);
1707                 $$->set_spot (THIS->here_input ());
1708         };
1709
1710 chord_additions: 
1711         {
1712                 $$ = SCM_EOL;
1713         } 
1714         | CHORD_COLON chord_notes {
1715                 $$ = $2;
1716         }
1717         ;
1718
1719 chord_notes:
1720         chord_step {
1721                 $$ = $1
1722         }
1723         | chord_notes '.' chord_step {
1724                 $$ = gh_append2 ($$, $3);
1725         }
1726         ;
1727
1728 chord_subtractions: 
1729         {
1730                 $$ = SCM_EOL;
1731         } 
1732         | CHORD_CARET chord_notes {
1733                 $$ = $2;
1734         }
1735         ;
1736
1737
1738 chord_inversion:
1739         {
1740                 $$ = SCM_EOL;
1741         }
1742         | '/' steno_tonic_pitch {
1743                 $$ = $2;
1744         }
1745         ;
1746
1747 chord_bass:
1748         {
1749                 $$ = SCM_EOL;
1750         }
1751         | CHORD_BASS steno_tonic_pitch {
1752                 $$ = $2;
1753         }
1754         ;
1755
1756 chord_step:
1757         chord_note {
1758                 $$ = gh_cons ($1, SCM_EOL);
1759         }
1760         | CHORDMODIFIER_PITCH {
1761                 $$ = gh_cons ($1, SCM_EOL);
1762         }
1763         | CHORDMODIFIER_PITCH chord_note { /* Ugh. */
1764                 $$ = gh_list ($1, $2, SCM_UNDEFINED);
1765         }
1766         ;
1767
1768 chord_note:
1769         bare_unsigned {
1770                  Pitch m;
1771                 m.notename_i_ = ($1 - 1) % 7;
1772                 m.octave_i_ = $1 > 7 ? 1 : 0;
1773                 m.alteration_i_ = 0;
1774
1775                 $$ = m.smobbed_copy ();
1776         } 
1777         | bare_unsigned '+' {
1778                 Pitch m;
1779                 m.notename_i_ = ($1 - 1) % 7;
1780                 m.octave_i_ = $1 > 7 ? 1 : 0;
1781                 m.alteration_i_ = 1;
1782
1783
1784                 $$ = m.smobbed_copy ();
1785         }
1786         | bare_unsigned CHORD_MINUS {
1787                 Pitch m;
1788                 m.notename_i_ = ($1 - 1) % 7;
1789                 m.octave_i_ = $1 > 7 ? 1 : 0;
1790                 m.alteration_i_ = -1;
1791
1792                 $$ = m.smobbed_copy ();
1793         }
1794         ;
1795
1796 /*
1797         UTILITIES
1798  */
1799 number_expression:
1800         bare_number {
1801                 $$ = $1;
1802         }
1803         | '-'  number_expression %prec UNARY_MINUS {
1804                 $$ = scm_difference ($2, SCM_UNDEFINED);
1805         }
1806         | number_expression '*' number_expression {
1807                 $$ = scm_product ($1, $3);
1808         }
1809         | number_expression '/' number_expression {
1810                 $$ = scm_divide ($1, $3);
1811         }
1812         | number_expression '+' number_expression {
1813                 $$ = scm_sum ($1, $3);
1814         }
1815         | number_expression '-' number_expression {
1816                 $$ = scm_difference ($1, $3);
1817         }
1818         | '(' number_expression ')'     {
1819                 $$ = $2;
1820         }
1821         ;
1822
1823 bare_number:
1824         UNSIGNED        {
1825                 $$ = gh_int2scm ($1);
1826         }
1827         | DIGIT         {
1828                 $$ = gh_int2scm ($1);
1829         }
1830         | REAL          {
1831                 $$ = gh_double2scm ($1);
1832         }
1833         | NUMBER_IDENTIFIER             {
1834                 $$ = $1;
1835         }
1836         | REAL CM_T     {
1837                 $$ = gh_double2scm ($1 CM);
1838         }
1839         | REAL PT_T     {
1840                 $$ = gh_double2scm ($1 PT);
1841         }
1842         | REAL IN_T     {
1843                 $$ = gh_double2scm ($1 INCH);
1844         }
1845         | REAL MM_T     {
1846                 $$ = gh_double2scm ($1 MM);
1847         }
1848         | REAL CHAR_T   {
1849                 $$ = gh_double2scm ($1 CHAR);
1850         }
1851         ;
1852
1853
1854 bare_unsigned:
1855         bare_number {
1856                 if (scm_integer_p ($1) == SCM_BOOL_T) {
1857                         $$ = gh_scm2int ($1);
1858
1859                 } else {
1860                         THIS->parser_error (_ ("need integer number arg"));
1861                         $$ = 0;
1862                 }
1863                 if ($$ < 0) {
1864                         THIS->parser_error (_ ("Must be positive integer"));
1865                         $$ = -$$;
1866                         }
1867
1868         }
1869         ;
1870 bare_int:
1871         bare_number {
1872                 if (scm_integer_p ($1) == SCM_BOOL_T)
1873                 {
1874                         int k = gh_scm2int ($1);
1875                         $$ = k;
1876                 } else
1877                 {
1878                         THIS->parser_error (_ ("need integer number arg"));
1879                         $$ = 0;
1880                 }
1881         }
1882         | '-' bare_int {
1883                 $$ = -$2;
1884         }
1885         ;
1886
1887
1888 string:
1889         STRING          {
1890                 $$ = $1;
1891         }
1892         | STRING_IDENTIFIER     {
1893                 $$ = $1;
1894         }
1895         | string '+' string {
1896                 $$ = scm_string_append (scm_listify ($1, $3, SCM_UNDEFINED));
1897         }
1898         ;
1899
1900
1901 exclamations:
1902                 { $$ = 0; }
1903         | exclamations '!'      { $$ ++; }
1904         ;
1905
1906 questions:
1907                 { $$ = 0; }
1908         | questions '?' { $$ ++; }
1909         ;
1910
1911
1912 semicolon:
1913         ';'
1914         ;
1915
1916 %%
1917
1918 void
1919 My_lily_parser::set_yydebug (bool b)
1920 {
1921 #ifdef YYDEBUG
1922         yydebug = b;
1923 #endif
1924 }
1925
1926 extern My_lily_parser * current_parser;
1927
1928 void
1929 My_lily_parser::do_yyparse ()
1930 {
1931
1932         current_parser = this;;
1933         yyparse ((void*)this);
1934 }
1935
1936