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