]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
359ae2545710a8e3fa16ba42b3126c5cca546808
[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 <stdlib.h>
57
58
59 #include "scm-option.hh"
60 #include "translator-def.hh"
61 #include "lily-guile.hh"
62 #include "misc.hh"
63 #include "my-lily-lexer.hh"
64 #include "paper-def.hh"
65 #include "midi-def.hh"
66 #include "main.hh"
67 #include "file-path.hh"
68 #include "warn.hh"
69 #include "dimensions.hh"
70 #include "my-lily-parser.hh"
71 #include "score.hh"
72 #include "input-file-results.hh"
73 #include "input.hh"
74 #include "lilypond-input-version.hh"
75 #include "scm-hash.hh"
76 #include "auto-change-iterator.hh"
77 #include "ly-modules.hh"
78 #include "music-sequence.hh"
79 #include "input-smob.hh"
80 #include "event.hh"
81 #include "text-item.hh"
82 #include "music-list.hh"
83
84
85 #define MY_MAKE_MUSIC(x)  make_music_by_name (ly_symbol2scm (x))
86
87
88
89 #define YYERROR_VERBOSE 1
90
91 My_lily_parser* my_lily_parser;
92 #define YYPARSE_PARAM my_lily_parser
93 #define YYLEX_PARAM my_lily_parser
94 #define THIS\
95         ((My_lily_parser *) my_lily_parser)
96
97 #define yyerror THIS->parser_error
98
99 /*
100   Add symbols to the  TAGS field of a music object. 
101 */
102
103 void
104 tag_music (Music*m,  SCM tag, Input ip)
105 {
106         SCM tags = m->get_mus_property ("tags");
107         if (gh_symbol_p (tag))
108                 tags = scm_cons (tag, tags);
109         else if (gh_list_p (tag))
110                 tags = gh_append2 (tag, tags);
111         else
112                 ip.warning (_("Tag must be symbol or list of symbols."));
113
114         m->set_mus_property ("tags", tags);
115 }
116
117
118
119 bool
120 regular_identifier_b (SCM id)
121 {
122   String str = ly_scm2string (id);
123   char const *s = str.to_str0 () ;
124
125   bool v = true;
126   while (*s && v)
127    {
128         v = v && isalpha (*s);
129         s++;
130    }
131   return v;
132 }
133
134 SCM
135 make_simple_markup (SCM a)
136 {
137         static SCM simple;
138         if (!simple)
139         simple = scm_c_eval_string ("simple-markup");
140
141         return scm_list_n (simple, a, SCM_UNDEFINED);
142 }
143
144
145 bool
146 is_duration_b (int t)
147 {
148   return t && t == 1 << intlog2 (t);
149 }
150
151 void
152 set_music_properties (Music *p, SCM a)
153 {
154   for (SCM k = a; gh_pair_p (k); k = ly_cdr (k))
155         {
156         p->internal_set_mus_property (ly_caar (k), ly_cdar (k));
157         }
158 }
159
160 SCM
161 make_chord_step (int step, int alter)
162 {
163         if (step == 7)
164                 alter--;
165
166         /* ugh: fucks up above 13 */
167         Pitch m(step > 7 ? 1 : 0,(step - 1) % 7, alter);
168         return m.smobbed_copy ();
169 }
170
171
172 SCM
173 make_chord (SCM pitch, SCM dur, SCM modification_list)
174 {
175         static SCM chord_ctor;
176         if (!chord_ctor)
177                 chord_ctor= scm_c_eval_string ("construct-chord");
178         SCM ch=  scm_call_3 (chord_ctor, pitch, dur, modification_list);
179         scm_gc_protect_object (ch);
180         return ch;
181 }
182
183 /*
184   Todo: actually also use apply iso. call too ... 
185 */
186 bool
187 ly_input_procedure_p (SCM x)
188 {
189         return gh_procedure_p (x)
190                 || (gh_pair_p (x) && gh_procedure_p (gh_car (x)));
191 }
192
193 Music* 
194 set_property_music (SCM sym, SCM value)
195 {
196         Music * p = MY_MAKE_MUSIC("PropertySet");
197         p->set_mus_property ("symbol", sym);
198         p->set_mus_property ("value", value);
199         return p;
200 }
201
202 %}
203
204 /* We use SCMs to do strings, because it saves us the trouble of
205 deleting them.  Let's hope that a stack overflow doesnt trigger a move
206 of the parse stack onto the heap. */
207
208
209 %union {
210         String * string;
211     Music *music;
212     Score *score;
213     Music_output_def * outputdef;
214     SCM scm;
215     int i;
216 }
217 %{
218
219 int
220 yylex (YYSTYPE *s,  void * v)
221 {
222         My_lily_parser   *pars = (My_lily_parser*) v;
223         My_lily_lexer * lex = pars->lexer_;
224
225         lex->lexval = (void*) s;
226         lex->prepare_for_next_token();
227         return lex->yylex ();
228 }
229
230
231 %}
232
233 %pure_parser
234
235
236 %token ACCEPTS
237 %token ADDLYRICS
238 %token ALIAS
239 %token ALTERNATIVE
240 %token APPLY
241 %token APPLYCONTEXT
242 %token APPLYOUTPUT
243 %token AUTOCHANGE
244 %token BAR
245 %token BREATHE
246 %token CHORDMODIFIERS  
247 %token CHORDS
248 %token LESSLESS
249 %token MOREMORE
250 %token CLEF
251 %token COMMANDSPANREQUEST
252 %token CONSISTS
253 %token CONSISTSEND
254 %token CONTEXT
255 %token DEFAULT
256 %token DENIES
257 %token DESCRIPTION
258 %token EXTENDER
259 %token FIGURES FIGURE_OPEN FIGURE_CLOSE
260 %token FIGURE_BRACKET_CLOSE FIGURE_BRACKET_OPEN
261 %token GRACE 
262 %token ACCIACCATURA
263 %token APPOGGIATURA 
264 %token GROBDESCRIPTIONS
265 %token HEADER
266 %token HYPHEN
267 %token INVALID
268 %token KEY
269 %token LYRICS
270 %token MARK
271 %token MIDI
272 %token MULTI_MEASURE_REST
273 %token NAME
274 %token NEWCONTEXT
275 %token NOTES
276 %token OCTAVE
277 %token ONCE
278 %token OUTPUTPROPERTY
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         | OUTPUTPROPERTY embedded_scm embedded_scm '=' embedded_scm     {
891                 SCM pred = $2;
892                 if (!gh_symbol_p ($3))
893                 {
894                         THIS->parser_error (_ ("Second argument must be a symbol")); 
895                 }
896                 /* Should check # args */
897                 if (!gh_procedure_p (pred))
898                 {
899                         THIS->parser_error (_ ("First argument must be a procedure taking one argument"));
900                 }
901
902                 Music*m = MY_MAKE_MUSIC("OutputPropertySetMusic");
903                 m->set_mus_property ("predicate", pred);
904                 m->set_mus_property ("grob-property", $3);
905                 m->set_mus_property ("grob-value",  $5);
906
907                 $$ = m;
908         }
909         | MUSIC_IDENTIFIER {
910                 $$ = unsmob_music ($1);
911         }
912         | property_def
913         | translator_change
914         ;
915
916
917 grace_head:
918         GRACE  { $$ = scm_makfrom0str ("Grace"); } 
919         | ACCIACCATURA { $$ = scm_makfrom0str ("Acciaccatura"); }
920         | APPOGGIATURA { $$ = scm_makfrom0str ("Appoggiatura"); }
921         ;
922         
923
924 Composite_music:
925         CONTEXT STRING Music    {
926                 Music*csm =MY_MAKE_MUSIC("ContextSpeccedMusic");
927
928                 csm->set_mus_property ("element", $3->self_scm ());
929                 scm_gc_unprotect_object ($3->self_scm ());
930
931                 csm->set_mus_property ("context-type", scm_string_to_symbol ($2));
932                 csm->set_mus_property ("context-id", scm_makfrom0str (""));
933
934                 $$ = csm;
935         }
936         | AUTOCHANGE STRING Music       {
937         Music*chm = MY_MAKE_MUSIC("AutoChangeMusic");
938                 chm->set_mus_property ("element", $3->self_scm ());
939                 chm->set_mus_property ("iterator-ctor", Auto_change_iterator::constructor_proc);
940
941                 scm_gc_unprotect_object ($3->self_scm ());
942                 chm->set_mus_property ("what", scm_string_to_symbol ($2));
943
944                 $$ = chm;
945                 chm->set_spot (*$3->origin ());
946         }
947         | grace_head Music {
948 #if 1
949         /*
950                 The other version is for easier debugging  of
951                 Sequential_music_iterator in combination with grace notes.
952         */
953
954 /*
955
956 TODO: should distinguish between both grace types in the
957 basic music objects too, since the meaning is different.
958
959 */
960
961                 String start_str = "start" + ly_scm2string ($1) + "Music"; 
962                 String stop_str = "stop" + ly_scm2string ($1) + "Music"; 
963                 
964                 SCM start = THIS->lexer_->lookup_identifier (start_str);
965                 SCM stop = THIS->lexer_->lookup_identifier (stop_str);
966
967                 Music *startm = unsmob_music (start);
968                 Music *stopm = unsmob_music (stop);
969
970                 SCM ms = SCM_EOL;
971                 if (stopm) {
972                         stopm = stopm->clone ();
973                         ms = scm_cons (stopm->self_scm (), ms);
974                         scm_gc_unprotect_object (stopm->self_scm ());
975                 }
976                 ms = scm_cons ($2->self_scm (), ms);
977                 scm_gc_unprotect_object ($2->self_scm());
978                 if (startm) {
979                         startm = startm->clone ();
980                         ms = scm_cons (startm->self_scm () , ms);
981                         scm_gc_unprotect_object (startm->self_scm ());
982                 }
983
984         
985                 Music* seq = MY_MAKE_MUSIC("SequentialMusic");
986                 seq->set_mus_property ("elements", ms);
987
988                 
989                 $$ = MY_MAKE_MUSIC("GraceMusic");
990                 $$->set_mus_property ("element", seq->self_scm ());
991                 scm_gc_unprotect_object (seq->self_scm ());
992 #else
993                 $$ = MY_MAKE_MUSIC("GraceMusic");
994                 $$->set_mus_property ("element", $2->self_scm ());
995                 scm_gc_unprotect_object ($2->self_scm ());
996 #endif
997         }
998         | CONTEXT string '=' string Music {
999                 Music * csm = MY_MAKE_MUSIC("ContextSpeccedMusic");
1000
1001                 csm->set_mus_property ("element", $5->self_scm ());
1002                 scm_gc_unprotect_object ($5->self_scm ());
1003
1004                 csm->set_mus_property ("context-type", scm_string_to_symbol ($2));
1005                 csm->set_mus_property ("context-id", $4);
1006
1007                 $$ = csm;
1008         }
1009         | NEWCONTEXT string Music {
1010                 static int new_context_count;
1011
1012                 Music * csm = MY_MAKE_MUSIC("ContextSpeccedMusic");
1013
1014                 csm->set_mus_property ("element", $3->self_scm ());
1015                 scm_gc_unprotect_object ($3->self_scm ());
1016
1017                 csm->set_mus_property ("context-type", scm_string_to_symbol ($2));
1018
1019                 char s[1024];
1020                 snprintf (s, 1024, "uniqueContext%d", new_context_count ++);
1021                 
1022                 SCM new_id = scm_makfrom0str (s);
1023                 csm->set_mus_property ("context-id", new_id);
1024                 $$ = csm;
1025         }
1026         | TIMES {
1027                 THIS->push_spot ();
1028         }
1029         /* CONTINUED */ 
1030                 fraction Music  
1031
1032         {
1033                 int n = gh_scm2int (ly_car ($3)); int d = gh_scm2int (ly_cdr ($3));
1034                 Music *mp = $4;
1035
1036                 $$= MY_MAKE_MUSIC("TimeScaledMusic");
1037                 $$->set_spot (THIS->pop_spot ());
1038
1039                 $$->set_mus_property ("element", mp->self_scm ());
1040                 scm_gc_unprotect_object (mp->self_scm ());
1041                 $$->set_mus_property ("numerator", gh_int2scm (n));
1042                 $$->set_mus_property ("denominator", gh_int2scm (d));
1043                 $$->compress (Moment (Rational (n,d)));
1044
1045         }
1046         | Repeated_music                { $$ = $1; }
1047         | Simultaneous_music            { $$ = $1; }
1048         | Sequential_music              { $$ = $1; }
1049         | TRANSPOSE pitch_also_in_chords pitch_also_in_chords Music {
1050                 $$ = MY_MAKE_MUSIC("TransposedMusic");
1051                 Music *p = $4;
1052                 Pitch from = *unsmob_pitch ($2);
1053                 Pitch to = *unsmob_pitch ($3);
1054
1055                 p->transpose (interval (from, to));
1056                 $$->set_mus_property ("element", p->self_scm ());
1057                 scm_gc_unprotect_object (p->self_scm ());
1058         }
1059         | APPLY embedded_scm Music  {
1060                 if (!ly_input_procedure_p ($2))
1061                         THIS->parser_error (_ ("\\apply takes function argument"));
1062                 
1063                 SCM ret = gh_call1 ($2, $3->self_scm ());
1064                 Music *m = unsmob_music (ret);
1065                 if (!m) {
1066                         THIS->parser_error ("\\apply must return a Music");
1067                         m = MY_MAKE_MUSIC("Music");
1068                         }
1069                 $$ = m;
1070         }
1071         | NOTES
1072                 { THIS->lexer_->push_note_state (); }
1073         Music
1074                 { $$ = $3;
1075                   THIS->lexer_->pop_state ();
1076                 }
1077         | FIGURES
1078                 { THIS->lexer_->push_figuredbass_state (); }
1079         Music
1080                 {
1081                   Music * chm = MY_MAKE_MUSIC("UntransposableMusic");
1082                   chm->set_mus_property ("element", $3->self_scm ());
1083                   $$ = chm;
1084                   scm_gc_unprotect_object ($3->self_scm());
1085
1086                   THIS->lexer_->pop_state ();
1087         }
1088         | CHORDS
1089                 { THIS->lexer_->push_chord_state (); }
1090         Music
1091                 {
1092                   Music * chm = MY_MAKE_MUSIC("UnrelativableMusic");
1093                   chm->set_mus_property ("element", $3->self_scm ());
1094                   scm_gc_unprotect_object ($3->self_scm());
1095                   $$ = chm;
1096
1097                   THIS->lexer_->pop_state ();
1098         }
1099         | LYRICS
1100                 { THIS->lexer_->push_lyric_state (); }
1101         Music
1102                 {
1103                   $$ = $3;
1104                   THIS->lexer_->pop_state ();
1105         }
1106         | relative_music        { $$ = $1; }
1107         | re_rhythmed_music     { $$ = $1; } 
1108         | part_combined_music   { $$ = $1; }
1109         | TAG embedded_scm Music {
1110                 tag_music ($3, $2, THIS->here_input ());
1111                 $$ = $3;
1112         }
1113         ;
1114
1115 relative_music:
1116         RELATIVE absolute_pitch Music {
1117                 Music * p = $3;
1118                 Pitch pit = *unsmob_pitch ($2);
1119                 $$ = MY_MAKE_MUSIC("RelativeOctaveMusic");
1120
1121                 $$->set_mus_property ("element", p->self_scm ());
1122                 scm_gc_unprotect_object (p->self_scm ());
1123
1124
1125                 Pitch retpitch = p->to_relative_octave (pit);
1126                 if (lily_1_8_relative)
1127                         $$->set_mus_property ("last-pitch", retpitch.smobbed_copy ());
1128         }
1129         ;
1130
1131 re_rhythmed_music:
1132         ADDLYRICS Music Music {
1133         Music*l =MY_MAKE_MUSIC("LyricCombineMusic");
1134           l->set_mus_property ("elements", gh_list ($2->self_scm (), $3->self_scm (), SCM_UNDEFINED));
1135           scm_gc_unprotect_object ($3->self_scm ());
1136           scm_gc_unprotect_object ($2->self_scm ());
1137           $$ = l;
1138         }
1139         ;
1140
1141 part_combined_music:
1142         PARTCOMBINE STRING Music Music {
1143                 Music * p= MY_MAKE_MUSIC("PartCombineMusic");
1144                 p->set_mus_property ("what", scm_string_to_symbol ($2));
1145                 p->set_mus_property ("elements", gh_list ($3->self_scm (),$4->self_scm (), SCM_UNDEFINED));  
1146
1147                 scm_gc_unprotect_object ($3->self_scm ());
1148                 scm_gc_unprotect_object ($4->self_scm ());  
1149
1150                 $$ = p;
1151         }
1152         ;
1153
1154 translator_change:
1155         TRANSLATOR STRING '=' STRING  {
1156                 Music*t= MY_MAKE_MUSIC("TranslatorChange");
1157                 t-> set_mus_property ("change-to-type", scm_string_to_symbol ($2));
1158                 t-> set_mus_property ("change-to-id", $4);
1159
1160                 $$ = t;
1161                 $$->set_spot (THIS->here_input ());
1162         }
1163         ;
1164
1165 property_def:
1166         simple_property_def
1167         | ONCE simple_property_def {
1168                 $$ = $2;
1169                 SCM e = $2->get_mus_property ("element");
1170                 unsmob_music (e)->set_mus_property ("once", SCM_BOOL_T);
1171         }
1172         ;
1173
1174 simple_property_def:
1175         PROPERTY STRING '.' STRING '='  scalar {
1176                 Music *t = set_property_music (scm_string_to_symbol ($4), $6);
1177                 Music *csm = MY_MAKE_MUSIC("ContextSpeccedMusic");
1178
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 UNSET {
1188                 
1189                 Music *t = MY_MAKE_MUSIC("PropertyUnset");
1190                 t->set_mus_property ("symbol", scm_string_to_symbol ($4));
1191
1192                 Music *csm = MY_MAKE_MUSIC("ContextSpeccedMusic");
1193                 csm->set_mus_property ("element", t->self_scm ());
1194                 scm_gc_unprotect_object (t->self_scm ());
1195
1196                 $$ = csm;
1197                 $$->set_spot (THIS->here_input ());
1198
1199                 csm-> set_mus_property ("context-type", scm_string_to_symbol ($2));
1200         }
1201         | PROPERTY STRING '.' STRING SET embedded_scm '=' embedded_scm {
1202                 bool autobeam
1203                   = gh_equal_p ($4, scm_makfrom0str ("autoBeamSettings"));
1204                 bool itc = internal_type_checking_global_b;
1205                 Music *t = MY_MAKE_MUSIC("OverrideProperty");
1206                 t->set_mus_property ("symbol", scm_string_to_symbol ($4));
1207                 t->set_mus_property ("pop-first", SCM_BOOL_T);
1208                 if (autobeam)
1209                         internal_type_checking_global_b = false;
1210                 t->set_mus_property ("grob-property", $6);
1211                 if (autobeam)
1212                         internal_type_checking_global_b = itc;
1213                 t->set_mus_property ("grob-value", $8);
1214
1215                 Music *csm = MY_MAKE_MUSIC("ContextSpeccedMusic");
1216                 csm->set_mus_property ("element", t->self_scm ());
1217                 scm_gc_unprotect_object (t->self_scm ());
1218                 $$ = csm;
1219                 $$->set_spot (THIS->here_input ());
1220
1221                 csm-> set_mus_property ("context-type", scm_string_to_symbol ($2));
1222         }
1223         | PROPERTY STRING '.' STRING OVERRIDE
1224                 embedded_scm '=' embedded_scm
1225         {
1226                 /*
1227                         UGH UGH UGH UGH.
1228                 */
1229                 bool autobeam
1230                   = gh_equal_p ($4, scm_makfrom0str ("autoBeamSettings"));
1231                 bool itc = internal_type_checking_global_b;
1232
1233                 Music *t = MY_MAKE_MUSIC("OverrideProperty");
1234                 t->set_mus_property ("symbol", scm_string_to_symbol ($4));
1235                 if (autobeam)
1236                         internal_type_checking_global_b = false;
1237                 t->set_mus_property ("grob-property", $6);
1238                 t->set_mus_property ("grob-value", $8);
1239                 if (autobeam)
1240                         internal_type_checking_global_b = itc;
1241
1242                 Music *csm = MY_MAKE_MUSIC("ContextSpeccedMusic");
1243                 csm->set_mus_property ("element", t->self_scm ());
1244                 scm_gc_unprotect_object (t->self_scm ());
1245
1246                 $$ = csm;
1247                 $$->set_spot (THIS->here_input ());
1248
1249                 csm-> set_mus_property ("context-type", scm_string_to_symbol ($2));
1250
1251         }
1252         | PROPERTY STRING '.' STRING REVERT embedded_scm {
1253                 Music *t = MY_MAKE_MUSIC("RevertProperty");
1254                 bool autobeam
1255                   = gh_equal_p ($4, scm_makfrom0str ("autoBeamSettings"));
1256                 bool itc = internal_type_checking_global_b;
1257
1258                 t->set_mus_property ("symbol", scm_string_to_symbol ($4));
1259                 if (autobeam)
1260                         internal_type_checking_global_b = false;
1261                 t->set_mus_property ("grob-property", $6);
1262                 if (autobeam)
1263                         internal_type_checking_global_b = itc;
1264         
1265                 Music *csm = MY_MAKE_MUSIC("ContextSpeccedMusic");
1266                 csm->set_mus_property ("element", t->self_scm ());
1267                 scm_gc_unprotect_object (t->self_scm ());
1268
1269                 $$ = csm;
1270                 $$->set_spot (THIS->here_input ());
1271
1272                 csm-> set_mus_property ("context-type", scm_string_to_symbol ($2));
1273         }
1274         ;
1275
1276
1277 scalar:
1278         string          { $$ = $1; }
1279         | bare_int      { $$ = gh_int2scm ($1); }
1280         | embedded_scm  { $$ = $1; }
1281         | full_markup {  $$ = $1; }
1282         | DIGIT { $$ = gh_int2scm ($1); }
1283         ;
1284
1285
1286
1287 /*
1288 This is a trick:
1289
1290 Adding pre_events to the simple_element
1291 makes the choice between
1292
1293   string:  STRING
1294
1295 and
1296
1297   simple_element: STRING
1298
1299 a single shift/reduction conflict.
1300
1301 nevertheless, this is not very clean, and we should find a different
1302 solution.  
1303
1304 */
1305 pre_events: {
1306                 THIS->push_spot ();
1307         }
1308         ;
1309
1310 event_chord:
1311         pre_events simple_element post_events   {
1312                 SCM elts = $2-> get_mus_property ("elements");
1313
1314                 elts = gh_append2 (elts, scm_reverse_x ($3, SCM_EOL));
1315
1316                 $2->set_mus_property ("elements", elts);
1317                 $$ = $2;
1318         }
1319         | command_element
1320         | note_chord_element
1321         ;
1322
1323
1324 note_chord_element:
1325         chord_body optional_notemode_duration post_events
1326         {
1327                 SCM dur = unsmob_duration ($2)->smobbed_copy();
1328                 SCM es = $1->get_mus_property ("elements");
1329                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
1330
1331                 for (SCM s = es; gh_pair_p (s); s = gh_cdr (s))
1332                   unsmob_music (gh_car(s))->set_mus_property ("duration", dur);
1333                 es = gh_append2 (es, postevs);
1334
1335                 $1-> set_mus_property ("elements", es);
1336                 $$ = $1;
1337         }
1338         ;
1339
1340 chord_open: '<'
1341         ;
1342
1343 chord_close: '>'
1344         ;
1345
1346 simul_open: LESSLESS
1347         ;
1348
1349 simul_close: MOREMORE
1350         ;
1351
1352 chord_body:
1353         chord_open chord_body_elements chord_close
1354         {
1355                 $$ = MY_MAKE_MUSIC("EventChord");
1356                 $$->set_mus_property ("elements",
1357                         scm_reverse_x ($2, SCM_EOL));
1358         }
1359         ;
1360
1361 chord_body_elements:
1362         /* empty */             { $$ = SCM_EOL; }
1363         | chord_body_elements chord_body_element {
1364                 $$ = gh_cons ($2->self_scm(), $1);
1365                 scm_gc_unprotect_object ($2->self_scm());
1366         }
1367         ;
1368
1369 chord_body_element:
1370         pitch exclamations questions post_events
1371         {
1372                 Music * n = MY_MAKE_MUSIC("NoteEvent");
1373                 n->set_mus_property ("pitch", $1);
1374                 if ($3 % 2)
1375                         n->set_mus_property ("cautionary", SCM_BOOL_T);
1376                 if ($2 % 2 || $3 % 2)
1377                         n->set_mus_property ("force-accidental", SCM_BOOL_T);
1378
1379                 SCM arts = scm_reverse_x ($4, SCM_EOL);
1380                 n->set_mus_property ("articulations", arts);
1381
1382                 $$ = n;
1383         }
1384         ;
1385
1386 command_element:
1387         command_req {
1388                 $$ = MY_MAKE_MUSIC("EventChord");
1389                 $$->set_mus_property ("elements", scm_cons ($1->self_scm (), SCM_EOL));
1390                 scm_gc_unprotect_object ($1->self_scm());
1391
1392                 $$-> set_spot (THIS->here_input ());
1393                 $1-> set_spot (THIS->here_input ());
1394         }
1395         | OCTAVE { THIS->push_spot (); }
1396           pitch {
1397                 Music *l = MY_MAKE_MUSIC("RelativeOctaveCheck");
1398                 $$ = l;
1399                 $$->set_spot (THIS->pop_spot ());
1400                 $$->set_mus_property ("pitch", $3);
1401         }
1402         | E_LEFTSQUARE {
1403                 Music *l = MY_MAKE_MUSIC("LigatureEvent");
1404                 l->set_mus_property ("span-direction", gh_int2scm (START));
1405                 l->set_spot (THIS->here_input ());
1406
1407                 $$ = MY_MAKE_MUSIC("EventChord");
1408                 $$->set_mus_property ("elements", scm_cons (l->self_scm (), SCM_EOL));
1409                 scm_gc_unprotect_object (l->self_scm());
1410                 $$->set_spot (THIS->here_input ());
1411         }
1412         | E_RIGHTSQUARE {
1413                 Music *l = MY_MAKE_MUSIC("LigatureEvent");
1414                 l->set_mus_property ("span-direction", gh_int2scm (STOP));
1415                 l->set_spot (THIS->here_input ());
1416
1417                 $$ = MY_MAKE_MUSIC("EventChord");
1418                 $$->set_mus_property ("elements", scm_cons (l->self_scm (), SCM_EOL));
1419                 $$->set_spot (THIS->here_input ());
1420                 scm_gc_unprotect_object (l->self_scm());
1421         }
1422         | E_BACKSLASH {
1423                 $$ = MY_MAKE_MUSIC("VoiceSeparator");
1424                 $$->set_spot (THIS->here_input ());
1425         }
1426         | '|'      {
1427
1428                 $$ = MY_MAKE_MUSIC("BarCheck");
1429                 $$->set_spot (THIS->here_input ());
1430         }
1431         | BAR STRING                    {
1432                 Music *t = set_property_music (ly_symbol2scm ("whichBar"), $2);
1433
1434                 Music *csm = MY_MAKE_MUSIC("ContextSpeccedMusic");
1435                 csm->set_mus_property ("element", t->self_scm ());
1436                 scm_gc_unprotect_object (t->self_scm ());
1437
1438                 $$ = csm;
1439                 $$->set_spot (THIS->here_input ());
1440
1441                 csm->set_mus_property ("context-type", ly_symbol2scm ("Timing"));
1442         }
1443         | PARTIAL duration_length       {
1444                 Moment m = - unsmob_duration ($2)->get_length ();
1445                 Music * p = set_property_music (ly_symbol2scm ( "measurePosition"),m.smobbed_copy ());
1446
1447                 Music * sp = MY_MAKE_MUSIC("ContextSpeccedMusic");
1448                 sp->set_mus_property ("element", p->self_scm ());
1449                 scm_gc_unprotect_object (p->self_scm ());
1450
1451                 $$ =sp ;
1452                 sp-> set_mus_property ("context-type", ly_symbol2scm ("Timing"));
1453         }
1454         | CLEF STRING  {
1455                 static SCM proc ;
1456                 if (!proc)
1457                         proc = scm_c_eval_string ("make-clef-set");
1458
1459                 SCM result = scm_call_1 (proc, $2);
1460                 scm_gc_protect_object (result);
1461                 $$ = unsmob_music (result);
1462         }
1463         | TIME_T fraction  {
1464                 static SCM proc;
1465                 if (!proc)
1466                         proc = scm_c_eval_string ("make-time-signature-set");
1467
1468                 SCM result = scm_apply_2   (proc, gh_car ($2), gh_cdr ($2), SCM_EOL);
1469                 scm_gc_protect_object (result);
1470                 $$ = unsmob_music (result);
1471         }
1472         ;
1473
1474 command_req:
1475         shorthand_command_req   { $$ = $1; }
1476         | verbose_command_req   { $$ = $1; }
1477         ;
1478
1479 shorthand_command_req:
1480         extender_req {
1481                 $$ = $1;
1482         }
1483         | hyphen_req {
1484                 $$ = $1;
1485         }
1486         | BREATHE {
1487                 $$ = MY_MAKE_MUSIC("BreathingSignEvent");
1488         }
1489         | E_TILDE {
1490                 $$ = MY_MAKE_MUSIC("PesOrFlexaEvent");
1491         }
1492         ;
1493
1494 verbose_command_req:
1495         MARK DEFAULT  {
1496                 Music * m = MY_MAKE_MUSIC("MarkEvent");
1497                 $$ = m;
1498         }
1499         | MARK scalar {
1500                 Music *m = MY_MAKE_MUSIC("MarkEvent");
1501                 m->set_mus_property ("label", $2);
1502                 $$ = m;
1503         }
1504         | SKIP duration_length {
1505                 Music * skip = MY_MAKE_MUSIC("SkipEvent");
1506                 skip->set_mus_property ("duration", $2);
1507
1508                 $$ = skip;
1509         }
1510         | tempo_event {
1511                 $$ = $1;
1512         }
1513         | KEY DEFAULT {
1514                 Music *key= MY_MAKE_MUSIC("KeyChangeEvent");
1515                 $$ = key;
1516         }
1517         | KEY NOTENAME_PITCH SCM_IDENTIFIER     {
1518
1519                 Music *key= MY_MAKE_MUSIC("KeyChangeEvent");
1520                 if (scm_ilength ($3) > 0)
1521                 {               
1522                         key->set_mus_property ("pitch-alist", $3);
1523                         key->set_mus_property ("tonic", Pitch (0,0,0).smobbed_copy());
1524                         ((Music*)key)->transpose (* unsmob_pitch ($2));
1525                 } else {
1526                         THIS->parser_error (_("Second argument must be pitch list."));
1527                 }
1528
1529                 $$ = key;
1530         }
1531         ;
1532
1533 post_events:
1534         /* empty */ {
1535                 $$ = SCM_EOL;
1536         }
1537         | post_events post_event {
1538                 $2->set_spot (THIS->here_input ());
1539                 $$ = gh_cons ($2->self_scm(), $$);
1540                 scm_gc_unprotect_object ($2->self_scm());
1541         }
1542         | post_events tagged_post_event {
1543                 $2 -> set_spot (THIS->here_input ());
1544                 $$ = scm_cons ($2->self_scm(), $$);
1545                 scm_gc_unprotect_object ($2->self_scm());
1546         }
1547         ;
1548
1549
1550 tagged_post_event:
1551         '-' TAG embedded_scm post_event {
1552                 tag_music ($4, $3, THIS->here_input ());
1553                 $$ = $4;
1554         }
1555         ;
1556
1557 post_event:
1558         direction_less_event {
1559                 $$ = $1;
1560         }
1561         | script_dir direction_reqd_event {
1562                 if ($1)
1563                         $2->set_mus_property ("direction", gh_int2scm ($1));
1564                 $$ = $2;
1565         }
1566         | script_dir direction_less_event {
1567                 if ($1)
1568                         $2->set_mus_property ("direction", gh_int2scm ($1));
1569                 $$ = $2;
1570         }
1571         | string_number_event
1572         ;
1573
1574 string_number_event:
1575         E_UNSIGNED {
1576                 Music * s = MY_MAKE_MUSIC("StringNumberEvent");
1577                 s->set_mus_property ("string-number",  gh_int2scm($1));
1578                 s->set_spot (THIS->here_input ());
1579                 $$ = s;
1580         }
1581         ;
1582
1583
1584 direction_less_event:
1585         '['  {
1586
1587
1588 /*
1589
1590 TODO: should take all these defs out of the parser, adn make use
1591 configurable, i.e.
1592
1593
1594 (set-articulation '~ "trill")
1595
1596 */
1597                 Music * m = MY_MAKE_MUSIC ("BeamEvent");
1598                 m->set_spot (THIS->here_input());
1599                 m->set_mus_property ("span-direction" , gh_int2scm (START));
1600                 $$ = m;
1601         }
1602         | ']'  {
1603                 Music * m = MY_MAKE_MUSIC ("BeamEvent");
1604                 m->set_spot (THIS->here_input());
1605                 m->set_mus_property ("span-direction" , gh_int2scm (STOP));
1606                 $$ = m;
1607         }
1608         | '~' {
1609                 Music * m = MY_MAKE_MUSIC ("TieEvent");
1610                 m->set_spot (THIS->here_input());
1611                 $$ = m;
1612         }
1613         | close_event {
1614                 $$ = $1;
1615                 dynamic_cast<Music *> ($$)->set_mus_property ("span-direction", gh_int2scm (START));
1616         }
1617         | open_event {
1618                 $$ = $1;
1619                 dynamic_cast<Music *> ($$)->set_mus_property ("span-direction", gh_int2scm (STOP));
1620         }
1621         | EVENT_IDENTIFIER      {
1622                 $$ = unsmob_music ($1);
1623         }
1624         | tremolo_type  {
1625                Music * a = MY_MAKE_MUSIC("TremoloEvent");
1626                a->set_spot (THIS->here_input ());
1627                a->set_mus_property ("tremolo-type", gh_int2scm ($1));
1628                $$ = a;
1629         }
1630         ;       
1631         
1632 direction_reqd_event:
1633         gen_text_def {
1634                 $$ = $1; 
1635         }
1636         | script_abbreviation {
1637                 SCM s = THIS->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
1638                 Music *a = MY_MAKE_MUSIC("ArticulationEvent");
1639                 if (gh_string_p (s))
1640                         a->set_mus_property ("articulation-type", s);
1641                 else THIS->parser_error (_ ("Expecting string as script definition"));
1642                 $$ = a;
1643         }
1644         ;
1645         
1646 sup_quotes:
1647         '\'' {
1648                 $$ = 1;
1649         }
1650         | sup_quotes '\'' {
1651                 $$ ++;
1652         }
1653         ;
1654
1655 sub_quotes:
1656         ',' {
1657                 $$ = 1;
1658         }
1659         | sub_quotes ',' {
1660                 $$ ++ ;
1661         }
1662         ;
1663
1664 steno_pitch:
1665         NOTENAME_PITCH  {
1666                 $$ = $1;
1667         }
1668         | NOTENAME_PITCH sup_quotes     {
1669                 Pitch p = *unsmob_pitch ($1);
1670                 p = p.transposed (Pitch ($2,0,0));
1671                 $$ = p.smobbed_copy ();
1672         }
1673         | NOTENAME_PITCH sub_quotes      {
1674                 Pitch p =* unsmob_pitch ($1);
1675                 p = p.transposed (Pitch (-$2,0,0));
1676                 $$ = p.smobbed_copy ();
1677         }
1678         ;
1679
1680 /*
1681 ugh. duplication
1682 */
1683
1684 steno_tonic_pitch:
1685         TONICNAME_PITCH {
1686                 $$ = $1;
1687         }
1688         | TONICNAME_PITCH sup_quotes    {
1689                 Pitch p = *unsmob_pitch ($1);
1690                 p = p.transposed (Pitch ($2,0,0));
1691                 $$ = p.smobbed_copy ();
1692         }
1693         | TONICNAME_PITCH sub_quotes     {
1694                 Pitch p =* unsmob_pitch ($1);
1695
1696                 p = p.transposed (Pitch (-$2,0,0));
1697                 $$ = p.smobbed_copy ();
1698         }
1699         ;
1700
1701 pitch:
1702         steno_pitch {
1703                 $$ = $1;
1704         }
1705         ;
1706
1707 pitch_also_in_chords:
1708         pitch
1709         | steno_tonic_pitch
1710         ;
1711
1712
1713
1714
1715
1716 extender_req:
1717         EXTENDER {
1718                 if (!THIS->lexer_->lyric_state_b ())
1719                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
1720                 $$ = MY_MAKE_MUSIC("ExtenderEvent");
1721         }
1722         ;
1723
1724 hyphen_req:
1725         HYPHEN {
1726                 if (!THIS->lexer_->lyric_state_b ())
1727                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
1728                 $$ = MY_MAKE_MUSIC("HyphenEvent");
1729         }
1730         ;
1731
1732 close_event:
1733         '('     {
1734                 Music * s= MY_MAKE_MUSIC("SlurEvent");
1735                 $$ = s;
1736                 s->set_spot (THIS->here_input());
1737         }
1738         | E_OPEN        {
1739                 Music * s= MY_MAKE_MUSIC("PhrasingSlurEvent");
1740                 $$ = s;
1741                 s->set_spot (THIS->here_input());
1742         }
1743         | E_SMALLER {
1744                 Music *s =MY_MAKE_MUSIC("CrescendoEvent");
1745                 $$ = s;
1746                 s->set_spot (THIS->here_input());
1747         }
1748         | E_BIGGER {
1749                 Music *s =MY_MAKE_MUSIC("DecrescendoEvent");
1750                 $$ = s;
1751                 s->set_spot (THIS->here_input());
1752         }
1753         ;
1754
1755
1756 open_event:
1757         E_EXCLAMATION   {
1758                 Music *s =  MY_MAKE_MUSIC("CrescendoEvent");
1759                 s->set_spot (THIS->here_input());
1760
1761                 $$ = s;
1762         }
1763         | ')'   {
1764                 Music * s= MY_MAKE_MUSIC("SlurEvent");
1765                 $$ = s;
1766                 s->set_spot (THIS->here_input());
1767
1768         }
1769         | E_CLOSE       {
1770                 Music * s= MY_MAKE_MUSIC("PhrasingSlurEvent");
1771                 $$ = s;
1772                 s->set_mus_property ("span-type", scm_makfrom0str ( "phrasing-slur"));
1773                 s->set_spot (THIS->here_input());
1774         }
1775         ;
1776
1777 gen_text_def:
1778         full_markup {
1779                 Music *t = MY_MAKE_MUSIC("TextScriptEvent");
1780                 t->set_mus_property ("text", $1);
1781                 t->set_spot (THIS->here_input ());
1782                 $$ = t; 
1783         }
1784         | string {
1785                 Music *t = MY_MAKE_MUSIC("TextScriptEvent");
1786                 t->set_mus_property ("text", make_simple_markup ($1));
1787                 t->set_spot (THIS->here_input ());
1788                 $$ = t;
1789         
1790         }
1791         | DIGIT {
1792                 Music * t = MY_MAKE_MUSIC("FingerEvent");
1793                 t->set_mus_property ("digit",  gh_int2scm ($1));
1794                 t->set_spot (THIS->here_input ());
1795                 $$ = t;
1796         }
1797         ;
1798
1799 script_abbreviation:
1800         '^'             {
1801                 $$ = scm_makfrom0str ("Hat");
1802         }
1803         | '+'           {
1804                 $$ = scm_makfrom0str ("Plus");
1805         }
1806         | '-'           {
1807                 $$ = scm_makfrom0str ("Dash");
1808         }
1809         | '|'           {
1810                 $$ = scm_makfrom0str ("Bar");
1811         }
1812         | '>'           {
1813                 $$ = scm_makfrom0str ("Larger");
1814         }
1815         | '.'           {
1816                 $$ = scm_makfrom0str ("Dot");
1817         }
1818         | '_' {
1819                 $$ = scm_makfrom0str ("Underscore");
1820         }
1821         ;
1822
1823 script_dir:
1824         '_'     { $$ = DOWN; }
1825         | '^'   { $$ = UP; }
1826         | '-'   { $$ = CENTER; }
1827         ;
1828
1829
1830 absolute_pitch:
1831         steno_pitch     {
1832                 $$ = $1;
1833         }
1834         ;
1835
1836 duration_length:
1837         multiplied_duration {
1838                 $$ = $1;
1839         }
1840         ;
1841
1842 optional_notemode_duration:
1843         {
1844                 Duration dd = THIS->default_duration_;
1845                 $$ = dd.smobbed_copy ();
1846
1847                 THIS->beam_check ($$);
1848         }
1849         | multiplied_duration   {
1850                 $$ = $1;
1851                 THIS->default_duration_ = *unsmob_duration ($$);
1852
1853                 THIS->beam_check ($$);
1854         }
1855         ;
1856
1857 steno_duration:
1858         bare_unsigned dots              {
1859                 int l = 0;
1860                 if (!is_duration_b ($1))
1861                         THIS->parser_error (_f ("not a duration: %d", $1));
1862                 else
1863                         l =  intlog2 ($1);
1864
1865                 $$ = Duration (l, $2).smobbed_copy ();
1866         }
1867         | DURATION_IDENTIFIER dots      {
1868                 Duration *d =unsmob_duration ($1);
1869                 Duration k (d->duration_log (),d->dot_count () + $2);
1870
1871                 *d = k;
1872                 $$ = $1;
1873         }
1874         ;
1875
1876
1877
1878
1879 multiplied_duration:
1880         steno_duration {
1881                 $$ = $1;
1882         }
1883         | multiplied_duration '*' bare_unsigned {
1884                 $$ = unsmob_duration ($$)->compressed ( $3) .smobbed_copy ();
1885         }
1886         | multiplied_duration '*' FRACTION {
1887                 Rational  m (gh_scm2int (ly_car ($3)), gh_scm2int (ly_cdr ($3)));
1888
1889                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
1890         }
1891         ;
1892
1893 fraction:
1894         FRACTION { $$ = $1; }
1895         | UNSIGNED '/' UNSIGNED {
1896                 $$ = scm_cons (gh_int2scm ($1), gh_int2scm ($3));
1897         }
1898         ;
1899
1900 dots:
1901         /* empty */     {
1902                 $$ = 0;
1903         }
1904         | dots '.' {
1905                 $$ ++;
1906         }
1907         ;
1908
1909
1910 tremolo_type: 
1911         ':'     {
1912                 $$ =0;
1913         }
1914         | ':' bare_unsigned {
1915                 if (!is_duration_b ($2))
1916                         THIS->parser_error (_f ("not a duration: %d", $2));
1917                 $$ = $2;
1918         }
1919         ;
1920
1921
1922
1923 /*****************************************************************
1924                 BASS FIGURES
1925 *****************************************************************/
1926 bass_number:
1927         DIGIT   {
1928                 $$ = scm_number_to_string (gh_int2scm ($1), gh_int2scm (10));
1929         }
1930         | UNSIGNED {
1931                 $$ = scm_number_to_string (gh_int2scm ($1), gh_int2scm (10));
1932         }
1933         | STRING { $$ =  $1; }
1934         ;
1935
1936 bass_mod:
1937         '-'     { $$ = -1; }
1938         | '+'   { $$ = 1; } 
1939         | '!'   { $$ = 0; }
1940         ;
1941
1942 bass_figure:
1943         FIGURE_SPACE {
1944                 Music *bfr = MY_MAKE_MUSIC("BassFigureEvent");
1945                 $$ = bfr->self_scm();
1946                 scm_gc_unprotect_object ($$);
1947         }
1948         | bass_number  {
1949                 Music *bfr = MY_MAKE_MUSIC("BassFigureEvent");
1950                 $$ = bfr->self_scm();
1951
1952                 bfr->set_mus_property ("figure", $1);
1953
1954                 scm_gc_unprotect_object ($$);
1955         }
1956         | bass_figure bass_mod {
1957                 Music *m = unsmob_music ($1);
1958                 if ($2) {
1959                         SCM salter =m->get_mus_property ("alteration");
1960                         int alter = gh_number_p ( salter) ? gh_scm2int (salter) : 0;
1961                         m->set_mus_property ("alteration",
1962                                 gh_int2scm (alter + $2));
1963                 } else {
1964                         m->set_mus_property ("alteration", gh_int2scm (0));
1965                 }
1966         }
1967         ;
1968
1969 br_bass_figure:
1970         '[' bass_figure {
1971                 $$ = $2;
1972                 unsmob_music ($$)->set_mus_property ("bracket-start", SCM_BOOL_T);
1973         }
1974         | bass_figure   {
1975                 $$ = $1;
1976         }
1977         | br_bass_figure ']' {
1978                 $$ = $1;
1979                 unsmob_music ($1)->set_mus_property ("bracket-stop", SCM_BOOL_T);
1980         }
1981         ;
1982
1983 figure_list:
1984         /**/            {
1985                 $$ = SCM_EOL;
1986         }
1987         | figure_list br_bass_figure {
1988                 $$ = scm_cons ($2, $1); 
1989         }
1990         ;
1991
1992 figure_spec:
1993         FIGURE_OPEN figure_list FIGURE_CLOSE {
1994                 Music * m = MY_MAKE_MUSIC("EventChord");
1995                 $2 = scm_reverse_x ($2, SCM_EOL);
1996                 m->set_mus_property ("elements",  $2);
1997                 $$ = m->self_scm ();
1998         }
1999         ;
2000
2001
2002 optional_rest:
2003         /**/   { $$ = 0; }
2004         | REST { $$ = 1; }
2005         ;
2006
2007 simple_element:
2008         pitch exclamations questions optional_notemode_duration optional_rest {
2009
2010                 Input i = THIS->pop_spot ();
2011                 if (!THIS->lexer_->note_state_b ())
2012                         THIS->parser_error (_ ("Have to be in Note mode for notes"));
2013
2014                 Music *n = 0;
2015                 if ($5)
2016                         n =  MY_MAKE_MUSIC("RestEvent");
2017                 else
2018                         n =  MY_MAKE_MUSIC("NoteEvent");
2019                 
2020                 n->set_mus_property ("pitch", $1);
2021                 n->set_mus_property ("duration", $4);
2022
2023
2024                 if ($3 % 2)
2025                         n->set_mus_property ("cautionary", SCM_BOOL_T);
2026                 if ($2 % 2 || $3 % 2)
2027                         n->set_mus_property ("force-accidental", SCM_BOOL_T);
2028
2029                 Music *v = MY_MAKE_MUSIC("EventChord");
2030                 v->set_mus_property ("elements", scm_list_n (n->self_scm (), SCM_UNDEFINED));
2031                 scm_gc_unprotect_object (n->self_scm());
2032
2033                 v->set_spot (i);
2034                 n->set_spot (i);
2035                 $$ = v;
2036         }
2037         | figure_spec optional_notemode_duration {
2038                 Music * m = unsmob_music ($1);
2039                 Input i = THIS->pop_spot (); 
2040                 m->set_spot (i);
2041                 for (SCM s = m->get_mus_property ("elements"); gh_pair_p (s); s = ly_cdr (s))
2042                 {
2043                         unsmob_music (ly_car (s))->set_mus_property ("duration", $2);
2044                 }
2045                 $$ = m;
2046         }       
2047         | RESTNAME optional_notemode_duration           {
2048
2049                 Input i = THIS->pop_spot ();
2050                 Music * ev = 0;
2051                 if (ly_scm2string ($1) =="s") {
2052                         /* Space */
2053                         ev = MY_MAKE_MUSIC("SkipEvent");
2054                   }
2055                 else {
2056                         ev = MY_MAKE_MUSIC("RestEvent");
2057                 
2058                     }
2059                 ev->set_mus_property ("duration" ,$2);
2060                 ev->set_spot (i);
2061                 Music * velt = MY_MAKE_MUSIC("EventChord");
2062                 velt->set_mus_property ("elements", scm_list_n (ev->self_scm (),SCM_UNDEFINED));
2063                 velt->set_spot (i);
2064
2065                 $$ = velt;
2066         }
2067         | MULTI_MEASURE_REST optional_notemode_duration         {
2068                 THIS->pop_spot ();
2069
2070                 static SCM proc ;
2071                 if (!proc)
2072                         proc = scm_c_eval_string ("make-multi-measure-rest");
2073
2074                 SCM mus = scm_call_2 (proc, $2,
2075                         make_input (THIS->here_input()));       
2076                 scm_gc_protect_object (mus);
2077                 $$ = unsmob_music (mus);
2078         }
2079         
2080         | lyric_element optional_notemode_duration      {
2081                 Input i = THIS->pop_spot ();
2082                 if (!THIS->lexer_->lyric_state_b ())
2083                         THIS->parser_error (_ ("Have to be in Lyric mode for lyrics"));
2084
2085                 Music * lreq = MY_MAKE_MUSIC("LyricEvent");
2086                 lreq->set_mus_property ("text", $1);
2087                 lreq->set_mus_property ("duration",$2);
2088                 lreq->set_spot (i);
2089                 Music * velt = MY_MAKE_MUSIC("EventChord");
2090                 velt->set_mus_property ("elements", scm_list_n (lreq->self_scm (), SCM_UNDEFINED));
2091
2092                 $$= velt;
2093         }
2094         | new_chord {
2095                 THIS->pop_spot ();
2096
2097                 if (!THIS->lexer_->chord_state_b ())
2098                         THIS->parser_error (_ ("Have to be in Chord mode for chords"));
2099                 $$ = unsmob_music ($1);
2100         }
2101         ;
2102
2103 lyric_element:
2104         full_markup { $$ = $1; }
2105         | STRING {  $$ = $1 ; }
2106         ;
2107
2108 new_chord:
2109         steno_tonic_pitch optional_notemode_duration   {
2110                 $$ = make_chord ($1, $2, SCM_EOL);
2111         }
2112         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
2113                 SCM its = scm_reverse_x ($4, SCM_EOL);
2114                 $$ = make_chord ($1, $2, gh_cons ($3, its));
2115         }
2116         ;
2117
2118 chord_items:
2119         /**/ {
2120                 $$ = SCM_EOL;           
2121         }
2122         | chord_items chord_item {
2123                 $$ = gh_cons ($2, $$);
2124         }
2125         ;
2126
2127 chord_separator:
2128         CHORD_COLON {
2129                 $$ = ly_symbol2scm ("chord-colon");
2130         }
2131         | CHORD_CARET {
2132                 $$ = ly_symbol2scm ("chord-caret"); 
2133         }
2134         | CHORD_SLASH steno_tonic_pitch {
2135                 $$ = scm_list_n (ly_symbol2scm ("chord-slash"), $2, SCM_UNDEFINED); 
2136         }
2137         | CHORD_BASS steno_tonic_pitch {
2138                 $$ = scm_list_n (ly_symbol2scm ("chord-bass"), $2, SCM_UNDEFINED); 
2139         }
2140         ;
2141
2142 chord_item:
2143         chord_separator {
2144                 $$ = $1;
2145         }
2146         | step_numbers {
2147                 $$ = scm_reverse_x ($1, SCM_EOL);
2148         }
2149         | CHORD_MODIFIER  {
2150                 $$ = $1;
2151         }
2152         ;
2153
2154 step_numbers:
2155         step_number { $$ = gh_cons ($1, SCM_EOL); } 
2156         | step_numbers '.' step_number {
2157                 $$ = gh_cons ($3, $$);
2158         }
2159         ;
2160
2161 step_number:
2162         bare_unsigned {
2163                 $$ = make_chord_step ($1, 0);
2164         } 
2165         | bare_unsigned '+' {
2166                 $$ = make_chord_step ($1, 1);
2167         }
2168         | bare_unsigned CHORD_MINUS {
2169                 $$ = make_chord_step ($1,-1);
2170         }
2171         ;       
2172
2173 /*
2174         UTILITIES
2175
2176 TODO: should deprecate in favor of Scheme?
2177
2178  */
2179 number_expression:
2180         number_expression '+' number_term {
2181                 $$ = scm_sum ($1, $3);
2182         }
2183         | number_expression '-' number_term {
2184                 $$ = scm_difference ($1, $3);
2185         }
2186         | number_term 
2187         ;
2188
2189 number_term:
2190         number_factor {
2191                 $$ = $1;
2192         }
2193         | number_factor '*' number_factor {
2194                 $$ = scm_product ($1, $3);
2195         }
2196         | number_factor '/' number_factor {
2197                 $$ = scm_divide ($1, $3);
2198         }
2199         ;
2200
2201 number_factor:
2202         '-'  number_factor { /* %prec UNARY_MINUS */
2203                 $$ = scm_difference ($2, SCM_UNDEFINED);
2204         }
2205         | bare_number
2206         ;
2207
2208
2209 bare_number:
2210         UNSIGNED        {
2211                 $$ = gh_int2scm ($1);
2212         }
2213         | REAL          {
2214                 $$ = $1;
2215         }
2216         | NUMBER_IDENTIFIER             {
2217                 $$ = $1;
2218         }
2219         | REAL NUMBER_IDENTIFIER        {
2220                 $$ = gh_double2scm (gh_scm2double ($1) * gh_scm2double ($2));
2221         }
2222         | UNSIGNED NUMBER_IDENTIFIER    {
2223                 $$ = gh_double2scm ($1 * gh_scm2double ($2));
2224         }
2225         ;
2226
2227
2228 bare_unsigned:
2229         UNSIGNED {
2230                         $$ = $1;
2231         }
2232         | DIGIT {
2233                 $$ = $1;
2234         }
2235         ;
2236
2237 bare_int:
2238         bare_number {
2239                 if (scm_integer_p ($1) == SCM_BOOL_T)
2240                 {
2241                         int k = gh_scm2int ($1);
2242                         $$ = k;
2243                 } else
2244                 {
2245                         THIS->parser_error (_ ("need integer number arg"));
2246                         $$ = 0;
2247                 }
2248         }
2249         | '-' bare_int {
2250                 $$ = -$2;
2251         }
2252         ;
2253
2254
2255 string:
2256         STRING          {
2257                 $$ = $1;
2258         }
2259         | STRING_IDENTIFIER     {
2260                 $$ = $1;
2261         }
2262         | string '+' string {
2263                 $$ = scm_string_append (scm_list_n ($1, $3, SCM_UNDEFINED));
2264         }
2265         ;
2266
2267
2268 exclamations:
2269                 { $$ = 0; }
2270         | exclamations '!'      { $$ ++; }
2271         ;
2272
2273 questions:
2274                 { $$ = 0; }
2275         | questions '?' { $$ ++; }
2276         ;
2277
2278
2279
2280 full_markup:
2281         MARKUP_IDENTIFIER {
2282                 $$ = $1;
2283         }
2284         | MARKUP 
2285                 { THIS->lexer_->push_markup_state (); }
2286         markup
2287                 { $$ = $3;
2288                   THIS->lexer_->pop_state ();
2289                 }
2290         ;
2291
2292
2293 /*
2294 This should be done more dynamically if possible.
2295 */ 
2296 markup:
2297         STRING {
2298                 $$ = make_simple_markup ($1);
2299         }
2300         | MARKUP_HEAD_MARKUP0 markup {
2301                 $$ = scm_list_n ($1, $2, SCM_UNDEFINED);
2302         }
2303         | MARKUP_HEAD_MARKUP0_MARKUP1 markup markup {
2304                 $$ = scm_list_n ($1, $2, $3, SCM_UNDEFINED);
2305         }
2306         | MARKUP_HEAD_SCM0_MARKUP1 SCM_T markup {
2307                 $$  = scm_list_n ($1, $2, $3, SCM_UNDEFINED); 
2308         }
2309         | markup_line {
2310                 $$ = $1;
2311         }
2312         | MARKUP_HEAD_LIST0 markup_list {
2313                 $$ = scm_list_n ($1,$2, SCM_UNDEFINED);
2314         }
2315         | MARKUP_HEAD_SCM0 embedded_scm {
2316                 $$ = scm_list_n ($1, $2, SCM_UNDEFINED);
2317         }
2318         | MARKUP_HEAD_SCM0_SCM1_MARKUP2 embedded_scm embedded_scm markup {
2319                 $$ = scm_list_n ($1, $2, $3, $4, SCM_UNDEFINED);
2320         }
2321         | MARKUP_HEAD_SCM0_SCM1_SCM2 embedded_scm embedded_scm embedded_scm {
2322                 $$ = scm_list_n ($1, $2, $3, $4, SCM_UNDEFINED);
2323         }
2324         | MARKUP_IDENTIFIER {
2325                 $$ = $1;
2326         }
2327         
2328         ;
2329
2330 markup_list:
2331         chord_open markup_list_body chord_close { $$ = scm_reverse_x ($2, SCM_EOL); }
2332         ;
2333
2334 markup_line:
2335         '{' markup_list_body '}' {
2336                 static SCM line ;
2337                 if (!line)
2338                         line = scm_c_eval_string ("line-markup");
2339         
2340                 $$ = scm_list_n (line, scm_reverse_x ($2, SCM_EOL), SCM_UNDEFINED);
2341         }
2342         ;
2343
2344 markup_list_body:
2345         /**/ {  $$ = SCM_EOL; }
2346         | markup_list_body markup {
2347                 $$ = gh_cons ($2, $1) ;
2348         }
2349         ;
2350
2351
2352 %%
2353
2354 void
2355 My_lily_parser::set_yydebug (bool )
2356 {
2357 #if 0
2358         yydebug = 1;
2359 #endif
2360 }
2361
2362 extern My_lily_parser * current_parser;
2363
2364 void
2365 My_lily_parser::do_yyparse ()
2366 {
2367         current_parser = this;;
2368         yyparse ((void*)this);
2369 }
2370
2371
2372 /*
2373 Should make this optional?    It will also complain when you do
2374
2375         [s4]
2376
2377 which is entirely legitimate.
2378
2379 Or we can scrap it. Barchecks should detect wrong durations, and
2380 skipTypesetting speeds it up a lot.
2381 */
2382
2383 void
2384 My_lily_parser::beam_check (SCM dur)
2385 {
2386   Duration *d = unsmob_duration (dur);
2387   if (unsmob_music (last_beam_start_) && d->duration_log () <= 2)
2388     {
2389       Music * m = unsmob_music (last_beam_start_);
2390       m->origin ()->warning (_("Suspect duration found following this beam"));
2391     }
2392   last_beam_start_ = SCM_EOL;
2393 }
2394
2395
2396
2397
2398 /*
2399
2400 It is a little strange to have this function in this file, but
2401 otherwise, we have to import music classes into the lexer.
2402
2403 */
2404 int
2405 My_lily_lexer::try_special_identifiers (SCM * destination, SCM sid)
2406 {
2407         if (gh_string_p (sid)) {
2408                 *destination = sid;
2409                 return STRING_IDENTIFIER;
2410         } else if (gh_number_p (sid)) {
2411                 *destination = sid;
2412                 return NUMBER_IDENTIFIER;
2413         } else if (unsmob_translator_def (sid)) {
2414                 *destination = unsmob_translator_def (sid)->clone_scm();
2415                 return TRANSLATOR_IDENTIFIER;
2416         } else if (unsmob_score (sid)) {
2417                 Score *sc =  new Score (*unsmob_score (sid));
2418                 *destination =sc->self_scm ();
2419                 return SCORE_IDENTIFIER;
2420         } else if (Music * mus =unsmob_music (sid)) {
2421                 *destination = unsmob_music (sid)->clone ()->self_scm();
2422                 unsmob_music (*destination)->
2423                         set_mus_property ("origin", make_input (last_input_));
2424                 return dynamic_cast<Event*> (mus)
2425                         ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
2426         } else if (unsmob_duration (sid)) {
2427                 *destination = unsmob_duration (sid)->smobbed_copy();
2428                 return DURATION_IDENTIFIER;
2429         } else if (unsmob_music_output_def (sid)) {
2430                 Music_output_def *p = unsmob_music_output_def (sid);
2431                 p = p->clone ();
2432
2433                 *destination = p->self_scm();
2434                 return MUSIC_OUTPUT_DEF_IDENTIFIER;
2435         } else if (Text_item::markup_p (sid)) {
2436                 *destination = sid;
2437                 return MARKUP_IDENTIFIER;
2438         }
2439
2440         return -1;      
2441 }