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