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