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