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