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