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