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