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