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