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