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