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