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