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