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