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