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