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