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