]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
* lily/my-lily-parser.cc (ly:parser-add-book-and-score): New function.
[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
201 %union {
202         Book *book;
203         Music_output_def *outputdef;
204         SCM scm;
205         String *string;
206         Music *music;
207         Score *score;
208         int i;
209 }
210 %{
211
212 int
213 yylex (YYSTYPE *s, void *v)
214 {
215         My_lily_parser *pars = (My_lily_parser*) v;
216         My_lily_lexer *lex = pars->lexer_;
217
218         lex->lexval = (void*) s;
219         lex->prepare_for_next_token ();
220         return lex->yylex ();
221 }
222
223
224 %}
225
226 %expect 3
227
228 /*
229   Three shift/reduce problems:
230
231 2. \markup identifier.
232
233 3. \markup { }
234
235 4.  \repeat
236         \repeat .. \alternative
237
238
239     \repeat { \repeat .. \alternative }
240
241 or
242
243     \repeat { \repeat } \alternative 
244
245 )
246  */
247
248
249 %pure_parser
250
251 %token ACCEPTS
252 %token ADDLYRICS
253 %token ADDQUOTE
254 %token LYRICSTO
255 %token ALIAS
256 %token ALTERNATIVE
257 %token APPLY
258 %token APPLYCONTEXT
259 %token APPLYOUTPUT
260 %token AUTOCHANGE
261 %token BAR
262 %token BOOK
263 %token BREATHE
264 %token CHANGE
265 %token CHORDMODIFIERS
266 %token CHORDS
267 %token LESSLESS
268 %token MOREMORE
269 %token CLEF
270 %token COMMANDSPANREQUEST
271 %token CONSISTS
272 %token CONSISTSEND
273 %token CONTEXT
274 %token DEFAULT
275 %token DENIES
276 %token DESCRIPTION
277 %token EOI
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
383 %type <music>   Repeated_music
384 %type <scm>     Alternative_music
385 //%type <scm>     Composite_music_list
386 %type <i>       tremolo_type
387 %type <i>       bare_int  bare_unsigned
388 %type <i>       script_dir
389 %type <scm>     identifier_init
390 %type <scm>     simple_string
391
392 %type <music> note_chord_element chord_body chord_body_element
393 %type <scm>  chord_body_elements
394 %type <scm> steno_duration optional_notemode_duration multiplied_duration
395         
396 %type <scm>   post_events
397 %type <music> gen_text_def direction_less_event direction_reqd_event
398 %type <scm>   steno_pitch pitch absolute_pitch pitch_also_in_chords
399 %type <scm>    steno_tonic_pitch
400 %type <scm>     duration_length fraction
401
402 %type <scm> new_chord step_number chord_items chord_item chord_separator step_numbers
403
404 %type <scm>  embedded_scm scalar
405 %type <music>   Music Sequential_music Simultaneous_music
406 %type <music>   relative_music re_rhythmed_music
407 %type <music>   music_property_def context_change
408 %type <scm> context_prop_spec
409 %type <scm> Music_list
410 %type <scm> property_operation context_mod context_def_mod optional_context_mod
411 %type <outputdef>  music_output_def_body music_output_def_head
412 %type <music>   post_event tagged_post_event
413 %type <music> command_req
414 %type <music> string_number_event
415 %type <scm>     string bare_number number_expression number_term number_factor
416 %type <score>   score_block score_body
417
418 %type <scm>     context_def_spec_block context_def_spec_body
419 %type <music>   tempo_event
420 %type <scm>     script_abbreviation
421
422
423
424 %left '-' '+'
425
426 /* We don't assign precedence to / and *, because we might need varied
427 prec levels in different prods */
428
429 %left UNARY_MINUS
430
431 %%
432
433 lilypond:       /* empty */
434         | lilypond EOI
435         | lilypond toplevel_expression {
436         }
437         | lilypond assignment {
438         }
439         | lilypond error {
440                 THIS->error_level_ = 1;
441         }
442         | lilypond INVALID      {
443                 THIS->error_level_ = 1;
444         }
445         ;
446
447 toplevel_expression:
448         lilypond_header {
449                 THIS->header_ = $1;
450         }
451         | toplevel_music EOI {
452                 Music_output_def *paper = get_paper (THIS);
453                 // delay?
454                 // SCM proc = paper->get_scmvar ("toplevel-music-handler");
455                 SCM proc = ly_scheme_function ("ly:parser-add-book-and-score");
456                 scm_call_2 (proc, THIS->self_scm (), $1->self_scm ());
457                 scm_gc_unprotect_object (paper->self_scm ());
458         }
459         | add_quote {
460         
461         }
462         | book_block {
463                 Book *book = $1;
464                 SCM header = THIS->header_;
465                 Path outname = split_path (THIS->output_basename_);
466                 int *c = &THIS->book_count_;
467                 if (*c)
468                         outname.base += "-" + to_string (*c);
469                 (*c)++;
470                 Music_output_def *paper = get_paper (THIS);
471                 book->process (outname.to_string (), paper, header);
472                 scm_gc_unprotect_object (book->self_scm ());
473                 scm_gc_unprotect_object (paper->self_scm ());
474         }
475         | score_block {
476                 /* TODO: implicit book, depending on --no-book/--no-page-layout
477                    option? */
478                 Score *score = $1;
479                 SCM head = is_module (score->header_) ? score->header_
480                         : THIS->header_.to_SCM ();
481
482                 Path p = split_path (THIS->output_basename_);
483                 int *c = &THIS->score_count_;
484                 if (*c)
485                         p.base += "-" + to_string (*c);
486
487                 (*c)++;
488                 SCM outname = scm_makfrom0str (p.to_string ().to_str0());
489
490                 for (int i = 0; i < score->defs_.size (); i++)
491                         default_rendering (score->music_,
492                                            score->defs_[i]->self_scm (), head,
493                                            outname);
494
495                 if (score->defs_.is_empty ())
496                 {
497                    Music_output_def *paper = get_paper (THIS);
498                    default_rendering (score->music_, paper->self_scm (), head,
499                         outname);
500                    scm_gc_unprotect_object (paper->self_scm ());
501                 }
502                 scm_gc_unprotect_object (score->self_scm ());
503         }
504         | output_def {
505                 SCM id = SCM_EOL;
506                 if (dynamic_cast<Paper_def*> ($1))
507                         id = scm_makfrom0str ("$defaultpaper");
508                 else if (dynamic_cast<Midi_def*> ($1))
509                         id = scm_makfrom0str ("$defaultmidi");
510                 THIS->lexer_->set_identifier (id, $1->self_scm ());
511                 scm_gc_unprotect_object ($1->self_scm ());
512         }
513         ;
514
515 toplevel_music:
516         Composite_music {
517         }
518         ;
519
520 embedded_scm:
521         SCM_T
522         | SCM_IDENTIFIER
523         ;
524
525
526 lilypond_header_body:
527         {
528                 $$ = ly_make_anonymous_module ();
529                 THIS->lexer_->add_scope ($$);
530         }
531         | lilypond_header_body assignment  {
532                 
533         }
534         ;
535
536 lilypond_header:
537         HEADER '{' lilypond_header_body '}'     {
538                 $$ = THIS->lexer_->remove_scope ();
539         }
540         ;
541
542
543 /*
544         DECLARATIONS
545 */
546 assignment:
547         STRING {
548                 THIS->push_spot ();
549         }
550         /* cont */ '=' identifier_init  {
551
552         /*
553                 Should find generic way of associating input with objects.
554         */
555                 Input ip = THIS->pop_spot ();
556
557                 if (! is_regular_identifier ($1))
558                 {
559                         ip.warning (_ ("Identifier should have alphabetic characters only"));
560                 }
561
562                 THIS->lexer_->set_identifier ($1, $4);
563
564 /*
565  TODO: devise standard for protection in parser.
566
567   The parser stack lives on the C-stack, which means that
568 all objects can be unprotected as soon as they're here.
569
570 */
571         }
572         | embedded_scm { }
573         ;
574
575
576 identifier_init:
577         score_block {
578                 $$ = $1->self_scm ();
579                 scm_gc_unprotect_object ($$);
580         }
581         | full_markup {
582                 $$ = $1;
583         }
584         | output_def {
585                 $$ = $1->self_scm ();
586                 scm_gc_unprotect_object ($$);
587         }
588         | context_def_spec_block {
589                 $$ = $1;
590         }
591         | Music  {
592                 $$ = $1->self_scm ();
593                 scm_gc_unprotect_object ($$);
594         }
595         | post_event {
596                 $$ = $1->self_scm ();
597                 scm_gc_unprotect_object ($$);
598         }
599         | number_expression {
600                 $$ = $1;
601         }
602         | string {
603                 $$ = $1;
604         }
605         | embedded_scm  {
606                 $$ = $1;
607         }
608         ;
609
610 context_def_spec_block:
611         CONTEXT '{' context_def_spec_body '}'
612                 {
613                 $$ = $3;
614         }
615         ;
616
617 context_def_spec_body:
618         /**/ {
619                 $$ = Context_def::make_scm ();
620                 unsmob_context_def ($$)->set_spot (THIS->here_input ());
621         }
622         | CONTEXT_DEF_IDENTIFIER        {
623                 $$ = $1;
624                 unsmob_context_def ($$)->set_spot (THIS->here_input ());
625         }
626         | context_def_spec_body GROBDESCRIPTIONS embedded_scm {
627                 Context_def*td = unsmob_context_def ($$);
628
629                 for (SCM p = $3; ly_c_pair_p (p); p = ly_cdr (p)) {
630                         SCM tag = ly_caar (p);
631
632                         /* TODO: should make new tag "grob-definition" ? */
633                         td->add_context_mod (scm_list_3 (ly_symbol2scm ("assign"),
634                                                         tag, scm_cons (ly_cdar (p), SCM_EOL)));
635                 }
636         }
637         | context_def_spec_body context_mod {
638                 unsmob_context_def ($$)->add_context_mod ($2);          
639         }
640         ;
641
642 book_block:
643         BOOK {
644                 THIS->push_spot ();
645         }
646         /*cont*/ '{' book_body '}'      {
647                 THIS->pop_spot ();
648                 $$ = $4;
649         }
650         ;
651
652 book_body:
653         {
654                 $$ = new Book;
655                 $$->set_spot (THIS->here_input ());
656         }
657         | book_body score_block {
658                 $$->scores_.push ($2);
659                 scm_gc_unprotect_object ($2->self_scm ());
660         }
661         | book_body error {
662         }
663         ;
664
665 score_block:
666         SCORE {
667                 THIS->push_spot ();
668         }
669         /*cont*/ '{' score_body '}'     {
670                 THIS->pop_spot ();
671                 $$ = $4;
672         }
673         ;
674
675 score_body:
676         Music   {
677                 $$ = new Score;
678         
679                 $$->set_spot (THIS->here_input ());
680                 SCM m = $1->self_scm ();
681                 scm_gc_unprotect_object (m);
682
683                 /*
684                         guh.
685                 */
686                 SCM check_funcs = ly_scheme_function ("toplevel-music-functions");
687                 for (; ly_c_pair_p (check_funcs); check_funcs = ly_cdr (check_funcs))
688                         m = scm_call_1 (ly_car (check_funcs), m);
689                 $$->music_ = m;
690
691         }
692         | SCORE_IDENTIFIER {
693                 $$ = new Score ( *unsmob_score ($1));
694                 $$->set_spot (THIS->here_input ());
695         }
696         | score_body lilypond_header    {
697                 $$->header_ = $2;
698         }
699         | score_body output_def {
700                 $$->defs_.push ($2);
701                 scm_gc_unprotect_object ($2->self_scm ());
702         }
703         | score_body error {
704
705         }
706         ;
707
708
709 /*
710         MIDI
711 */
712 output_def:
713         music_output_def_body '}' {
714                 $$ = $1;
715                 THIS->lexer_->remove_scope ();
716                 THIS->lexer_->pop_state ();
717         }
718         ;
719
720 music_output_def_head:
721         MIDI    {
722                 Music_output_def *id = unsmob_music_output_def (THIS->lexer_->lookup_identifier ("$defaultmidi"));
723
724
725                 Midi_def *p = 0;
726                 if (id)
727                         p = dynamic_cast<Midi_def*> (id->clone ());
728                 else
729                         p = new Midi_def;
730
731                 $$ = p;
732                 THIS->lexer_->add_scope (p->scope_);
733         }
734         | PAPER         {
735                 Music_output_def *id = unsmob_music_output_def (THIS->lexer_->lookup_identifier ("$defaultpaper"));
736                   Paper_def *p = 0;
737                 if (id)
738                         p = dynamic_cast<Paper_def*> (id->clone ());
739                 else
740                         p = new Paper_def;
741
742                 THIS->lexer_->add_scope (p->scope_);
743                 $$ = p;
744         }
745         ;
746
747
748 music_output_def_body:
749         music_output_def_head '{' {
750                 $$ = $1;
751                 $$->input_origin_. set_spot (THIS->here_input ());
752                 THIS->lexer_->push_initial_state ();
753         }
754         | music_output_def_head '{' MUSIC_OUTPUT_DEF_IDENTIFIER         {
755                 scm_gc_unprotect_object ($1->self_scm ());
756                 Music_output_def *o = unsmob_music_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         | music_output_def_body assignment  {
764
765         }
766         | music_output_def_body context_def_spec_block  {
767                 $$->assign_context_def ($2);
768         }
769         | music_output_def_body tempo_event  {
770                 /*
771                         junk this ? there already is tempo stuff in
772                         music.
773                 */
774                 int m = ly_scm2int ( $2->get_property ("metronome-count"));
775                 Duration *d = unsmob_duration ($2->get_property ("tempo-unit"));
776                 Midi_def *md = dynamic_cast<Midi_def*> ($$);
777                 if (md)
778                         md->set_tempo (d->get_length (), m);
779                 scm_gc_unprotect_object ($2->self_scm ());
780         }
781         | music_output_def_body error {
782
783         }
784         ;
785
786 tempo_event:
787         TEMPO steno_duration '=' bare_unsigned  {
788                 $$ = MY_MAKE_MUSIC ("MetronomeChangeEvent");
789                 $$->set_property ("tempo-unit", $2);
790                 $$->set_property ("metronome-count", scm_int2num ( $4));
791         }
792         ;
793
794 /*
795 The representation of a  list is the
796
797   (LIST . LAST-CONS)
798
799  to have  efficient append.
800 */
801 Music_list:
802         /* empty */ {
803                 $$ = scm_cons (SCM_EOL, SCM_EOL);
804         }
805         | Music_list Music {
806                 SCM s = $$;
807                 SCM c = scm_cons ($2->self_scm (), SCM_EOL);
808                 scm_gc_unprotect_object ($2->self_scm ()); /* UGH */
809                 if (ly_c_pair_p (ly_cdr (s)))
810                         scm_set_cdr_x (ly_cdr (s), c); /* append */
811                 else
812                         scm_set_car_x (s, c); /* set first cons */
813                 scm_set_cdr_x (s, c);  /* remember last cell */
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 Composite_music_list: {};
836  Too many s/r r/r problems
837         Composite_music {
838                 $$ = scm_cons ($1, SCM_EOL);
839         }
840         | '{' Music_list '}' {
841                 $$ = $2;
842         }
843         ;
844 */
845
846 Repeated_music:
847         REPEAT string bare_unsigned Music Alternative_music
848         {
849                 Music *beg = $4;
850                 int times = $3;
851                 SCM alts = ly_c_pair_p ($5) ? ly_car ($5) : SCM_EOL;
852                 if (times < scm_ilength (alts)) {
853                   unsmob_music (ly_car (alts))
854                     ->origin ()->warning (
855                     _ ("More alternatives than repeats.  Junking excess alternatives."));
856                   alts = ly_truncate_list (times, alts);
857                 }
858
859
860                 SCM proc = ly_scheme_function ("make-repeated-music");
861
862                 SCM mus = scm_call_1 (proc, $2);
863                 scm_gc_protect_object (mus); // UGH.
864                 Music *r = unsmob_music (mus);
865                 if (beg)
866                         {
867                         r-> set_property ("element", beg->self_scm ());
868                         scm_gc_unprotect_object (beg->self_scm ());
869                         }
870                 r->set_property ("repeat-count", scm_int2num (times >? 1));
871
872                 r-> set_property ("elements",alts);
873                 if (ly_c_equal_p ($2, scm_makfrom0str ("tremolo"))) {
874                         /*
875                         TODO: move this code to Scheme.
876                         */
877
878                         /*
879                         we can not get durations and other stuff correct down the line, so we have to
880                         add to the duration log here.
881                         */
882                         SCM func = ly_scheme_function ("shift-duration-log");
883
884                         int dots = ($3 % 3) ? 0 : 1;
885                         int shift = -intlog2 ((dots) ? ($3*2/3) : $3);
886
887                         Sequential_music *seq = dynamic_cast<Sequential_music*> ($4);
888                         
889                         if (seq) {
890                                 int list_len = scm_ilength (seq->music_list ());
891                                 if (list_len != 2)
892                                         seq->origin ()->warning ("Chord tremolo must have 2 elements.");
893                                 shift -= 1;
894                                 r->compress (Moment (Rational (1, list_len)));
895                                 }
896                         scm_call_3 (func, r->self_scm (), scm_int2num (shift),scm_int2num (dots));
897
898                 }
899                 r->set_spot (*$4->origin ());
900
901                 $$ = r;
902         }
903         ;
904
905 Sequential_music:
906         SEQUENTIAL '{' Music_list '}'           {
907                 $$ = MY_MAKE_MUSIC ("SequentialMusic");
908                 $$->set_property ("elements", ly_car ($3));
909                 $$->set_spot (THIS->here_input ());
910         }
911         | '{' Music_list '}'            {
912                 $$ = MY_MAKE_MUSIC ("SequentialMusic");
913                 $$->set_property ("elements", ly_car ($2));
914                 $$->set_spot (THIS->here_input ());
915         }
916         ;
917
918 Simultaneous_music:
919         SIMULTANEOUS '{' Music_list '}'{
920                 $$ = MY_MAKE_MUSIC ("SimultaneousMusic");
921                 $$->set_property ("elements", ly_car ($3));
922                 $$->set_spot (THIS->here_input ());
923
924         }
925         | simul_open Music_list simul_close     {
926                 $$ = MY_MAKE_MUSIC ("SimultaneousMusic");
927                 $$->set_property ("elements", ly_car ($2));
928                 $$->set_spot (THIS->here_input ());
929         }
930         ;
931
932 Simple_music:
933         event_chord             { $$ = $1; }
934         | APPLYOUTPUT embedded_scm {
935                 if (!ly_input_procedure_p ($2))
936                         THIS->parser_error (_ ("\\applycontext takes function argument"));
937                 $$ = MY_MAKE_MUSIC ("ApplyOutputEvent");
938                 $$->set_property ("procedure", $2);
939                 $$->set_spot (THIS->here_input ());
940         }
941         | APPLYCONTEXT embedded_scm {
942                 if (!ly_input_procedure_p ($2))
943                         THIS->parser_error (_ ("\\applycontext takes function argument"));
944                 $$ = MY_MAKE_MUSIC ("ApplyContext");
945                 $$->set_property ("procedure", $2);
946                 $$->set_spot (THIS->here_input ());
947         }
948         | MUSIC_IDENTIFIER {
949                 $$ = unsmob_music ($1);
950         }
951         | music_property_def
952         | context_change
953         ;
954
955
956 optional_context_mod:
957         /**/ { $$ = SCM_EOL; }
958         | WITH '{' context_mod_list '}'  { $$ = $3; }
959         ;
960
961 grace_head:
962         GRACE  { $$ = scm_makfrom0str ("Grace"); }
963         | ACCIACCATURA { $$ = scm_makfrom0str ("Acciaccatura"); }
964         | APPOGGIATURA { $$ = scm_makfrom0str ("Appoggiatura"); }
965         ;
966
967 context_mod_list:
968         /* */  { $$ = SCM_EOL; }
969         | context_mod_list context_mod  {
970                  $$ = scm_cons ($2, $1);
971         }
972         ;
973
974 Composite_music:
975         AUTOCHANGE Music        {
976                 SCM proc = ly_scheme_function ("make-autochange-music");
977         
978                 SCM res = scm_call_1 (proc, $2->self_scm ());
979                 scm_gc_unprotect_object ($2->self_scm ());
980                 $$ = unsmob_music (res);
981                 scm_gc_protect_object (res);
982                 $$->set_spot (THIS->here_input ());
983         }
984         | PARTCOMBINE Music Music {
985                 SCM proc = ly_scheme_function ("make-part-combine-music");
986
987                 SCM res = scm_call_1 (proc, scm_listify ($2->self_scm (),
988                         $3->self_scm (), SCM_UNDEFINED));
989                 scm_gc_unprotect_object ($3->self_scm ());
990                 scm_gc_unprotect_object ($2->self_scm ());
991                 $$ = unsmob_music (res);
992                 scm_gc_protect_object (res);
993                 $$->set_spot (THIS->here_input ());
994         }
995         | grace_head Music {
996 #if 1
997         /*
998                 The other version is for easier debugging  of
999                 Sequential_music_iterator in combination with grace notes.
1000         */
1001
1002 /*
1003
1004 TODO: should distinguish between both grace types in the
1005 basic music objects too, since the meaning is different.
1006
1007 */
1008
1009                 String start_str = "start" + ly_scm2string ($1) + "Music";
1010                 String stop_str = "stop" + ly_scm2string ($1) + "Music";
1011                 
1012                 SCM start = THIS->lexer_->lookup_identifier (start_str);
1013                 SCM stop = THIS->lexer_->lookup_identifier (stop_str);
1014
1015                 Music *startm = unsmob_music (start);
1016                 Music *stopm = unsmob_music (stop);
1017
1018                 SCM ms = SCM_EOL;
1019                 if (stopm) {
1020                         stopm = stopm->clone ();
1021                         ms = scm_cons (stopm->self_scm (), ms);
1022                         scm_gc_unprotect_object (stopm->self_scm ());
1023                 }
1024                 ms = scm_cons ($2->self_scm (), ms);
1025                 scm_gc_unprotect_object ($2->self_scm ());
1026                 if (startm) {
1027                         startm = startm->clone ();
1028                         ms = scm_cons (startm->self_scm (), ms);
1029                         scm_gc_unprotect_object (startm->self_scm ());
1030                 }
1031
1032                 Music* seq = MY_MAKE_MUSIC ("SequentialMusic");
1033                 seq->set_property ("elements", ms);
1034
1035                 
1036                 $$ = MY_MAKE_MUSIC ("GraceMusic");
1037                 $$->set_property ("element", seq->self_scm ());
1038                 scm_gc_unprotect_object (seq->self_scm ());
1039 #else
1040                 $$ = MY_MAKE_MUSIC ("GraceMusic");
1041                 $$->set_property ("element", $2->self_scm ());
1042                 scm_gc_unprotect_object ($2->self_scm ());
1043 #endif
1044         }
1045         | CONTEXT string '=' string optional_context_mod Music {
1046                 $$ = context_spec_music ($2, $4, $6, $5);
1047
1048         }
1049         | CONTEXT simple_string optional_context_mod Music {
1050                 $$ = context_spec_music ($2, SCM_UNDEFINED, $4, $3);
1051         }
1052         | NEWCONTEXT string optional_context_mod Music {
1053                 $$ = context_spec_music ($2, get_next_unique_context (),
1054                                          $4, $3);
1055         }
1056
1057         | TIMES {
1058                 THIS->push_spot ();
1059         }
1060         /* CONTINUED */
1061                 fraction Music  
1062
1063         {
1064                 int n = ly_scm2int (ly_car ($3)); int d = ly_scm2int (ly_cdr ($3));
1065                 Music *mp = $4;
1066
1067                 $$= MY_MAKE_MUSIC ("TimeScaledMusic");
1068                 $$->set_spot (THIS->pop_spot ());
1069
1070                 $$->set_property ("element", mp->self_scm ());
1071                 scm_gc_unprotect_object (mp->self_scm ());
1072                 $$->set_property ("numerator", scm_int2num (n));
1073                 $$->set_property ("denominator", scm_int2num (d));
1074                 $$->compress (Moment (Rational (n,d)));
1075
1076         }
1077         | Repeated_music                { $$ = $1; }
1078         | Simultaneous_music            { $$ = $1; }
1079         | Sequential_music              { $$ = $1; }
1080         | TRANSPOSE pitch_also_in_chords pitch_also_in_chords Music {
1081                 $$ = MY_MAKE_MUSIC ("TransposedMusic");
1082                 Music *p = $4;
1083                 Pitch from = *unsmob_pitch ($2);
1084                 Pitch to = *unsmob_pitch ($3);
1085
1086                 p->transpose (interval (from, to));
1087                 $$->set_property ("element", p->self_scm ());
1088                 scm_gc_unprotect_object (p->self_scm ());
1089         }
1090         | APPLY embedded_scm Music  {
1091                 if (!ly_input_procedure_p ($2))
1092                         {
1093                         THIS->parser_error (_ ("\\apply takes function argument"));
1094                         $$ = $3;
1095                         }
1096                 else
1097                         {
1098                         SCM ret = scm_call_1 ($2, $3->self_scm ());
1099                         Music *m = unsmob_music (ret);
1100                         if (!m) {
1101                                 THIS->parser_error ("\\apply must return a Music");
1102                                 m = MY_MAKE_MUSIC ("Music");
1103                                 }
1104                         $$ = m;
1105                         }
1106         }
1107         | NOTES
1108                 {
1109                 SCM nn = THIS->lexer_->lookup_identifier ("pitchnames");
1110                 THIS->lexer_->push_note_state (alist_to_hashq (nn));
1111         }
1112         Music
1113                 { $$ = $3;
1114                   THIS->lexer_->pop_state ();
1115                 }
1116         | DRUMS
1117                 {
1118                 SCM nn = THIS->lexer_->lookup_identifier ("drumPitchNames");
1119                 THIS->lexer_->push_note_state (alist_to_hashq (nn));
1120         }
1121         Music
1122                 { $$ = $3;
1123                   THIS->lexer_->pop_state ();
1124                 }
1125         | FIGURES
1126                 { THIS->lexer_->push_figuredbass_state (); }
1127         Music
1128                 {
1129                   Music *chm = MY_MAKE_MUSIC ("UntransposableMusic");
1130                   chm->set_property ("element", $3->self_scm ());
1131                   $$ = chm;
1132                   scm_gc_unprotect_object ($3->self_scm ());
1133
1134                   THIS->lexer_->pop_state ();
1135         }
1136         | CHORDS {
1137                 SCM nn = THIS->lexer_->lookup_identifier ("chordmodifiers");
1138                 THIS->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
1139                 nn = THIS->lexer_->lookup_identifier ("pitchnames");
1140                 THIS->lexer_->push_chord_state (alist_to_hashq (nn));
1141
1142         } Music {
1143                   Music *chm = MY_MAKE_MUSIC ("UnrelativableMusic");
1144                   chm->set_property ("element", $3->self_scm ());
1145                   scm_gc_unprotect_object ($3->self_scm ());
1146                   $$ = chm;
1147
1148                   THIS->lexer_->pop_state ();
1149         }
1150         | LYRICS
1151                 { THIS->lexer_->push_lyric_state (); }
1152         Music
1153                 {
1154                   $$ = $3;
1155                   THIS->lexer_->pop_state ();
1156         }
1157         | relative_music        { $$ = $1; }
1158         | re_rhythmed_music     { $$ = $1; }
1159         | TAG embedded_scm Music {
1160                 tag_music ($3, $2, THIS->here_input ());
1161                 $$ = $3;
1162         }
1163         ;
1164
1165 relative_music:
1166         RELATIVE absolute_pitch Music {
1167                 Music *m = $3;
1168                 Pitch start = *unsmob_pitch ($2);
1169                 $$ = make_music_relative (start, m);
1170                 scm_gc_unprotect_object (m->self_scm ());
1171         }
1172         | RELATIVE Composite_music {
1173                 Music *m = $2;
1174                 /* FIXME: why is octave==0 and default not middleC? */
1175                 Pitch middle_c (-1, 0, 0);
1176                 $$ = make_music_relative (middle_c, m);
1177                 scm_gc_unprotect_object (m->self_scm ());
1178         }
1179         ;
1180
1181 re_rhythmed_music:
1182         ADDLYRICS Music Music {
1183                 Music *m = MY_MAKE_MUSIC ("LyricCombineMusic");
1184                 m->set_property ("elements", scm_listify ($2->self_scm (),
1185                         $3->self_scm (), SCM_UNDEFINED));
1186                 scm_gc_unprotect_object ($3->self_scm ());
1187                 scm_gc_unprotect_object ($2->self_scm ());
1188                 $$ = m;
1189         }
1190         |
1191 /* Too many s/r r/r problems
1192         Music
1193 */
1194         NEWLYRICS { THIS->lexer_->push_lyric_state (); }
1195         /* cont */
1196 /* Too many s/r r/r problems
1197         Composite_music_list
1198 */
1199         Music {
1200                 THIS->lexer_->pop_state ();
1201
1202                 Music *music = $3;
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