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