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