]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
1ebb6f2f045a8adc5a4e0f67ec9ee84d9b619e48
[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--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9            Jan Nieuwenhuizen <janneke@gnu.org>
10 */
11
12 /*
13   Four shift/reduce problems:
14
15 1.      foo = bar.
16
17         "bar" -> String -> Lyric -> Music -> music-assignment
18
19         "bar" -> String -> string-assignment
20
21
22 Similar problem for
23
24  * \markup identifier.
25  * \markup { }
26
27
28 2.  \repeat
29         \repeat .. \alternative
30
31
32     \repeat { \repeat .. \alternative }
33
34 or
35
36     \repeat { \repeat } \alternative 
37
38 )
39
40 --hwn
41
42  */
43
44 /*
45
46 TODO:
47
48 * The rules for who is protecting what are very shady. Uniformise
49   this.
50
51 * There are too many lexical modes?
52
53 */
54
55 #include <ctype.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58
59
60 #include "scm-option.hh"
61 #include "translator-def.hh"
62 #include "lily-guile.hh"
63 #include "misc.hh"
64 #include "my-lily-lexer.hh"
65 #include "paper-def.hh"
66 #include "midi-def.hh"
67 #include "main.hh"
68 #include "file-path.hh"
69 #include "warn.hh"
70 #include "dimensions.hh"
71 #include "my-lily-parser.hh"
72 #include "score.hh"
73 #include "input-file-results.hh"
74 #include "input.hh"
75 #include "lilypond-input-version.hh"
76 #include "scm-hash.hh"
77 #include "auto-change-iterator.hh"
78 #include "ly-modules.hh"
79 #include "music-sequence.hh"
80 #include "input-smob.hh"
81 #include "event.hh"
82 #include "text-item.hh"
83 #include "music-list.hh"
84
85
86 #define MY_MAKE_MUSIC(x)  make_music_by_name (ly_symbol2scm (x))
87
88
89
90 #define YYERROR_VERBOSE 1
91
92 My_lily_parser* my_lily_parser;
93 #define YYPARSE_PARAM my_lily_parser
94 #define YYLEX_PARAM my_lily_parser
95 #define THIS\
96         ((My_lily_parser *) my_lily_parser)
97
98 #define yyerror THIS->parser_error
99
100 /*
101   Add symbols to the  TAGS field of a music object. 
102 */
103
104 void
105 tag_music (Music*m,  SCM tag, Input ip)
106 {
107         SCM tags = m->get_mus_property ("tags");
108         if (gh_symbol_p (tag))
109                 tags = scm_cons (tag, tags);
110         else if (gh_list_p (tag))
111                 tags = gh_append2 (tag, tags);
112         else
113                 ip.warning (_("Tag must be symbol or list of symbols."));
114
115         m->set_mus_property ("tags", tags);
116 }
117
118
119
120 bool
121 regular_identifier_b (SCM id)
122 {
123   String str = ly_scm2string (id);
124   char const *s = str.to_str0 () ;
125
126   bool v = true;
127   while (*s && v)
128    {
129         v = v && isalpha (*s);
130         s++;
131    }
132   return v;
133 }
134
135 SCM
136 make_simple_markup (SCM a)
137 {
138         static SCM simple;
139         if (!simple)
140         simple = scm_c_eval_string ("simple-markup");
141
142         return scm_list_n (simple, a, SCM_UNDEFINED);
143 }
144
145
146 bool
147 is_duration_b (int t)
148 {
149   return t && t == 1 << intlog2 (t);
150 }
151
152 void
153 set_music_properties (Music *p, SCM a)
154 {
155   for (SCM k = a; gh_pair_p (k); k = ly_cdr (k))
156         {
157         p->internal_set_mus_property (ly_caar (k), ly_cdar (k));
158         }
159 }
160
161 SCM
162 make_chord_step (int step, int alter)
163 {
164         if (step == 7)
165                 alter--;
166
167         /* ugh: fucks up above 13 */
168         Pitch m(step > 7 ? 1 : 0,(step - 1) % 7, alter);
169         return m.smobbed_copy ();
170 }
171
172
173 SCM
174 make_chord (SCM pitch, SCM dur, SCM modification_list)
175 {
176         static SCM chord_ctor;
177         if (!chord_ctor)
178                 chord_ctor= scm_c_eval_string ("construct-chord");
179         SCM ch=  scm_call_3 (chord_ctor, pitch, dur, modification_list);
180         scm_gc_protect_object (ch);
181         return ch;
182 }
183
184 /*
185   Todo: actually also use apply iso. call too ... 
186 */
187 bool
188 ly_input_procedure_p (SCM x)
189 {
190         return gh_procedure_p (x)
191                 || (gh_pair_p (x) && gh_procedure_p (gh_car (x)));
192 }
193
194 Music* 
195 set_property_music (SCM sym, SCM value)
196 {
197         Music * p = MY_MAKE_MUSIC("PropertySet");
198         p->set_mus_property ("symbol", sym);
199         p->set_mus_property ("value", value);
200         return p;
201 }
202
203 %}
204
205 /* We use SCMs to do strings, because it saves us the trouble of
206 deleting them.  Let's hope that a stack overflow doesnt trigger a move
207 of the parse stack onto the heap. */
208
209
210 %union {
211         String * string;
212     Music *music;
213     Score *score;
214     Music_output_def * outputdef;
215     SCM scm;
216     int i;
217 }
218 %{
219
220 int
221 yylex (YYSTYPE *s,  void * v)
222 {
223         My_lily_parser   *pars = (My_lily_parser*) v;
224         My_lily_lexer * lex = pars->lexer_;
225
226         lex->lexval = (void*) s;
227         lex->prepare_for_next_token();
228         return lex->yylex ();
229 }
230
231
232 %}
233
234 %pure_parser
235
236
237 %token ACCEPTS
238 %token ADDLYRICS
239 %token ALIAS
240 %token ALTERNATIVE
241 %token APPLY
242 %token APPLYCONTEXT
243 %token APPLYOUTPUT
244 %token AUTOCHANGE
245 %token BAR
246 %token BREATHE
247 %token CHORDMODIFIERS  
248 %token CHORDS
249 %token LESSLESS
250 %token MOREMORE
251 %token CLEF
252 %token COMMANDSPANREQUEST
253 %token CONSISTS
254 %token CONSISTSEND
255 %token CONTEXT
256 %token DEFAULT
257 %token DENIES
258 %token DESCRIPTION
259 %token EXTENDER
260 %token FIGURES FIGURE_OPEN FIGURE_CLOSE
261 %token FIGURE_BRACKET_CLOSE FIGURE_BRACKET_OPEN
262 %token GRACE 
263 %token ACCIACCATURA
264 %token APPOGGIATURA 
265 %token GROBDESCRIPTIONS
266 %token HEADER
267 %token HYPHEN
268 %token INVALID
269 %token KEY
270 %token LYRICS
271 %token MARK
272 %token MIDI
273 %token MULTI_MEASURE_REST
274 %token NAME
275 %token NEWCONTEXT
276 %token NOTES
277 %token OCTAVE
278 %token ONCE
279 %token OVERRIDE SET REVERT 
280 %token PAPER
281 %token PARTCOMBINE
282 %token PARTIAL
283 %token PITCHNAMES
284 %token PROPERTY
285 %token RELATIVE
286 %token REMOVE
287 %token REPEAT
288 %token REST
289 %token SCM_T
290 %token SCORE
291 %token SEQUENTIAL
292 %token SIMULTANEOUS
293 %token SKIP
294 %token SPANREQUEST
295 %token TAG
296 %token TEMPO
297 %token TIMES
298 %token TIME_T
299 %token TRANSLATOR
300 %token TRANSPOSE
301 %token TYPE
302 %token UNSET
303
304
305 /* escaped */
306 %token E_CHAR E_EXCLAMATION E_SMALLER E_BIGGER E_OPEN E_CLOSE
307 %token E_LEFTSQUARE E_RIGHTSQUARE E_TILDE
308 %token E_BACKSLASH
309 %token <i> E_UNSIGNED
310 %token CHORD_BASS CHORD_COLON CHORD_MINUS CHORD_CARET  CHORD_SLASH
311 %token FIGURE_SPACE
312
313 %type <i>       exclamations questions dots optional_rest
314 %type <i>        bass_mod
315 %type <scm>     grace_head
316 %type <scm>     lyric_element
317 %type <scm>     bass_number br_bass_figure bass_figure figure_list figure_spec
318 %token <i>      DIGIT
319 %token <scm>    NOTENAME_PITCH
320 %token <scm>    TONICNAME_PITCH
321 %token <scm>    CHORDMODIFIER_PITCH
322 %token <scm>    DURATION_IDENTIFIER
323 %token <scm>    FRACTION
324 %token <id>     IDENTIFIER
325 %token <scm>    CHORDNAMES
326
327 %token <scm> CHORD_MODIFIER
328
329 %token <scm>    SCORE_IDENTIFIER
330 %token <scm>    MUSIC_OUTPUT_DEF_IDENTIFIER
331 %token <scm>    NUMBER_IDENTIFIER
332 %token <scm>    EVENT_IDENTIFIER
333 %token <scm>    MUSIC_IDENTIFIER TRANSLATOR_IDENTIFIER
334 %token <scm>    STRING_IDENTIFIER SCM_IDENTIFIER 
335 %token <scm>    RESTNAME
336 %token <scm>    STRING   
337 %token <scm>    SCM_T
338 %token <i>      UNSIGNED
339 %token <scm>   REAL
340
341 %token MARKUP
342 %token <scm> MARKUP_HEAD_MARKUP0
343 %token <scm> MARKUP_HEAD_MARKUP0_MARKUP1
344 %token <scm> MARKUP_HEAD_SCM0
345 %token <scm> MARKUP_HEAD_SCM0_MARKUP1
346 %token <scm> MARKUP_HEAD_SCM0_SCM1 
347 %token <scm> MARKUP_HEAD_SCM0_SCM1_SCM2 
348 %token <scm> MARKUP_HEAD_SCM0_SCM1_MARKUP2
349
350 %token <scm> MARKUP_IDENTIFIER MARKUP_HEAD_LIST0
351 %type <scm> markup markup_line markup_list  markup_list_body full_markup 
352
353 %type <outputdef> output_def
354 %type <scm>     lilypond_header lilypond_header_body
355 %type <music>   open_event close_event
356 %type <i>       sub_quotes sup_quotes
357 %type <music>   simple_element  event_chord command_element Simple_music  Composite_music 
358 %type <music>   Repeated_music
359 %type <scm>     Alternative_music
360 %type <i>       tremolo_type
361 %type <i>       bare_int  bare_unsigned
362 %type <i>       script_dir
363 %type <scm>     identifier_init 
364
365 %type <music> note_chord_element chord_body chord_body_element
366 %type <scm>  chord_body_elements 
367 %type <scm> steno_duration optional_notemode_duration multiplied_duration
368         
369 %type <scm>   post_events 
370 %type <music> gen_text_def direction_less_event direction_reqd_event
371 %type <scm>   steno_pitch pitch absolute_pitch pitch_also_in_chords
372 %type <scm>    steno_tonic_pitch
373 %type <scm>     duration_length fraction
374
375 %type <scm> new_chord step_number chord_items chord_item chord_separator step_numbers
376
377 %type <scm>  embedded_scm scalar
378 %type <music>   Music Sequential_music Simultaneous_music 
379 %type <music>   relative_music re_rhythmed_music part_combined_music
380 %type <music>   property_def translator_change  simple_property_def
381 %type <scm> Music_list
382 %type <outputdef>  music_output_def_body
383 %type <music> shorthand_command_req
384 %type <music>   post_event tagged_post_event
385 %type <music> command_req verbose_command_req
386 %type <music>   extender_req
387 %type <music> hyphen_req
388 %type <music> string_number_event
389 %type <scm>     string bare_number number_expression number_term number_factor 
390 %type <score>   score_block score_body
391
392 %type <scm>     translator_spec_block translator_spec_body
393 %type <music>   tempo_event
394 %type <scm> notenames_body notenames_block chordmodifiers_block
395 %type <scm>     script_abbreviation
396
397
398
399 %left '-' '+'
400
401 /* We don't assign precedence to / and *, because we might need varied
402 prec levels in different prods */
403
404 %left UNARY_MINUS
405
406 %%
407
408 lilypond:       /* empty */
409         | lilypond toplevel_expression {}
410         | lilypond assignment  { }
411         | lilypond error {
412                 THIS->error_level_  = 1;
413         }
414         | lilypond INVALID      {
415                 THIS->error_level_  = 1;
416         }
417         ;
418
419 toplevel_expression:
420         notenames_block                 {
421                 THIS->lexer_->pitchname_tab_ =  $1;
422         }
423         | chordmodifiers_block                  {
424                 THIS->lexer_->chordmodifier_tab_  = $1;
425         }
426         | lilypond_header {
427                 THIS->input_file_->header_ = $1;
428         }
429         | score_block {
430                 THIS->input_file_->scores_.push ($1);
431         }
432         | output_def {
433                 if (dynamic_cast<Paper_def*> ($1))
434                         THIS->lexer_->set_identifier (scm_makfrom0str ("$defaultpaper"), $1->self_scm ());
435                 else if (dynamic_cast<Midi_def*> ($1))
436                         THIS->lexer_->set_identifier (scm_makfrom0str ("$defaultmidi"), $1->self_scm ());
437         }
438         ;
439
440 embedded_scm:
441         SCM_T
442         | SCM_IDENTIFIER 
443         ;
444
445
446 chordmodifiers_block:
447         CHORDMODIFIERS notenames_body   {  $$ = $2; }
448         ;
449
450 notenames_block:
451         PITCHNAMES notenames_body   {  $$ = $2; }
452         ;
453
454 notenames_body:
455         embedded_scm    {
456           int i = scm_ilength ($1);
457
458           SCM tab = scm_make_vector (gh_int2scm (i), SCM_EOL);
459           for (SCM s = $1; gh_pair_p (s); s = ly_cdr (s)) {
460                 SCM pt = ly_cdar (s);
461                 scm_hashq_set_x (tab, ly_caar (s), pt);
462           }
463           $$ = tab;
464         }
465         ;
466
467 lilypond_header_body:
468         {
469                 $$ = ly_make_anonymous_module (); 
470                 THIS->lexer_->add_scope ($$);
471         }
472         | lilypond_header_body assignment  { 
473                 
474         }
475         ;
476
477 lilypond_header:
478         HEADER '{' lilypond_header_body '}'     {
479                 $$ = THIS->lexer_-> remove_scope();
480         }
481         ;
482
483
484 /*
485         DECLARATIONS
486 */
487 assignment:
488         STRING {
489                 THIS->push_spot ();
490         }
491         /* cont */ '=' identifier_init  {
492
493         /*
494                 Should find generic way of associating input with objects.
495         */
496                 Input ip = THIS->pop_spot ();
497
498                 if (! regular_identifier_b ($1))
499                 {
500                         ip.warning (_ ("Identifier should have alphabetic characters only"));
501                 }
502
503                 THIS->lexer_->set_identifier ($1, $4);
504
505 /*
506  TODO: devise standard for protection in parser.
507
508   The parser stack lives on the C-stack, which means that
509 all objects can be unprotected as soon as they're here.
510
511 */
512         }
513         | embedded_scm { }
514         ;
515
516
517
518 identifier_init:
519         score_block {
520                 $$ = $1->self_scm ();
521                 scm_gc_unprotect_object ($$);
522         }
523         | full_markup {
524                 $$ = $1;
525         }
526         | output_def {
527                 $$ = $1->self_scm ();
528                 scm_gc_unprotect_object ($$);
529         }
530         | translator_spec_block {
531                 $$ = $1;
532         }
533         | Music  {
534                 $$ = $1->self_scm ();
535                 scm_gc_unprotect_object ($$);
536         }
537         | post_event {
538                 $$ = $1->self_scm ();
539                 scm_gc_unprotect_object ($$);
540         }
541         | number_expression {
542                 $$ = $1;
543         }
544         | string {
545                 $$ = $1;
546         }
547         | embedded_scm  {
548                 $$ = $1;
549         }
550         ;
551
552 translator_spec_block:
553         TRANSLATOR '{' translator_spec_body '}'
554                 {
555                 $$ = $3;
556         }
557         ;
558
559 translator_spec_body:
560         TRANSLATOR_IDENTIFIER   {
561                 $$ = $1;
562                 unsmob_translator_def ($$)-> set_spot (THIS->here_input ());
563         }
564         | TYPE STRING   {
565                 $$ = Translator_def::make_scm ();
566                 Translator_def*td =  unsmob_translator_def ($$);
567                 td->translator_group_type_ = $2;
568                 td->set_spot (THIS->here_input ());
569         }
570         | translator_spec_body DESCRIPTION string  {
571                 unsmob_translator_def ($$)->description_ = $3;
572         }
573         | translator_spec_body STRING '=' embedded_scm                  {
574                 unsmob_translator_def ($$)->add_property_assign ($2, $4);
575         }
576         | translator_spec_body STRING OVERRIDE embedded_scm '=' embedded_scm {
577                 unsmob_translator_def ($$)
578                         ->add_push_property (scm_string_to_symbol ($2), $4, $6);
579         }
580         | translator_spec_body STRING SET embedded_scm '=' embedded_scm {
581                 unsmob_translator_def ($$)
582                         ->add_push_property (scm_string_to_symbol ($2), $4, $6);
583         }
584         | translator_spec_body STRING REVERT embedded_scm  {
585           unsmob_translator_def ($$)->add_pop_property (
586                 scm_string_to_symbol ($2), $4);
587         }
588         | translator_spec_body NAME STRING  {
589                 unsmob_translator_def ($$)->type_name_ = scm_string_to_symbol ($3);
590         }
591         | translator_spec_body CONSISTS STRING  {
592                 unsmob_translator_def ($$)->add_element ($3);
593         }
594         | translator_spec_body ALIAS STRING  {
595                 Translator_def*td = unsmob_translator_def ($$);
596                 td->type_aliases_ = scm_cons (scm_string_to_symbol ($3), td->type_aliases_);
597         }
598         | translator_spec_body GROBDESCRIPTIONS embedded_scm {
599                 Translator_def*td = unsmob_translator_def($$);
600                 // td->add_property_assign (ly_symbol2scm ("allGrobDescriptions"), $3);
601                 for (SCM p = $3; gh_pair_p (p); p = ly_cdr (p))
602                         td->add_property_assign (scm_symbol_to_string (ly_caar (p)), ly_cdar (p));
603         }
604         | translator_spec_body CONSISTSEND STRING  {
605                 unsmob_translator_def ($$)->add_last_element ( $3);
606         }
607         | translator_spec_body ACCEPTS STRING  {
608                 unsmob_translator_def ($$)->set_acceptor (scm_string_to_symbol ($3), true);
609         }
610         | translator_spec_body DENIES STRING  {
611                 unsmob_translator_def ($$)->set_acceptor (scm_string_to_symbol ($3), false);
612         }
613         | translator_spec_body REMOVE STRING  {
614                 unsmob_translator_def ($$)->remove_element ($3);
615         }
616         ;
617
618 /*
619         SCORE
620 */
621 score_block:
622         SCORE { 
623                 THIS->push_spot ();
624         }
625         /*cont*/ '{' score_body '}'     {
626                 THIS->pop_spot ();
627                 $$ = $4;
628                 if (!$$->defs_.size ())
629                 {
630                   Music_output_def *id =
631                         unsmob_music_output_def (THIS->lexer_->lookup_identifier ("$defaultpaper"));
632                   $$->add_output (id ? id->clone () :  new Paper_def );
633                 }
634         }
635         ;
636
637 score_body:
638         Music   {
639                 $$ = new Score;
640         
641                 $$->set_spot (THIS->here_input ());
642                 SCM m = $1->self_scm ();
643                 scm_gc_unprotect_object (m);
644
645                 /*
646                         guh.
647                 */
648                 SCM check_funcs = scm_c_eval_string ("toplevel-music-functions");
649                 for (; gh_pair_p (check_funcs); check_funcs = gh_cdr (check_funcs))
650                         m = gh_call1 (gh_car (check_funcs), m);
651                 $$->music_ = m;
652
653         }
654         | SCORE_IDENTIFIER {
655                 $$ = unsmob_score ($1);
656                 $$->set_spot (THIS->here_input ());
657         }
658         | score_body lilypond_header    {
659                 $$->header_ = $2;
660         }
661         | score_body output_def {
662                 $$->add_output ($2);
663         }
664         | score_body error {
665
666         }
667         ;
668
669
670 /*
671         MIDI
672 */
673 output_def:
674         music_output_def_body '}' {
675                 $$ = $1;
676                 THIS-> lexer_-> remove_scope ();
677         }
678         ;
679
680 music_output_def_body:
681         MIDI '{'    {
682                 Music_output_def *id = unsmob_music_output_def (THIS->lexer_->lookup_identifier ("$defaultmidi"));
683
684
685                 Midi_def* p =0;
686                 if (id)
687                         p = dynamic_cast<Midi_def*> (id->clone ());
688                 else
689                         p = new Midi_def;
690
691                 $$ = p;
692                 THIS->lexer_->add_scope (p->scope_);
693         }
694         | PAPER '{'     {
695                 Music_output_def *id = unsmob_music_output_def (THIS->lexer_->lookup_identifier ("$defaultpaper"));
696                   Paper_def *p = 0;
697                 if (id)
698                         p = dynamic_cast<Paper_def*> (id->clone ());
699                 else
700                         p = new Paper_def;
701
702                 THIS->lexer_->add_scope (p->scope_);
703                 $$ = p;
704         }
705         | PAPER '{' MUSIC_OUTPUT_DEF_IDENTIFIER         {
706                 Music_output_def * o =  unsmob_music_output_def ($3);
707                 $$ =o;
708
709                 THIS->lexer_->add_scope (o->scope_);
710         }
711         | MIDI '{' MUSIC_OUTPUT_DEF_IDENTIFIER  {
712                 Music_output_def * o =  unsmob_music_output_def ($3);
713                 $$ = o;
714
715                 THIS->lexer_->add_scope (o->scope_);
716         }
717         | music_output_def_body assignment  {
718
719         }
720         | music_output_def_body translator_spec_block   {
721                 $$->assign_translator ($2);
722         }
723         | music_output_def_body tempo_event  {
724                 /*
725                         junk this ? there already is tempo stuff in
726                         music.
727                 */
728                 int m = gh_scm2int ( $2->get_mus_property ("metronome-count"));
729                 Duration *d = unsmob_duration ($2->get_mus_property ("tempo-unit"));
730                 Midi_def * md = dynamic_cast<Midi_def*> ($$);
731                 if (md)
732                         md->set_tempo (d->get_length (), m);
733         }
734         | music_output_def_body error {
735
736         }
737         ;
738
739 tempo_event:
740         TEMPO steno_duration '=' bare_unsigned  {
741                 $$ = MY_MAKE_MUSIC("MetronomeChangeEvent");
742                 $$->set_mus_property ("tempo-unit", $2);
743                 $$->set_mus_property ("metronome-count", gh_int2scm ( $4));
744         }
745         ;
746
747 /*
748 The representation of a  list is the
749
750   (LIST . LAST-CONS)
751
752  to have  efficient append.
753 */
754 Music_list:
755         /* empty */ {
756                 $$ = scm_cons (SCM_EOL, SCM_EOL);
757         }
758         | Music_list Music {
759                 SCM s = $$;
760                 SCM c = scm_cons ($2->self_scm (), SCM_EOL);
761                 scm_gc_unprotect_object ($2->self_scm ()); /* UGH */
762                 if (gh_pair_p (ly_cdr (s)))
763                         gh_set_cdr_x (ly_cdr (s), c); /* append */
764                 else
765                         gh_set_car_x (s, c); /* set first cons */
766                 gh_set_cdr_x (s, c) ;  /* remember last cell */ 
767         }
768         | Music_list error {
769         }
770         ;
771
772
773 Music:
774         Simple_music
775         | Composite_music
776         ;
777
778 Alternative_music:
779         /* empty */ {
780                 $$ = SCM_EOL;
781         }
782         | ALTERNATIVE '{' Music_list '}' {
783                 $$ = $3;
784         }
785         ;
786
787 Repeated_music:
788         REPEAT string bare_unsigned Music Alternative_music
789         {
790                 Music *beg = $4;
791                 int times = $3;
792                 SCM alts = gh_pair_p ($5) ? gh_car ($5) : SCM_EOL;
793                 if (times < scm_ilength (alts)) {
794                   unsmob_music (gh_car (alts))
795                     ->origin ()->warning (
796                     _("More alternatives than repeats.  Junking excess alternatives."));
797                   alts = ly_truncate_list (times, alts);
798                 }
799
800
801                 static SCM proc;
802                 if (!proc)
803                         proc = scm_c_eval_string ("make-repeated-music");
804
805                 SCM mus = scm_call_1 (proc, $2);
806                 scm_gc_protect_object (mus); // UGH. 
807                 Music *r =unsmob_music (mus);
808                 if (beg)
809                         {
810                         r-> set_mus_property ("element", beg->self_scm ());
811                         scm_gc_unprotect_object (beg->self_scm ());
812                         }
813                 r->set_mus_property ("repeat-count", gh_int2scm (times >? 1));
814
815                 r-> set_mus_property ("elements",alts);
816                 if (gh_equal_p ($2, scm_makfrom0str ("tremolo"))) {
817                         /*
818                         we can not get durations and other stuff correct down the line, so we have to
819                         add to the duration log here.
820                         */
821                         static SCM func;
822
823                         if (!func)
824                                 func = scm_primitive_eval (ly_symbol2scm ("shift-duration-log"));
825
826                         int dots = ($3 % 3) ? 0 : 1;
827                         int shift = -intlog2 ((dots) ? ($3*2/3) : $3);
828
829                         Sequential_music * seq = dynamic_cast<Sequential_music*> ($4);
830                         
831                         if (seq) {
832                                 int list_len =scm_ilength (seq->music_list ());
833                                 if (list_len != 2)
834                                         seq->origin ()->warning ("Chord tremolo must have 2 elements.");
835                                 shift -= 1;
836                                 r->compress (Moment (Rational (1,list_len)));
837                                 }
838                         gh_call3 (func, r->self_scm (), gh_int2scm(shift),gh_int2scm(dots));
839
840                 }
841                 r->set_spot (*$4->origin ());
842
843                 $$ = r;
844         }
845         ;
846
847 Sequential_music:
848         SEQUENTIAL '{' Music_list '}'           {
849                 $$ = MY_MAKE_MUSIC("SequentialMusic");
850                 $$->set_mus_property ("elements", ly_car ($3));
851                 $$->set_spot(THIS->here_input());
852         }
853         | '{' Music_list '}'            {
854                 $$ = MY_MAKE_MUSIC("SequentialMusic");
855                 $$->set_mus_property ("elements", ly_car ($2));
856                 $$->set_spot(THIS->here_input());
857         }
858         ;
859
860 Simultaneous_music:
861         SIMULTANEOUS '{' Music_list '}'{
862                 $$ = MY_MAKE_MUSIC("SimultaneousMusic");
863                 $$->set_mus_property ("elements", ly_car ($3));
864                 $$->set_spot(THIS->here_input());
865
866         }
867         | simul_open Music_list simul_close     {
868                 $$ = MY_MAKE_MUSIC("SimultaneousMusic");
869                 $$->set_mus_property ("elements", ly_car ($2));
870                 $$->set_spot(THIS->here_input());
871         }
872         ;
873
874 Simple_music:
875         event_chord             { $$ = $1; }
876         | APPLYOUTPUT embedded_scm {
877                 if (!ly_input_procedure_p ($2))
878                         THIS->parser_error (_ ("\\applycontext takes function argument"));
879                 $$ = MY_MAKE_MUSIC ("ApplyOutputEvent");
880                 $$->set_mus_property ("procedure", $2);
881                 $$->set_spot (THIS->here_input());
882         }
883         | APPLYCONTEXT embedded_scm {
884                 if (!ly_input_procedure_p ($2))
885                         THIS->parser_error (_ ("\\applycontext takes function argument"));
886                 $$ = MY_MAKE_MUSIC ("ApplyContext");
887                 $$->set_mus_property ("procedure", $2);
888                 $$->set_spot (THIS->here_input());
889         }
890         | MUSIC_IDENTIFIER {
891                 $$ = unsmob_music ($1);
892         }
893         | property_def
894         | translator_change
895         ;
896
897
898 grace_head:
899         GRACE  { $$ = scm_makfrom0str ("Grace"); } 
900         | ACCIACCATURA { $$ = scm_makfrom0str ("Acciaccatura"); }
901         | APPOGGIATURA { $$ = scm_makfrom0str ("Appoggiatura"); }
902         ;
903         
904
905 Composite_music:
906         CONTEXT STRING Music    {
907                 Music*csm =MY_MAKE_MUSIC("ContextSpeccedMusic");
908
909                 csm->set_mus_property ("element", $3->self_scm ());
910                 scm_gc_unprotect_object ($3->self_scm ());
911
912                 csm->set_mus_property ("context-type", scm_string_to_symbol ($2));
913                 csm->set_mus_property ("context-id", scm_makfrom0str (""));
914
915                 $$ = csm;
916         }
917         | AUTOCHANGE STRING Music       {
918         Music*chm = MY_MAKE_MUSIC("AutoChangeMusic");
919                 chm->set_mus_property ("element", $3->self_scm ());
920                 chm->set_mus_property ("iterator-ctor", Auto_change_iterator::constructor_proc);
921
922                 scm_gc_unprotect_object ($3->self_scm ());
923                 chm->set_mus_property ("what", scm_string_to_symbol ($2));
924
925                 $$ = chm;
926                 chm->set_spot (*$3->origin ());
927         }
928         | grace_head Music {
929 #if 1
930         /*
931                 The other version is for easier debugging  of
932                 Sequential_music_iterator in combination with grace notes.
933         */
934
935 /*
936
937 TODO: should distinguish between both grace types in the
938 basic music objects too, since the meaning is different.
939
940 */
941
942                 String start_str = "start" + ly_scm2string ($1) + "Music"; 
943                 String stop_str = "stop" + ly_scm2string ($1) + "Music"; 
944                 
945                 SCM start = THIS->lexer_->lookup_identifier (start_str);
946                 SCM stop = THIS->lexer_->lookup_identifier (stop_str);
947
948                 Music *startm = unsmob_music (start);
949                 Music *stopm = unsmob_music (stop);
950
951                 SCM ms = SCM_EOL;
952                 if (stopm) {
953                         stopm = stopm->clone ();
954                         ms = scm_cons (stopm->self_scm (), ms);
955                         scm_gc_unprotect_object (stopm->self_scm ());
956                 }
957                 ms = scm_cons ($2->self_scm (), ms);
958                 scm_gc_unprotect_object ($2->self_scm());
959                 if (startm) {
960                         startm = startm->clone ();
961                         ms = scm_cons (startm->self_scm () , ms);
962                         scm_gc_unprotect_object (startm->self_scm ());
963                 }
964
965         
966                 Music* seq = MY_MAKE_MUSIC("SequentialMusic");
967                 seq->set_mus_property ("elements", ms);
968
969                 
970                 $$ = MY_MAKE_MUSIC("GraceMusic");
971                 $$->set_mus_property ("element", seq->self_scm ());
972                 scm_gc_unprotect_object (seq->self_scm ());
973 #else
974                 $$ = MY_MAKE_MUSIC("GraceMusic");
975                 $$->set_mus_property ("element", $2->self_scm ());
976                 scm_gc_unprotect_object ($2->self_scm ());
977 #endif
978         }
979         | CONTEXT string '=' string Music {
980                 Music * csm = MY_MAKE_MUSIC("ContextSpeccedMusic");
981
982                 csm->set_mus_property ("element", $5->self_scm ());
983                 scm_gc_unprotect_object ($5->self_scm ());
984
985                 csm->set_mus_property ("context-type", scm_string_to_symbol ($2));
986                 csm->set_mus_property ("context-id", $4);
987
988                 $$ = csm;
989         }
990         | NEWCONTEXT string Music {
991                 static int new_context_count;
992
993                 Music * csm = MY_MAKE_MUSIC("ContextSpeccedMusic");
994
995                 csm->set_mus_property ("element", $3->self_scm ());
996                 scm_gc_unprotect_object ($3->self_scm ());
997
998                 csm->set_mus_property ("context-type", scm_string_to_symbol ($2));
999
1000                 char s[1024];
1001                 snprintf (s, 1024, "uniqueContext%d", new_context_count ++);
1002                 
1003                 SCM new_id = scm_makfrom0str (s);
1004                 csm->set_mus_property ("context-id", new_id);
1005                 $$ = csm;
1006         }
1007         | TIMES {
1008                 THIS->push_spot ();
1009         }
1010         /* CONTINUED */ 
1011                 fraction Music  
1012
1013         {
1014                 int n = gh_scm2int (ly_car ($3)); int d = gh_scm2int (ly_cdr ($3));
1015                 Music *mp = $4;
1016
1017                 $$= MY_MAKE_MUSIC("TimeScaledMusic");
1018                 $$->set_spot (THIS->pop_spot ());
1019
1020                 $$->set_mus_property ("element", mp->self_scm ());
1021                 scm_gc_unprotect_object (mp->self_scm ());
1022                 $$->set_mus_property ("numerator", gh_int2scm (n));
1023                 $$->set_mus_property ("denominator", gh_int2scm (d));
1024                 $$->compress (Moment (Rational (n,d)));
1025
1026         }
1027         | Repeated_music                { $$ = $1; }
1028         | Simultaneous_music            { $$ = $1; }
1029         | Sequential_music              { $$ = $1; }
1030         | TRANSPOSE pitch_also_in_chords pitch_also_in_chords Music {
1031                 $$ = MY_MAKE_MUSIC("TransposedMusic");
1032                 Music *p = $4;
1033                 Pitch from = *unsmob_pitch ($2);
1034                 Pitch to = *unsmob_pitch ($3);
1035
1036                 p->transpose (interval (from, to));
1037                 $$->set_mus_property ("element", p->self_scm ());
1038                 scm_gc_unprotect_object (p->self_scm ());
1039         }
1040         | APPLY embedded_scm Music  {
1041                 if (!ly_input_procedure_p ($2))
1042                         {
1043                         THIS->parser_error (_ ("\\apply takes function argument"));
1044                         $$ = $3;
1045                         }
1046                 else
1047                         {
1048                         SCM ret = gh_call1 ($2, $3->self_scm ());
1049                         Music *m = unsmob_music (ret);
1050                         if (!m) {
1051                                 THIS->parser_error ("\\apply must return a Music");
1052                                 m = MY_MAKE_MUSIC("Music");
1053                                 }
1054                         $$ = m;
1055                         }
1056         }
1057         | NOTES
1058                 { THIS->lexer_->push_note_state (); }
1059         Music
1060                 { $$ = $3;
1061                   THIS->lexer_->pop_state ();
1062                 }
1063         | FIGURES
1064                 { THIS->lexer_->push_figuredbass_state (); }
1065         Music
1066                 {
1067                   Music * chm = MY_MAKE_MUSIC("UntransposableMusic");
1068                   chm->set_mus_property ("element", $3->self_scm ());
1069                   $$ = chm;
1070                   scm_gc_unprotect_object ($3->self_scm());
1071
1072                   THIS->lexer_->pop_state ();
1073         }
1074         | CHORDS
1075                 { THIS->lexer_->push_chord_state (); }
1076         Music
1077                 {
1078                   Music * chm = MY_MAKE_MUSIC("UnrelativableMusic");
1079                   chm->set_mus_property ("element", $3->self_scm ());
1080                   scm_gc_unprotect_object ($3->self_scm());
1081                   $$ = chm;
1082
1083                   THIS->lexer_->pop_state ();
1084         }
1085         | LYRICS
1086                 { THIS->lexer_->push_lyric_state (); }
1087         Music
1088                 {
1089                   $$ = $3;
1090                   THIS->lexer_->pop_state ();
1091         }
1092         | relative_music        { $$ = $1; }
1093         | re_rhythmed_music     { $$ = $1; } 
1094         | part_combined_music   { $$ = $1; }
1095         | TAG embedded_scm Music {
1096                 tag_music ($3, $2, THIS->here_input ());
1097                 $$ = $3;
1098         }
1099         ;
1100
1101 relative_music:
1102         RELATIVE absolute_pitch Music {
1103                 Music * p = $3;
1104                 Pitch pit = *unsmob_pitch ($2);
1105                 $$ = MY_MAKE_MUSIC("RelativeOctaveMusic");
1106
1107                 $$->set_mus_property ("element", p->self_scm ());
1108                 scm_gc_unprotect_object (p->self_scm ());
1109
1110
1111                 Pitch retpitch = p->to_relative_octave (pit);
1112                 if (lily_1_8_relative)
1113                         $$->set_mus_property ("last-pitch", retpitch.smobbed_copy ());
1114         }
1115         ;
1116
1117 re_rhythmed_music:
1118         ADDLYRICS Music Music {
1119         Music*l =MY_MAKE_MUSIC("LyricCombineMusic");
1120           l->set_mus_property ("elements", gh_list ($2->self_scm (), $3->self_scm (), SCM_UNDEFINED));
1121           scm_gc_unprotect_object ($3->self_scm ());
1122           scm_gc_unprotect_object ($2->self_scm ());
1123           $$ = l;
1124         }
1125         ;
1126
1127 part_combined_music:
1128         PARTCOMBINE STRING Music Music {
1129                 Music * p= MY_MAKE_MUSIC("PartCombineMusic");
1130                 p->set_mus_property ("what", scm_string_to_symbol ($2));
1131                 p->set_mus_property ("elements", gh_list ($3->self_scm (),$4->self_scm (), SCM_UNDEFINED));  
1132
1133                 scm_gc_unprotect_object ($3->self_scm ());
1134                 scm_gc_unprotect_object ($4->self_scm ());  
1135
1136                 $$ = p;
1137         }
1138         ;
1139
1140 translator_change:
1141         TRANSLATOR STRING '=' STRING  {
1142                 Music*t= MY_MAKE_MUSIC("TranslatorChange");
1143                 t-> set_mus_property ("change-to-type", scm_string_to_symbol ($2));
1144                 t-> set_mus_property ("change-to-id", $4);
1145
1146                 $$ = t;
1147                 $$->set_spot (THIS->here_input ());
1148         }
1149         ;
1150
1151 property_def:
1152         simple_property_def
1153         | ONCE simple_property_def {
1154                 $$ = $2;
1155                 SCM e = $2->get_mus_property ("element");
1156                 unsmob_music (e)->set_mus_property ("once", SCM_BOOL_T);
1157         }
1158         ;
1159
1160 simple_property_def:
1161         PROPERTY STRING '.' STRING '='  scalar {
1162                 Music *t = set_property_music (scm_string_to_symbol ($4), $6);
1163                 Music *csm = MY_MAKE_MUSIC("ContextSpeccedMusic");
1164
1165                 csm->set_mus_property ("element", t->self_scm ());
1166                 scm_gc_unprotect_object (t->self_scm ());
1167
1168                 $$ = csm;
1169                 $$->set_spot (THIS->here_input ());
1170
1171                 csm-> set_mus_property ("context-type", scm_string_to_symbol ($2));
1172         }
1173         | PROPERTY STRING '.' STRING UNSET {
1174                 
1175                 Music *t = MY_MAKE_MUSIC("PropertyUnset");
1176                 t->set_mus_property ("symbol", scm_string_to_symbol ($4));
1177
1178                 Music *csm = MY_MAKE_MUSIC("ContextSpeccedMusic");
1179                 csm->set_mus_property ("element", t->self_scm ());
1180                 scm_gc_unprotect_object (t->self_scm ());
1181
1182                 $$ = csm;
1183                 $$->set_spot (THIS->here_input ());
1184
1185                 csm-> set_mus_property ("context-type", scm_string_to_symbol ($2));
1186         }
1187         | PROPERTY STRING '.' STRING SET embedded_scm '=' embedded_scm {
1188                 bool autobeam
1189                   = gh_equal_p ($4, scm_makfrom0str ("autoBeamSettings"));
1190                 bool itc = internal_type_checking_global_b;
1191                 Music *t = MY_MAKE_MUSIC("OverrideProperty");
1192                 t->set_mus_property ("symbol", scm_string_to_symbol ($4));
1193                 t->set_mus_property ("pop-first", SCM_BOOL_T);
1194                 if (autobeam)
1195                         internal_type_checking_global_b = false;
1196                 t->set_mus_property ("grob-property", $6);
1197                 if (autobeam)
1198                         internal_type_checking_global_b = itc;
1199                 t->set_mus_property ("grob-value", $8);
1200
1201                 Music *csm = MY_MAKE_MUSIC("ContextSpeccedMusic");
1202                 csm->set_mus_property ("element", t->self_scm ());
1203                 scm_gc_unprotect_object (t->self_scm ());
1204                 $$ = csm;
1205                 $$->set_spot (THIS->here_input ());
1206
1207                 csm-> set_mus_property ("context-type", scm_string_to_symbol ($2));
1208         }
1209         | PROPERTY STRING '.' STRING OVERRIDE
1210                 embedded_scm '=' embedded_scm
1211         {
1212                 /*
1213                         UGH UGH UGH UGH.
1214                 */
1215                 bool autobeam
1216                   = gh_equal_p ($4, scm_makfrom0str ("autoBeamSettings"));
1217                 bool itc = internal_type_checking_global_b;
1218
1219                 Music *t = MY_MAKE_MUSIC("OverrideProperty");
1220                 t->set_mus_property ("symbol", scm_string_to_symbol ($4));
1221                 if (autobeam)
1222                         internal_type_checking_global_b = false;
1223                 t->set_mus_property ("grob-property", $6);
1224                 t->set_mus_property ("grob-value", $8);
1225                 if (autobeam)
1226                         internal_type_checking_global_b = itc;
1227
1228                 Music *csm = MY_MAKE_MUSIC("ContextSpeccedMusic");
1229                 csm->set_mus_property ("element", t->self_scm ());
1230                 scm_gc_unprotect_object (t->self_scm ());
1231
1232                 $$ = csm;
1233                 $$->set_spot (THIS->here_input ());
1234
1235                 csm-> set_mus_property ("context-type", scm_string_to_symbol ($2));
1236
1237         }
1238         | PROPERTY STRING '.' STRING REVERT embedded_scm {
1239                 Music *t = MY_MAKE_MUSIC("RevertProperty");
1240                 bool autobeam
1241                   = gh_equal_p ($4, scm_makfrom0str ("autoBeamSettings"));
1242                 bool itc = internal_type_checking_global_b;
1243
1244                 t->set_mus_property ("symbol", scm_string_to_symbol ($4));
1245                 if (autobeam)
1246                         internal_type_checking_global_b = false;
1247                 t->set_mus_property ("grob-property", $6);
1248                 if (autobeam)
1249                         internal_type_checking_global_b = itc;
1250         
1251                 Music *csm = MY_MAKE_MUSIC("ContextSpeccedMusic");
1252                 csm->set_mus_property ("element", t->self_scm ());
1253                 scm_gc_unprotect_object (t->self_scm ());
1254
1255                 $$ = csm;
1256                 $$->set_spot (THIS->here_input ());
1257
1258                 csm-> set_mus_property ("context-type", scm_string_to_symbol ($2));
1259         }
1260         ;
1261
1262
1263 scalar:
1264         string          { $$ = $1; }
1265         | bare_int      { $$ = gh_int2scm ($1); }
1266         | embedded_scm  { $$ = $1; }
1267         | full_markup {  $$ = $1; }
1268         | DIGIT { $$ = gh_int2scm ($1); }
1269         ;
1270
1271
1272
1273 /*
1274 This is a trick:
1275
1276 Adding pre_events to the simple_element
1277 makes the choice between
1278
1279   string:  STRING
1280
1281 and
1282
1283   simple_element: STRING
1284
1285 a single shift/reduction conflict.
1286
1287 nevertheless, this is not very clean, and we should find a different
1288 solution.  
1289
1290 */
1291 pre_events: {
1292                 THIS->push_spot ();
1293         }
1294         ;
1295
1296 event_chord:
1297         pre_events simple_element post_events   {
1298                 SCM elts = $2-> get_mus_property ("elements");
1299
1300                 elts = gh_append2 (elts, scm_reverse_x ($3, SCM_EOL));
1301
1302                 $2->set_mus_property ("elements", elts);
1303                 $$ = $2;
1304         }
1305         | command_element
1306         | note_chord_element
1307         ;
1308
1309
1310 note_chord_element:
1311         chord_body optional_notemode_duration post_events
1312         {
1313                 SCM dur = unsmob_duration ($2)->smobbed_copy();
1314                 SCM es = $1->get_mus_property ("elements");
1315                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
1316
1317                 for (SCM s = es; gh_pair_p (s); s = gh_cdr (s))
1318                   unsmob_music (gh_car(s))->set_mus_property ("duration", dur);
1319                 es = gh_append2 (es, postevs);
1320
1321                 $1-> set_mus_property ("elements", es);
1322                 $$ = $1;
1323         }
1324         ;
1325
1326 chord_open: '<'
1327         ;
1328
1329 chord_close: '>'
1330         ;
1331
1332 simul_open: LESSLESS
1333         ;
1334
1335 simul_close: MOREMORE
1336         ;
1337
1338 chord_body:
1339         chord_open chord_body_elements chord_close
1340         {
1341                 $$ = MY_MAKE_MUSIC("EventChord");
1342                 $$->set_mus_property ("elements",
1343                         scm_reverse_x ($2, SCM_EOL));
1344         }
1345         ;
1346
1347 chord_body_elements:
1348         /* empty */             { $$ = SCM_EOL; }
1349         | chord_body_elements chord_body_element {
1350                 $$ = gh_cons ($2->self_scm(), $1);
1351                 scm_gc_unprotect_object ($2->self_scm());
1352         }
1353         ;
1354
1355 chord_body_element:
1356         pitch exclamations questions post_events
1357         {
1358                 Music * n = MY_MAKE_MUSIC("NoteEvent");
1359                 n->set_mus_property ("pitch", $1);
1360                 if ($3 % 2)
1361                         n->set_mus_property ("cautionary", SCM_BOOL_T);
1362                 if ($2 % 2 || $3 % 2)
1363                         n->set_mus_property ("force-accidental", SCM_BOOL_T);
1364
1365                 SCM arts = scm_reverse_x ($4, SCM_EOL);
1366                 n->set_mus_property ("articulations", arts);
1367
1368                 $$ = n;
1369         }
1370         ;
1371
1372 command_element:
1373         command_req {
1374                 $$ = MY_MAKE_MUSIC("EventChord");
1375                 $$->set_mus_property ("elements", scm_cons ($1->self_scm (), SCM_EOL));
1376                 scm_gc_unprotect_object ($1->self_scm());
1377
1378                 $$-> set_spot (THIS->here_input ());
1379                 $1-> set_spot (THIS->here_input ());
1380         }
1381         | OCTAVE { THIS->push_spot (); }
1382           pitch {
1383                 Music *l = MY_MAKE_MUSIC("RelativeOctaveCheck");
1384                 $$ = l;
1385                 $$->set_spot (THIS->pop_spot ());
1386                 $$->set_mus_property ("pitch", $3);
1387         }
1388         | E_LEFTSQUARE {
1389                 Music *l = MY_MAKE_MUSIC("LigatureEvent");
1390                 l->set_mus_property ("span-direction", gh_int2scm (START));
1391                 l->set_spot (THIS->here_input ());
1392
1393                 $$ = MY_MAKE_MUSIC("EventChord");
1394                 $$->set_mus_property ("elements", scm_cons (l->self_scm (), SCM_EOL));
1395                 scm_gc_unprotect_object (l->self_scm());
1396                 $$->set_spot (THIS->here_input ());
1397         }
1398         | E_RIGHTSQUARE {
1399                 Music *l = MY_MAKE_MUSIC("LigatureEvent");
1400                 l->set_mus_property ("span-direction", gh_int2scm (STOP));
1401                 l->set_spot (THIS->here_input ());
1402
1403                 $$ = MY_MAKE_MUSIC("EventChord");
1404                 $$->set_mus_property ("elements", scm_cons (l->self_scm (), SCM_EOL));
1405                 $$->set_spot (THIS->here_input ());
1406                 scm_gc_unprotect_object (l->self_scm());
1407         }
1408         | E_BACKSLASH {
1409                 $$ = MY_MAKE_MUSIC("VoiceSeparator");
1410                 $$->set_spot (THIS->here_input ());
1411         }
1412         | '|'      {
1413
1414                 $$ = MY_MAKE_MUSIC("BarCheck");
1415                 $$->set_spot (THIS->here_input ());
1416         }
1417         | BAR STRING                    {
1418                 Music *t = set_property_music (ly_symbol2scm ("whichBar"), $2);
1419
1420                 Music *csm = MY_MAKE_MUSIC("ContextSpeccedMusic");
1421                 csm->set_mus_property ("element", t->self_scm ());
1422                 scm_gc_unprotect_object (t->self_scm ());
1423
1424                 $$ = csm;
1425                 $$->set_spot (THIS->here_input ());
1426
1427                 csm->set_mus_property ("context-type", ly_symbol2scm ("Timing"));
1428         }
1429         | PARTIAL duration_length       {
1430                 Moment m = - unsmob_duration ($2)->get_length ();
1431                 Music * p = set_property_music (ly_symbol2scm ( "measurePosition"),m.smobbed_copy ());
1432
1433                 Music * sp = MY_MAKE_MUSIC("ContextSpeccedMusic");
1434                 sp->set_mus_property ("element", p->self_scm ());
1435                 scm_gc_unprotect_object (p->self_scm ());
1436
1437                 $$ =sp ;
1438                 sp-> set_mus_property ("context-type", ly_symbol2scm ("Timing"));
1439         }
1440         | CLEF STRING  {
1441                 static SCM proc ;
1442                 if (!proc)
1443                         proc = scm_c_eval_string ("make-clef-set");
1444
1445                 SCM result = scm_call_1 (proc, $2);
1446                 scm_gc_protect_object (result);
1447                 $$ = unsmob_music (result);
1448         }
1449         | TIME_T fraction  {
1450                 static SCM proc;
1451                 if (!proc)
1452                         proc = scm_c_eval_string ("make-time-signature-set");
1453
1454                 SCM result = scm_apply_2   (proc, gh_car ($2), gh_cdr ($2), SCM_EOL);
1455                 scm_gc_protect_object (result);
1456                 $$ = unsmob_music (result);
1457         }
1458         ;
1459
1460 command_req:
1461         shorthand_command_req   { $$ = $1; }
1462         | verbose_command_req   { $$ = $1; }
1463         ;
1464
1465 shorthand_command_req:
1466         extender_req {
1467                 $$ = $1;
1468         }
1469         | hyphen_req {
1470                 $$ = $1;
1471         }
1472         | BREATHE {
1473                 $$ = MY_MAKE_MUSIC("BreathingSignEvent");
1474         }
1475         | E_TILDE {
1476                 $$ = MY_MAKE_MUSIC("PesOrFlexaEvent");
1477         }
1478         ;
1479
1480 verbose_command_req:
1481         MARK DEFAULT  {
1482                 Music * m = MY_MAKE_MUSIC("MarkEvent");
1483                 $$ = m;
1484         }
1485         | MARK scalar {
1486                 Music *m = MY_MAKE_MUSIC("MarkEvent");
1487                 m->set_mus_property ("label", $2);
1488                 $$ = m;
1489         }
1490         | SKIP duration_length {
1491                 Music * skip = MY_MAKE_MUSIC("SkipEvent");
1492                 skip->set_mus_property ("duration", $2);
1493
1494                 $$ = skip;
1495         }
1496         | tempo_event {
1497                 $$ = $1;
1498         }
1499         | KEY DEFAULT {
1500                 Music *key= MY_MAKE_MUSIC("KeyChangeEvent");
1501                 $$ = key;
1502         }
1503         | KEY NOTENAME_PITCH SCM_IDENTIFIER     {
1504
1505                 Music *key= MY_MAKE_MUSIC("KeyChangeEvent");
1506                 if (scm_ilength ($3) > 0)
1507                 {               
1508                         key->set_mus_property ("pitch-alist", $3);
1509                         key->set_mus_property ("tonic", Pitch (0,0,0).smobbed_copy());
1510                         ((Music*)key)->transpose (* unsmob_pitch ($2));
1511                 } else {
1512                         THIS->parser_error (_("Second argument must be pitch list."));
1513                 }
1514
1515                 $$ = key;
1516         }
1517         ;
1518
1519 post_events:
1520         /* empty */ {
1521                 $$ = SCM_EOL;
1522         }
1523         | post_events post_event {
1524                 $2->set_spot (THIS->here_input ());
1525                 $$ = gh_cons ($2->self_scm(), $$);
1526                 scm_gc_unprotect_object ($2->self_scm());
1527         }
1528         | post_events tagged_post_event {
1529                 $2 -> set_spot (THIS->here_input ());
1530                 $$ = scm_cons ($2->self_scm(), $$);
1531                 scm_gc_unprotect_object ($2->self_scm());
1532         }
1533         ;
1534
1535
1536 tagged_post_event:
1537         '-' TAG embedded_scm post_event {
1538                 tag_music ($4, $3, THIS->here_input ());
1539                 $$ = $4;
1540         }
1541         ;
1542
1543 post_event:
1544         direction_less_event {
1545                 $$ = $1;
1546         }
1547         | script_dir direction_reqd_event {
1548                 if ($1)
1549                         $2->set_mus_property ("direction", gh_int2scm ($1));
1550                 $$ = $2;
1551         }
1552         | script_dir direction_less_event {
1553                 if ($1)
1554                         $2->set_mus_property ("direction", gh_int2scm ($1));
1555                 $$ = $2;
1556         }
1557         | string_number_event
1558         ;
1559
1560 string_number_event:
1561         E_UNSIGNED {
1562                 Music * s = MY_MAKE_MUSIC("StringNumberEvent");
1563                 s->set_mus_property ("string-number",  gh_int2scm($1));
1564                 s->set_spot (THIS->here_input ());
1565                 $$ = s;
1566         }
1567         ;
1568
1569
1570 direction_less_event:
1571         '['  {
1572
1573
1574 /*
1575
1576 TODO: should take all these defs out of the parser, adn make use
1577 configurable, i.e.
1578
1579
1580 (set-articulation '~ "trill")
1581
1582 */
1583                 Music * m = MY_MAKE_MUSIC ("BeamEvent");
1584                 m->set_spot (THIS->here_input());
1585                 m->set_mus_property ("span-direction" , gh_int2scm (START));
1586                 $$ = m;
1587         }
1588         | ']'  {
1589                 Music * m = MY_MAKE_MUSIC ("BeamEvent");
1590                 m->set_spot (THIS->here_input());
1591                 m->set_mus_property ("span-direction" , gh_int2scm (STOP));
1592                 $$ = m;
1593         }
1594         | '~' {
1595                 Music * m = MY_MAKE_MUSIC ("TieEvent");
1596                 m->set_spot (THIS->here_input());
1597                 $$ = m;
1598         }
1599         | close_event {
1600                 $$ = $1;
1601                 dynamic_cast<Music *> ($$)->set_mus_property ("span-direction", gh_int2scm (START));
1602         }
1603         | open_event {
1604                 $$ = $1;
1605                 dynamic_cast<Music *> ($$)->set_mus_property ("span-direction", gh_int2scm (STOP));
1606         }
1607         | EVENT_IDENTIFIER      {
1608                 $$ = unsmob_music ($1);
1609         }
1610         | tremolo_type  {
1611                Music * a = MY_MAKE_MUSIC("TremoloEvent");
1612                a->set_spot (THIS->here_input ());
1613                a->set_mus_property ("tremolo-type", gh_int2scm ($1));
1614                $$ = a;
1615         }
1616         ;       
1617         
1618 direction_reqd_event:
1619         gen_text_def {
1620                 $$ = $1; 
1621         }
1622         | script_abbreviation {
1623                 SCM s = THIS->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
1624                 Music *a = MY_MAKE_MUSIC("ArticulationEvent");
1625                 if (gh_string_p (s))
1626                         a->set_mus_property ("articulation-type", s);
1627                 else THIS->parser_error (_ ("Expecting string as script definition"));
1628                 $$ = a;
1629         }
1630         ;
1631         
1632 sup_quotes:
1633         '\'' {
1634                 $$ = 1;
1635         }
1636         | sup_quotes '\'' {
1637                 $$ ++;
1638         }
1639         ;
1640
1641 sub_quotes:
1642         ',' {
1643                 $$ = 1;
1644         }
1645         | sub_quotes ',' {
1646                 $$ ++ ;
1647         }
1648         ;
1649
1650 steno_pitch:
1651         NOTENAME_PITCH  {
1652                 $$ = $1;
1653         }
1654         | NOTENAME_PITCH sup_quotes     {
1655                 Pitch p = *unsmob_pitch ($1);
1656                 p = p.transposed (Pitch ($2,0,0));
1657                 $$ = p.smobbed_copy ();
1658         }
1659         | NOTENAME_PITCH sub_quotes      {
1660                 Pitch p =* unsmob_pitch ($1);
1661                 p = p.transposed (Pitch (-$2,0,0));
1662                 $$ = p.smobbed_copy ();
1663         }
1664         ;
1665
1666 /*
1667 ugh. duplication
1668 */
1669
1670 steno_tonic_pitch:
1671         TONICNAME_PITCH {
1672                 $$ = $1;
1673         }
1674         | TONICNAME_PITCH sup_quotes    {
1675                 Pitch p = *unsmob_pitch ($1);
1676                 p = p.transposed (Pitch ($2,0,0));
1677                 $$ = p.smobbed_copy ();
1678         }
1679         | TONICNAME_PITCH sub_quotes     {
1680                 Pitch p =* unsmob_pitch ($1);
1681
1682                 p = p.transposed (Pitch (-$2,0,0));
1683                 $$ = p.smobbed_copy ();
1684         }
1685         ;
1686
1687 pitch:
1688         steno_pitch {
1689                 $$ = $1;
1690         }
1691         ;
1692
1693 pitch_also_in_chords:
1694         pitch
1695         | steno_tonic_pitch
1696         ;
1697
1698
1699
1700
1701
1702 extender_req:
1703         EXTENDER {
1704                 if (!THIS->lexer_->lyric_state_b ())
1705                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
1706                 $$ = MY_MAKE_MUSIC("ExtenderEvent");
1707         }
1708         ;
1709
1710 hyphen_req:
1711         HYPHEN {
1712                 if (!THIS->lexer_->lyric_state_b ())
1713                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
1714                 $$ = MY_MAKE_MUSIC("HyphenEvent");
1715         }
1716         ;
1717
1718 close_event:
1719         '('     {
1720                 Music * s= MY_MAKE_MUSIC("SlurEvent");
1721                 $$ = s;
1722                 s->set_spot (THIS->here_input());
1723         }
1724         | E_OPEN        {
1725                 Music * s= MY_MAKE_MUSIC("PhrasingSlurEvent");
1726                 $$ = s;
1727                 s->set_spot (THIS->here_input());
1728         }
1729         | E_SMALLER {
1730                 Music *s =MY_MAKE_MUSIC("CrescendoEvent");
1731                 $$ = s;
1732                 s->set_spot (THIS->here_input());
1733         }
1734         | E_BIGGER {
1735                 Music *s =MY_MAKE_MUSIC("DecrescendoEvent");
1736                 $$ = s;
1737                 s->set_spot (THIS->here_input());
1738         }
1739         ;
1740
1741
1742 open_event:
1743         E_EXCLAMATION   {
1744                 Music *s =  MY_MAKE_MUSIC("CrescendoEvent");
1745                 s->set_spot (THIS->here_input());
1746
1747                 $$ = s;
1748         }
1749         | ')'   {
1750                 Music * s= MY_MAKE_MUSIC("SlurEvent");
1751                 $$ = s;
1752                 s->set_spot (THIS->here_input());
1753
1754         }
1755         | E_CLOSE       {
1756                 Music * s= MY_MAKE_MUSIC("PhrasingSlurEvent");
1757                 $$ = s;
1758                 s->set_mus_property ("span-type", scm_makfrom0str ( "phrasing-slur"));
1759                 s->set_spot (THIS->here_input());
1760         }
1761         ;
1762
1763 gen_text_def:
1764         full_markup {
1765                 Music *t = MY_MAKE_MUSIC("TextScriptEvent");
1766                 t->set_mus_property ("text", $1);
1767                 t->set_spot (THIS->here_input ());
1768                 $$ = t; 
1769         }
1770         | string {
1771                 Music *t = MY_MAKE_MUSIC("TextScriptEvent");
1772                 t->set_mus_property ("text", make_simple_markup ($1));
1773                 t->set_spot (THIS->here_input ());
1774                 $$ = t;
1775         
1776         }
1777         | DIGIT {
1778                 Music * t = MY_MAKE_MUSIC("FingerEvent");
1779                 t->set_mus_property ("digit",  gh_int2scm ($1));
1780                 t->set_spot (THIS->here_input ());
1781                 $$ = t;
1782         }
1783         ;
1784
1785 script_abbreviation:
1786         '^'             {
1787                 $$ = scm_makfrom0str ("Hat");
1788         }
1789         | '+'           {
1790                 $$ = scm_makfrom0str ("Plus");
1791         }
1792         | '-'           {
1793                 $$ = scm_makfrom0str ("Dash");
1794         }
1795         | '|'           {
1796                 $$ = scm_makfrom0str ("Bar");
1797         }
1798         | '>'           {
1799                 $$ = scm_makfrom0str ("Larger");
1800         }
1801         | '.'           {
1802                 $$ = scm_makfrom0str ("Dot");
1803         }
1804         | '_' {
1805                 $$ = scm_makfrom0str ("Underscore");
1806         }
1807         ;
1808
1809 script_dir:
1810         '_'     { $$ = DOWN; }
1811         | '^'   { $$ = UP; }
1812         | '-'   { $$ = CENTER; }
1813         ;
1814
1815
1816 absolute_pitch:
1817         steno_pitch     {
1818                 $$ = $1;
1819         }
1820         ;
1821
1822 duration_length:
1823         multiplied_duration {
1824                 $$ = $1;
1825         }
1826         ;
1827
1828 optional_notemode_duration:
1829         {
1830                 Duration dd = THIS->default_duration_;
1831                 $$ = dd.smobbed_copy ();
1832
1833                 THIS->beam_check ($$);
1834         }
1835         | multiplied_duration   {
1836                 $$ = $1;
1837                 THIS->default_duration_ = *unsmob_duration ($$);
1838
1839                 THIS->beam_check ($$);
1840         }
1841         ;
1842
1843 steno_duration:
1844         bare_unsigned dots              {
1845                 int l = 0;
1846                 if (!is_duration_b ($1))
1847                         THIS->parser_error (_f ("not a duration: %d", $1));
1848                 else
1849                         l =  intlog2 ($1);
1850
1851                 $$ = Duration (l, $2).smobbed_copy ();
1852         }
1853         | DURATION_IDENTIFIER dots      {
1854                 Duration *d =unsmob_duration ($1);
1855                 Duration k (d->duration_log (),d->dot_count () + $2);
1856
1857                 *d = k;
1858                 $$ = $1;
1859         }
1860         ;
1861
1862
1863
1864
1865 multiplied_duration:
1866         steno_duration {
1867                 $$ = $1;
1868         }
1869         | multiplied_duration '*' bare_unsigned {
1870                 $$ = unsmob_duration ($$)->compressed ( $3) .smobbed_copy ();
1871         }
1872         | multiplied_duration '*' FRACTION {
1873                 Rational  m (gh_scm2int (ly_car ($3)), gh_scm2int (ly_cdr ($3)));
1874
1875                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
1876         }
1877         ;
1878
1879 fraction:
1880         FRACTION { $$ = $1; }
1881         | UNSIGNED '/' UNSIGNED {
1882                 $$ = scm_cons (gh_int2scm ($1), gh_int2scm ($3));
1883         }
1884         ;
1885
1886 dots:
1887         /* empty */     {
1888                 $$ = 0;
1889         }
1890         | dots '.' {
1891                 $$ ++;
1892         }
1893         ;
1894
1895
1896 tremolo_type: 
1897         ':'     {
1898                 $$ =0;
1899         }
1900         | ':' bare_unsigned {
1901                 if (!is_duration_b ($2))
1902                         THIS->parser_error (_f ("not a duration: %d", $2));
1903                 $$ = $2;
1904         }
1905         ;
1906
1907
1908
1909 /*****************************************************************
1910                 BASS FIGURES
1911 *****************************************************************/
1912 bass_number:
1913         DIGIT   {
1914                 $$ = scm_number_to_string (gh_int2scm ($1), gh_int2scm (10));
1915         }
1916         | UNSIGNED {
1917                 $$ = scm_number_to_string (gh_int2scm ($1), gh_int2scm (10));
1918         }
1919         | STRING { $$ =  $1; }
1920         ;
1921
1922 bass_mod:
1923         '-'     { $$ = -1; }
1924         | '+'   { $$ = 1; } 
1925         | '!'   { $$ = 0; }
1926         ;
1927
1928 bass_figure:
1929         FIGURE_SPACE {
1930                 Music *bfr = MY_MAKE_MUSIC("BassFigureEvent");
1931                 $$ = bfr->self_scm();
1932                 scm_gc_unprotect_object ($$);
1933         }
1934         | bass_number  {
1935                 Music *bfr = MY_MAKE_MUSIC("BassFigureEvent");
1936                 $$ = bfr->self_scm();
1937
1938                 bfr->set_mus_property ("figure", $1);
1939
1940                 scm_gc_unprotect_object ($$);
1941         }
1942         | bass_figure bass_mod {
1943                 Music *m = unsmob_music ($1);
1944                 if ($2) {
1945                         SCM salter =m->get_mus_property ("alteration");
1946                         int alter = gh_number_p ( salter) ? gh_scm2int (salter) : 0;
1947                         m->set_mus_property ("alteration",
1948                                 gh_int2scm (alter + $2));
1949                 } else {
1950                         m->set_mus_property ("alteration", gh_int2scm (0));
1951                 }
1952         }
1953         ;
1954
1955 br_bass_figure:
1956         '[' bass_figure {
1957                 $$ = $2;
1958                 unsmob_music ($$)->set_mus_property ("bracket-start", SCM_BOOL_T);
1959         }
1960         | bass_figure   {
1961                 $$ = $1;
1962         }
1963         | br_bass_figure ']' {
1964                 $$ = $1;
1965                 unsmob_music ($1)->set_mus_property ("bracket-stop", SCM_BOOL_T);
1966         }
1967         ;
1968
1969 figure_list:
1970         /**/            {
1971                 $$ = SCM_EOL;
1972         }
1973         | figure_list br_bass_figure {
1974                 $$ = scm_cons ($2, $1); 
1975         }
1976         ;
1977
1978 figure_spec:
1979         FIGURE_OPEN figure_list FIGURE_CLOSE {
1980                 Music * m = MY_MAKE_MUSIC("EventChord");
1981                 $2 = scm_reverse_x ($2, SCM_EOL);
1982                 m->set_mus_property ("elements",  $2);
1983                 $$ = m->self_scm ();
1984         }
1985         ;
1986
1987
1988 optional_rest:
1989         /**/   { $$ = 0; }
1990         | REST { $$ = 1; }
1991         ;
1992
1993 simple_element:
1994         pitch exclamations questions optional_notemode_duration optional_rest {
1995
1996                 Input i = THIS->pop_spot ();
1997                 if (!THIS->lexer_->note_state_b ())
1998                         THIS->parser_error (_ ("Have to be in Note mode for notes"));
1999
2000                 Music *n = 0;
2001                 if ($5)
2002                         n =  MY_MAKE_MUSIC("RestEvent");
2003                 else
2004                         n =  MY_MAKE_MUSIC("NoteEvent");
2005                 
2006                 n->set_mus_property ("pitch", $1);
2007                 n->set_mus_property ("duration", $4);
2008
2009
2010                 if ($3 % 2)
2011                         n->set_mus_property ("cautionary", SCM_BOOL_T);
2012                 if ($2 % 2 || $3 % 2)
2013                         n->set_mus_property ("force-accidental", SCM_BOOL_T);
2014
2015                 Music *v = MY_MAKE_MUSIC("EventChord");
2016                 v->set_mus_property ("elements", scm_list_n (n->self_scm (), SCM_UNDEFINED));
2017                 scm_gc_unprotect_object (n->self_scm());
2018
2019                 v->set_spot (i);
2020                 n->set_spot (i);
2021                 $$ = v;
2022         }
2023         | figure_spec optional_notemode_duration {
2024                 Music * m = unsmob_music ($1);
2025                 Input i = THIS->pop_spot (); 
2026                 m->set_spot (i);
2027                 for (SCM s = m->get_mus_property ("elements"); gh_pair_p (s); s = ly_cdr (s))
2028                 {
2029                         unsmob_music (ly_car (s))->set_mus_property ("duration", $2);
2030                 }
2031                 $$ = m;
2032         }       
2033         | RESTNAME optional_notemode_duration           {
2034
2035                 Input i = THIS->pop_spot ();
2036                 Music * ev = 0;
2037                 if (ly_scm2string ($1) =="s") {
2038                         /* Space */
2039                         ev = MY_MAKE_MUSIC("SkipEvent");
2040                   }
2041                 else {
2042                         ev = MY_MAKE_MUSIC("RestEvent");
2043                 
2044                     }
2045                 ev->set_mus_property ("duration" ,$2);
2046                 ev->set_spot (i);
2047                 Music * velt = MY_MAKE_MUSIC("EventChord");
2048                 velt->set_mus_property ("elements", scm_list_n (ev->self_scm (),SCM_UNDEFINED));
2049                 velt->set_spot (i);
2050
2051                 $$ = velt;
2052         }
2053         | MULTI_MEASURE_REST optional_notemode_duration         {
2054                 THIS->pop_spot ();
2055
2056                 static SCM proc ;
2057                 if (!proc)
2058                         proc = scm_c_eval_string ("make-multi-measure-rest");
2059
2060                 SCM mus = scm_call_2 (proc, $2,
2061                         make_input (THIS->here_input()));       
2062                 scm_gc_protect_object (mus);
2063                 $$ = unsmob_music (mus);
2064         }
2065         
2066         | lyric_element optional_notemode_duration      {
2067                 Input i = THIS->pop_spot ();
2068                 if (!THIS->lexer_->lyric_state_b ())
2069                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
2070
2071                 Music * lreq = MY_MAKE_MUSIC("LyricEvent");
2072                 lreq->set_mus_property ("text", $1);
2073                 lreq->set_mus_property ("duration",$2);
2074                 lreq->set_spot (i);
2075                 Music * velt = MY_MAKE_MUSIC("EventChord");
2076                 velt->set_mus_property ("elements", scm_list_n (lreq->self_scm (), SCM_UNDEFINED));
2077
2078                 $$= velt;
2079         }
2080         | new_chord {
2081                 THIS->pop_spot ();
2082
2083                 if (!THIS->lexer_->chord_state_b ())
2084                         THIS->parser_error (_ ("Have to be in Chord mode for chords"));
2085                 $$ = unsmob_music ($1);
2086         }
2087         ;
2088
2089 lyric_element:
2090         full_markup { $$ = $1; }
2091         | STRING {  $$ = $1 ; }
2092         ;
2093
2094 new_chord:
2095         steno_tonic_pitch optional_notemode_duration   {
2096                 $$ = make_chord ($1, $2, SCM_EOL);
2097         }
2098         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
2099                 SCM its = scm_reverse_x ($4, SCM_EOL);
2100                 $$ = make_chord ($1, $2, gh_cons ($3, its));
2101         }
2102         ;
2103
2104 chord_items:
2105         /**/ {
2106                 $$ = SCM_EOL;           
2107         }
2108         | chord_items chord_item {
2109                 $$ = gh_cons ($2, $$);
2110         }
2111         ;
2112
2113 chord_separator:
2114         CHORD_COLON {
2115                 $$ = ly_symbol2scm ("chord-colon");
2116         }
2117         | CHORD_CARET {
2118                 $$ = ly_symbol2scm ("chord-caret"); 
2119         }
2120         | CHORD_SLASH steno_tonic_pitch {
2121                 $$ = scm_list_n (ly_symbol2scm ("chord-slash"), $2, SCM_UNDEFINED); 
2122         }
2123         | CHORD_BASS steno_tonic_pitch {
2124                 $$ = scm_list_n (ly_symbol2scm ("chord-bass"), $2, SCM_UNDEFINED); 
2125         }
2126         ;
2127
2128 chord_item:
2129         chord_separator {
2130                 $$ = $1;
2131         }
2132         | step_numbers {
2133                 $$ = scm_reverse_x ($1, SCM_EOL);
2134         }
2135         | CHORD_MODIFIER  {
2136                 $$ = $1;
2137         }
2138         ;
2139
2140 step_numbers:
2141         step_number { $$ = gh_cons ($1, SCM_EOL); } 
2142         | step_numbers '.' step_number {
2143                 $$ = gh_cons ($3, $$);
2144         }
2145         ;
2146
2147 step_number:
2148         bare_unsigned {
2149                 $$ = make_chord_step ($1, 0);
2150         } 
2151         | bare_unsigned '+' {
2152                 $$ = make_chord_step ($1, 1);
2153         }
2154         | bare_unsigned CHORD_MINUS {
2155                 $$ = make_chord_step ($1,-1);
2156         }
2157         ;       
2158
2159 /*
2160         UTILITIES
2161
2162 TODO: should deprecate in favor of Scheme?
2163
2164  */
2165 number_expression:
2166         number_expression '+' number_term {
2167                 $$ = scm_sum ($1, $3);
2168         }
2169         | number_expression '-' number_term {
2170                 $$ = scm_difference ($1, $3);
2171         }
2172         | number_term 
2173         ;
2174
2175 number_term:
2176         number_factor {
2177                 $$ = $1;
2178         }
2179         | number_factor '*' number_factor {
2180                 $$ = scm_product ($1, $3);
2181         }
2182         | number_factor '/' number_factor {
2183                 $$ = scm_divide ($1, $3);
2184         }
2185         ;
2186
2187 number_factor:
2188         '-'  number_factor { /* %prec UNARY_MINUS */
2189                 $$ = scm_difference ($2, SCM_UNDEFINED);
2190         }
2191         | bare_number
2192         ;
2193
2194
2195 bare_number:
2196         UNSIGNED        {
2197                 $$ = gh_int2scm ($1);
2198         }
2199         | REAL          {
2200                 $$ = $1;
2201         }
2202         | NUMBER_IDENTIFIER             {
2203                 $$ = $1;
2204         }
2205         | REAL NUMBER_IDENTIFIER        {
2206                 $$ = gh_double2scm (gh_scm2double ($1) * gh_scm2double ($2));
2207         }
2208         | UNSIGNED NUMBER_IDENTIFIER    {
2209                 $$ = gh_double2scm ($1 * gh_scm2double ($2));
2210         }
2211         ;
2212
2213
2214 bare_unsigned:
2215         UNSIGNED {
2216                         $$ = $1;
2217         }
2218         | DIGIT {
2219                 $$ = $1;
2220         }
2221         ;
2222
2223 bare_int:
2224         bare_number {
2225                 if (scm_integer_p ($1) == SCM_BOOL_T)
2226                 {
2227                         int k = gh_scm2int ($1);
2228                         $$ = k;
2229                 } else
2230                 {
2231                         THIS->parser_error (_ ("need integer number arg"));
2232                         $$ = 0;
2233                 }
2234         }
2235         | '-' bare_int {
2236                 $$ = -$2;
2237         }
2238         ;
2239
2240
2241 string:
2242         STRING          {
2243                 $$ = $1;
2244         }
2245         | STRING_IDENTIFIER     {
2246                 $$ = $1;
2247         }
2248         | string '+' string {
2249                 $$ = scm_string_append (scm_list_n ($1, $3, SCM_UNDEFINED));
2250         }
2251         ;
2252
2253
2254 exclamations:
2255                 { $$ = 0; }
2256         | exclamations '!'      { $$ ++; }
2257         ;
2258
2259 questions:
2260                 { $$ = 0; }
2261         | questions '?' { $$ ++; }
2262         ;
2263
2264
2265
2266 full_markup:
2267         MARKUP_IDENTIFIER {
2268                 $$ = $1;
2269         }
2270         | MARKUP 
2271                 { THIS->lexer_->push_markup_state (); }
2272         markup
2273                 { $$ = $3;
2274                   THIS->lexer_->pop_state ();
2275                 }
2276         ;
2277
2278
2279 /*
2280 This should be done more dynamically if possible.
2281 */ 
2282 markup:
2283         STRING {
2284                 $$ = make_simple_markup ($1);
2285         }
2286         | MARKUP_HEAD_MARKUP0 markup {
2287                 $$ = scm_list_n ($1, $2, SCM_UNDEFINED);
2288         }
2289         | MARKUP_HEAD_MARKUP0_MARKUP1 markup markup {
2290                 $$ = scm_list_n ($1, $2, $3, SCM_UNDEFINED);
2291         }
2292         | MARKUP_HEAD_SCM0_MARKUP1 SCM_T markup {
2293                 $$  = scm_list_n ($1, $2, $3, SCM_UNDEFINED); 
2294         }
2295         | markup_line {
2296                 $$ = $1;
2297         }
2298         | MARKUP_HEAD_LIST0 markup_list {
2299                 $$ = scm_list_n ($1,$2, SCM_UNDEFINED);
2300         }
2301         | MARKUP_HEAD_SCM0 embedded_scm {
2302                 $$ = scm_list_n ($1, $2, SCM_UNDEFINED);
2303         }
2304         | MARKUP_HEAD_SCM0_SCM1_MARKUP2 embedded_scm embedded_scm markup {
2305                 $$ = scm_list_n ($1, $2, $3, $4, SCM_UNDEFINED);
2306         }
2307         | MARKUP_HEAD_SCM0_SCM1_SCM2 embedded_scm embedded_scm embedded_scm {
2308                 $$ = scm_list_n ($1, $2, $3, $4, SCM_UNDEFINED);
2309         }
2310         | MARKUP_IDENTIFIER {
2311                 $$ = $1;
2312         }
2313         
2314         ;
2315
2316 markup_list:
2317         chord_open markup_list_body chord_close { $$ = scm_reverse_x ($2, SCM_EOL); }
2318         ;
2319
2320 markup_line:
2321         '{' markup_list_body '}' {
2322                 static SCM line ;
2323                 if (!line)
2324                         line = scm_c_eval_string ("line-markup");
2325         
2326                 $$ = scm_list_n (line, scm_reverse_x ($2, SCM_EOL), SCM_UNDEFINED);
2327         }
2328         ;
2329
2330 markup_list_body:
2331         /**/ {  $$ = SCM_EOL; }
2332         | markup_list_body markup {
2333                 $$ = gh_cons ($2, $1) ;
2334         }
2335         ;
2336
2337
2338 %%
2339
2340 void
2341 My_lily_parser::set_yydebug (bool )
2342 {
2343 #if 0
2344         yydebug = 1;
2345 #endif
2346 }
2347
2348 extern My_lily_parser * current_parser;
2349
2350 void
2351 My_lily_parser::do_yyparse ()
2352 {
2353         current_parser = this;;
2354         yyparse ((void*)this);
2355 }
2356
2357
2358 /*
2359 Should make this optional?    It will also complain when you do
2360
2361         [s4]
2362
2363 which is entirely legitimate.
2364
2365 Or we can scrap it. Barchecks should detect wrong durations, and
2366 skipTypesetting speeds it up a lot.
2367 */
2368
2369 void
2370 My_lily_parser::beam_check (SCM dur)
2371 {
2372   Duration *d = unsmob_duration (dur);
2373   if (unsmob_music (last_beam_start_) && d->duration_log () <= 2)
2374     {
2375       Music * m = unsmob_music (last_beam_start_);
2376       m->origin ()->warning (_("Suspect duration found following this beam"));
2377     }
2378   last_beam_start_ = SCM_EOL;
2379 }
2380
2381
2382
2383
2384 /*
2385
2386 It is a little strange to have this function in this file, but
2387 otherwise, we have to import music classes into the lexer.
2388
2389 */
2390 int
2391 My_lily_lexer::try_special_identifiers (SCM * destination, SCM sid)
2392 {
2393         if (gh_string_p (sid)) {
2394                 *destination = sid;
2395                 return STRING_IDENTIFIER;
2396         } else if (gh_number_p (sid)) {
2397                 *destination = sid;
2398                 return NUMBER_IDENTIFIER;
2399         } else if (unsmob_translator_def (sid)) {
2400                 *destination = unsmob_translator_def (sid)->clone_scm();
2401                 return TRANSLATOR_IDENTIFIER;
2402         } else if (unsmob_score (sid)) {
2403                 Score *sc =  new Score (*unsmob_score (sid));
2404                 *destination =sc->self_scm ();
2405                 return SCORE_IDENTIFIER;
2406         } else if (Music * mus =unsmob_music (sid)) {
2407                 *destination = unsmob_music (sid)->clone ()->self_scm();
2408                 unsmob_music (*destination)->
2409                         set_mus_property ("origin", make_input (last_input_));
2410                 return dynamic_cast<Event*> (mus)
2411                         ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
2412         } else if (unsmob_duration (sid)) {
2413                 *destination = unsmob_duration (sid)->smobbed_copy();
2414                 return DURATION_IDENTIFIER;
2415         } else if (unsmob_music_output_def (sid)) {
2416                 Music_output_def *p = unsmob_music_output_def (sid);
2417                 p = p->clone ();
2418
2419                 *destination = p->self_scm();
2420                 return MUSIC_OUTPUT_DEF_IDENTIFIER;
2421         } else if (Text_item::markup_p (sid)) {
2422                 *destination = sid;
2423                 return MARKUP_IDENTIFIER;
2424         }
2425
2426         return -1;      
2427 }