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