]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
*** empty log message ***
[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
967 optional_context_mod:
968         /**/ { $$ = SCM_EOL; }
969         | WITH { THIS->lexer_->push_initial_state (); }
970         '{' context_mod_list '}'
971         {
972                 THIS->lexer_->pop_state ();
973                 $$ = $4;
974         }
975         ;
976
977 context_mod_list:
978         /* */  { $$ = SCM_EOL; }
979         | context_mod_list context_mod  {
980                  $$ = scm_cons ($2, $1);
981         }
982         ;
983
984
985 Composite_music:
986         Prefix_composite_music { $$ = $1; }
987         | Grouped_music_list { $$ = $1; }
988         ;
989
990 Grouped_music_list:
991         Simultaneous_music              { $$ = $1; }
992         | Sequential_music              { $$ = $1; }
993         ;
994
995
996 function_scm_argument:
997         embedded_scm  
998         | simple_string
999         ;
1000
1001 Generic_prefix_music_scm:
1002         MUSIC_FUNCTION {
1003                 $$ = scm_list_2 ($1, make_input (@$));
1004         }
1005         | MUSIC_FUNCTION_SCM function_scm_argument {
1006                 $$ = scm_list_3 ($1, make_input (@$), $2);
1007         }
1008         | MUSIC_FUNCTION_MARKUP full_markup {
1009                 $$ = scm_list_3 ($1, make_input (@$), $2);
1010         }
1011
1012         | MUSIC_FUNCTION_SCM_MUSIC function_scm_argument Music {
1013                 $$ = scm_list_4 ($1, make_input (@$), $2, $3->self_scm ());
1014                 $3->unprotect ();
1015         }
1016         | MUSIC_FUNCTION_SCM_SCM function_scm_argument function_scm_argument {
1017                 $$ = scm_list_4 ($1, make_input (@$), $2, $3);
1018         }
1019         | MUSIC_FUNCTION_SCM_SCM_SCM function_scm_argument function_scm_argument function_scm_argument {
1020                 $$ = scm_list_5 ($1, make_input (@$), $2, $3, $4);
1021         }
1022         | MUSIC_FUNCTION_MUSIC Music {
1023                 $$ = scm_list_3 ($1, make_input (@$), $2->self_scm ());
1024                 $2->unprotect ();
1025         }
1026         | MUSIC_FUNCTION_SCM_SCM_MUSIC function_scm_argument function_scm_argument Music {
1027                 $$ = scm_list_5 ($1, make_input (@$), $2, $3, $4->self_scm ());
1028         }
1029         | MUSIC_FUNCTION_MARKUP_MUSIC full_markup Music {
1030                 $$ = scm_list_4 ($1, make_input (@$), $2, $3->self_scm ());
1031                 $3->unprotect ();
1032         }
1033         | MUSIC_FUNCTION_MARKUP_MARKUP full_markup full_markup {
1034                 $$ = scm_list_4 ($1, make_input (@$), $2, $3);
1035         }
1036         | MUSIC_FUNCTION_MUSIC_MUSIC Music Music {
1037                 $$ = scm_list_4 ($1, make_input (@$), $2->self_scm (), $3->self_scm ());
1038                 $2->unprotect ();
1039                 $3->unprotect ();
1040         }
1041         | MUSIC_FUNCTION_SCM_MUSIC_MUSIC function_scm_argument Music Music {
1042                 $$ = scm_list_5 ($1, make_input (@$), $2, $3->self_scm (), $4->self_scm ());
1043                 $4->unprotect ();
1044                 $3->unprotect ();
1045         }
1046         | MUSIC_FUNCTION_MARKUP_MUSIC_MUSIC full_markup Music Music {
1047                 $$ = scm_list_5 ($1, make_input (@$), $2, $3->self_scm (), $4->self_scm ());
1048                 $3->unprotect ();
1049                 $4->unprotect ();
1050         }
1051         ;
1052
1053 Prefix_composite_music:
1054         Generic_prefix_music_scm {
1055                 $$ = run_music_function (THIS, $1);
1056         }
1057         | CONTEXT simple_string '=' simple_string optional_context_mod Music {
1058                 $$ = context_spec_music ($2, $4, $6, $5);
1059
1060         }
1061         | CONTEXT simple_string optional_context_mod Music {
1062                 $$ = context_spec_music ($2, SCM_UNDEFINED, $4, $3);
1063         }
1064         | NEWCONTEXT simple_string optional_context_mod Music {
1065                 $$ = context_spec_music ($2, get_next_unique_context_id (), $4,
1066                         $3);
1067         }
1068
1069         | TIMES fraction Music  
1070
1071         {
1072                 int n = scm_to_int (scm_car ($2)); int d = scm_to_int (scm_cdr ($2));
1073                 Music *mp = $3;
1074
1075                 $$= MY_MAKE_MUSIC ("TimeScaledMusic");
1076                 $$->set_spot (@$);
1077
1078                 $$->set_property ("element", mp->self_scm ());
1079                 mp->unprotect();
1080                 $$->set_property ("numerator", scm_from_int (n));
1081                 $$->set_property ("denominator", scm_from_int (d));
1082                 $$->compress (Moment (Rational (n,d)));
1083
1084         }
1085         | Repeated_music                { $$ = $1; }
1086         | TRANSPOSE pitch_also_in_chords pitch_also_in_chords Music {
1087                 $$ = MY_MAKE_MUSIC ("TransposedMusic");
1088                 Music *p = $4;
1089                 Pitch from = *unsmob_pitch ($2);
1090                 Pitch to = *unsmob_pitch ($3);
1091
1092                 p->transpose (pitch_interval (from, to));
1093                 $$->set_property ("element", p->self_scm ());
1094                 p->unprotect();
1095         }
1096         | mode_changing_head Grouped_music_list {
1097                 if ($1 == ly_symbol2scm ("chords"))
1098                 {
1099                   Music *chm = MY_MAKE_MUSIC ("UnrelativableMusic");
1100                   chm->set_property ("element", $2->self_scm ());
1101                   $$ = chm;
1102                   $2->unprotect();
1103                 }
1104                 else
1105                 {
1106                   $$ = $2;
1107                 }
1108                 THIS->lexer_->pop_state ();
1109         }
1110         | mode_changing_head_with_context optional_context_mod Grouped_music_list {
1111                 $$ = context_spec_music ($1, get_next_unique_context_id (),
1112                                          $3, $2);
1113                 if ($1 == ly_symbol2scm ("ChordNames"))
1114                 {
1115                   Music *chm = MY_MAKE_MUSIC ("UnrelativableMusic");
1116                   chm->set_property ("element", $$->self_scm ());
1117                   $$->unprotect();
1118                   $$ = chm;
1119                 }
1120                 THIS->lexer_->pop_state ();
1121         }
1122         | relative_music        { $$ = $1; }
1123         | re_rhythmed_music     { $$ = $1; }
1124         ;
1125
1126 mode_changing_head: 
1127         NOTEMODE {
1128                 SCM nn = THIS->lexer_->lookup_identifier ("pitchnames");
1129                 THIS->lexer_->push_note_state (alist_to_hashq (nn));
1130
1131                 $$ = ly_symbol2scm ("notes");
1132         }
1133         | DRUMMODE 
1134                 {
1135                 SCM nn = THIS->lexer_->lookup_identifier ("drumPitchNames");
1136                 THIS->lexer_->push_note_state (alist_to_hashq (nn));
1137
1138                 $$ = ly_symbol2scm ("drums");
1139         }
1140         | FIGUREMODE {
1141                 THIS->lexer_->push_figuredbass_state ();
1142
1143                 $$ = ly_symbol2scm ("figures");
1144         }
1145         | CHORDMODE {
1146                 SCM nn = THIS->lexer_->lookup_identifier ("chordmodifiers");
1147                 THIS->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
1148                 nn = THIS->lexer_->lookup_identifier ("pitchnames");
1149                 THIS->lexer_->push_chord_state (alist_to_hashq (nn));
1150                 $$ = ly_symbol2scm ("chords");
1151
1152         }
1153         | LYRICMODE
1154                 { THIS->lexer_->push_lyric_state ();
1155                 $$ = ly_symbol2scm ("lyrics");
1156         }
1157         ;
1158
1159 mode_changing_head_with_context: 
1160         DRUMS {
1161                 SCM nn = THIS->lexer_->lookup_identifier ("drumPitchNames");
1162                 THIS->lexer_->push_note_state (alist_to_hashq (nn));
1163
1164                 $$ = ly_symbol2scm ("DrumStaff");
1165         }
1166         | FIGURES {
1167                 THIS->lexer_->push_figuredbass_state ();
1168
1169                 $$ = ly_symbol2scm ("FiguredBass");
1170         }
1171         | CHORDS {
1172                 SCM nn = THIS->lexer_->lookup_identifier ("chordmodifiers");
1173                 THIS->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
1174                 nn = THIS->lexer_->lookup_identifier ("pitchnames");
1175                 THIS->lexer_->push_chord_state (alist_to_hashq (nn));
1176                 $$ = ly_symbol2scm ("ChordNames");
1177         }
1178         | LYRICS
1179                 { THIS->lexer_->push_lyric_state ();
1180                 $$ = ly_symbol2scm ("Lyrics");
1181         }
1182         ;
1183
1184
1185 relative_music:
1186         RELATIVE absolute_pitch Music {
1187                 Music *m = $3;
1188                 Pitch start = *unsmob_pitch ($2);
1189                 $$ = make_music_relative (start, m);
1190                 m->unprotect();
1191         }
1192         | RELATIVE Composite_music {
1193                 Music *m = $2;
1194
1195                 Pitch middle_c (0, 0, 0);
1196                 $$ = make_music_relative (middle_c, m);
1197                 m->unprotect();
1198         }
1199         ;
1200
1201 new_lyrics:
1202         ADDLYRICS { THIS->lexer_->push_lyric_state (); }
1203         /*cont */
1204         Grouped_music_list {
1205         /* Can also use Music at the expensive of two S/Rs similar to
1206            \repeat \alternative */
1207                 THIS->lexer_->pop_state ();
1208
1209                 $$ = scm_cons ($3->self_scm (), SCM_EOL);
1210         }
1211         | new_lyrics ADDLYRICS {
1212                 THIS->lexer_->push_lyric_state ();
1213         } Grouped_music_list {
1214                 THIS->lexer_->pop_state ();
1215                 $$ = scm_cons ($4->self_scm (), $1);
1216         }
1217         ;
1218
1219 re_rhythmed_music:
1220         Grouped_music_list new_lyrics {
1221                 Music * voice = $1;
1222                 SCM name = get_first_context_id (scm_makfrom0str ("Voice"), voice); 
1223                 if (!scm_is_string (name))
1224                 {
1225                         name = get_next_unique_lyrics_context_id ();
1226                         voice = context_spec_music (scm_makfrom0str ("Voice"),
1227                                                     name,
1228                                                     voice, SCM_EOL);
1229                 }
1230
1231                 SCM context = scm_makfrom0str ("Lyrics");
1232                 Music *all = MY_MAKE_MUSIC ("SimultaneousMusic");
1233
1234                 SCM lst = SCM_EOL;
1235                 for (SCM s = $2; scm_is_pair (s); s = scm_cdr (s))
1236                 {
1237                         Music *music = unsmob_music (scm_car (s));
1238                         Music *com = make_lyric_combine_music (name, music);
1239                         Music *csm = context_spec_music (context,
1240                                 get_next_unique_context_id (), com, SCM_EOL);
1241                         lst = scm_cons (csm->self_scm (), lst);
1242                 }
1243                 all->set_property ("elements", scm_cons (voice->self_scm (),
1244                         lst));
1245                 $$ = all;
1246                 voice->unprotect ();
1247         }
1248         | LYRICSTO simple_string {
1249                 THIS->lexer_->push_lyric_state ();
1250         } Music {
1251                 THIS->lexer_->pop_state ();
1252                 Music *music = $4;
1253                 SCM name = $2;
1254                 $$ = make_lyric_combine_music (name, music);
1255                 music->unprotect();
1256         }
1257         ;
1258
1259 context_change:
1260         CHANGE STRING '=' STRING  {
1261                 Music*t = MY_MAKE_MUSIC ("ContextChange");
1262                 t-> set_property ("change-to-type", scm_string_to_symbol ($2));
1263                 t-> set_property ("change-to-id", $4);
1264
1265                 $$ = t;
1266                 $$->set_spot (@$);
1267         }
1268         ;
1269
1270 property_operation:
1271         STRING '=' scalar {
1272                 $$ = scm_list_3 (ly_symbol2scm ("assign"),
1273                         scm_string_to_symbol ($1), $3);
1274         }
1275         | UNSET simple_string {
1276                 $$ = scm_list_2 (ly_symbol2scm ("unset"),
1277                         scm_string_to_symbol ($2));
1278         }
1279         | OVERRIDE simple_string embedded_scm '=' embedded_scm {
1280                 $$ = scm_list_4 (ly_symbol2scm ("push"),
1281                         scm_string_to_symbol ($2), $5, $3);
1282         }
1283         | OVERRIDE simple_string embedded_scm embedded_scm '=' embedded_scm {
1284                 $$ = scm_list_5 (ly_symbol2scm ("push"),
1285                                 scm_string_to_symbol ($2), $6, $4, $3);
1286         }
1287         | REVERT simple_string embedded_scm {
1288                 $$ = scm_list_3 (ly_symbol2scm ("pop"),
1289                         scm_string_to_symbol ($2), $3);
1290         }
1291         ;
1292
1293 context_def_mod:
1294         CONSISTS { $$ = ly_symbol2scm ("consists"); }
1295         | REMOVE { $$ = ly_symbol2scm ("remove"); }
1296
1297         | ACCEPTS { $$ = ly_symbol2scm ("accepts"); }
1298         | DEFAULTCHILD { $$ = ly_symbol2scm ("default-child"); }
1299         | DENIES { $$ = ly_symbol2scm ("denies"); }
1300
1301         | ALIAS { $$ = ly_symbol2scm ("alias"); }
1302         | TYPE { $$ = ly_symbol2scm ("translator-type"); }
1303         | DESCRIPTION { $$ = ly_symbol2scm ("description"); }
1304         | NAME { $$ = ly_symbol2scm ("context-name"); }
1305         ;
1306
1307 context_mod:
1308         property_operation { $$ = $1; }
1309         | context_def_mod STRING {
1310                 $$ = scm_list_2 ($1, $2);
1311         }
1312         ;
1313
1314 context_prop_spec:
1315         simple_string {
1316                 if (!is_regular_identifier ($1))
1317                 {
1318                         @$.error (_("Grob name should be alphanumeric"));
1319                 }
1320
1321                 $$ = scm_list_2 (ly_symbol2scm ("Bottom"),
1322                         scm_string_to_symbol ($1));
1323         }
1324         | simple_string '.' simple_string {
1325                 $$ = scm_list_2 (scm_string_to_symbol ($1),
1326                         scm_string_to_symbol ($3));
1327         }
1328         ;
1329
1330 music_property_def:
1331         OVERRIDE context_prop_spec embedded_scm '=' scalar {
1332                 $$ = property_op_to_music (scm_list_4 (
1333                         ly_symbol2scm ("push"),
1334                         scm_cadr ($2),
1335                         $5, $3));
1336                 $$ = context_spec_music (scm_car ($2), SCM_UNDEFINED, $$, SCM_EOL);
1337         }
1338         | OVERRIDE context_prop_spec embedded_scm  embedded_scm '=' scalar {
1339                 $$ = property_op_to_music (scm_list_5 (
1340                         ly_symbol2scm ("push"),
1341                         scm_cadr ($2),
1342                         $6, $4, $3));
1343                 $$ = context_spec_music (scm_car ($2), SCM_UNDEFINED, $$, SCM_EOL);
1344         }
1345         | REVERT context_prop_spec embedded_scm {
1346                 $$ = property_op_to_music (scm_list_3 (
1347                         ly_symbol2scm ("pop"),
1348                         scm_cadr ($2),
1349                         $3));
1350
1351                 $$= context_spec_music (scm_car ($2), SCM_UNDEFINED, $$, SCM_EOL);
1352         }
1353         | SET context_prop_spec '=' scalar {
1354                 $$ = property_op_to_music (scm_list_3 (
1355                         ly_symbol2scm ("assign"),
1356                         scm_cadr ($2),
1357                         $4));
1358                 $$= context_spec_music (scm_car ($2), SCM_UNDEFINED, $$, SCM_EOL);
1359         }
1360         | UNSET context_prop_spec {
1361                 $$ = property_op_to_music (scm_list_2 (
1362                         ly_symbol2scm ("unset"),
1363                         scm_cadr ($2)));
1364                 $$= context_spec_music (scm_car ($2), SCM_UNDEFINED, $$, SCM_EOL);
1365         }
1366         | ONCE music_property_def {
1367                 SCM e = $2->get_property ("element");
1368                 unsmob_music (e)->set_property ("once", SCM_BOOL_T);
1369                 $$ = $2;
1370
1371         }
1372         ;
1373
1374
1375 string:
1376         STRING {
1377                 $$ = $1;
1378         }
1379         | STRING_IDENTIFIER {
1380                 $$ = $1;
1381         }
1382         | string '+' string {
1383                 $$ = scm_string_append (scm_list_2 ($1, $3));
1384         }
1385         ;
1386
1387 simple_string: STRING {
1388                 $$ = $1;
1389         }
1390         | LYRICS_STRING {
1391                 $$ = $1;
1392         }
1393         | STRING_IDENTIFIER {
1394                 $$ = $1;
1395         }
1396         ;
1397
1398 scalar: string {
1399                 $$ = $1;
1400         }
1401         | LYRICS_STRING {
1402                 $$ = $1;
1403         }
1404         | bare_number {
1405                 $$ = $1;
1406         }
1407         | embedded_scm {
1408                 $$ = $1;
1409         }
1410         | full_markup {
1411                 $$ = $1;
1412         }
1413         | DIGIT {
1414                 $$ = scm_from_int ($1);
1415         }
1416         ;
1417
1418 /*
1419
1420 pre_events doesn't contain anything. It is a trick:
1421
1422 Adding pre_events to the simple_element
1423 makes the choice between
1424
1425   string:  STRING
1426
1427 and
1428
1429   simple_element: STRING
1430
1431 a single shift/reduction conflict.
1432
1433 nevertheless, this is not very clean, and we should find a different
1434 solution.
1435
1436 */
1437 pre_events: /* empty */
1438         ;
1439
1440 event_chord:
1441         pre_events simple_element post_events   {
1442                 SCM elts = $2-> get_property ("elements");
1443
1444                 elts = ly_append2 (elts, scm_reverse_x ($3, SCM_EOL));
1445
1446                 $2->set_property ("elements", elts);
1447                 /* why is this giving wrong start location? -ns
1448                  * $2->set_spot (@$); */
1449                 Input i;
1450                 i.set_location (@2, @3);
1451                 $2->set_spot (i);
1452                 $$ = $2;
1453         }
1454         | command_element
1455         | note_chord_element
1456         ;
1457
1458
1459 note_chord_element:
1460         chord_body optional_notemode_duration post_events
1461         {
1462                 SCM dur = unsmob_duration ($2)->smobbed_copy ();
1463                 SCM es = $1->get_property ("elements");
1464                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
1465
1466                 for (SCM s = es; scm_is_pair (s); s = scm_cdr (s))
1467                   unsmob_music (scm_car (s))->set_property ("duration", dur);
1468                 es = ly_append2 (es, postevs);
1469
1470                 $1-> set_property ("elements", es);
1471                 $1->set_spot (@$);
1472                 $$ = $1;
1473         }
1474         ;
1475
1476 chord_open: '<'
1477         ;
1478
1479 chord_close: '>'
1480         ;
1481
1482 simul_open: DOUBLE_ANGLE_OPEN
1483         ;
1484
1485 simul_close: DOUBLE_ANGLE_CLOSE
1486         ;
1487
1488 chord_body:
1489         chord_open chord_body_elements chord_close
1490         {
1491                 $$ = MY_MAKE_MUSIC ("EventChord");
1492                 $$->set_spot (@$);
1493                 $$->set_property ("elements",
1494                         scm_reverse_x ($2, SCM_EOL));
1495         }
1496         ;
1497
1498 chord_body_elements:
1499         /* empty */             { $$ = SCM_EOL; }
1500         | chord_body_elements chord_body_element {
1501                 $$ = scm_cons ($2->self_scm (), $1);
1502                 $2->unprotect ();
1503         }
1504         ;
1505
1506 chord_body_element:
1507         pitch exclamations questions octave_check post_events
1508         {
1509                 int q = $3;
1510                 int ex = $2;
1511                 SCM check = $4;
1512                 SCM post = $5;
1513
1514                 Music *n = MY_MAKE_MUSIC ("NoteEvent");
1515                 n->set_property ("pitch", $1);
1516                 n->set_spot (@$);
1517                 if (q % 2)
1518                         n->set_property ("cautionary", SCM_BOOL_T);
1519                 if (ex % 2 || q % 2)
1520                         n->set_property ("force-accidental", SCM_BOOL_T);
1521
1522                 if (scm_is_pair (post)) {
1523                         SCM arts = scm_reverse_x (post, SCM_EOL);
1524                         n->set_property ("articulations", arts);
1525                 }
1526                 if (scm_is_number (check))
1527                 {
1528                         int q = scm_to_int (check);
1529                         n->set_property ("absolute-octave", scm_from_int (q-1));
1530                 }
1531
1532                 
1533                 $$ = n;
1534         }
1535         | DRUM_PITCH post_events {
1536                 Music *n = MY_MAKE_MUSIC ("NoteEvent");
1537                 n->set_property ("duration", $2);
1538                 n->set_property ("drum-type", $1);
1539                 n->set_spot (@$);
1540
1541                 if (scm_is_pair ($2)) {
1542                         SCM arts = scm_reverse_x ($2, SCM_EOL);
1543                         n->set_property ("articulations", arts);
1544                 }
1545                 $$ = n;
1546         }
1547         | music_function_chord_body { 
1548                 $$ = run_music_function (THIS, $1);
1549                 $$->set_spot (@$);
1550         }
1551         ;
1552
1553 music_function_chord_body:
1554         MUSIC_FUNCTION {
1555                 $$ = scm_list_2 ($1, make_input (@$));
1556         }
1557         | MUSIC_FUNCTION_MUSIC chord_body_element {
1558                 $$ = scm_list_3 ($1, make_input (@$),
1559                                         $2->self_scm ());
1560         }
1561         | MUSIC_FUNCTION_SCM_MUSIC function_scm_argument chord_body_element {
1562                 $$ = scm_list_4 ($1, make_input (@$),
1563                                         $2, $3->self_scm ());
1564         }
1565         | MUSIC_FUNCTION_SCM_SCM_MUSIC function_scm_argument function_scm_argument
1566                 chord_body_element {
1567
1568                 $$ = scm_list_5 ($1, make_input (@$),
1569                                         $2, $3, $4->self_scm ());
1570         }
1571         ;
1572
1573
1574 add_quote:
1575         ADDQUOTE string Music {
1576                 SCM adder = ly_lily_module_constant ("add-quotable");
1577                 
1578                 scm_call_2 (adder, $2, $3->self_scm ());
1579                 $3->unprotect();
1580         }
1581         ;
1582
1583 command_element:
1584         command_event {
1585                 $$ = MY_MAKE_MUSIC ("EventChord");
1586                 $$->set_property ("elements", scm_cons ($1->self_scm (), SCM_EOL));
1587                 $1->unprotect();
1588
1589                 $$-> set_spot (@$);
1590                 $1-> set_spot (@$);
1591         }
1592         | SKIP duration_length {
1593                 Music *skip = MY_MAKE_MUSIC ("SkipMusic");
1594                 skip->set_property ("duration", $2);
1595                 skip->set_spot (@$);
1596                 $$ = skip;
1597         }
1598         | OCTAVE pitch {
1599                 Music *m = MY_MAKE_MUSIC ("RelativeOctaveCheck");
1600                 $$ = m;
1601                 $$->set_spot (@$);
1602                 $$->set_property ("pitch", $2);
1603         }
1604         | E_BRACKET_OPEN {
1605                 Music *m = MY_MAKE_MUSIC ("LigatureEvent");
1606                 m->set_property ("span-direction", scm_from_int (START));
1607                 m->set_spot (@$);
1608
1609                 $$ = MY_MAKE_MUSIC ("EventChord");
1610                 $$->set_property ("elements", scm_cons (m->self_scm (), SCM_EOL));
1611                 m->unprotect();
1612                 $$->set_spot (@$);
1613         }
1614         | E_BRACKET_CLOSE {
1615                 Music *m = MY_MAKE_MUSIC ("LigatureEvent");
1616                 m->set_property ("span-direction", scm_from_int (STOP));
1617                 m->set_spot (@$);
1618
1619                 $$ = MY_MAKE_MUSIC ("EventChord");
1620                 $$->set_property ("elements", scm_cons (m->self_scm (), SCM_EOL));
1621                 $$->set_spot (@$);
1622                 m->unprotect ();
1623         }
1624         | E_BACKSLASH {
1625                 $$ = MY_MAKE_MUSIC ("VoiceSeparator");
1626                 $$->set_spot (@$);
1627         }
1628         | '|'      {
1629                 SCM pipe = THIS->lexer_->lookup_identifier ("pipeSymbol");
1630
1631                 if (Music * m = unsmob_music (pipe))
1632                         $$ = m->clone ();
1633                 else
1634                         $$ = MY_MAKE_MUSIC ("BarCheck");
1635
1636                 $$->set_spot (@$);
1637         }
1638         | TRANSPOSITION pitch {
1639                 Pitch middle_c;
1640                 Pitch sounds_as_c = pitch_interval (*unsmob_pitch ($2), middle_c);
1641                 $$ = set_property_music (ly_symbol2scm ("instrumentTransposition"),
1642                                          sounds_as_c.smobbed_copy());
1643                 $$->set_spot (@$);
1644                 $$ = context_spec_music (ly_symbol2scm ("Staff"), SCM_UNDEFINED,
1645                         $$, SCM_EOL);
1646         }
1647         | PARTIAL duration_length       {
1648                 Moment m = - unsmob_duration ($2)->get_length ();
1649                 Music *p = set_property_music (ly_symbol2scm ( "measurePosition"),m.smobbed_copy ());
1650                 p->set_spot (@$);
1651                 p = context_spec_music (ly_symbol2scm ("Timing"), SCM_UNDEFINED,
1652                                         p, SCM_EOL);
1653                 p = context_spec_music (ly_symbol2scm ("Score"), SCM_UNDEFINED,
1654                                         p, SCM_EOL);
1655                 $$ = p;
1656         }
1657
1658         | TIME_T fraction  {
1659                 SCM proc = ly_lily_module_constant ("make-time-signature-set");
1660
1661                 SCM result = scm_apply_2   (proc, scm_car ($2), scm_cdr ($2), SCM_EOL);
1662                 $$ = unsmob_music (result);
1663                 $$->protect ();
1664         }
1665         | MARK scalar {
1666                 SCM proc = ly_lily_module_constant ("make-mark-set");
1667
1668                 SCM result = scm_call_1 (proc, $2);
1669                 $$ = unsmob_music (result);
1670                 $$->protect ();
1671         }
1672         ;
1673
1674 command_event:
1675         E_TILDE {
1676                 $$ = MY_MAKE_MUSIC ("PesOrFlexaEvent");
1677         }
1678         | MARK DEFAULT  {
1679                 Music *m = MY_MAKE_MUSIC ("MarkEvent");
1680                 $$ = m;
1681         }
1682         | tempo_event {
1683                 $$ = $1;
1684         }
1685         | KEY DEFAULT {
1686                 Music *key = MY_MAKE_MUSIC ("KeyChangeEvent");
1687                 $$ = key;
1688         }
1689         | KEY NOTENAME_PITCH SCM_IDENTIFIER     {
1690
1691                 Music *key = MY_MAKE_MUSIC ("KeyChangeEvent");
1692                 if (scm_ilength ($3) > 0)
1693                 {               
1694                         key->set_property ("pitch-alist", $3);
1695                         key->set_property ("tonic", Pitch (0, 0, 0).smobbed_copy ());
1696                         key->transpose (* unsmob_pitch ($2));
1697                 } else {
1698                         THIS->parser_error (@3, _ ("second argument must be pitch list"));
1699                 }
1700
1701                 $$ = key;
1702         }
1703         ;
1704
1705
1706 post_events:
1707         /* empty */ {
1708                 $$ = SCM_EOL;
1709         }
1710         | post_events post_event {
1711                 $2->set_spot (@2);
1712                 $$ = scm_cons ($2->self_scm (), $$);
1713                 $2->unprotect ();
1714         }
1715         ;
1716
1717 music_function_event:
1718         MUSIC_FUNCTION_MUSIC post_event {
1719                 $$ = scm_list_3 ($1, make_input (@$), $2->self_scm ());
1720         }
1721         | MUSIC_FUNCTION_SCM_MUSIC function_scm_argument post_event {
1722                 $$ = scm_list_4 ($1, make_input (@$), $2, $3->self_scm ());
1723         }
1724         | MUSIC_FUNCTION_SCM_SCM_MUSIC function_scm_argument function_scm_argument post_event {
1725                 $$ = scm_list_5 ($1, make_input (@$), $2, $3, $4->self_scm ());
1726         }
1727         ;
1728
1729         
1730 post_event:
1731         direction_less_event {
1732                 $$ = $1;
1733         }
1734         | '-' music_function_event {
1735                 Music *mus = run_music_function (THIS, $2);
1736                 mus->set_spot (@1);
1737                 $$ = mus;
1738         }
1739         | HYPHEN {
1740                 if (!THIS->lexer_->is_lyric_state ())
1741                         THIS->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
1742                 $$ = MY_MAKE_MUSIC ("HyphenEvent");
1743         }
1744         | EXTENDER {
1745                 if (!THIS->lexer_->is_lyric_state ())
1746                         THIS->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
1747                 $$ = MY_MAKE_MUSIC ("ExtenderEvent");
1748         }
1749         | script_dir direction_reqd_event {
1750                 if ($1)
1751                         $2->set_property ("direction", scm_from_int ($1));
1752                 $$ = $2;
1753         }
1754         | script_dir direction_less_event {
1755                 if ($1)
1756                         $2->set_property ("direction", scm_from_int ($1));
1757                 $$ = $2;
1758         }
1759         | string_number_event
1760         ;
1761
1762 string_number_event:
1763         E_UNSIGNED {
1764                 Music *s = MY_MAKE_MUSIC ("StringNumberEvent");
1765                 s->set_property ("string-number", scm_from_int ($1));
1766                 s->set_spot (@$);
1767                 $$ = s;
1768         }
1769         ;
1770
1771 direction_less_char:
1772         '['  {
1773                 $$ = ly_symbol2scm ("bracketOpenSymbol");
1774         }
1775         | ']'  {
1776                 $$ = ly_symbol2scm ("bracketCloseSymbol"); 
1777         }
1778         | '~'  {
1779                 $$ = ly_symbol2scm ("tildeSymbol");
1780         }
1781         | '('  {
1782                 $$ = ly_symbol2scm ("parenthesisOpenSymbol");
1783         }
1784         | ')'  {
1785                 $$ = ly_symbol2scm ("parenthesisCloseSymbol");
1786         }
1787         | E_EXCLAMATION  {
1788                 $$ = ly_symbol2scm ("escapedExclamationSymbol");
1789         }
1790         | E_OPEN  {
1791                 $$ = ly_symbol2scm ("escapedParenthesisOpenSymbol");
1792         }
1793         | E_CLOSE  {
1794                 $$ = ly_symbol2scm ("escapedParenthesisCloseSymbol");
1795         }
1796         | E_ANGLE_CLOSE  {
1797                 $$ = ly_symbol2scm ("escapedBiggerSymbol");
1798         }
1799         | E_ANGLE_OPEN  {
1800                 $$ = ly_symbol2scm ("escapedSmallerSymbol");
1801         }
1802         ;
1803
1804 direction_less_event:
1805         direction_less_char {
1806                 SCM predefd = THIS->lexer_->lookup_identifier_symbol ($1);
1807                 Music * m = 0;
1808                 if (unsmob_music (predefd))
1809                 {
1810                         m = unsmob_music (predefd)->clone ();
1811                 }
1812                 else
1813                 {
1814                         m = MY_MAKE_MUSIC ("Music");
1815                 }
1816                 m->set_spot (@$);
1817                 $$ = m;         
1818         }
1819         | EVENT_IDENTIFIER      {
1820                 $$ = unsmob_music ($1);
1821         }
1822         | tremolo_type  {
1823                Music *a = MY_MAKE_MUSIC ("TremoloEvent");
1824                a->set_spot (@$);
1825                a->set_property ("tremolo-type", scm_from_int ($1));
1826                $$ = a;
1827         }
1828         ;       
1829         
1830 direction_reqd_event:
1831         gen_text_def {
1832                 $$ = $1;
1833         }
1834         | script_abbreviation {
1835                 SCM s = THIS->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
1836                 Music *a = MY_MAKE_MUSIC ("ArticulationEvent");
1837                 if (scm_is_string (s))
1838                         a->set_property ("articulation-type", s);
1839                 else THIS->parser_error (@1, _ ("expecting string as script definition"));
1840                 $$ = a;
1841         }
1842         ;
1843
1844 octave_check:
1845         /**/ { $$ = SCM_EOL; }
1846         | '='  { $$ = scm_from_int (0); }
1847         | '=' sub_quotes { $$ = scm_from_int ($2); }
1848         | '=' sup_quotes { $$ = scm_from_int ($2); }
1849         ;
1850
1851 sup_quotes:
1852         '\'' {
1853                 $$ = 1;
1854         }
1855         | sup_quotes '\'' {
1856                 $$ ++;
1857         }
1858         ;
1859
1860 sub_quotes:
1861         ',' {
1862                 $$ = 1;
1863         }
1864         | sub_quotes ',' {
1865                 $$++;
1866         }
1867         ;
1868
1869 steno_pitch:
1870         NOTENAME_PITCH  {
1871                 $$ = $1;
1872         }
1873         | NOTENAME_PITCH sup_quotes     {
1874                 Pitch p = *unsmob_pitch ($1);
1875                 p = p.transposed (Pitch ($2,0,0));
1876                 $$ = p.smobbed_copy ();
1877         }
1878         | NOTENAME_PITCH sub_quotes      {
1879                 Pitch p =* unsmob_pitch ($1);
1880                 p = p.transposed (Pitch (-$2,0,0));
1881                 $$ = p.smobbed_copy ();
1882         }
1883         ;
1884
1885 /*
1886 ugh. duplication
1887 */
1888
1889 steno_tonic_pitch:
1890         TONICNAME_PITCH {
1891                 $$ = $1;
1892         }
1893         | TONICNAME_PITCH sup_quotes    {
1894                 Pitch p = *unsmob_pitch ($1);
1895                 p = p.transposed (Pitch ($2,0,0));
1896                 $$ = p.smobbed_copy ();
1897         }
1898         | TONICNAME_PITCH sub_quotes     {
1899                 Pitch p =* unsmob_pitch ($1);
1900
1901                 p = p.transposed (Pitch (-$2,0,0));
1902                 $$ = p.smobbed_copy ();
1903         }
1904         ;
1905
1906 pitch:
1907         steno_pitch {
1908                 $$ = $1;
1909         }
1910         ;
1911
1912 pitch_also_in_chords:
1913         pitch
1914         | steno_tonic_pitch
1915         ;
1916
1917 gen_text_def:
1918         full_markup {
1919                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent");
1920                 t->set_property ("text", $1);
1921                 t->set_spot (@$);
1922                 $$ = t; 
1923         }
1924         | string {
1925                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent");
1926                 t->set_property ("text",
1927                         make_simple_markup ($1));
1928                 t->set_spot (@$);
1929                 $$ = t;
1930         
1931         }
1932         | DIGIT {
1933                 Music *t = MY_MAKE_MUSIC ("FingerEvent");
1934                 t->set_property ("digit", scm_from_int ($1));
1935                 t->set_spot (@$);
1936                 $$ = t;
1937         }
1938         ;
1939
1940 script_abbreviation:
1941         '^'             {
1942                 $$ = scm_makfrom0str ("Hat");
1943         }
1944         | '+'           {
1945                 $$ = scm_makfrom0str ("Plus");
1946         }
1947         | '-'           {
1948                 $$ = scm_makfrom0str ("Dash");
1949         }
1950         | '|'           {
1951                 $$ = scm_makfrom0str ("Bar");
1952         }
1953         | '>'           {
1954                 $$ = scm_makfrom0str ("Larger");
1955         }
1956         | '.'           {
1957                 $$ = scm_makfrom0str ("Dot");
1958         }
1959         | '_' {
1960                 $$ = scm_makfrom0str ("Underscore");
1961         }
1962         ;
1963
1964 script_dir:
1965         '_'     { $$ = DOWN; }
1966         | '^'   { $$ = UP; }
1967         | '-'   { $$ = CENTER; }
1968         ;
1969
1970
1971 absolute_pitch:
1972         steno_pitch     {
1973                 $$ = $1;
1974         }
1975         ;
1976
1977 duration_length:
1978         multiplied_duration {
1979                 $$ = $1;
1980         }
1981         ;
1982
1983 optional_notemode_duration:
1984         {
1985                 Duration dd = THIS->default_duration_;
1986                 $$ = dd.smobbed_copy ();
1987         }
1988         | multiplied_duration   {
1989                 $$ = $1;
1990                 THIS->default_duration_ = *unsmob_duration ($$);
1991         }
1992         ;
1993
1994 steno_duration:
1995         bare_unsigned dots              {
1996                 int len = 0;
1997                 if (!is_duration ($1))
1998                         THIS->parser_error (@1, _f ("not a duration: %d", $1));
1999                 else
2000                         len = intlog2 ($1);
2001
2002                 $$ = Duration (len, $2).smobbed_copy ();
2003         }
2004         | DURATION_IDENTIFIER dots      {
2005                 Duration *d = unsmob_duration ($1);
2006                 Duration k (d->duration_log (), d->dot_count () + $2);
2007                 *d = k;
2008                 $$ = $1;
2009         }
2010         ;
2011
2012 multiplied_duration:
2013         steno_duration {
2014                 $$ = $1;
2015         }
2016         | multiplied_duration '*' bare_unsigned {
2017                 $$ = unsmob_duration ($$)->compressed ( $3) .smobbed_copy ();
2018         }
2019         | multiplied_duration '*' FRACTION {
2020                 Rational  m (scm_to_int (scm_car ($3)), scm_to_int (scm_cdr ($3)));
2021
2022                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
2023         }
2024         ;
2025
2026 fraction:
2027         FRACTION { $$ = $1; }
2028         | UNSIGNED '/' UNSIGNED {
2029                 $$ = scm_cons (scm_from_int ($1), scm_from_int ($3));
2030         }
2031         ;
2032
2033 dots:
2034         /* empty */     {
2035                 $$ = 0;
2036         }
2037         | dots '.' {
2038                 $$ ++;
2039         }
2040         ;
2041
2042 tremolo_type:
2043         ':'     {
2044                 $$ = 0;
2045         }
2046         | ':' bare_unsigned {
2047                 if (!is_duration ($2))
2048                         THIS->parser_error (@2, _f ("not a duration: %d", $2));
2049                 $$ = $2;
2050         }
2051         ;
2052
2053 bass_number:
2054         DIGIT   {
2055                 $$ = scm_from_int ($1);
2056         }
2057         | UNSIGNED {
2058                 $$ = scm_from_int ($1);
2059         }
2060         | STRING { $$ = $1; }
2061         | full_markup { $$ = $1; }
2062         ;
2063
2064 figured_bass_alteration:
2065         '-'     { $$ = -2; }
2066         | '+'   { $$ = 2; }
2067         | '!'   { $$ = 0; }
2068         ;
2069
2070 bass_figure:
2071         FIGURE_SPACE {
2072                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent");
2073                 $$ = bfr->self_scm ();
2074                 bfr->unprotect ();
2075                 bfr->set_spot (@1);
2076         }
2077         | bass_number  {
2078                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent");
2079                 $$ = bfr->self_scm ();
2080
2081                 if (scm_is_number ($1))
2082                         bfr->set_property ("figure", $1);
2083                 else if (Text_interface::is_markup ($1))
2084                         bfr->set_property ("text", $1);
2085
2086                 bfr->unprotect ();
2087                 bfr->set_spot (@1);
2088         }
2089         | bass_figure ']' {
2090                 $$ = $1;
2091                 unsmob_music ($1)->set_property ("bracket-stop", SCM_BOOL_T);
2092         }
2093         | bass_figure figured_bass_alteration {
2094                 Music *m = unsmob_music ($1);
2095                 if ($2) {
2096                         SCM salter = m->get_property ("alteration");
2097                         int alter = scm_is_number (salter) ? scm_to_int (salter) : 0;
2098                         m->set_property ("alteration",
2099                                 scm_from_int (alter + $2));
2100                 } else {
2101                         m->set_property ("alteration", scm_from_int (0));
2102                 }
2103         }
2104         | bass_figure figured_bass_modification  {
2105                 Music *m = unsmob_music ($1);
2106                 if ($2 == ly_symbol2scm ("plus"))
2107                         {
2108                         m->set_property ("augmented", SCM_BOOL_T);
2109                         }
2110                 else if ($2 == ly_symbol2scm ("slash"))
2111                         {
2112                         m->set_property ("diminished", SCM_BOOL_T);
2113                         }
2114                 else if ($2 == ly_symbol2scm ("exclamation"))
2115                         {
2116                         m->set_property ("no-continuation", SCM_BOOL_T);
2117                         }
2118         }
2119         ;
2120
2121
2122 figured_bass_modification:
2123         E_PLUS          {
2124                 $$ = ly_symbol2scm ("plus");
2125         }
2126         | E_EXCLAMATION {
2127                 $$ = ly_symbol2scm ("exclamation");
2128         }
2129         | '/'           {
2130                 $$ = ly_symbol2scm ("slash");
2131         }
2132         ;
2133
2134 br_bass_figure:
2135         bass_figure {
2136                 $$ = $1;
2137         }
2138         | '[' bass_figure {
2139                 $$ = $2;
2140                 unsmob_music ($$)->set_property ("bracket-start", SCM_BOOL_T);
2141         }
2142         ;
2143
2144 figure_list:
2145         /**/            {
2146                 $$ = SCM_EOL;
2147         }
2148         | figure_list br_bass_figure {
2149                 $$ = scm_cons ($2, $1);
2150         }
2151         ;
2152
2153 figure_spec:
2154         FIGURE_OPEN figure_list FIGURE_CLOSE {
2155                 Music *m = MY_MAKE_MUSIC ("EventChord");
2156                 $2 = scm_reverse_x ($2, SCM_EOL);
2157                 m->set_property ("elements", $2);
2158                 $$ = m->self_scm ();
2159         }
2160         ;
2161
2162
2163 optional_rest:
2164         /**/   { $$ = 0; }
2165         | REST { $$ = 1; }
2166         ;
2167
2168 simple_element:
2169         pitch exclamations questions octave_check optional_notemode_duration optional_rest {
2170                 if (!THIS->lexer_->is_note_state ())
2171                         THIS->parser_error (@1, _ ("have to be in Note mode for notes"));
2172
2173                 Music *n = 0;
2174                 if ($6)
2175                         n = MY_MAKE_MUSIC ("RestEvent");
2176                 else
2177                         n = MY_MAKE_MUSIC ("NoteEvent");
2178                 
2179                 n->set_property ("pitch", $1);
2180                 n->set_property ("duration", $5);
2181
2182                 if (scm_is_number ($4))
2183                 {
2184                         int q = scm_to_int ($4);
2185                         n->set_property ("absolute-octave", scm_from_int (q-1));
2186                 }
2187
2188                 if ($3 % 2)
2189                         n->set_property ("cautionary", SCM_BOOL_T);
2190                 if ($2 % 2 || $3 % 2)
2191                         n->set_property ("force-accidental", SCM_BOOL_T);
2192
2193                 Music *v = MY_MAKE_MUSIC ("EventChord");
2194                 v->set_property ("elements", scm_list_1 (n->self_scm ()));
2195                 n->unprotect ();
2196
2197                 v->set_spot (@$);
2198                 n->set_spot (@$);
2199                 $$ = v;
2200         }
2201         | DRUM_PITCH optional_notemode_duration {
2202                 Music *n = MY_MAKE_MUSIC ("NoteEvent");
2203                 n->set_property ("duration", $2);
2204                 n->set_property ("drum-type", $1);
2205
2206                 Music *v = MY_MAKE_MUSIC ("EventChord");
2207                 v->set_property ("elements", scm_list_1 (n->self_scm ()));
2208                 n->unprotect ();
2209                 v->set_spot (@$);
2210                 n->set_spot (@$);
2211                 $$ = v;
2212                 
2213         }
2214         | figure_spec optional_notemode_duration {
2215                 Music *m = unsmob_music ($1);
2216                 m->set_spot (@$);
2217                 for (SCM s = m->get_property ("elements"); scm_is_pair (s); s = scm_cdr (s))
2218                 {
2219                         unsmob_music (scm_car (s))->set_property ("duration", $2);
2220                 }
2221                 $$ = m;
2222         }       
2223         | RESTNAME optional_notemode_duration           {
2224                 Music *ev = 0;
2225                 if (ly_scm2string ($1) == "s") {
2226                         /* Space */
2227                         ev = MY_MAKE_MUSIC ("SkipEvent");
2228                   }
2229                 else {
2230                         ev = MY_MAKE_MUSIC ("RestEvent");
2231                 
2232                     }
2233                 ev->set_property ("duration", $2);
2234                 ev->set_spot (@$);
2235                 Music *velt = MY_MAKE_MUSIC ("EventChord");
2236                 velt->set_property ("elements", scm_list_1 (ev->self_scm ()));
2237                 velt->set_spot (@$);
2238
2239                 ev->unprotect();
2240
2241                 $$ = velt;
2242         }
2243         | MULTI_MEASURE_REST optional_notemode_duration         {
2244                 SCM proc = ly_lily_module_constant ("make-multi-measure-rest");
2245                 SCM mus = scm_call_2 (proc, $2, make_input (@$));
2246                 $$ = unsmob_music (mus);
2247                 $$->protect ();
2248         }
2249         
2250         | lyric_element optional_notemode_duration      {
2251                 if (!THIS->lexer_->is_lyric_state ())
2252                         THIS->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2253
2254                 Music *levent = MY_MAKE_MUSIC ("LyricEvent");
2255                 levent->set_property ("text", $1);
2256                 levent->set_property ("duration",$2);
2257                 levent->set_spot (@$);
2258                 Music *velt = MY_MAKE_MUSIC ("EventChord");
2259                 velt->set_property ("elements", scm_list_1 (levent->self_scm ()));
2260
2261                 $$= velt;
2262         }
2263         | new_chord {
2264                 if (!THIS->lexer_->is_chord_state ())
2265                         THIS->parser_error (@1, _ ("have to be in Chord mode for chords"));
2266                 $$ = unsmob_music ($1);
2267         }
2268         ;
2269
2270 lyric_element:
2271         lyric_markup {
2272                 $$ = $1;
2273         }
2274         | LYRICS_STRING {
2275                 $$ = $1;
2276         }
2277         ;
2278
2279 new_chord:
2280         steno_tonic_pitch optional_notemode_duration   {
2281                 $$ = make_chord ($1, $2, SCM_EOL);
2282         }
2283         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
2284                 SCM its = scm_reverse_x ($4, SCM_EOL);
2285                 $$ = make_chord ($1, $2, scm_cons ($3, its));
2286         }
2287         ;
2288
2289 chord_items:
2290         /**/ {
2291                 $$ = SCM_EOL;           
2292         }
2293         | chord_items chord_item {
2294                 $$ = scm_cons ($2, $$);
2295         }
2296         ;
2297
2298 chord_separator:
2299         CHORD_COLON {
2300                 $$ = ly_symbol2scm ("chord-colon");
2301         }
2302         | CHORD_CARET {
2303                 $$ = ly_symbol2scm ("chord-caret");
2304         }
2305         | CHORD_SLASH steno_tonic_pitch {
2306                 $$ = scm_list_2 (ly_symbol2scm ("chord-slash"), $2);
2307         }
2308         | CHORD_BASS steno_tonic_pitch {
2309                 $$ = scm_list_2 (ly_symbol2scm ("chord-bass"), $2);
2310         }
2311         ;
2312
2313 chord_item:
2314         chord_separator {
2315                 $$ = $1;
2316         }
2317         | step_numbers {
2318                 $$ = scm_reverse_x ($1, SCM_EOL);
2319         }
2320         | CHORD_MODIFIER  {
2321                 $$ = $1;
2322         }
2323         ;
2324
2325 step_numbers:
2326         step_number { $$ = scm_cons ($1, SCM_EOL); }
2327         | step_numbers '.' step_number {
2328                 $$ = scm_cons ($3, $$);
2329         }
2330         ;
2331
2332 step_number:
2333         bare_unsigned {
2334                 $$ = make_chord_step ($1, 0);
2335         }
2336         | bare_unsigned '+' {
2337                 $$ = make_chord_step ($1, SHARP);
2338         }
2339         | bare_unsigned CHORD_MINUS {
2340                 $$ = make_chord_step ($1, FLAT);
2341         }
2342         ;       
2343
2344 /*
2345         UTILITIES
2346
2347 TODO: should deprecate in favor of Scheme?
2348
2349  */
2350 number_expression:
2351         number_expression '+' number_term {
2352                 $$ = scm_sum ($1, $3);
2353         }
2354         | number_expression '-' number_term {
2355                 $$ = scm_difference ($1, $3);
2356         }
2357         | number_term
2358         ;
2359
2360 number_term:
2361         number_factor {
2362                 $$ = $1;
2363         }
2364         | number_factor '*' number_factor {
2365                 $$ = scm_product ($1, $3);
2366         }
2367         | number_factor '/' number_factor {
2368                 $$ = scm_divide ($1, $3);
2369         }
2370         ;
2371
2372 number_factor:
2373         '-'  number_factor { /* %prec UNARY_MINUS */
2374                 $$ = scm_difference ($2, SCM_UNDEFINED);
2375         }
2376         | bare_number
2377         ;
2378
2379
2380 bare_number:
2381         UNSIGNED        {
2382                 $$ = scm_from_int ($1);
2383         }
2384         | REAL          {
2385                 $$ = $1;
2386         }
2387         | NUMBER_IDENTIFIER             {
2388                 $$ = $1;
2389         }
2390         | REAL NUMBER_IDENTIFIER        {
2391                 $$ = scm_from_double (scm_to_double ($1) *scm_to_double ($2));
2392         }
2393         | UNSIGNED NUMBER_IDENTIFIER    {
2394                 $$ = scm_from_double ($1 *scm_to_double ($2));
2395         }
2396         ;
2397
2398
2399 bare_unsigned:
2400         UNSIGNED {
2401                         $$ = $1;
2402         }
2403         | DIGIT {
2404                 $$ = $1;
2405         }
2406         ;
2407
2408 exclamations:
2409                 { $$ = 0; }
2410         | exclamations '!'      { $$ ++; }
2411         ;
2412
2413 questions:
2414                 { $$ = 0; }
2415         | questions '?' { $$ ++; }
2416         ;
2417
2418 /*
2419 This should be done more dynamically if possible.
2420 */
2421
2422 lyric_markup:
2423         LYRIC_MARKUP_IDENTIFIER {
2424                 $$ = $1;
2425         }
2426         | LYRIC_MARKUP
2427                 { THIS->lexer_->push_markup_state (); }
2428         markup_top {
2429                 $$ = $3;
2430                 THIS->lexer_->pop_state ();
2431         }
2432         ;
2433
2434 full_markup:
2435         MARKUP_IDENTIFIER {
2436                 $$ = $1;
2437         }
2438         | MARKUP
2439                 { THIS->lexer_->push_markup_state (); }
2440         markup_top {
2441                 $$ = $3;
2442                 THIS->lexer_->pop_state ();
2443         }
2444         ;
2445
2446 markup_top:
2447         markup_list { 
2448                 $$ = scm_list_2 (ly_lily_module_constant ("line-markup"),  $1); 
2449         }
2450         | markup_head_1_list simple_markup      {
2451                 $$ = scm_car (scm_call_2 (ly_lily_module_constant ("map-markup-command-list"), $1, scm_list_1 ($2)));
2452         }
2453         | simple_markup {
2454                 $$ = $1;
2455         }
2456         ;
2457
2458 markup_list:
2459         markup_composed_list {
2460                 $$ = $1;
2461         }
2462         | markup_braced_list {
2463                 $$ = $1;
2464         }
2465         ;
2466
2467 markup_composed_list:
2468         markup_head_1_list markup_braced_list {
2469                 $$ = scm_call_2 (ly_lily_module_constant ("map-markup-command-list"), $1, $2);
2470                 
2471         }
2472         ;
2473
2474 markup_braced_list:
2475         '{' markup_braced_list_body '}' {
2476                 $$ = scm_reverse_x ($2, SCM_EOL);
2477         }
2478         ;
2479
2480 markup_braced_list_body:
2481         /* empty */     {  $$ = SCM_EOL; }
2482         | markup_braced_list_body markup {
2483                 $$ = scm_cons ($2, $1);
2484         }
2485         | markup_braced_list_body markup_list {
2486                 $$ = scm_append_x (scm_list_2 (scm_reverse_x ($2, SCM_EOL), $1));
2487         }
2488         ;
2489
2490 markup_head_1_item:
2491         MARKUP_HEAD_MARKUP0     {
2492                 $$ = scm_list_1 ($1);
2493         }
2494         | MARKUP_HEAD_SCM0_MARKUP1 embedded_scm {
2495                 $$ = scm_list_2 ($1, $2);
2496         }
2497         | MARKUP_HEAD_SCM0_SCM1_MARKUP2 embedded_scm embedded_scm       {
2498                 $$ = scm_list_3 ($1, $2, $3);
2499         }
2500         ;
2501
2502 markup_head_1_list:
2503         markup_head_1_item      {
2504                 $$ = scm_list_1 ($1);
2505         }
2506         | markup_head_1_list markup_head_1_item {
2507                 $$ = scm_cons ($2, $1);
2508         }
2509         ;
2510
2511 simple_markup:
2512         STRING {
2513                 $$ = make_simple_markup ($1);
2514         }
2515         | MARKUP_IDENTIFIER {
2516                 $$ = $1;
2517         }
2518         | LYRIC_MARKUP_IDENTIFIER {
2519                 $$ = $1;
2520         }
2521         | STRING_IDENTIFIER {
2522                 $$ = $1;
2523         }
2524         | SCORE {
2525                 SCM nn = THIS->lexer_->lookup_identifier ("pitchnames");
2526                 THIS->lexer_->push_note_state (alist_to_hashq (nn));
2527         } '{' score_body '}' {
2528                 Score * sc = $4;
2529                 $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), sc->self_scm ());
2530                 sc->unprotect ();
2531                 THIS->lexer_->pop_state ();
2532         }
2533         | MARKUP_HEAD_SCM0 embedded_scm {
2534                 $$ = scm_list_2 ($1, $2);
2535         }
2536         | MARKUP_HEAD_SCM0_SCM1_SCM2 embedded_scm embedded_scm embedded_scm {
2537                 $$ = scm_list_4 ($1, $2, $3, $4);
2538         }
2539         | MARKUP_HEAD_SCM0_SCM1 embedded_scm embedded_scm {
2540                 $$ = scm_list_3 ($1, $2, $3);
2541         }
2542         | MARKUP_HEAD_EMPTY {
2543                 $$ = scm_list_1 ($1);
2544         }
2545         | MARKUP_HEAD_LIST0 markup_list {
2546                 $$ = scm_list_2 ($1,$2);
2547         }
2548         | MARKUP_HEAD_MARKUP0_MARKUP1 markup markup {
2549                 $$ = scm_list_3 ($1, $2, $3);
2550         }
2551         ;
2552         
2553 markup:
2554         markup_head_1_list simple_markup        {
2555                 SCM mapper = ly_lily_module_constant ("map-markup-command-list");
2556                 $$ = scm_car (scm_call_2 (mapper, $1, scm_list_1 ($2)));
2557         }
2558         | simple_markup {
2559                 $$ = $1;
2560         }
2561         ;
2562
2563 %%
2564
2565 void
2566 Lily_parser::set_yydebug (bool )
2567 {
2568 #if 0
2569         yydebug = 1;
2570 #endif
2571 }
2572
2573 void
2574 Lily_parser::do_yyparse ()
2575 {
2576         yyparse ((void*)this);
2577 }
2578
2579
2580
2581
2582
2583 /*
2584
2585 It is a little strange to have this function in this file, but
2586 otherwise, we have to import music classes into the lexer.
2587
2588 */
2589 int
2590 Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
2591 {
2592         if (scm_is_string (sid)) {
2593                 *destination = sid;
2594                 return STRING_IDENTIFIER;
2595         } else if (unsmob_book (sid)) {
2596                 *destination = unsmob_book (sid)->clone ()->self_scm ();
2597                 return BOOK_IDENTIFIER;
2598         } else if (scm_is_number (sid)) {
2599                 *destination = sid;
2600                 return NUMBER_IDENTIFIER;
2601         } else if (unsmob_context_def (sid)) {
2602                 *destination = unsmob_context_def (sid)->clone_scm ();
2603                 return CONTEXT_DEF_IDENTIFIER;
2604         } else if (unsmob_score (sid)) {
2605                 Score *score = new Score (*unsmob_score (sid));
2606                 *destination = score->self_scm ();
2607                 return SCORE_IDENTIFIER;
2608         } else if (Music *mus = unsmob_music (sid)) {
2609                 mus = mus->clone ();
2610                 *destination = mus->self_scm ();
2611                 unsmob_music (*destination)->
2612                         set_property ("origin", make_input (last_input_));
2613
2614                 bool is_event = scm_memq (ly_symbol2scm ("event"), mus->get_property ("types"))
2615                         != SCM_BOOL_F;
2616
2617                 return is_event ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
2618         } else if (unsmob_duration (sid)) {
2619                 *destination = unsmob_duration (sid)->smobbed_copy ();
2620                 return DURATION_IDENTIFIER;
2621         } else if (unsmob_output_def (sid)) {
2622                 Output_def *p = unsmob_output_def (sid);
2623                 p = p->clone ();
2624
2625                 *destination = p->self_scm ();
2626                 return OUTPUT_DEF_IDENTIFIER;
2627         } else if (Text_interface::is_markup (sid)) {
2628                 *destination = sid;
2629                 if (is_lyric_state ())
2630                         return LYRIC_MARKUP_IDENTIFIER;
2631                 return MARKUP_IDENTIFIER;
2632         }
2633
2634         return -1;      
2635 }
2636
2637 Music *
2638 property_op_to_music (SCM op)
2639 {
2640         Music *m = 0;
2641         SCM tag = scm_car (op);
2642         SCM symbol = scm_cadr (op);
2643         SCM args = scm_cddr (op);
2644         SCM grob_val = SCM_UNDEFINED;
2645         SCM grob_path = SCM_UNDEFINED;
2646         SCM val = SCM_UNDEFINED;
2647         
2648         if (tag == ly_symbol2scm ("assign"))
2649                 {
2650                 m = MY_MAKE_MUSIC ("PropertySet");
2651                 val = scm_car (args);
2652                 }
2653         else if (tag == ly_symbol2scm ("unset"))
2654                 m = MY_MAKE_MUSIC ("PropertyUnset");
2655         else if (tag == ly_symbol2scm ("push"))
2656                 {
2657                 m = MY_MAKE_MUSIC ("OverrideProperty");
2658                 grob_val = scm_car (args);
2659                 grob_path = scm_cdr (args);
2660                 m->set_property ("pop-first", SCM_BOOL_T);
2661                 }
2662         else if (tag == ly_symbol2scm ("pop")) {
2663                 m = MY_MAKE_MUSIC ("RevertProperty");
2664                 grob_path = args;
2665                 }
2666
2667         m->set_property ("symbol", symbol);
2668
2669         if (val != SCM_UNDEFINED)
2670                 m->set_property ("value", val);
2671         if (grob_val != SCM_UNDEFINED)
2672                 m->set_property ("grob-value", grob_val);
2673         if (grob_path != SCM_UNDEFINED)
2674                 m->set_property ("grob-property-path", grob_path);
2675
2676         return m;
2677 }
2678
2679 Music*
2680 context_spec_music (SCM type, SCM id, Music *m, SCM ops)
2681 {
2682         Music *csm = MY_MAKE_MUSIC ("ContextSpeccedMusic");
2683
2684         csm->set_property ("element", m->self_scm ());
2685         m->unprotect ();
2686
2687         csm->set_property ("context-type",
2688                 scm_is_symbol (type) ? type : scm_string_to_symbol (type));
2689         csm->set_property ("property-operations", ops);
2690
2691         if (scm_is_string (id))
2692                 csm->set_property ("context-id", id);
2693         return csm;
2694 }
2695
2696 SCM
2697 get_next_unique_context_id ()
2698 {
2699         return scm_makfrom0str ("$uniqueContextId");
2700 }
2701
2702
2703 SCM
2704 get_next_unique_lyrics_context_id ()
2705 {
2706         static int new_context_count;
2707         char s[128];
2708         snprintf (s, sizeof (s)-1, "uniqueContext%d", new_context_count++);
2709         return scm_makfrom0str (s);
2710 }
2711
2712
2713 Music *
2714 run_music_function (Lily_parser *parser, SCM expr)
2715 {
2716         SCM func = scm_car (expr);
2717         Input *loc = unsmob_input (scm_cadr (expr));
2718         SCM args = scm_cddr (expr);
2719         SCM sig = scm_object_property (func, ly_symbol2scm ("music-function-signature"));
2720
2721         SCM type_check_proc = ly_lily_module_constant ("type-check-list");
2722         bool ok  = true;
2723
2724         if (!to_boolean (scm_call_3  (type_check_proc, scm_cadr (expr), sig, args)))
2725         {
2726                 parser->error_level_ = 1;
2727                 ok = false;
2728         }
2729
2730         SCM m = SCM_EOL;
2731         if (ok)
2732                 m = scm_apply_0 (func, scm_cons (parser->self_scm(),
2733                                                  scm_cdr (expr)));
2734
2735
2736         Music* retval = 0;
2737         if (unsmob_music (m))
2738                 {
2739                 retval = unsmob_music (m);
2740                 retval->protect ();
2741                 }
2742         else
2743                 {
2744                 if (ok)
2745                         loc->error (_ ("music head function must return Music object")); 
2746                 retval = MY_MAKE_MUSIC ("Music");
2747                 }
2748         retval->set_spot (*loc);
2749         return retval;
2750 }
2751
2752 bool
2753 is_regular_identifier (SCM id)
2754 {
2755   std::string str = ly_scm2string (id);
2756   char const *s = str.c_str ();
2757
2758   bool v = true;
2759 #if 0
2760   isalpha (*s);
2761   s++;
2762 #endif
2763   while (*s && v)
2764    {
2765         v = v && isalnum (*s);
2766         s++;
2767    }
2768   return v;
2769 }
2770
2771
2772 SCM
2773 get_first_context_id (SCM type, Music *m)
2774 {
2775         SCM id = m->get_property ("context-id");
2776         if (SCM_BOOL_T == scm_equal_p (m->get_property ("context-type"), type)
2777             && scm_is_string (m->get_property ("context-id"))
2778             && scm_c_string_length (id) > 0)
2779         {
2780                 return id;
2781         }
2782         return SCM_EOL;
2783 }
2784
2785 SCM
2786 make_simple_markup (SCM a)
2787 {
2788         return a;
2789 }
2790
2791 bool
2792 is_duration (int t)
2793 {
2794   return t && t == 1 << intlog2 (t);
2795 }
2796
2797 void
2798 set_music_properties (Music *p, SCM a)
2799 {
2800   for (SCM k = a; scm_is_pair (k); k = scm_cdr (k))
2801         p->internal_set_property (scm_caar (k), scm_cdar (k));
2802 }
2803
2804
2805 SCM
2806 make_chord_step (int step, int alter)
2807 {
2808         if (step == 7)
2809                 alter += FLAT;
2810
2811         while (step < 0)
2812                 step += 7;
2813         Pitch m ((step -1) / 7, (step - 1) % 7, alter);
2814         return m.smobbed_copy ();
2815 }
2816
2817
2818 SCM
2819 make_chord (SCM pitch, SCM dur, SCM modification_list)
2820 {
2821         SCM chord_ctor = ly_lily_module_constant ("construct-chord");
2822         SCM ch = scm_call_3 (chord_ctor, pitch, dur, modification_list);
2823
2824         unsmob_music (ch)->protect();   
2825         return ch;
2826 }
2827
2828
2829 /* Todo: actually also use apply iso. call too ...  */
2830 bool
2831 ly_input_procedure_p (SCM x)
2832 {
2833         return ly_is_procedure (x)
2834                 || (scm_is_pair (x) && ly_is_procedure (scm_car (x)));
2835 }
2836
2837 Music*
2838 set_property_music (SCM sym, SCM value)
2839 {
2840         Music *p = MY_MAKE_MUSIC ("PropertySet");
2841         p->set_property ("symbol", sym);
2842         p->set_property ("value", value);
2843         return p;
2844 }
2845
2846 Music*
2847 make_music_relative (Pitch start, Music *music)
2848 {
2849         Music *relative = MY_MAKE_MUSIC ("RelativeOctaveMusic");
2850         relative->set_property ("element", music->self_scm ());
2851         
2852         Pitch last = music->to_relative_octave (start);
2853         if (lily_1_8_relative)
2854                 music->set_property ("last-pitch", last.smobbed_copy ());
2855         return relative;
2856 }
2857
2858 Music *
2859 make_lyric_combine_music (SCM name, Music *music)
2860 {
2861         Music *combine = MY_MAKE_MUSIC ("LyricCombineMusic");
2862         combine->set_property ("element", music->self_scm ());
2863         combine->set_property ("associated-context", name);
2864         return combine;
2865 }
2866
2867
2868 int
2869 yylex (YYSTYPE *s, YYLTYPE *loc, void *v)
2870 {
2871         Lily_parser *pars = (Lily_parser*) v;
2872         Lily_lexer *lex = pars->lexer_;
2873
2874         lex->lexval = (void*) s;
2875         lex->lexloc = loc;
2876         lex->prepare_for_next_token ();
2877         return lex->yylex ();
2878 }