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