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