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