]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
web: move some old news from front page to archive
[lilypond.git] / lily / parser.yy
1 /* -*- mode: c++; c-file-style: "linux" -*- */
2 /*
3   This file is part of LilyPond, the GNU music typesetter.
4
5   Copyright (C) 1997--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
6                  Jan Nieuwenhuizen <janneke@gnu.org>
7
8   LilyPond is free software: you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation, either version 3 of the License, or
11   (at your option) any later version.
12
13   LilyPond is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 /* Mode and indentation are at best a rough approximation based on TAB
23  * formatting (reasonable for compatibility with unspecific editor
24  * modes as Bison modes are hard to find) and need manual correction
25  * frequently.  Without a reasonably dependable way of formatting a
26  * Bison file sensibly, there is little point in trying to fix the
27  * inconsistent state of indentation.
28  */
29
30 %{
31
32 #define YYDEBUG 1
33 #define YYERROR_VERBOSE 1
34
35 #define yyerror Lily_parser::parser_error
36
37 /* We use custom location type: Input objects */
38 #define YYLTYPE Input
39 #define YYLLOC_DEFAULT(Current,Rhs,N) \
40         ((Current).set_location ((Rhs)[1], (Rhs)[N]))
41
42
43 %}
44
45 %parse-param {Lily_parser *parser}
46 %lex-param {Lily_parser *parser}
47
48 /* We use SCMs to do strings, because it saves us the trouble of
49 deleting them.  Let's hope that a stack overflow doesn't trigger a move
50 of the parse stack onto the heap. */
51
52 %left PREC_BOT
53 %nonassoc REPEAT
54 %nonassoc ALTERNATIVE
55
56 /* The above precedences tackle the shift/reduce problem
57
58 1.  \repeat
59         \repeat .. \alternative
60
61     \repeat { \repeat .. \alternative }
62
63 or
64
65     \repeat { \repeat } \alternative
66 */
67
68 %nonassoc COMPOSITE
69 %left ADDLYRICS
70
71  /* ADDLYRICS needs to have lower precedence than argument scanning,
72   * or we won't be able to tell music apart from closed_music without
73   * lookahead in the context of function calls.
74   */
75
76 %nonassoc DEFAULT
77
78  /* \default is only applied after exhausting function arguments */
79
80 %nonassoc FUNCTION_ARGLIST
81
82  /* expressions with units are permitted into argument lists */
83
84 %right PITCH_IDENTIFIER NOTENAME_PITCH TONICNAME_PITCH
85       UNSIGNED REAL DURATION_IDENTIFIER ':'
86
87  /* The above are the symbols that can start optional function arguments
88     that are recognized in the grammar rather than by predicate
89  */
90
91 %nonassoc NUMBER_IDENTIFIER '/'
92
93  /* Number-unit expressions, where permitted, are concatenated into
94   * function arguments, just like fractions and tremoli.  Tremoli must
95   * not have higher precedence than UNSIGNED, or Lilypond will not
96   * join ':' with a following optional number.
97   */
98
99 %left PREC_TOP
100
101
102
103
104 %pure_parser
105 %locations
106
107
108
109 %{ // -*-Fundamental-*-
110
111 /*
112 FIXME:
113
114    * The rules for who is protecting what are very shady.  Uniformise
115      this.
116
117    * There are too many lexical modes?
118 */
119
120 #include "config.hh"
121
122 #include <cctype>
123 #include <cstdlib>
124 #include <cstdio>
125 using namespace std;
126
127 #include "book.hh"
128 #include "context-def.hh"
129 #include "context-mod.hh"
130 #include "dimensions.hh"
131 #include "file-path.hh"
132 #include "input.hh"
133 #include "international.hh"
134 #include "lily-guile.hh"
135 #include "lily-lexer.hh"
136 #include "lily-parser.hh"
137 #include "main.hh"
138 #include "misc.hh"
139 #include "music.hh"
140 #include "output-def.hh"
141 #include "paper-book.hh"
142 #include "scm-hash.hh"
143 #include "score.hh"
144 #include "text-interface.hh"
145 #include "warn.hh"
146
147 void
148 Lily_parser::parser_error (Input const *i, Lily_parser *parser, string s)
149 {
150         parser->parser_error (*i, s);
151 }
152
153 #define MYBACKUP(Token, Value, Location)                                \
154 do                                                                      \
155         if (yychar == YYEMPTY)                                          \
156         {                                                               \
157                 if (Token)                                              \
158                         parser->lexer_->push_extra_token (Token, Value); \
159                 parser->lexer_->push_extra_token (BACKUP);              \
160         } else {                                                        \
161                 parser->parser_error                                    \
162                         (Location, _("Too much lookahead"));            \
163         }                                                               \
164 while (0)
165
166
167 #define MYREPARSE(Location, Pred, Token, Value)                         \
168 do                                                                      \
169         if (yychar == YYEMPTY)                                          \
170         {                                                               \
171                 parser->lexer_->push_extra_token (Token, Value);        \
172                 parser->lexer_->push_extra_token (REPARSE,              \
173                                                   Pred);                \
174         } else {                                                        \
175                 parser->parser_error                                    \
176                         (Location, _("Too much lookahead"));            \
177         }                                                               \
178 while (0)
179
180 %}
181
182
183 %union {
184         Book *book;
185         Output_def *outputdef;
186         SCM scm;
187         std::string *string;
188         Score *score;
189         int i;
190 }
191
192 %{
193
194 #define MY_MAKE_MUSIC(x, spot)  make_music_with_input (ly_symbol2scm (x), spot)
195
196 /* ES TODO:
197 - Don't use lily module, create a new module instead.
198 - delay application of the function
199 */
200 #define LOWLEVEL_MAKE_SYNTAX(proc, args)        \
201   scm_apply_0 (proc, args)
202 /* Syntactic Sugar. */
203 #define MAKE_SYNTAX(name, location, ...)        \
204   LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant (name), scm_list_n (parser->self_scm (), make_input (location) , ##__VA_ARGS__, SCM_UNDEFINED))
205 #define START_MAKE_SYNTAX(name, ...)                                    \
206         scm_list_n (ly_lily_module_constant (name) , ##__VA_ARGS__, SCM_UNDEFINED)
207 #define FINISH_MAKE_SYNTAX(start, location, ...)                        \
208         LOWLEVEL_MAKE_SYNTAX (scm_car (start), scm_cons2 (parser->self_scm (), make_input (location), scm_append_x (scm_list_2 (scm_cdr (start), scm_list_n (__VA_ARGS__, SCM_UNDEFINED)))))
209
210 SCM get_next_unique_context_id ();
211 SCM get_next_unique_lyrics_context_id ();
212
213 #undef _
214 #if !HAVE_GETTEXT
215 #define _(x) x
216 #else
217 #include <libintl.h>
218 #define _(x) gettext (x)
219 #endif
220
221
222 static Music *make_music_with_input (SCM name, Input where);
223 SCM check_scheme_arg (Lily_parser *parser, Input loc,
224                       SCM arg, SCM args, SCM pred);
225 SCM loc_on_music (Input loc, SCM arg);
226 SCM make_chord_elements (Input loc, SCM pitch, SCM dur, SCM modification_list);
227 SCM make_chord_step (int step, Rational alter);
228 SCM make_simple_markup (SCM a);
229 bool is_duration (int t);
230 bool is_regular_identifier (SCM id);
231 int yylex (YYSTYPE *s, YYLTYPE *loc, Lily_parser *parser);
232 void set_music_properties (Music *p, SCM a);
233
234 %}
235
236 /* The third option is an alias that will be used to display the
237    syntax error.  Bison CVS now correctly handles backslash escapes.
238
239    FIXME: Bison needs to translate some of these, eg, STRING.
240
241 */
242
243 /* Keyword tokens with plain escaped name.  */
244 %token ACCEPTS "\\accepts"
245 %token ADDLYRICS "\\addlyrics"
246 %token ALIAS "\\alias"
247 %token ALTERNATIVE "\\alternative"
248 %token BOOK "\\book"
249 %token BOOKPART "\\bookpart"
250 %token CHANGE "\\change"
251 %token CHORDMODE "\\chordmode"
252 %token CHORDS "\\chords"
253 %token CONSISTS "\\consists"
254 %token CONTEXT "\\context"
255 %token DEFAULT "\\default"
256 %token DEFAULTCHILD "\\defaultchild"
257 %token DENIES "\\denies"
258 %token DESCRIPTION "\\description"
259 %token DRUMMODE "\\drummode"
260 %token DRUMS "\\drums"
261 %token FIGUREMODE "\\figuremode"
262 %token FIGURES "\\figures"
263 %token HEADER "\\header"
264 %token INVALID "\\version-error"
265 %token LAYOUT "\\layout"
266 %token LYRICMODE "\\lyricmode"
267 %token LYRICS "\\lyrics"
268 %token LYRICSTO "\\lyricsto"
269 %token MARKUP "\\markup"
270 %token MARKUPLIST "\\markuplist"
271 %token MIDI "\\midi"
272 %token NAME "\\name"
273 %token NOTEMODE "\\notemode"
274 %token OVERRIDE "\\override"
275 %token PAPER "\\paper"
276 %token REMOVE "\\remove"
277 %token REPEAT "\\repeat"
278 %token REST "\\rest"
279 %token REVERT "\\revert"
280 %token SCORE "\\score"
281 %token SEQUENTIAL "\\sequential"
282 %token SET "\\set"
283 %token SIMULTANEOUS "\\simultaneous"
284 %token TEMPO "\\tempo"
285 %token TYPE "\\type"
286 %token UNSET "\\unset"
287 %token WITH "\\with"
288
289 /* Keyword token exceptions.  */
290 %token NEWCONTEXT "\\new"
291
292
293 /* Other string tokens.  */
294
295 %token CHORD_BASS "/+"
296 %token CHORD_CARET "^"
297 %token CHORD_COLON ":"
298 %token CHORD_MINUS "-"
299 %token CHORD_SLASH "/"
300 %token ANGLE_OPEN "<"
301 %token ANGLE_CLOSE ">"
302 %token DOUBLE_ANGLE_OPEN "<<"
303 %token DOUBLE_ANGLE_CLOSE ">>"
304 %token E_BACKSLASH "\\"
305 %token E_ANGLE_CLOSE "\\>"
306 %token E_CHAR "\\C[haracter]"
307 %token E_CLOSE "\\)"
308 %token E_EXCLAMATION "\\!"
309 %token E_BRACKET_OPEN "\\["
310 %token E_OPEN "\\("
311 %token E_BRACKET_CLOSE "\\]"
312 %token E_ANGLE_OPEN "\\<"
313 %token E_PLUS "\\+"
314 %token E_TILDE "\\~"
315 %token EXTENDER "__"
316
317 /*
318 If we give names, Bison complains.
319 */
320 %token FIGURE_CLOSE /* "\\>" */
321 %token FIGURE_OPEN /* "\\<" */
322 %token FIGURE_SPACE "_"
323 %token HYPHEN "--"
324
325 %token CHORDMODIFIERS
326 %token LYRIC_MARKUP
327 %token MULTI_MEASURE_REST
328
329
330 %token <i> E_UNSIGNED
331 %token <scm> UNSIGNED
332
333 /* Artificial tokens, for more generic function syntax */
334 %token <i> EXPECT_MARKUP "markup?"
335 %token <i> EXPECT_PITCH "ly:pitch?"
336 %token <i> EXPECT_DURATION "ly:duration?"
337 %token <scm> EXPECT_SCM "scheme?"
338 %token <scm> BACKUP "(backed-up?)"
339 %token <scm> REPARSE "(reparsed?)"
340 %token <i> EXPECT_MARKUP_LIST "markup-list?"
341 %token <scm> EXPECT_OPTIONAL "optional?"
342 /* After the last argument. */
343 %token <i> EXPECT_NO_MORE_ARGS;
344
345 /* An artificial token for parsing embedded Lilypond */
346 %token <i> EMBEDDED_LILY "#{"
347
348 %token <scm> BOOK_IDENTIFIER
349 %token <scm> CHORDMODIFIER_PITCH
350 %token <scm> CHORD_MODIFIER
351 %token <scm> CHORD_REPETITION
352 %token <scm> CONTEXT_DEF_IDENTIFIER
353 %token <scm> CONTEXT_MOD_IDENTIFIER
354 %token <scm> DRUM_PITCH
355 %token <scm> PITCH_IDENTIFIER
356 %token <scm> DURATION_IDENTIFIER
357 %token <scm> EVENT_IDENTIFIER
358 %token <scm> EVENT_FUNCTION
359 %token <scm> FRACTION
360 %token <scm> LYRICS_STRING
361 %token <scm> LYRIC_ELEMENT
362 %token <scm> LYRIC_MARKUP_IDENTIFIER
363 %token <scm> MARKUP_FUNCTION
364 %token <scm> MARKUP_LIST_FUNCTION
365 %token <scm> MARKUP_IDENTIFIER
366 %token <scm> MARKUPLIST_IDENTIFIER
367 %token <scm> MUSIC_FUNCTION
368 %token <scm> MUSIC_IDENTIFIER
369 %token <scm> NOTENAME_PITCH
370 %token <scm> NUMBER_IDENTIFIER
371 %token <scm> OUTPUT_DEF_IDENTIFIER
372 %token <scm> REAL
373 %token <scm> RESTNAME
374 %token <scm> SCM_FUNCTION
375 %token <scm> SCM_IDENTIFIER
376 %token <scm> SCM_TOKEN
377 %token <scm> SCORE_IDENTIFIER
378 %token <scm> STRING
379 %token <scm> STRING_IDENTIFIER
380 %token <scm> TONICNAME_PITCH
381
382
383 %type <book> book_block
384 %type <book> book_body
385 %type <book> bookpart_block
386 %type <book> bookpart_body
387
388 %type <i> bare_unsigned
389 %type <scm> figured_bass_alteration
390 %type <i> dots
391 %type <i> exclamations
392 %type <i> optional_rest
393 %type <i> questions
394 %type <i> script_dir
395 %type <i> sub_quotes
396 %type <i> sup_quotes
397 %type <i> tremolo_type
398
399 /* Music */
400 %type <scm> composite_music
401 %type <scm> grouped_music_list
402 %type <scm> braced_music_list
403 %type <scm> closed_music
404 %type <scm> music
405 %type <scm> music_bare
406 %type <scm> music_arg
407 %type <scm> music_assign
408 %type <scm> music_embedded
409 %type <scm> complex_music
410 %type <scm> complex_music_prefix
411 %type <scm> mode_changed_music
412 %type <scm> repeated_music
413 %type <scm> sequential_music
414 %type <scm> simple_music
415 %type <scm> simultaneous_music
416 %type <scm> chord_body
417 %type <scm> chord_body_element
418 %type <scm> command_element
419 %type <scm> command_event
420 %type <scm> context_modification
421 %type <scm> context_change
422 %type <scm> direction_less_event
423 %type <scm> direction_reqd_event
424 %type <scm> embedded_lilypond
425 %type <scm> event_chord
426 %type <scm> fingering
427 %type <scm> gen_text_def
428 %type <scm> music_property_def
429 %type <scm> note_chord_element
430 %type <scm> post_event
431 %type <scm> post_event_nofinger
432 %type <scm> re_rhythmed_music
433 %type <scm> simple_element
434 %type <scm> simple_music_property_def
435 %type <scm> start_symbol
436 %type <scm> string_number_event
437 %type <scm> tempo_event
438
439 %type <outputdef> output_def_body
440 %type <outputdef> output_def_head
441 %type <outputdef> output_def_head_with_mode_switch
442 %type <outputdef> output_def
443 %type <outputdef> paper_block
444
445 %type <scm> music_function_call
446 %type <scm> music_list
447 %type <scm> assignment_id
448 %type <scm> bare_number
449 %type <scm> bare_number_closed
450 %type <scm> unsigned_number
451 %type <scm> bass_figure
452 %type <scm> figured_bass_modification
453 %type <scm> br_bass_figure
454 %type <scm> bass_number
455 %type <scm> chord_body_elements
456 %type <scm> chord_item
457 %type <scm> chord_items
458 %type <scm> chord_separator
459 %type <scm> context_def_mod
460 %type <scm> context_def_spec_block
461 %type <scm> context_def_spec_body
462 %type <scm> context_mod
463 %type <scm> context_mod_arg
464 %type <scm> context_mod_embedded
465 %type <scm> context_mod_list
466 %type <scm> context_prop_spec
467 %type <scm> direction_less_char
468 %type <scm> duration_length
469 %type <scm> embedded_scm
470 %type <scm> embedded_scm_arg
471 %type <scm> embedded_scm_arg_closed
472 %type <scm> embedded_scm_bare
473 %type <scm> embedded_scm_bare_arg
474 %type <scm> embedded_scm_closed
475 %type <scm> event_function_event
476 %type <scm> figure_list
477 %type <scm> figure_spec
478 %type <scm> fraction
479 %type <scm> full_markup
480 %type <scm> full_markup_list
481 %type <scm> function_arglist
482 %type <scm> function_arglist_optional
483 %type <scm> function_arglist_backup
484 %type <scm> function_arglist_nonbackup
485 %type <scm> function_arglist_nonbackup_common
486 %type <scm> function_arglist_closed_nonbackup
487 %type <scm> function_arglist_skip
488 %type <scm> function_arglist_bare
489 %type <scm> function_arglist_closed
490 %type <scm> function_arglist_closed_optional
491 %type <scm> function_arglist_common
492 %type <scm> function_arglist_common_lyric
493 %type <scm> function_arglist_common_minus
494 %type <scm> function_arglist_closed_common
495 %type <scm> function_arglist_keep
496 %type <scm> function_arglist_closed_keep
497 %type <scm> identifier_init
498 %type <scm> lilypond
499 %type <scm> lilypond_header
500 %type <scm> lilypond_header_body
501 %type <scm> lyric_element
502 %type <scm> lyric_element_arg
503 %type <scm> lyric_element_music
504 %type <scm> lyric_markup
505 %type <scm> markup
506 %type <scm> markup_braced_list
507 %type <scm> markup_braced_list_body
508 %type <scm> markup_composed_list
509 %type <scm> markup_command_list
510 %type <scm> markup_command_list_arguments
511 %type <scm> markup_command_basic_arguments
512 %type <scm> markup_head_1_item
513 %type <scm> markup_head_1_list
514 %type <scm> markup_list
515 %type <scm> markup_top
516 %type <scm> maybe_notemode_duration
517 %type <scm> mode_changing_head
518 %type <scm> mode_changing_head_with_context
519 %type <scm> multiplied_duration
520 %type <scm> music_function_event
521 %type <scm> music_function_chord_body
522 %type <scm> new_chord
523 %type <scm> new_lyrics
524 %type <scm> number_expression
525 %type <scm> number_factor
526 %type <scm> number_term
527 %type <scm> octave_check
528 %type <scm> optional_context_mod
529 %type <scm> optional_id
530 %type <scm> optional_notemode_duration
531 %type <scm> pitch
532 %type <scm> pitch_also_in_chords
533 %type <scm> post_events
534 %type <scm> property_operation
535 %type <scm> property_path property_path_revved
536 %type <scm> scalar
537 %type <scm> scalar_closed
538 %type <scm> scm_function_call
539 %type <scm> scm_function_call_closed
540 %type <scm> script_abbreviation
541 %type <scm> simple_chord_elements
542 %type <scm> simple_markup
543 %type <scm> simple_string
544 %type <scm> steno_duration
545 %type <scm> steno_pitch
546 %type <scm> steno_tonic_pitch
547 %type <scm> step_number
548 %type <scm> step_numbers
549 %type <scm> string
550 %type <scm> tempo_range
551
552 %type <score> score_block
553 %type <score> score_body
554
555
556 %left '-' '+'
557
558 /* We don't assign precedence to / and *, because we might need varied
559 prec levels in different prods */
560
561 %left UNARY_MINUS
562
563 %%
564
565 start_symbol:
566         lilypond
567         | EMBEDDED_LILY {
568                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
569                 parser->lexer_->push_note_state (alist_to_hashq (nn));
570         } embedded_lilypond {
571                 parser->lexer_->pop_state ();
572                 parser->lexer_->set_identifier (ly_symbol2scm ("parseStringResult"), $3);
573         }
574         ;
575
576 lilypond:       /* empty */ { }
577         | lilypond toplevel_expression {
578         }
579         | lilypond assignment {
580         }
581         | lilypond error {
582                 parser->error_level_ = 1;
583         }
584         | lilypond INVALID      {
585                 parser->error_level_ = 1;
586         }
587         ;
588
589
590 toplevel_expression:
591         lilypond_header {
592                 parser->lexer_->set_identifier (ly_symbol2scm ("$defaultheader"), $1);
593         }
594         | book_block {
595                 Book *book = $1;
596                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-book-handler");
597                 scm_call_2 (proc, parser->self_scm (), book->self_scm ());
598                 book->unprotect ();
599         }
600         | bookpart_block {
601                 Book *bookpart = $1;
602                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-bookpart-handler");
603                 scm_call_2 (proc, parser->self_scm (), bookpart->self_scm ());
604                 bookpart->unprotect ();
605         }
606         | score_block {
607                 Score *score = $1;
608
609                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-score-handler");
610                 scm_call_2 (proc, parser->self_scm (), score->self_scm ());
611                 score->unprotect ();
612         }
613         | composite_music {
614                 Music *music = unsmob_music ($1);
615                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-music-handler");
616                 scm_call_2 (proc, parser->self_scm (), music->self_scm ());
617         }
618         | full_markup {
619                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-text-handler");
620                 scm_call_2 (proc, parser->self_scm (), scm_list_1 ($1));
621         }
622         | full_markup_list {
623                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-text-handler");
624                 scm_call_2 (proc, parser->self_scm (), $1);
625         }
626         | output_def {
627                 SCM id = SCM_EOL;
628                 Output_def * od = $1;
629
630                 if ($1->c_variable ("is-paper") == SCM_BOOL_T)
631                         id = ly_symbol2scm ("$defaultpaper");
632                 else if ($1->c_variable ("is-midi") == SCM_BOOL_T)
633                         id = ly_symbol2scm ("$defaultmidi");
634                 else if ($1->c_variable ("is-layout") == SCM_BOOL_T)
635                         id = ly_symbol2scm ("$defaultlayout");
636
637                 parser->lexer_->set_identifier (id, od->self_scm ());
638                 od->unprotect();
639         }
640         ;
641
642 embedded_scm_bare:
643         SCM_TOKEN
644         {
645                 $$ = parser->lexer_->eval_scm_token ($1);
646         }
647         | SCM_IDENTIFIER
648         ;
649
650 embedded_scm_bare_arg:
651         embedded_scm_bare
652         | STRING
653         | STRING_IDENTIFIER
654         | full_markup
655         | full_markup_list
656         | context_modification
657         | score_block
658         {
659                 $$ = $1->self_scm ();
660                 $1->unprotect ();
661         }
662         | context_def_spec_block
663         | book_block
664         {
665                 $$ = $1->self_scm ();
666                 $1->unprotect ();
667         }
668         | bookpart_block
669         {
670                 $$ = $1->self_scm ();
671                 $1->unprotect ();
672         }
673         | output_def
674         {
675                 $$ = $1->self_scm ();
676                 $1->unprotect ();
677         }
678         ;
679
680 /* The generic version may end in music, or not */
681
682 embedded_scm:
683         embedded_scm_bare
684         | scm_function_call
685         ;
686
687 embedded_scm_arg:
688         embedded_scm_bare_arg
689         | scm_function_call
690         | music_arg
691         ;
692
693 scm_function_call:
694         SCM_FUNCTION function_arglist {
695                 $$ = MAKE_SYNTAX ("music-function", @$,
696                                          $1, $2);
697         }
698         ;
699
700 embedded_lilypond:
701         /* empty */
702         {
703                 $$ = MAKE_SYNTAX ("void-music", @$);
704         }
705         | identifier_init
706         | music_embedded music_embedded music_list {
707                 $3 = scm_reverse_x ($3, SCM_EOL);
708                 if (unsmob_music ($2))
709                         $3 = scm_cons ($2, $3);
710                 if (unsmob_music ($1))
711                         $3 = scm_cons ($1, $3);
712                 $$ = MAKE_SYNTAX ("sequential-music", @$, $3);
713         }
714         | error {
715                 parser->error_level_ = 1;
716         }
717         | INVALID embedded_lilypond {
718                 parser->error_level_ = 1;
719         }
720         ;
721
722
723 lilypond_header_body:
724         {
725                 $$ = get_header (parser);
726                 parser->lexer_->add_scope ($$);
727         }
728         | lilypond_header_body assignment  {
729
730         }
731         ;
732
733 lilypond_header:
734         HEADER '{' lilypond_header_body '}'     {
735                 $$ = parser->lexer_->remove_scope ();
736         }
737         ;
738
739 /*
740         DECLARATIONS
741 */
742 assignment_id:
743         STRING          { $$ = $1; }
744         | LYRICS_STRING { $$ = $1; }
745         ;
746
747 assignment:
748         assignment_id '=' identifier_init  {
749                 parser->lexer_->set_identifier ($1, $3);
750         }
751         | assignment_id property_path '=' identifier_init {
752                 SCM path = scm_cons (scm_string_to_symbol ($1), $2);
753                 parser->lexer_->set_identifier (path, $4);
754         ;
755 /*
756  TODO: devise standard for protection in parser.
757
758   The parser stack lives on the C-stack, which means that
759 all objects can be unprotected as soon as they're here.
760
761 */
762         }
763         | embedded_scm { }
764         ;
765
766
767 identifier_init:
768         score_block {
769                 $$ = $1->self_scm ();
770                 $1->unprotect ();
771         }
772         | book_block {
773                 $$ = $1->self_scm ();
774                 $1->unprotect ();
775         }
776         | bookpart_block {
777                 $$ = $1->self_scm ();
778                 $1->unprotect ();
779         }
780         | output_def {
781                 $$ = $1->self_scm ();
782                 $1->unprotect ();
783         }
784         | context_def_spec_block {
785                 $$ = $1;
786         }
787         | music_assign  {
788                 $$ = $1;
789         }
790         | post_event_nofinger {
791                 $$ = $1;
792         }
793         | number_expression {
794                 $$ = $1;
795         }
796         | FRACTION {
797                 $$ = $1;
798         }
799         | string {
800                 $$ = $1;
801         }
802         | embedded_scm {
803                 $$ = $1;
804         }
805         | full_markup {
806                 $$ = $1;
807         }
808         | full_markup_list {
809                 $$ = $1;
810         }
811         | context_modification {
812                 $$ = $1;
813         }
814         ;
815
816 context_def_spec_block:
817         CONTEXT '{' context_def_spec_body '}'
818                 {
819                 $$ = $3;
820         }
821         ;
822
823 context_mod_arg:
824         embedded_scm
825         | composite_music
826         ;
827
828 context_mod_embedded:
829         context_mod_arg
830         {
831                 if (unsmob_music ($1)) {
832                         SCM proc = parser->lexer_->lookup_identifier ("context-mod-music-handler");
833                         $1 = scm_call_2 (proc, parser->self_scm (), $1);
834                 }
835                 if (unsmob_context_mod ($1))
836                         $$ = $1;
837                 else {
838                         parser->parser_error (@1, _ ("not a context mod"));
839                 }
840         }
841         ;
842
843
844 context_def_spec_body:
845         /**/ {
846                 $$ = Context_def::make_scm ();
847                 unsmob_context_def ($$)->origin ()->set_spot (@$);
848         }
849         | CONTEXT_DEF_IDENTIFIER {
850                 $$ = $1;
851                 unsmob_context_def ($$)->origin ()->set_spot (@$);
852         }
853         | context_def_spec_body context_mod {
854                 unsmob_context_def ($$)->add_context_mod ($2);
855         }
856         | context_def_spec_body context_modification {
857                 Context_def *td = unsmob_context_def ($$);
858                 SCM new_mods = unsmob_context_mod ($2)->get_mods ();
859                 for (SCM m = new_mods; scm_is_pair (m); m = scm_cdr (m)) {
860                     td->add_context_mod (scm_car (m));
861                 }
862         }
863         | context_def_spec_body context_mod_embedded {
864                 Context_def *td = unsmob_context_def ($$);
865                 SCM new_mods = unsmob_context_mod ($2)->get_mods ();
866                 for (SCM m = new_mods; scm_is_pair (m); m = scm_cdr (m)) {
867                     td->add_context_mod (scm_car (m));
868                 }
869         }
870         ;
871
872
873
874 book_block:
875         BOOK '{' book_body '}'  {
876                 $$ = $3;
877                 pop_paper (parser);
878                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-book"), SCM_BOOL_F);
879         }
880         ;
881
882 /* FIXME:
883    * Use 'handlers' like for toplevel-* stuff?
884    * grok \layout and \midi?  */
885 book_body:
886         {
887                 $$ = new Book;
888                 init_papers (parser);
889                 $$->origin ()->set_spot (@$);
890                 $$->paper_ = dynamic_cast<Output_def*> (unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultpaper"))->clone ());
891                 $$->paper_->unprotect ();
892                 push_paper (parser, $$->paper_);
893                 $$->header_ = parser->lexer_->lookup_identifier ("$defaultheader");
894                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-book"), $$->self_scm ());
895         }
896         | BOOK_IDENTIFIER {
897                 $$ = unsmob_book ($1);
898                 $$->protect ();
899                 $$->origin ()->set_spot (@$);
900                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-book"), $1);
901         }
902         | book_body paper_block {
903                 $$->paper_ = $2;
904                 $2->unprotect ();
905                 set_paper (parser, $2);
906         }
907         | book_body bookpart_block {
908                 Book *bookpart = $2;
909                 SCM proc = parser->lexer_->lookup_identifier ("book-bookpart-handler");
910                 scm_call_2 (proc, $$->self_scm (), bookpart->self_scm ());
911                 bookpart->unprotect ();
912         }
913         | book_body score_block {
914                 Score *score = $2;
915                 SCM proc = parser->lexer_->lookup_identifier ("book-score-handler");
916                 scm_call_2 (proc, $$->self_scm (), score->self_scm ());
917                 score->unprotect ();
918         }
919         | book_body composite_music {
920                 Music *music = unsmob_music ($2);
921                 SCM proc = parser->lexer_->lookup_identifier ("book-music-handler");
922                 scm_call_3 (proc, parser->self_scm (), $$->self_scm (), music->self_scm ());
923         }
924         | book_body full_markup {
925                 SCM proc = parser->lexer_->lookup_identifier ("book-text-handler");
926                 scm_call_2 (proc, $$->self_scm (), scm_list_1 ($2));
927         }
928         | book_body full_markup_list {
929                 SCM proc = parser->lexer_->lookup_identifier ("book-text-handler");
930                 scm_call_2 (proc, $$->self_scm (), $2);
931         }
932         | book_body lilypond_header {
933                 $$->header_ = $2;
934         }
935         | book_body embedded_scm { }
936         | book_body error {
937                 $$->paper_ = 0;
938                 $$->scores_ = SCM_EOL;
939                 $$->bookparts_ = SCM_EOL;
940         }
941         ;
942
943 bookpart_block:
944         BOOKPART '{' bookpart_body '}' {
945                 $$ = $3;
946                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), SCM_BOOL_F);
947         }
948         ;
949
950 bookpart_body:
951         {
952                 $$ = new Book;
953                 $$->origin ()->set_spot (@$);
954                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), $$->self_scm ());
955         }
956         | BOOK_IDENTIFIER {
957                 $$ = unsmob_book ($1);
958                 $$->protect ();
959                 $$->origin ()->set_spot (@$);
960                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), $1);
961         }
962         | bookpart_body paper_block {
963                 $$->paper_ = $2;
964                 $2->unprotect ();
965         }
966         | bookpart_body score_block {
967                 Score *score = $2;
968                 SCM proc = parser->lexer_->lookup_identifier ("bookpart-score-handler");
969                 scm_call_2 (proc, $$->self_scm (), score->self_scm ());
970                 score->unprotect ();
971         }
972         | bookpart_body composite_music {
973                 Music *music = unsmob_music ($2);
974                 SCM proc = parser->lexer_->lookup_identifier ("bookpart-music-handler");
975                 scm_call_3 (proc, parser->self_scm (), $$->self_scm (), music->self_scm ());
976         }
977         | bookpart_body full_markup {
978                 SCM proc = parser->lexer_->lookup_identifier ("bookpart-text-handler");
979                 scm_call_2 (proc, $$->self_scm (), scm_list_1 ($2));
980         }
981         | bookpart_body full_markup_list {
982                 SCM proc = parser->lexer_->lookup_identifier ("bookpart-text-handler");
983                 scm_call_2 (proc, $$->self_scm (), $2);
984         }
985         | bookpart_body lilypond_header {
986                 $$->header_ = $2;
987         }
988         | bookpart_body embedded_scm { }
989         | bookpart_body error {
990                 $$->paper_ = 0;
991                 $$->scores_ = SCM_EOL;
992         }
993         ;
994
995 score_block:
996         SCORE '{' score_body '}'        {
997                 $$ = $3;
998         }
999         ;
1000
1001 score_body:
1002         music {
1003                 SCM m = $1;
1004                 SCM scorify = ly_lily_module_constant ("scorify-music");
1005                 SCM score = scm_call_2 (scorify, m, parser->self_scm ());
1006
1007                 // pass ownernship to C++ again.
1008                 $$ = unsmob_score (score);
1009                 $$->protect ();
1010                 $$->origin ()->set_spot (@$);
1011         }
1012         | SCORE_IDENTIFIER {
1013                 $$ = unsmob_score ($1);
1014                 $$->protect ();
1015                 $$->origin ()->set_spot (@$);
1016         }
1017         | score_body lilypond_header    {
1018                 $$->set_header ($2);
1019         }
1020         | score_body output_def {
1021                 if ($2->lookup_variable (ly_symbol2scm ("is-paper")) == SCM_BOOL_T)
1022                 {
1023                         parser->parser_error (@2, _("\\paper cannot be used in \\score, use \\layout instead"));
1024
1025                 }
1026                 else
1027                 {
1028                         $$->add_output_def ($2);
1029                 }
1030                 $2->unprotect ();
1031         }
1032         | score_body error {
1033                 $$->error_found_ = true;
1034         }
1035         ;
1036
1037
1038 /*
1039         OUTPUT DEF
1040 */
1041
1042 paper_block:
1043         output_def {
1044                 $$ = $1;
1045                 if ($$->lookup_variable (ly_symbol2scm ("is-paper")) != SCM_BOOL_T)
1046                 {
1047                         parser->parser_error (@1, _ ("need \\paper for paper block"));
1048                         $1->unprotect ();
1049                         $$ = get_paper (parser);
1050                 }
1051         }
1052         ;
1053
1054
1055 output_def:
1056         output_def_body '}' {
1057                 $$ = $1;
1058
1059                 parser->lexer_->remove_scope ();
1060                 parser->lexer_->pop_state ();
1061         }
1062         ;
1063
1064 output_def_head:
1065         PAPER {
1066                 $$ = get_paper (parser);
1067                 $$->input_origin_ = @$;
1068                 parser->lexer_->add_scope ($$->scope_);
1069         }
1070         | MIDI    {
1071                 Output_def *p = get_midi (parser);
1072                 $$ = p;
1073                 parser->lexer_->add_scope (p->scope_);
1074         }
1075         | LAYOUT        {
1076                 Output_def *p = get_layout (parser);
1077
1078                 parser->lexer_->add_scope (p->scope_);
1079                 $$ = p;
1080         }
1081         ;
1082
1083 output_def_head_with_mode_switch:
1084         output_def_head {
1085                 parser->lexer_->push_initial_state ();
1086                 $$ = $1;
1087         }
1088         ;
1089
1090 output_def_body:
1091         output_def_head_with_mode_switch '{' {
1092                 $$ = $1;
1093                 $$->input_origin_.set_spot (@$);
1094         }
1095         | output_def_head_with_mode_switch '{' OUTPUT_DEF_IDENTIFIER    {
1096                 $1->unprotect ();
1097
1098                 Output_def *o = unsmob_output_def ($3);
1099                 o->input_origin_.set_spot (@$);
1100                 $$ = o;
1101                 $$->protect ();
1102                 parser->lexer_->remove_scope ();
1103                 parser->lexer_->add_scope (o->scope_);
1104         }
1105         | output_def_body assignment  {
1106
1107         }
1108         | output_def_body context_def_spec_block        {
1109                 assign_context_def ($$, $2);
1110         }
1111         | output_def_body music_arg {
1112                 SCM proc = parser->lexer_->lookup_identifier ("output-def-music-handler");
1113                 scm_call_3 (proc, parser->self_scm (), $1->self_scm (), $2);
1114         }
1115         | output_def_body error {
1116
1117         }
1118         ;
1119
1120 tempo_event:
1121         TEMPO steno_duration '=' tempo_range    {
1122                 $$ = MAKE_SYNTAX ("tempo", @$, SCM_EOL, $2, $4);
1123         }
1124         | TEMPO scalar_closed steno_duration '=' tempo_range    {
1125                 $$ = MAKE_SYNTAX ("tempo", @$, $2, $3, $5);
1126         }
1127         | TEMPO scalar {
1128                 $$ = MAKE_SYNTAX ("tempo", @$, $2);
1129         }
1130         ;
1131
1132 /*
1133 The representation of a  list is reversed to have efficient append.  */
1134
1135 music_list:
1136         /* empty */ {
1137                 $$ = SCM_EOL;
1138         }
1139         | music_list music_embedded {
1140                 if (unsmob_music ($2))
1141                         $$ = scm_cons ($2, $1);
1142         }
1143         | music_list error {
1144                 Music *m = MY_MAKE_MUSIC("Music", @$);
1145                 // ugh. code dup
1146                 m->set_property ("error-found", SCM_BOOL_T);
1147                 $$ = scm_cons (m->self_scm (), $1);
1148                 m->unprotect (); /* UGH */
1149         }
1150         ;
1151
1152 braced_music_list:
1153         '{' music_list '}'
1154         {
1155                 $$ = scm_reverse_x ($2, SCM_EOL);
1156         }
1157         ;
1158
1159 music:  music_arg
1160         | lyric_element_music
1161         ;
1162
1163 music_embedded:
1164         music
1165         | embedded_scm
1166         {
1167                 if (unsmob_music ($1)
1168                     || scm_is_eq ($1, SCM_UNSPECIFIED))
1169                         $$ = $1;
1170                 else
1171                 {
1172                         @$.warning (_ ("Ignoring non-music expression"));
1173                         $$ = SCM_UNSPECIFIED;
1174                 }
1175         }
1176         ;
1177
1178 music_arg:
1179         simple_music
1180         {
1181                 if (unsmob_pitch ($1)) {
1182                         Music *n = MY_MAKE_MUSIC ("NoteEvent", @1);
1183                         n->set_property ("pitch", $1);
1184                         n->set_property ("duration",
1185                                          parser->default_duration_.smobbed_copy ());
1186                         $$ = n->unprotect ();
1187                 }
1188         }
1189         | composite_music %prec COMPOSITE
1190         ;
1191
1192 music_assign:
1193         simple_music
1194         | composite_music %prec COMPOSITE
1195         | lyric_element_music
1196         ;
1197
1198 repeated_music:
1199         REPEAT simple_string unsigned_number music
1200         {
1201                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, SCM_EOL);
1202         }
1203         | REPEAT simple_string unsigned_number music ALTERNATIVE braced_music_list
1204         {
1205                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, $6);
1206         }
1207         ;
1208
1209 sequential_music:
1210         SEQUENTIAL braced_music_list {
1211                 $$ = MAKE_SYNTAX ("sequential-music", @$, $2);
1212         }
1213         | braced_music_list {
1214                 $$ = MAKE_SYNTAX ("sequential-music", @$, $1);
1215         }
1216         ;
1217
1218 simultaneous_music:
1219         SIMULTANEOUS braced_music_list {
1220                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, $2);
1221         }
1222         | DOUBLE_ANGLE_OPEN music_list DOUBLE_ANGLE_CLOSE       {
1223                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, scm_reverse_x ($2, SCM_EOL));
1224         }
1225         ;
1226
1227 simple_music:
1228         event_chord
1229         | music_property_def
1230         | context_change
1231         ;
1232
1233 context_modification:
1234         WITH { parser->lexer_->push_initial_state (); } '{' context_mod_list '}'
1235         {
1236                 parser->lexer_->pop_state ();
1237                 $$ = $4;
1238         }
1239         | WITH CONTEXT_MOD_IDENTIFIER
1240         {
1241                 $$ = $2;
1242         }
1243         | CONTEXT_MOD_IDENTIFIER
1244         {
1245                 $$ = $1;
1246         }
1247         | WITH embedded_scm_closed
1248         {
1249                 if (unsmob_context_mod ($2))
1250                         $$ = $2;
1251                 else {
1252                         parser->parser_error (@2, _ ("not a context mod"));
1253                         $$ = Context_mod ().smobbed_copy ();
1254                 }
1255         }
1256         ;
1257
1258 optional_context_mod:
1259         /**/ {
1260             $$ = SCM_EOL;
1261         }
1262         | context_modification
1263         {
1264               $$ = $1;
1265         }
1266         ;
1267
1268 context_mod_list:
1269         /**/ {
1270             $$ = Context_mod ().smobbed_copy ();
1271         }
1272         | context_mod_list context_mod  {
1273                  unsmob_context_mod ($1)->add_context_mod ($2);
1274         }
1275         | context_mod_list CONTEXT_MOD_IDENTIFIER {
1276                  Context_mod *md = unsmob_context_mod ($2);
1277                  if (md)
1278                      unsmob_context_mod ($1)->add_context_mods (md->get_mods ());
1279         }
1280         | context_mod_list context_mod_embedded {
1281                 unsmob_context_mod ($1)->add_context_mods
1282                         (unsmob_context_mod ($2)->get_mods ());
1283         }
1284         ;
1285
1286 composite_music:
1287         complex_music
1288         | music_bare
1289         ;
1290
1291 /* Music that can be parsed without lookahead */
1292 closed_music:
1293         music_bare
1294         | complex_music_prefix closed_music
1295         {
1296                 $$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
1297         }
1298         ;
1299
1300 music_bare:
1301         mode_changed_music
1302         | MUSIC_IDENTIFIER
1303         | grouped_music_list
1304         ;
1305
1306 grouped_music_list:
1307         simultaneous_music              { $$ = $1; }
1308         | sequential_music              { $$ = $1; }
1309         ;
1310
1311 /* An argument list. If a function \foo expects scm scm pitch, then the lexer expands \foo into the token sequence:
1312  MUSIC_FUNCTION EXPECT_PITCH EXPECT_SCM EXPECT_SCM EXPECT_NO_MORE_ARGS
1313 and this rule returns the reversed list of arguments. */
1314
1315 function_arglist_skip:
1316         function_arglist_common
1317         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_skip
1318         {
1319                 $$ = scm_cons ($1, $3);
1320         } %prec FUNCTION_ARGLIST
1321         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_skip
1322         {
1323                 $$ = scm_cons ($1, $3);
1324         } %prec FUNCTION_ARGLIST
1325         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip
1326         {
1327                 $$ = scm_cons ($1, $3);
1328         } %prec FUNCTION_ARGLIST
1329         ;
1330
1331
1332 function_arglist_nonbackup_common:
1333         EXPECT_OPTIONAL EXPECT_PITCH function_arglist pitch_also_in_chords {
1334                 $$ = scm_cons ($4, $3);
1335         }
1336         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_closed duration_length {
1337                 $$ = scm_cons ($4, $3);
1338         }
1339         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed FRACTION
1340         {
1341                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1342         }
1343         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed post_event_nofinger
1344         {
1345                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1346         }
1347         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed '-' UNSIGNED
1348         {
1349                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1350                 if (scm_is_true (scm_call_1 ($2, n)))
1351                         $$ = scm_cons (n, $3);
1352                 else {
1353                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1354                         t->set_property ("digit", $5);
1355                         $$ = t->unprotect ();
1356                         if (scm_is_true (scm_call_1 ($2, $$)))
1357                                 $$ = scm_cons ($$, $3);
1358                         else
1359                                 $$ = check_scheme_arg (parser, @4, n, $3, $2);
1360                 }
1361                 
1362         }
1363         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed '-' REAL
1364         {
1365                 $$ = check_scheme_arg (parser, @4,
1366                                        scm_difference ($5, SCM_UNDEFINED),
1367                                        $3, $2);
1368         }
1369         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed '-' NUMBER_IDENTIFIER
1370         {
1371                 $$ = check_scheme_arg (parser, @4,
1372                                        scm_difference ($5, SCM_UNDEFINED),
1373                                        $3, $2);
1374         }
1375         ;
1376
1377 function_arglist_closed_nonbackup:
1378         function_arglist_nonbackup_common
1379         | EXPECT_OPTIONAL EXPECT_SCM function_arglist embedded_scm_arg_closed
1380         {
1381                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1382         }
1383         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed bare_number_closed
1384         {
1385                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1386         }
1387         ;
1388
1389 function_arglist_nonbackup:
1390         function_arglist_nonbackup_common
1391         | EXPECT_OPTIONAL EXPECT_SCM function_arglist embedded_scm_arg
1392         {
1393                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1394         }
1395         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed bare_number
1396         {
1397                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1398         }
1399         ;
1400
1401 function_arglist_keep:
1402         function_arglist_common
1403         | function_arglist_backup
1404         ;
1405
1406 function_arglist_closed_keep:
1407         function_arglist_closed_common
1408         | function_arglist_backup
1409         ;
1410
1411 function_arglist_backup:
1412         EXPECT_OPTIONAL EXPECT_SCM function_arglist_keep embedded_scm_arg_closed
1413         {
1414                 if (scm_is_true (scm_call_1 ($2, $4)))
1415                 {
1416                         $$ = scm_cons ($4, $3);
1417                 } else {
1418                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1419                         MYBACKUP (SCM_IDENTIFIER, $4, @4);
1420                 }
1421         }
1422         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep post_event_nofinger
1423         {
1424                 if (scm_is_true (scm_call_1 ($2, $4)))
1425                 {
1426                         $$ = scm_cons ($4, $3);
1427                 } else {
1428                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1429                         MYBACKUP (EVENT_IDENTIFIER, $4, @4);
1430                 }
1431         }
1432         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_keep lyric_element
1433         {
1434                 // There is no point interpreting a lyrics string as
1435                 // an event, since we don't allow music possibly
1436                 // followed by durations or postevent into closed
1437                 // music, and we only accept closed music in optional
1438                 // arguments at the moment.  If this changes, more
1439                 // complex schemes might become interesting here as
1440                 // well: see how we do this at the mandatory argument
1441                 // point.
1442                 if (scm_is_true (scm_call_1 ($2, $4)))
1443                         $$ = scm_cons ($4, $3);
1444                 else {
1445                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1446                         MYBACKUP (LYRICS_STRING, $4, @4);
1447                 }
1448         }
1449         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep UNSIGNED
1450         {
1451                 if (scm_is_true (scm_call_1 ($2, $4)))
1452                 {
1453                         $$ = $3;
1454                         MYREPARSE (@4, $2, UNSIGNED, $4);
1455                 } else {
1456                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1457                         MYBACKUP (UNSIGNED, $4, @4);
1458                 }
1459         }
1460         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep REAL
1461         {
1462                 if (scm_is_true (scm_call_1 ($2, $4)))
1463                 {
1464                         $$ = $3;
1465                         MYREPARSE (@4, $2, REAL, $4);
1466                 } else {
1467                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1468                         MYBACKUP (REAL, $4, @4);
1469                 }
1470         }
1471         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep NUMBER_IDENTIFIER
1472         {
1473                 if (scm_is_true (scm_call_1 ($2, $4)))
1474                 {
1475                         $$ = scm_cons ($4, $3);
1476                 } else {
1477                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1478                         MYBACKUP (NUMBER_IDENTIFIER, $4, @4);
1479                 }
1480         }
1481         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep FRACTION
1482         {
1483                 if (scm_is_true (scm_call_1 ($2, $4)))
1484                 {
1485                         $$ = scm_cons ($4, $3);
1486                 } else {
1487                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1488                         MYBACKUP (FRACTION, $4, @4);
1489                 }
1490         }
1491         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep '-' UNSIGNED
1492         {
1493                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1494                 if (scm_is_true (scm_call_1 ($2, n))) {
1495                         $$ = $3;
1496                         MYREPARSE (@5, $2, REAL, n);
1497                 } else {
1498                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1499                         t->set_property ("digit", $5);
1500                         $$ = t->unprotect ();
1501                         if (scm_is_true (scm_call_1 ($2, $$)))
1502                                 $$ = scm_cons ($$, $3);
1503                         else {
1504                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1505                                 MYBACKUP (UNSIGNED, $5, @5);
1506                                 parser->lexer_->push_extra_token ('-');
1507                         }
1508                 }
1509                 
1510         }
1511         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep '-' REAL
1512         {
1513                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1514                 if (scm_is_true (scm_call_1 ($2, n))) {
1515                         MYREPARSE (@5, $2, REAL, n);
1516                         $$ = $3;
1517                 } else {
1518                         MYBACKUP (REAL, n, @5);
1519                 }
1520         }
1521         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep '-' NUMBER_IDENTIFIER
1522         {
1523                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1524                 if (scm_is_true (scm_call_1 ($2, n))) {
1525                         $$ = scm_cons (n, $3);
1526                 } else {
1527                         MYBACKUP (NUMBER_IDENTIFIER, n, @5);
1528                 }
1529         }
1530         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_keep pitch_also_in_chords
1531         {
1532                 $$ = scm_cons ($4, $3);
1533         }
1534         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_closed_keep duration_length
1535         {
1536                 $$ = scm_cons ($4, $3);
1537         }
1538         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup BACKUP
1539         {
1540                 $$ = scm_cons ($1, $3);
1541                 MYBACKUP(0, SCM_UNDEFINED, @3);
1542         }
1543         | function_arglist_backup REPARSE embedded_scm_arg_closed
1544         {
1545                 $$ = check_scheme_arg (parser, @3,
1546                                        $3, $1, $2);
1547         }
1548         | function_arglist_backup REPARSE bare_number
1549         {
1550                 $$ = check_scheme_arg (parser, @3,
1551                                        $3, $1, $2);
1552         }
1553         | function_arglist_backup REPARSE fraction
1554         {
1555                 $$ = check_scheme_arg (parser, @3,
1556                                        $3, $1, $2);
1557         }
1558         ;
1559
1560 function_arglist:
1561         function_arglist_common
1562         | function_arglist_nonbackup
1563         ;
1564
1565 function_arglist_common:
1566         function_arglist_bare
1567         | EXPECT_SCM function_arglist_optional embedded_scm_arg
1568         {
1569                 $$ = check_scheme_arg (parser, @3,
1570                                        $3, $2, $1);
1571         }
1572         | EXPECT_SCM function_arglist_closed_optional bare_number
1573         {
1574                 $$ = check_scheme_arg (parser, @3,
1575                                        $3, $2, $1);
1576         }
1577         | EXPECT_SCM function_arglist_closed_optional fraction
1578         {
1579                 $$ = check_scheme_arg (parser, @3,
1580                                        $3, $2, $1);
1581         }
1582         | EXPECT_SCM function_arglist_closed_optional post_event_nofinger
1583         {
1584                 $$ = check_scheme_arg (parser, @3,
1585                                        $3, $2, $1);
1586         }
1587         | function_arglist_common_minus
1588         | function_arglist_common_lyric
1589         ;
1590
1591 function_arglist_common_lyric:
1592         EXPECT_SCM function_arglist_optional lyric_element
1593         {
1594                 // We check how the predicate thinks about a lyrics
1595                 // event or about a markup.  If it accepts neither, we
1596                 // backup the original token.  Otherwise we commit to
1597                 // taking the token.  Depending on what the predicate
1598                 // is willing to accept, we interpret as a string, as
1599                 // a lyric event, or ambiguously (meaning that if
1600                 // something looking like a duration or post event
1601                 // follows, we take the event, otherwise the string).
1602                 SCM lyric_event = MAKE_SYNTAX ("lyric-event", @3, $3,
1603                                                parser->default_duration_.smobbed_copy ());
1604                 if (scm_is_true (scm_call_1 ($1, $3)))
1605                         if (scm_is_true (scm_call_1 ($1, lyric_event)))
1606                         {
1607                                 $$ = $2;
1608                                 MYREPARSE (@3, $1, LYRICS_STRING, $3);
1609                         } else {
1610                                 $$ = scm_cons ($3, $2);
1611                         }
1612                 else if (scm_is_true (scm_call_1 ($1, lyric_event)))
1613                 {
1614                         $$ = $2;
1615                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1616                 } else {
1617                         // This is going to flag a syntax error, we
1618                         // know the predicate to be false.
1619                         check_scheme_arg (parser, @3,
1620                                           $3, $2, $1);
1621                 }
1622         }
1623         | function_arglist_common_lyric REPARSE lyric_element_arg
1624         {
1625                 // This should never be false
1626                 $$ = check_scheme_arg (parser, @3,
1627                                        $3, $1, $2);
1628         }
1629         ;
1630
1631 function_arglist_common_minus:
1632         EXPECT_SCM function_arglist_closed_optional '-' UNSIGNED
1633         {
1634                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1635                 if (scm_is_true (scm_call_1 ($1, n))) {
1636                         $$ = $2;
1637                         MYREPARSE (@4, $1, REAL, n);
1638                 } else {
1639                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @4);
1640                         t->set_property ("digit", $4);
1641                         $$ = t->unprotect ();
1642                         if (scm_is_true (scm_call_1 ($1, $$)))
1643                                 $$ = scm_cons ($$, $2);
1644                         else
1645                                 $$ = check_scheme_arg (parser, @3, n, $2, $1);
1646                 }
1647                 
1648         }
1649         | EXPECT_SCM function_arglist_closed_optional '-' REAL
1650         {
1651                 $$ = $2;
1652                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1653                 MYREPARSE (@4, $1, REAL, n);
1654         }
1655         | EXPECT_SCM function_arglist_closed_optional '-' NUMBER_IDENTIFIER
1656         {
1657                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1658                 $$ = check_scheme_arg (parser, @4, n, $2, $1);
1659         }
1660         | function_arglist_common_minus REPARSE bare_number
1661         {
1662                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1663         }
1664         ;
1665
1666 function_arglist_closed:
1667         function_arglist_closed_common
1668         | function_arglist_closed_nonbackup
1669         ;
1670
1671 function_arglist_closed_common:
1672         function_arglist_bare
1673         | EXPECT_SCM function_arglist_optional embedded_scm_arg_closed
1674         {
1675                 $$ = check_scheme_arg (parser, @3,
1676                                        $3, $2, $1);
1677         }
1678         | EXPECT_SCM function_arglist_closed_optional bare_number
1679         {
1680                 $$ = check_scheme_arg (parser, @3,
1681                                        $3, $2, $1);
1682         }
1683         | EXPECT_SCM function_arglist_closed_optional '-' UNSIGNED
1684         {
1685                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1686                 if (scm_is_true (scm_call_1 ($1, n))) {
1687                         $$ = scm_cons (n, $2);
1688                 } else {
1689                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @4);
1690                         t->set_property ("digit", $4);
1691                         $$ = t->unprotect ();
1692                         if (scm_is_true (scm_call_1 ($1, $$)))
1693                                 $$ = scm_cons ($$, $2);
1694                         else
1695                                 $$ = check_scheme_arg (parser, @3, n, $2, $1);
1696                 }
1697                 
1698         }
1699         | EXPECT_SCM function_arglist_closed_optional '-' REAL
1700         {
1701                 $$ = check_scheme_arg (parser, @3,
1702                                        scm_difference ($4, SCM_UNDEFINED),
1703                                        $2, $1);
1704         }
1705         | EXPECT_SCM function_arglist_closed_optional '-' NUMBER_IDENTIFIER
1706         {
1707                 $$ = check_scheme_arg (parser, @3,
1708                                        scm_difference ($4, SCM_UNDEFINED),
1709                                        $2, $1);
1710         }
1711         | EXPECT_SCM function_arglist_closed_optional post_event_nofinger
1712         {
1713                 $$ = check_scheme_arg (parser, @3,
1714                                        $3, $2, $1);
1715         }
1716         | EXPECT_SCM function_arglist_closed_optional fraction
1717         {
1718                 $$ = check_scheme_arg (parser, @3,
1719                                        $3, $2, $1);
1720         }
1721         | EXPECT_SCM function_arglist_optional lyric_element
1722         {
1723                 $$ = check_scheme_arg (parser, @3,
1724                                        $3, $2, $1);
1725         }
1726         ;
1727
1728 function_arglist_optional:
1729         function_arglist_keep %prec FUNCTION_ARGLIST
1730         | function_arglist_backup BACKUP
1731         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_optional
1732         {
1733                 $$ = scm_cons ($1, $3);
1734         }
1735         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_optional
1736         {
1737                 $$ = scm_cons ($1, $3);
1738         }
1739         ;
1740
1741 function_arglist_closed_optional:
1742         function_arglist_closed_keep %prec FUNCTION_ARGLIST
1743         | function_arglist_backup BACKUP
1744         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_closed_optional
1745         {
1746                 $$ = scm_cons ($1, $3);
1747         }
1748         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_closed_optional
1749         {
1750                 $$ = scm_cons ($1, $3);
1751         }
1752         ;
1753
1754 embedded_scm_closed:
1755         embedded_scm_bare
1756         | scm_function_call_closed
1757         ;
1758
1759 embedded_scm_arg_closed:
1760         embedded_scm_bare_arg
1761         | scm_function_call_closed
1762         | closed_music
1763         ;
1764
1765 scm_function_call_closed:
1766         SCM_FUNCTION function_arglist_closed {
1767                 $$ = MAKE_SYNTAX ("music-function", @$,
1768                                          $1, $2);
1769         } %prec FUNCTION_ARGLIST
1770         ;
1771
1772 function_arglist_bare:
1773         EXPECT_NO_MORE_ARGS {
1774                 $$ = SCM_EOL;
1775         }
1776         | EXPECT_PITCH function_arglist_optional pitch_also_in_chords {
1777                 $$ = scm_cons ($3, $2);
1778         }
1779         | EXPECT_DURATION function_arglist_closed_optional duration_length {
1780                 $$ = scm_cons ($3, $2);
1781         }
1782         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_skip DEFAULT {
1783                 $$ = scm_cons ($1, $3);
1784         }
1785         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_skip DEFAULT {
1786                 $$ = scm_cons ($1, $3);
1787         }
1788         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip DEFAULT {
1789                 $$ = scm_cons ($1, $3);
1790         }
1791         ;
1792
1793 music_function_call:
1794         MUSIC_FUNCTION function_arglist {
1795                 $$ = MAKE_SYNTAX ("music-function", @$,
1796                                          $1, $2);
1797         }
1798         ;
1799
1800
1801 optional_id:
1802         /**/ { $$ = SCM_EOL; }
1803         | '=' simple_string {
1804                 $$ = $2;
1805         }
1806         ;
1807
1808 complex_music:
1809         music_function_call
1810         | repeated_music                { $$ = $1; }
1811         | re_rhythmed_music     { $$ = $1; }
1812         | complex_music_prefix music
1813         {
1814                 $$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
1815         }
1816         ;
1817
1818 complex_music_prefix:
1819         CONTEXT simple_string optional_id optional_context_mod {
1820                 Context_mod *ctxmod = unsmob_context_mod ($4);
1821                 SCM mods = SCM_EOL;
1822                 if (ctxmod)
1823                         mods = ctxmod->get_mods ();
1824                 $$ = START_MAKE_SYNTAX ("context-specification", $2, $3, mods, SCM_BOOL_F);
1825         }
1826         | NEWCONTEXT simple_string optional_id optional_context_mod {
1827                 Context_mod *ctxmod = unsmob_context_mod ($4);
1828                 SCM mods = SCM_EOL;
1829                 if (ctxmod)
1830                         mods = ctxmod->get_mods ();
1831                 $$ = START_MAKE_SYNTAX ("context-specification", $2, $3, mods, SCM_BOOL_T);
1832         }
1833         ;
1834
1835 mode_changed_music:
1836         mode_changing_head grouped_music_list {
1837                 if ($1 == ly_symbol2scm ("chords"))
1838                 {
1839                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $2);
1840                 }
1841                 else
1842                 {
1843                   $$ = $2;
1844                 }
1845                 parser->lexer_->pop_state ();
1846         }
1847         | mode_changing_head_with_context optional_context_mod grouped_music_list {
1848                 Context_mod *ctxmod = unsmob_context_mod ($2);
1849                 SCM mods = SCM_EOL;
1850                 if (ctxmod)
1851                         mods = ctxmod->get_mods ();
1852                 $$ = MAKE_SYNTAX ("context-specification", @$, $1, SCM_EOL, mods, SCM_BOOL_T, $3);
1853                 if ($1 == ly_symbol2scm ("ChordNames"))
1854                 {
1855                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $$);
1856                 }
1857                 parser->lexer_->pop_state ();
1858         }
1859         ;
1860
1861 mode_changing_head:
1862         NOTEMODE {
1863                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
1864                 parser->lexer_->push_note_state (alist_to_hashq (nn));
1865
1866                 $$ = ly_symbol2scm ("notes");
1867         }
1868         | DRUMMODE
1869                 {
1870                 SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
1871                 parser->lexer_->push_note_state (alist_to_hashq (nn));
1872
1873                 $$ = ly_symbol2scm ("drums");
1874         }
1875         | FIGUREMODE {
1876                 parser->lexer_->push_figuredbass_state ();
1877
1878                 $$ = ly_symbol2scm ("figures");
1879         }
1880         | CHORDMODE {
1881                 SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
1882                 parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
1883                 nn = parser->lexer_->lookup_identifier ("pitchnames");
1884                 parser->lexer_->push_chord_state (alist_to_hashq (nn));
1885                 $$ = ly_symbol2scm ("chords");
1886
1887         }
1888         | LYRICMODE
1889                 { parser->lexer_->push_lyric_state ();
1890                 $$ = ly_symbol2scm ("lyrics");
1891         }
1892         ;
1893
1894 mode_changing_head_with_context:
1895         DRUMS {
1896                 SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
1897                 parser->lexer_->push_note_state (alist_to_hashq (nn));
1898
1899                 $$ = ly_symbol2scm ("DrumStaff");
1900         }
1901         | FIGURES {
1902                 parser->lexer_->push_figuredbass_state ();
1903
1904                 $$ = ly_symbol2scm ("FiguredBass");
1905         }
1906         | CHORDS {
1907                 SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
1908                 parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
1909                 nn = parser->lexer_->lookup_identifier ("pitchnames");
1910                 parser->lexer_->push_chord_state (alist_to_hashq (nn));
1911                 $$ = ly_symbol2scm ("ChordNames");
1912         }
1913         | LYRICS
1914                 { parser->lexer_->push_lyric_state ();
1915                 $$ = ly_symbol2scm ("Lyrics");
1916         }
1917         ;
1918
1919 new_lyrics:
1920         ADDLYRICS { parser->lexer_->push_lyric_state (); }
1921         /*cont */
1922         composite_music {
1923         /* Can also use music at the expensive of two S/Rs similar to
1924            \repeat \alternative */
1925                 parser->lexer_->pop_state ();
1926
1927                 $$ = scm_cons ($3, SCM_EOL);
1928         }
1929         | new_lyrics ADDLYRICS {
1930                 parser->lexer_->push_lyric_state ();
1931         } composite_music {
1932                 parser->lexer_->pop_state ();
1933                 $$ = scm_cons ($4, $1);
1934         }
1935         ;
1936
1937 re_rhythmed_music:
1938         composite_music new_lyrics {
1939                 $$ = MAKE_SYNTAX ("add-lyrics", @$, $1, scm_reverse_x ($2, SCM_EOL));
1940         } %prec COMPOSITE
1941         | LYRICSTO simple_string {
1942                 parser->lexer_->push_lyric_state ();
1943         } music {
1944                 parser->lexer_->pop_state ();
1945                 $$ = MAKE_SYNTAX ("lyric-combine", @$, $2, $4);
1946         }
1947         ;
1948
1949 context_change:
1950         CHANGE STRING '=' STRING  {
1951                 $$ = MAKE_SYNTAX ("context-change", @$, scm_string_to_symbol ($2), $4);
1952         }
1953         ;
1954
1955
1956 property_path_revved:
1957         embedded_scm_closed {
1958                 $$ = scm_cons ($1, SCM_EOL);
1959         }
1960         | property_path_revved embedded_scm_closed {
1961                 $$ = scm_cons ($2, $1);
1962         }
1963         ;
1964
1965 property_path:
1966         property_path_revved  {
1967                 $$ = scm_reverse_x ($1, SCM_EOL);
1968         }
1969         ;
1970
1971 property_operation:
1972         STRING '=' scalar {
1973                 $$ = scm_list_3 (ly_symbol2scm ("assign"),
1974                         scm_string_to_symbol ($1), $3);
1975         }
1976         | UNSET simple_string {
1977                 $$ = scm_list_2 (ly_symbol2scm ("unset"),
1978                         scm_string_to_symbol ($2));
1979         }
1980         | OVERRIDE simple_string property_path '=' scalar {
1981                 $$ = scm_append (scm_list_2 (scm_list_3 (ly_symbol2scm ("push"),
1982                                                         scm_string_to_symbol ($2), $5),
1983                                              $3));
1984         }
1985         | REVERT simple_string embedded_scm {
1986                 $$ = scm_list_3 (ly_symbol2scm ("pop"),
1987                         scm_string_to_symbol ($2), $3);
1988         }
1989         ;
1990
1991 context_def_mod:
1992         CONSISTS { $$ = ly_symbol2scm ("consists"); }
1993         | REMOVE { $$ = ly_symbol2scm ("remove"); }
1994
1995         | ACCEPTS { $$ = ly_symbol2scm ("accepts"); }
1996         | DEFAULTCHILD { $$ = ly_symbol2scm ("default-child"); }
1997         | DENIES { $$ = ly_symbol2scm ("denies"); }
1998
1999         | ALIAS { $$ = ly_symbol2scm ("alias"); }
2000         | TYPE { $$ = ly_symbol2scm ("translator-type"); }
2001         | DESCRIPTION { $$ = ly_symbol2scm ("description"); }
2002         | NAME { $$ = ly_symbol2scm ("context-name"); }
2003         ;
2004
2005 context_mod:
2006         property_operation { $$ = $1; }
2007         | context_def_mod STRING {
2008                 $$ = scm_list_2 ($1, $2);
2009         }
2010         | context_def_mod embedded_scm
2011         {
2012                 if (!scm_is_string ($2)
2013                     && ly_symbol2scm ("consists") != $1
2014                     && ly_symbol2scm ("remove") != $1)
2015                 {
2016                         $$ = SCM_EOL;
2017                         parser->parser_error (@1, _ ("only \\consists and \\remove take non-string argument."));
2018                 }
2019                 else
2020                 {
2021                         $$ = scm_list_2 ($1, $2);
2022                 }
2023         }
2024         ;
2025
2026 context_prop_spec:
2027         simple_string {
2028                 if (!is_regular_identifier ($1))
2029                 {
2030                         @$.error (_("Grob name should be alphanumeric"));
2031                 }
2032
2033                 $$ = scm_list_2 (ly_symbol2scm ("Bottom"),
2034                         scm_string_to_symbol ($1));
2035         }
2036         | simple_string '.' simple_string {
2037                 $$ = scm_list_2 (scm_string_to_symbol ($1),
2038                         scm_string_to_symbol ($3));
2039         }
2040         ;
2041
2042 simple_music_property_def:
2043         OVERRIDE context_prop_spec property_path '=' scalar {
2044                 $$ = scm_append (scm_list_2 (scm_list_n (scm_car ($2),
2045                                 ly_symbol2scm ("OverrideProperty"),
2046                                 scm_cadr ($2),
2047                                 $5, SCM_UNDEFINED),
2048                                 $3));
2049         }
2050         | REVERT context_prop_spec embedded_scm {
2051                 $$ = scm_list_4 (scm_car ($2),
2052                         ly_symbol2scm ("RevertProperty"),
2053                         scm_cadr ($2),
2054                         $3);
2055         }
2056         | SET context_prop_spec '=' scalar {
2057                 $$ = scm_list_4 (scm_car ($2),
2058                         ly_symbol2scm ("PropertySet"),
2059                         scm_cadr ($2),
2060                         $4);
2061         }
2062         | UNSET context_prop_spec {
2063                 $$ = scm_list_3 (scm_car ($2),
2064                         ly_symbol2scm ("PropertyUnset"),
2065                         scm_cadr ($2));
2066         }
2067         ;
2068
2069 music_property_def:
2070         simple_music_property_def {
2071                 $$ = LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("property-operation"), scm_cons2 (parser->self_scm (), make_input (@$), $1));
2072         }
2073         ;
2074
2075 string:
2076         STRING {
2077                 $$ = $1;
2078         }
2079         | STRING_IDENTIFIER {
2080                 $$ = $1;
2081         }
2082         | string '+' string {
2083                 $$ = scm_string_append (scm_list_2 ($1, $3));
2084         }
2085         ;
2086
2087 simple_string: STRING {
2088                 $$ = $1;
2089         }
2090         | LYRICS_STRING {
2091                 $$ = $1;
2092         }
2093         | STRING_IDENTIFIER {
2094                 $$ = $1;
2095         }
2096         ;
2097
2098 scalar:
2099         embedded_scm_arg
2100         | bare_number
2101         | FRACTION
2102         | lyric_element
2103         ;
2104
2105 scalar_closed:
2106         embedded_scm_arg_closed
2107         | bare_number
2108         | FRACTION
2109         | lyric_element
2110         ;
2111
2112
2113 event_chord:
2114         simple_element post_events {
2115                 // Let the rhythmic music iterator sort this mess out.
2116                 if (scm_is_pair ($2)) {
2117                         if (unsmob_pitch ($1)) {
2118                                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @1);
2119                                 n->set_property ("pitch", $1);
2120                                 n->set_property ("duration",
2121                                                  parser->default_duration_.smobbed_copy ());
2122                                 $$ = n->unprotect ();
2123                         }
2124                         unsmob_music ($$)->set_property ("articulations",
2125                                                          scm_reverse_x ($2, SCM_EOL));
2126                 }
2127         }
2128         | simple_chord_elements post_events     {
2129                 SCM elts = ly_append2 ($1, scm_reverse_x ($2, SCM_EOL));
2130
2131                 Input i;
2132                 /* why is this giving wrong start location? -ns
2133                  * i = @$; */
2134                 i.set_location (@1, @2);
2135                 $$ = MAKE_SYNTAX ("event-chord", i, elts);
2136         }
2137         | CHORD_REPETITION optional_notemode_duration post_events {
2138                 Input i;
2139                 i.set_location (@1, @3);
2140                 $$ = MAKE_SYNTAX ("repetition-chord", i,
2141                                   $2, scm_reverse_x ($3, SCM_EOL));
2142         }
2143         | MULTI_MEASURE_REST optional_notemode_duration post_events {
2144                 Input i;
2145                 i.set_location (@1, @3);
2146                 $$ = MAKE_SYNTAX ("multi-measure-rest", i, $2,
2147                                   scm_reverse_x ($3, SCM_EOL));
2148         }
2149         | command_element
2150         | note_chord_element
2151         ;
2152
2153
2154 note_chord_element:
2155         chord_body optional_notemode_duration post_events
2156         {
2157                 Music *m = unsmob_music ($1);
2158                 SCM dur = unsmob_duration ($2)->smobbed_copy ();
2159                 SCM es = m->get_property ("elements");
2160                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
2161
2162                 for (SCM s = es; scm_is_pair (s); s = scm_cdr (s))
2163                   unsmob_music (scm_car (s))->set_property ("duration", dur);
2164                 es = ly_append2 (es, postevs);
2165
2166                 m-> set_property ("elements", es);
2167                 m->set_spot (@$);
2168                 $$ = m->self_scm ();
2169         }
2170         ;
2171
2172 chord_body:
2173         ANGLE_OPEN chord_body_elements ANGLE_CLOSE
2174         {
2175                 $$ = MAKE_SYNTAX ("event-chord", @$, scm_reverse_x ($2, SCM_EOL));
2176         }
2177         ;
2178
2179 chord_body_elements:
2180         /* empty */             { $$ = SCM_EOL; }
2181         | chord_body_elements chord_body_element {
2182                 if (!SCM_UNBNDP ($2))
2183                         $$ = scm_cons ($2, $1);
2184         }
2185         ;
2186
2187 chord_body_element:
2188         pitch exclamations questions octave_check post_events
2189         {
2190                 int q = $3;
2191                 int ex = $2;
2192                 SCM check = $4;
2193                 SCM post = $5;
2194
2195                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2196                 n->set_property ("pitch", $1);
2197                 if (q % 2)
2198                         n->set_property ("cautionary", SCM_BOOL_T);
2199                 if (ex % 2 || q % 2)
2200                         n->set_property ("force-accidental", SCM_BOOL_T);
2201
2202                 if (scm_is_pair (post)) {
2203                         SCM arts = scm_reverse_x (post, SCM_EOL);
2204                         n->set_property ("articulations", arts);
2205                 }
2206                 if (scm_is_number (check))
2207                 {
2208                         int q = scm_to_int (check);
2209                         n->set_property ("absolute-octave", scm_from_int (q-1));
2210                 }
2211
2212                 $$ = n->unprotect ();
2213         }
2214         | DRUM_PITCH post_events {
2215                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2216                 n->set_property ("drum-type", $1);
2217
2218                 if (scm_is_pair ($2)) {
2219                         SCM arts = scm_reverse_x ($2, SCM_EOL);
2220                         n->set_property ("articulations", arts);
2221                 }
2222                 $$ = n->unprotect ();
2223         }
2224         | music_function_chord_body
2225         {
2226                 Music *m = unsmob_music ($1);
2227
2228                 while (m && m->is_mus_type ("music-wrapper-music")) {
2229                         $$ = m->get_property ("element");
2230                         m = unsmob_music ($$);
2231                 }
2232
2233                 if (!(m && m->is_mus_type ("rhythmic-event"))) {
2234                         parser->parser_error (@$, _ ("not a rhythmic event"));
2235                         $$ = SCM_UNDEFINED;
2236                 }
2237         }
2238         ;
2239
2240 music_function_chord_body:
2241         music_function_call
2242         | MUSIC_IDENTIFIER
2243         ;
2244
2245 // Event functions may only take closed arglists, otherwise it would
2246 // not be clear whether a following postevent should be associated
2247 // with the last argument of the event function or with the expression
2248 // for which the function call acts itself as event.
2249
2250 music_function_event:
2251         MUSIC_FUNCTION function_arglist_closed {
2252                 $$ = MAKE_SYNTAX ("music-function", @$,
2253                                          $1, $2);
2254         }
2255         ;
2256
2257 event_function_event:
2258         EVENT_FUNCTION function_arglist_closed {
2259                 $$ = MAKE_SYNTAX ("music-function", @$,
2260                                          $1, $2);
2261         }
2262         ;
2263
2264 command_element:
2265         command_event {
2266                 $$ = $1;
2267         }
2268         | E_BRACKET_OPEN {
2269                 Music *m = MY_MAKE_MUSIC ("LigatureEvent", @$);
2270                 m->set_property ("span-direction", scm_from_int (START));
2271                 $$ = m->unprotect();
2272         }
2273         | E_BRACKET_CLOSE {
2274                 Music *m = MY_MAKE_MUSIC ("LigatureEvent", @$);
2275                 m->set_property ("span-direction", scm_from_int (STOP));
2276                 $$ = m->unprotect ();
2277         }
2278         | E_BACKSLASH {
2279                 $$ = MAKE_SYNTAX ("voice-separator", @$);
2280         }
2281         | '|'      {
2282                 SCM pipe = parser->lexer_->lookup_identifier ("pipeSymbol");
2283
2284                 Music *m = unsmob_music (pipe);
2285                 if (m)
2286                 {
2287                         m = m->clone ();
2288                         m->set_spot (@$);
2289                         $$ = m->unprotect ();
2290                 }
2291                 else
2292                         $$ = MAKE_SYNTAX ("bar-check", @$);
2293
2294         }
2295         ;
2296
2297 command_event:
2298         E_TILDE {
2299                 $$ = MY_MAKE_MUSIC ("PesOrFlexaEvent", @$)->unprotect ();
2300         }
2301         | tempo_event {
2302                 $$ = $1;
2303         }
2304         ;
2305
2306
2307 post_events:
2308         /* empty */ {
2309                 $$ = SCM_EOL;
2310         }
2311         | post_events post_event {
2312                 unsmob_music ($2)->set_spot (@2);
2313                 $$ = scm_cons ($2, $$);
2314         }
2315         ;
2316
2317 post_event_nofinger:
2318         direction_less_event {
2319                 $$ = $1;
2320         }
2321         | script_dir music_function_event {
2322                 $$ = $2;
2323                 if ($1)
2324                 {
2325                         unsmob_music ($$)->set_property ("direction", scm_from_int ($1));
2326                 }
2327         }
2328         | HYPHEN {
2329                 if (!parser->lexer_->is_lyric_state ())
2330                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2331                 $$ = MY_MAKE_MUSIC ("HyphenEvent", @$)->unprotect ();
2332         }
2333         | EXTENDER {
2334                 if (!parser->lexer_->is_lyric_state ())
2335                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2336                 $$ = MY_MAKE_MUSIC ("ExtenderEvent", @$)->unprotect ();
2337         }
2338         | script_dir direction_reqd_event {
2339                 if ($1)
2340                 {
2341                         Music *m = unsmob_music ($2);
2342                         m->set_property ("direction", scm_from_int ($1));
2343                 }
2344                 $$ = $2;
2345         }
2346         | script_dir direction_less_event {
2347                 if ($1)
2348                 {
2349                         Music *m = unsmob_music ($2);
2350                         m->set_property ("direction", scm_from_int ($1));
2351                 }
2352                 $$ = $2;
2353         }
2354         | string_number_event
2355         | '^' fingering
2356         {
2357                 $$ = $2;
2358                 unsmob_music ($$)->set_property ("direction", scm_from_int (UP));
2359         }
2360         | '_' fingering
2361         {
2362                 $$ = $2;
2363                 unsmob_music ($$)->set_property ("direction", scm_from_int (DOWN));
2364         }                       
2365         ;
2366
2367 post_event:
2368         post_event_nofinger
2369         | '-' fingering {
2370                 $$ = $2;
2371         }
2372         ;
2373
2374 string_number_event:
2375         E_UNSIGNED {
2376                 Music *s = MY_MAKE_MUSIC ("StringNumberEvent", @$);
2377                 s->set_property ("string-number", scm_from_int ($1));
2378                 $$ = s->unprotect ();
2379         }
2380         ;
2381
2382 direction_less_char:
2383         '['  {
2384                 $$ = ly_symbol2scm ("bracketOpenSymbol");
2385         }
2386         | ']'  {
2387                 $$ = ly_symbol2scm ("bracketCloseSymbol");
2388         }
2389         | '~'  {
2390                 $$ = ly_symbol2scm ("tildeSymbol");
2391         }
2392         | '('  {
2393                 $$ = ly_symbol2scm ("parenthesisOpenSymbol");
2394         }
2395         | ')'  {
2396                 $$ = ly_symbol2scm ("parenthesisCloseSymbol");
2397         }
2398         | E_EXCLAMATION  {
2399                 $$ = ly_symbol2scm ("escapedExclamationSymbol");
2400         }
2401         | E_OPEN  {
2402                 $$ = ly_symbol2scm ("escapedParenthesisOpenSymbol");
2403         }
2404         | E_CLOSE  {
2405                 $$ = ly_symbol2scm ("escapedParenthesisCloseSymbol");
2406         }
2407         | E_ANGLE_CLOSE  {
2408                 $$ = ly_symbol2scm ("escapedBiggerSymbol");
2409         }
2410         | E_ANGLE_OPEN  {
2411                 $$ = ly_symbol2scm ("escapedSmallerSymbol");
2412         }
2413         ;
2414
2415 direction_less_event:
2416         direction_less_char {
2417                 SCM predefd = parser->lexer_->lookup_identifier_symbol ($1);
2418                 Music *m = 0;
2419                 if (unsmob_music (predefd))
2420                 {
2421                         m = unsmob_music (predefd)->clone ();
2422                         m->set_spot (@$);
2423                 }
2424                 else
2425                 {
2426                         m = MY_MAKE_MUSIC ("Music", @$);
2427                 }
2428                 $$ = m->unprotect ();
2429         }
2430         | EVENT_IDENTIFIER      {
2431                 $$ = $1;
2432         }
2433         | tremolo_type  {
2434                Music *a = MY_MAKE_MUSIC ("TremoloEvent", @$);
2435                a->set_property ("tremolo-type", scm_from_int ($1));
2436                $$ = a->unprotect ();
2437         }
2438         | event_function_event  
2439         ;
2440
2441 direction_reqd_event:
2442         gen_text_def {
2443                 $$ = $1;
2444         }
2445         | script_abbreviation {
2446                 SCM s = parser->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
2447                 Music *a = MY_MAKE_MUSIC ("ArticulationEvent", @$);
2448                 if (scm_is_string (s))
2449                         a->set_property ("articulation-type", s);
2450                 else parser->parser_error (@1, _ ("expecting string as script definition"));
2451                 $$ = a->unprotect ();
2452         }
2453         ;
2454
2455 octave_check:
2456         /**/ { $$ = SCM_EOL; }
2457         | '='  { $$ = scm_from_int (0); }
2458         | '=' sub_quotes { $$ = scm_from_int (-$2); }
2459         | '=' sup_quotes { $$ = scm_from_int ($2); }
2460         ;
2461
2462 sup_quotes:
2463         '\'' {
2464                 $$ = 1;
2465         }
2466         | sup_quotes '\'' {
2467                 $$ ++;
2468         }
2469         ;
2470
2471 sub_quotes:
2472         ',' {
2473                 $$ = 1;
2474         }
2475         | sub_quotes ',' {
2476                 $$++;
2477         }
2478         ;
2479
2480 steno_pitch:
2481         NOTENAME_PITCH  {
2482                 $$ = $1;
2483         }
2484         | NOTENAME_PITCH sup_quotes     {
2485                 Pitch p = *unsmob_pitch ($1);
2486                 p = p.transposed (Pitch ($2,0,0));
2487                 $$ = p.smobbed_copy ();
2488         }
2489         | NOTENAME_PITCH sub_quotes      {
2490                 Pitch p =* unsmob_pitch ($1);
2491                 p = p.transposed (Pitch (-$2,0,0));
2492                 $$ = p.smobbed_copy ();
2493         }
2494         ;
2495
2496 /*
2497 ugh. duplication
2498 */
2499
2500 steno_tonic_pitch:
2501         TONICNAME_PITCH {
2502                 $$ = $1;
2503         }
2504         | TONICNAME_PITCH sup_quotes    {
2505                 Pitch p = *unsmob_pitch ($1);
2506                 p = p.transposed (Pitch ($2,0,0));
2507                 $$ = p.smobbed_copy ();
2508         }
2509         | TONICNAME_PITCH sub_quotes     {
2510                 Pitch p = *unsmob_pitch ($1);
2511
2512                 p = p.transposed (Pitch (-$2,0,0));
2513                 $$ = p.smobbed_copy ();
2514         }
2515         ;
2516
2517 pitch:
2518         steno_pitch {
2519                 $$ = $1;
2520         }
2521         | PITCH_IDENTIFIER
2522         ;
2523
2524 pitch_also_in_chords:
2525         pitch
2526         | steno_tonic_pitch
2527         ;
2528
2529 gen_text_def:
2530         full_markup {
2531                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2532                 t->set_property ("text", $1);
2533                 $$ = t->unprotect ();
2534         }
2535         | simple_string {
2536                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2537                 t->set_property ("text",
2538                         make_simple_markup ($1));
2539                 $$ = t->unprotect ();
2540         }
2541         ;
2542
2543 fingering:
2544         UNSIGNED {
2545                 Music *t = MY_MAKE_MUSIC ("FingeringEvent", @$);
2546                 t->set_property ("digit", $1);
2547                 $$ = t->unprotect ();
2548         }
2549         ;
2550
2551 script_abbreviation:
2552         '^'             {
2553                 $$ = scm_from_locale_string ("Hat");
2554         }
2555         | '+'           {
2556                 $$ = scm_from_locale_string ("Plus");
2557         }
2558         | '-'           {
2559                 $$ = scm_from_locale_string ("Dash");
2560         }
2561         | '|'           {
2562                 $$ = scm_from_locale_string ("Bar");
2563         }
2564         | ANGLE_CLOSE   {
2565                 $$ = scm_from_locale_string ("Larger");
2566         }
2567         | '.'           {
2568                 $$ = scm_from_locale_string ("Dot");
2569         }
2570         | '_' {
2571                 $$ = scm_from_locale_string ("Underscore");
2572         }
2573         ;
2574
2575 script_dir:
2576         '_'     { $$ = DOWN; }
2577         | '^'   { $$ = UP; }
2578         | '-'   { $$ = CENTER; }
2579         ;
2580
2581 duration_length:
2582         multiplied_duration {
2583                 $$ = $1;
2584         }
2585         ;
2586
2587 maybe_notemode_duration:
2588         {
2589                 $$ = SCM_UNDEFINED;
2590         }
2591         | multiplied_duration   {
2592                 $$ = $1;
2593                 parser->default_duration_ = *unsmob_duration ($$);
2594         }
2595 ;
2596
2597
2598 optional_notemode_duration:
2599         maybe_notemode_duration
2600         {
2601                 if (SCM_UNBNDP ($$))
2602                         $$ = parser->default_duration_.smobbed_copy ();
2603         }
2604         ;
2605
2606 steno_duration:
2607         bare_unsigned dots              {
2608                 int len = 0;
2609                 if (!is_duration ($1))
2610                         parser->parser_error (@1, _f ("not a duration: %d", $1));
2611                 else
2612                         len = intlog2 ($1);
2613
2614                 $$ = Duration (len, $2).smobbed_copy ();
2615         }
2616         | DURATION_IDENTIFIER dots      {
2617                 Duration *d = unsmob_duration ($1);
2618                 Duration k (d->duration_log (), d->dot_count () + $2);
2619                 k = k.compressed (d->factor ());
2620                 *d = k;
2621                 $$ = $1;
2622         }
2623         ;
2624
2625 multiplied_duration:
2626         steno_duration {
2627                 $$ = $1;
2628         }
2629         | multiplied_duration '*' bare_unsigned {
2630                 $$ = unsmob_duration ($$)->compressed ( $3) .smobbed_copy ();
2631         }
2632         | multiplied_duration '*' FRACTION {
2633                 Rational  m (scm_to_int (scm_car ($3)), scm_to_int (scm_cdr ($3)));
2634
2635                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
2636         }
2637         ;
2638
2639 fraction:
2640         FRACTION { $$ = $1; }
2641         | UNSIGNED '/' UNSIGNED {
2642                 $$ = scm_cons ($1, $3);
2643         }
2644         ;
2645
2646 dots:
2647         /* empty */     {
2648                 $$ = 0;
2649         }
2650         | dots '.' {
2651                 $$ ++;
2652         }
2653         ;
2654
2655 tremolo_type:
2656         ':'     {
2657                 $$ = 0;
2658         }
2659         | ':' bare_unsigned {
2660                 if (!is_duration ($2))
2661                         parser->parser_error (@2, _f ("not a duration: %d", $2));
2662                 $$ = $2;
2663         }
2664         ;
2665
2666 bass_number:
2667         UNSIGNED { $$ = $1; }
2668         | STRING { $$ = $1; }
2669         | full_markup { $$ = $1; }
2670         ;
2671
2672 figured_bass_alteration:
2673         '-'     { $$ = ly_rational2scm (FLAT_ALTERATION); }
2674         | '+'   { $$ = ly_rational2scm (SHARP_ALTERATION); }
2675         | '!'   { $$ = scm_from_int (0); }
2676         ;
2677
2678 bass_figure:
2679         FIGURE_SPACE {
2680                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
2681                 $$ = bfr->unprotect ();
2682         }
2683         | bass_number  {
2684                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
2685                 $$ = bfr->self_scm ();
2686
2687                 if (scm_is_number ($1))
2688                         bfr->set_property ("figure", $1);
2689                 else if (Text_interface::is_markup ($1))
2690                         bfr->set_property ("text", $1);
2691
2692                 bfr->unprotect ();
2693         }
2694         | bass_figure ']' {
2695                 $$ = $1;
2696                 unsmob_music ($1)->set_property ("bracket-stop", SCM_BOOL_T);
2697         }
2698         | bass_figure figured_bass_alteration {
2699                 Music *m = unsmob_music ($1);
2700                 if (scm_to_double ($2)) {
2701                         SCM salter = m->get_property ("alteration");
2702                         SCM alter = scm_is_number (salter) ? salter : scm_from_int (0);
2703                         m->set_property ("alteration",
2704                                          scm_sum (alter, $2));
2705                 } else {
2706                         m->set_property ("alteration", scm_from_int (0));
2707                 }
2708         }
2709         | bass_figure figured_bass_modification  {
2710                 Music *m = unsmob_music ($1);
2711                 if ($2 == ly_symbol2scm ("plus"))
2712                         {
2713                         m->set_property ("augmented", SCM_BOOL_T);
2714                         }
2715                 else if ($2 == ly_symbol2scm ("slash"))
2716                         {
2717                         m->set_property ("diminished", SCM_BOOL_T);
2718                         }
2719                 else if ($2 == ly_symbol2scm ("exclamation"))
2720                         {
2721                         m->set_property ("no-continuation", SCM_BOOL_T);
2722                         }
2723                 else if ($2 == ly_symbol2scm ("backslash"))
2724                         {
2725                         m->set_property ("augmented-slash", SCM_BOOL_T);
2726                         }
2727         }
2728         ;
2729
2730
2731 figured_bass_modification:
2732         E_PLUS          {
2733                 $$ = ly_symbol2scm ("plus");
2734         }
2735         | E_EXCLAMATION {
2736                 $$ = ly_symbol2scm ("exclamation");
2737         }
2738         | '/'           {
2739                 $$ = ly_symbol2scm ("slash");
2740         }
2741         | E_BACKSLASH {
2742                 $$ = ly_symbol2scm ("backslash");
2743         }
2744         ;
2745
2746 br_bass_figure:
2747         bass_figure {
2748                 $$ = $1;
2749         }
2750         | '[' bass_figure {
2751                 $$ = $2;
2752                 unsmob_music ($$)->set_property ("bracket-start", SCM_BOOL_T);
2753         }
2754         ;
2755
2756 figure_list:
2757         /**/            {
2758                 $$ = SCM_EOL;
2759         }
2760         | figure_list br_bass_figure {
2761                 $$ = scm_cons ($2, $1);
2762         }
2763         ;
2764
2765 figure_spec:
2766         FIGURE_OPEN figure_list FIGURE_CLOSE {
2767                 $$ = scm_reverse_x ($2, SCM_EOL);
2768         }
2769         ;
2770
2771
2772 optional_rest:
2773         /**/   { $$ = 0; }
2774         | REST { $$ = 1; }
2775         ;
2776
2777 simple_element:
2778         pitch exclamations questions octave_check maybe_notemode_duration optional_rest {
2779                 if (!parser->lexer_->is_note_state ())
2780                         parser->parser_error (@1, _ ("have to be in Note mode for notes"));
2781                 if ($2 || $3 || scm_is_number ($4) || !SCM_UNBNDP ($5) || $6)
2782                 {
2783                         Music *n = 0;
2784                         if ($6)
2785                                 n = MY_MAKE_MUSIC ("RestEvent", @$);
2786                         else
2787                                 n = MY_MAKE_MUSIC ("NoteEvent", @$);
2788                         
2789                         n->set_property ("pitch", $1);
2790                         if (SCM_UNBNDP ($5))
2791                                 n->set_property ("duration",
2792                                                  parser->default_duration_.smobbed_copy ());
2793                         else
2794                                 n->set_property ("duration", $5);
2795                         
2796                         if (scm_is_number ($4))
2797                         {
2798                                 int q = scm_to_int ($4);
2799                                 n->set_property ("absolute-octave", scm_from_int (q-1));
2800                         }
2801                         
2802                         if ($3 % 2)
2803                                 n->set_property ("cautionary", SCM_BOOL_T);
2804                         if ($2 % 2 || $3 % 2)
2805                                 n->set_property ("force-accidental", SCM_BOOL_T);
2806                         
2807                         $$ = n->unprotect ();
2808                 }
2809         }
2810         | DRUM_PITCH optional_notemode_duration {
2811                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2812                 n->set_property ("duration", $2);
2813                 n->set_property ("drum-type", $1);
2814
2815                 $$ = n->unprotect ();
2816         }
2817         | RESTNAME optional_notemode_duration           {
2818                 Music *ev = 0;
2819                 if (ly_scm2string ($1) == "s") {
2820                         /* Space */
2821                         ev = MY_MAKE_MUSIC ("SkipEvent", @$);
2822                   }
2823                 else {
2824                         ev = MY_MAKE_MUSIC ("RestEvent", @$);
2825
2826                     }
2827                 ev->set_property ("duration", $2);
2828                 $$ = ev->unprotect ();
2829         }
2830         ;
2831
2832 simple_chord_elements:
2833         new_chord {
2834                 if (!parser->lexer_->is_chord_state ())
2835                         parser->parser_error (@1, _ ("have to be in Chord mode for chords"));
2836                 $$ = $1;
2837         }
2838         | figure_spec optional_notemode_duration {
2839                 for (SCM s = $1; scm_is_pair (s); s = scm_cdr (s))
2840                 {
2841                         unsmob_music (scm_car (s))->set_property ("duration", $2);
2842                 }
2843                 $$ = $1;
2844         }
2845         ;
2846
2847 lyric_element:
2848         lyric_markup {
2849                 $$ = $1;
2850         }
2851         | LYRICS_STRING {
2852                 $$ = $1;
2853         }
2854         ;
2855
2856 lyric_element_arg:
2857         lyric_element
2858         | lyric_element multiplied_duration post_events {
2859                 $$ = MAKE_SYNTAX ("lyric-event", @$, $1, $2);
2860                 if (scm_is_pair ($3))
2861                         unsmob_music ($$)->set_property
2862                                 ("articulations", scm_reverse_x ($3, SCM_EOL));
2863         }
2864         | lyric_element post_event post_events {
2865                 $$ = MAKE_SYNTAX ("lyric-event", @$, $1,
2866                                   parser->default_duration_.smobbed_copy ());
2867                 unsmob_music ($$)->set_property
2868                         ("articulations", scm_cons ($2, scm_reverse_x ($3, SCM_EOL)));
2869         }
2870         | LYRIC_ELEMENT optional_notemode_duration post_events {
2871                 $$ = MAKE_SYNTAX ("lyric-event", @$, $1, $2);
2872                 if (scm_is_pair ($3))
2873                         unsmob_music ($$)->set_property
2874                                 ("articulations", scm_reverse_x ($3, SCM_EOL));
2875         }
2876         ;
2877
2878
2879 lyric_element_music:
2880         lyric_element optional_notemode_duration post_events {
2881                 $$ = MAKE_SYNTAX ("lyric-event", @$, $1, $2);
2882                 if (scm_is_pair ($3))
2883                         unsmob_music ($$)->set_property
2884                                 ("articulations", scm_reverse_x ($3, SCM_EOL));
2885         }
2886         ;
2887
2888 new_chord:
2889         steno_tonic_pitch optional_notemode_duration   {
2890                 $$ = make_chord_elements (@$, $1, $2, SCM_EOL);
2891         }
2892         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
2893                 SCM its = scm_reverse_x ($4, SCM_EOL);
2894                 $$ = make_chord_elements (@$, $1, $2, scm_cons ($3, its));
2895         }
2896         ;
2897
2898 chord_items:
2899         /**/ {
2900                 $$ = SCM_EOL;
2901         }
2902         | chord_items chord_item {
2903                 $$ = scm_cons ($2, $$);
2904         }
2905         ;
2906
2907 chord_separator:
2908         CHORD_COLON {
2909                 $$ = ly_symbol2scm ("chord-colon");
2910         }
2911         | CHORD_CARET {
2912                 $$ = ly_symbol2scm ("chord-caret");
2913         }
2914         | CHORD_SLASH steno_tonic_pitch {
2915                 $$ = scm_list_2 (ly_symbol2scm ("chord-slash"), $2);
2916         }
2917         | CHORD_BASS steno_tonic_pitch {
2918                 $$ = scm_list_2 (ly_symbol2scm ("chord-bass"), $2);
2919         }
2920         ;
2921
2922 chord_item:
2923         chord_separator {
2924                 $$ = $1;
2925         }
2926         | step_numbers {
2927                 $$ = scm_reverse_x ($1, SCM_EOL);
2928         }
2929         | CHORD_MODIFIER  {
2930                 $$ = $1;
2931         }
2932         ;
2933
2934 step_numbers:
2935         step_number { $$ = scm_cons ($1, SCM_EOL); }
2936         | step_numbers '.' step_number {
2937                 $$ = scm_cons ($3, $$);
2938         }
2939         ;
2940
2941 step_number:
2942         bare_unsigned {
2943                 $$ = make_chord_step ($1, 0);
2944         }
2945         | bare_unsigned '+' {
2946                 $$ = make_chord_step ($1, SHARP_ALTERATION);
2947         }
2948         | bare_unsigned CHORD_MINUS {
2949                 $$ = make_chord_step ($1, FLAT_ALTERATION);
2950         }
2951         ;
2952
2953 tempo_range:
2954         bare_unsigned {
2955                 $$ = scm_from_int ($1);
2956         }
2957         | bare_unsigned '~' bare_unsigned {
2958                 $$ = scm_cons (scm_from_int ($1), scm_from_int ($3));
2959         }
2960         ;
2961
2962 /*
2963         UTILITIES
2964
2965 TODO: should deprecate in favor of Scheme?
2966
2967  */
2968 number_expression:
2969         number_expression '+' number_term {
2970                 $$ = scm_sum ($1, $3);
2971         }
2972         | number_expression '-' number_term {
2973                 $$ = scm_difference ($1, $3);
2974         }
2975         | number_term
2976         ;
2977
2978 number_term:
2979         number_factor {
2980                 $$ = $1;
2981         }
2982         | number_factor '*' number_factor {
2983                 $$ = scm_product ($1, $3);
2984         }
2985         | number_factor '/' number_factor {
2986                 $$ = scm_divide ($1, $3);
2987         }
2988         ;
2989
2990 number_factor:
2991         '-'  number_factor { /* %prec UNARY_MINUS */
2992                 $$ = scm_difference ($2, SCM_UNDEFINED);
2993         }
2994         | bare_number
2995         ;
2996
2997
2998 bare_number:
2999         bare_number_closed
3000         | UNSIGNED NUMBER_IDENTIFIER    {
3001                 $$ = scm_product ($1, $2);
3002         }
3003         | REAL NUMBER_IDENTIFIER        {
3004                 $$ = scm_product ($1, $2);
3005         }
3006         ;
3007
3008 bare_number_closed:
3009         UNSIGNED
3010         | REAL
3011         | NUMBER_IDENTIFIER
3012         ;
3013
3014 bare_unsigned:
3015         UNSIGNED {
3016                 $$ = scm_to_int ($1);
3017         }
3018         ;
3019
3020 unsigned_number:
3021         UNSIGNED
3022         | NUMBER_IDENTIFIER
3023         ;
3024
3025 exclamations:
3026                 { $$ = 0; }
3027         | exclamations '!'      { $$ ++; }
3028         ;
3029
3030 questions:
3031                 { $$ = 0; }
3032         | questions '?' { $$ ++; }
3033         ;
3034
3035 /*
3036 This should be done more dynamically if possible.
3037 */
3038
3039 lyric_markup:
3040         LYRIC_MARKUP_IDENTIFIER {
3041                 $$ = $1;
3042         }
3043         | LYRIC_MARKUP
3044                 { parser->lexer_->push_markup_state (); }
3045         markup_top {
3046                 $$ = $3;
3047                 parser->lexer_->pop_state ();
3048         }
3049         ;
3050
3051 full_markup_list:
3052         MARKUPLIST_IDENTIFIER {
3053                 $$ = $1;
3054         }
3055         | MARKUPLIST
3056                 { parser->lexer_->push_markup_state (); }
3057         markup_list {
3058                 $$ = $3;
3059                 parser->lexer_->pop_state ();
3060         }
3061         ;
3062
3063 full_markup:
3064         MARKUP_IDENTIFIER {
3065                 $$ = $1;
3066         }
3067         | MARKUP
3068                 { parser->lexer_->push_markup_state (); }
3069         markup_top {
3070                 $$ = $3;
3071                 parser->lexer_->pop_state ();
3072         }
3073         ;
3074
3075 markup_top:
3076         markup_list {
3077                 $$ = scm_list_2 (ly_lily_module_constant ("line-markup"),  $1);
3078         }
3079         | markup_head_1_list simple_markup      {
3080                 $$ = scm_car (scm_call_2 (ly_lily_module_constant ("map-markup-command-list"), $1, scm_list_1 ($2)));
3081         }
3082         | simple_markup {
3083                 $$ = $1;
3084         }
3085         ;
3086
3087 markup_scm:
3088         embedded_scm_bare
3089         {
3090                 if (Text_interface::is_markup ($1))
3091                         MYBACKUP (MARKUP_IDENTIFIER, $1, @1);
3092                 else if (Text_interface::is_markup_list ($1))
3093                         MYBACKUP (MARKUPLIST_IDENTIFIER, $1, @1);
3094                 else {
3095                         parser->parser_error (@1, _ ("not a markup"));
3096                         MYBACKUP (MARKUP_IDENTIFIER, scm_string (SCM_EOL), @1);
3097                 }
3098         } BACKUP
3099         ;
3100                         
3101
3102 markup_list:
3103         MARKUPLIST_IDENTIFIER {
3104                 $$ = $1;
3105         }
3106         | markup_composed_list {
3107                 $$ = $1;
3108         }
3109         | markup_braced_list {
3110                 $$ = $1;
3111         }
3112         | markup_command_list {
3113                 $$ = scm_list_1 ($1);
3114         }
3115         | markup_scm MARKUPLIST_IDENTIFIER
3116         {
3117                 $$ = $2;
3118         }
3119         ;
3120
3121 markup_composed_list:
3122         markup_head_1_list markup_braced_list {
3123                 $$ = scm_call_2 (ly_lily_module_constant ("map-markup-command-list"), $1, $2);
3124
3125         }
3126         ;
3127
3128 markup_braced_list:
3129         '{' markup_braced_list_body '}' {
3130                 $$ = scm_reverse_x ($2, SCM_EOL);
3131         }
3132         ;
3133
3134 markup_braced_list_body:
3135         /* empty */     {  $$ = SCM_EOL; }
3136         | markup_braced_list_body markup {
3137                 $$ = scm_cons ($2, $1);
3138         }
3139         | markup_braced_list_body markup_list {
3140                 $$ = scm_reverse_x ($2, $1);
3141         }
3142         ;
3143
3144 markup_command_list:
3145         MARKUP_LIST_FUNCTION markup_command_list_arguments {
3146           $$ = scm_cons ($1, scm_reverse_x($2, SCM_EOL));
3147         }
3148         ;
3149
3150 markup_command_basic_arguments:
3151         EXPECT_MARKUP_LIST markup_command_list_arguments markup_list {
3152           $$ = scm_cons ($3, $2);
3153         }
3154         | EXPECT_SCM markup_command_list_arguments embedded_scm_closed {
3155           $$ = check_scheme_arg (parser, @3, $3, $2, $1);
3156         }
3157         | EXPECT_NO_MORE_ARGS {
3158           $$ = SCM_EOL;
3159         }
3160         ;
3161
3162 markup_command_list_arguments:
3163         markup_command_basic_arguments { $$ = $1; }
3164         | EXPECT_MARKUP markup_command_list_arguments markup {
3165           $$ = scm_cons ($3, $2);
3166         }
3167         ;
3168
3169 markup_head_1_item:
3170         MARKUP_FUNCTION EXPECT_MARKUP markup_command_list_arguments {
3171           $$ = scm_cons ($1, scm_reverse_x ($3, SCM_EOL));
3172         }
3173         ;
3174
3175 markup_head_1_list:
3176         markup_head_1_item      {
3177                 $$ = scm_list_1 ($1);
3178         }
3179         | markup_head_1_list markup_head_1_item {
3180                 $$ = scm_cons ($2, $1);
3181         }
3182         ;
3183
3184 simple_markup:
3185         STRING {
3186                 $$ = make_simple_markup ($1);
3187         }
3188         | MARKUP_IDENTIFIER {
3189                 $$ = $1;
3190         }
3191         | LYRIC_MARKUP_IDENTIFIER {
3192                 $$ = $1;
3193         }
3194         | STRING_IDENTIFIER {
3195                 $$ = $1;
3196         }
3197         | SCORE {
3198                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3199                 parser->lexer_->push_note_state (alist_to_hashq (nn));
3200         } '{' score_body '}' {
3201                 Score * sc = $4;
3202                 $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), sc->self_scm ());
3203                 sc->unprotect ();
3204                 parser->lexer_->pop_state ();
3205         }
3206         | MARKUP_FUNCTION markup_command_basic_arguments {
3207                 $$ = scm_cons ($1, scm_reverse_x ($2, SCM_EOL));
3208         }
3209         | markup_scm MARKUP_IDENTIFIER
3210         {
3211                 $$ = $2;
3212         }
3213         ;
3214
3215 markup:
3216         markup_head_1_list simple_markup        {
3217                 SCM mapper = ly_lily_module_constant ("map-markup-command-list");
3218                 $$ = scm_car (scm_call_2 (mapper, $1, scm_list_1 ($2)));
3219         }
3220         | simple_markup {
3221                 $$ = $1;
3222         }
3223         ;
3224
3225 %%
3226
3227 void
3228 Lily_parser::set_yydebug (bool x)
3229 {
3230         yydebug = x;
3231 }
3232
3233 void
3234 Lily_parser::do_yyparse ()
3235 {
3236         yyparse (this);
3237 }
3238
3239
3240
3241
3242
3243 /*
3244
3245 It is a little strange to have this function in this file, but
3246 otherwise, we have to import music classes into the lexer.
3247
3248 */
3249 int
3250 Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
3251 {
3252         if (scm_is_string (sid)) {
3253                 *destination = sid;
3254                 return STRING_IDENTIFIER;
3255         } else if (unsmob_book (sid)) {
3256                 Book *book =  unsmob_book (sid)->clone ();
3257                 *destination = book->self_scm ();
3258                 book->unprotect ();
3259
3260                 return BOOK_IDENTIFIER;
3261         } else if (scm_is_number (sid)) {
3262                 *destination = sid;
3263                 return NUMBER_IDENTIFIER;
3264         } else if (unsmob_context_def (sid)) {
3265                 Context_def *def= unsmob_context_def (sid)->clone ();
3266
3267                 *destination = def->self_scm ();
3268                 def->unprotect ();
3269
3270                 return CONTEXT_DEF_IDENTIFIER;
3271         } else if (unsmob_context_mod (sid)) {
3272                 *destination = unsmob_context_mod (sid)->smobbed_copy ();
3273
3274                 return CONTEXT_MOD_IDENTIFIER;
3275         } else if (unsmob_score (sid)) {
3276                 Score *score = new Score (*unsmob_score (sid));
3277                 *destination = score->self_scm ();
3278
3279                 score->unprotect ();
3280                 return SCORE_IDENTIFIER;
3281         } else if (Music *mus = unsmob_music (sid)) {
3282                 mus = mus->clone ();
3283                 *destination = mus->self_scm ();
3284                 unsmob_music (*destination)->
3285                         set_property ("origin", make_input (last_input_));
3286
3287                 bool is_event = mus->is_mus_type ("post-event");
3288                 mus->unprotect ();
3289                 return is_event ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
3290         } else if (unsmob_pitch (sid)) {
3291                 *destination = unsmob_pitch (sid)->smobbed_copy ();
3292                 return PITCH_IDENTIFIER;
3293         } else if (unsmob_duration (sid)) {
3294                 *destination = unsmob_duration (sid)->smobbed_copy ();
3295                 return DURATION_IDENTIFIER;
3296         } else if (unsmob_output_def (sid)) {
3297                 Output_def *p = unsmob_output_def (sid);
3298                 p = p->clone ();
3299
3300                 *destination = p->self_scm ();
3301                 p->unprotect ();
3302                 return OUTPUT_DEF_IDENTIFIER;
3303         } else if (Text_interface::is_markup (sid)) {
3304                 *destination = sid;
3305                 if (is_lyric_state ())
3306                         return LYRIC_MARKUP_IDENTIFIER;
3307                 return MARKUP_IDENTIFIER;
3308         } else if (Text_interface::is_markup_list (sid)) {
3309                 *destination = sid;
3310                 return MARKUPLIST_IDENTIFIER;
3311         }
3312
3313         return -1;
3314 }
3315
3316 SCM
3317 get_next_unique_context_id ()
3318 {
3319         return scm_from_locale_string ("$uniqueContextId");
3320 }
3321
3322
3323 SCM
3324 get_next_unique_lyrics_context_id ()
3325 {
3326         static int new_context_count;
3327         char s[128];
3328         snprintf (s, sizeof (s)-1, "uniqueContext%d", new_context_count++);
3329         return scm_from_locale_string (s);
3330 }
3331
3332 SCM check_scheme_arg (Lily_parser *parser, Input loc,
3333                       SCM arg, SCM args, SCM pred)
3334 {
3335         args = scm_cons (arg, args);
3336         if (scm_is_true (scm_call_1 (pred, arg)))
3337                 return args;
3338         scm_set_cdr_x (scm_last_pair (args), SCM_EOL);
3339         MAKE_SYNTAX ("argument-error", loc, scm_length (args), pred, arg);
3340         scm_set_cdr_x (scm_last_pair (args), SCM_BOOL_F);
3341         return args;
3342 }
3343
3344 SCM loc_on_music (Input loc, SCM arg)
3345 {
3346         if (Music *m = unsmob_music (arg))
3347         {
3348                 m = m->clone ();
3349                 m->set_spot (loc);
3350                 return m->unprotect ();
3351         }
3352         return arg;
3353 }
3354
3355 bool
3356 is_regular_identifier (SCM id)
3357 {
3358   string str = ly_scm2string (id);
3359   char const *s = str.c_str ();
3360
3361   bool v = true;
3362 #if 0
3363   isalpha (*s);
3364   s++;
3365 #endif
3366   while (*s && v)
3367    {
3368         v = v && isalnum (*s);
3369         s++;
3370    }
3371   return v;
3372 }
3373
3374 Music *
3375 make_music_with_input (SCM name, Input where)
3376 {
3377        Music *m = make_music_by_name (name);
3378        m->set_spot (where);
3379        return m;
3380 }
3381
3382 SCM
3383 make_simple_markup (SCM a)
3384 {
3385         return a;
3386 }
3387
3388 bool
3389 is_duration (int t)
3390 {
3391   return t && t == 1 << intlog2 (t);
3392 }
3393
3394 void
3395 set_music_properties (Music *p, SCM a)
3396 {
3397   for (SCM k = a; scm_is_pair (k); k = scm_cdr (k))
3398         p->set_property (scm_caar (k), scm_cdar (k));
3399 }
3400
3401
3402 SCM
3403 make_chord_step (int step, Rational alter)
3404 {
3405         if (step == 7)
3406                 alter += FLAT_ALTERATION;
3407
3408         while (step < 0)
3409                 step += 7;
3410         Pitch m ((step -1) / 7, (step - 1) % 7, alter);
3411         return m.smobbed_copy ();
3412 }
3413
3414
3415 SCM
3416 make_chord_elements (Input loc, SCM pitch, SCM dur, SCM modification_list)
3417 {
3418         SCM chord_ctor = ly_lily_module_constant ("construct-chord-elements");
3419         SCM res = scm_call_3 (chord_ctor, pitch, dur, modification_list);
3420         for (SCM s = res; scm_is_pair (s); s = scm_cdr (s))
3421         {
3422                 unsmob_music (scm_car (s))->set_spot (loc);
3423         }
3424         return res;
3425 }
3426
3427 int
3428 yylex (YYSTYPE *s, YYLTYPE *loc, Lily_parser *parser)
3429 {
3430         Lily_lexer *lex = parser->lexer_;
3431
3432         lex->lexval_ = s;
3433         lex->lexloc_ = loc;
3434         lex->prepare_for_next_token ();
3435         return lex->yylex ();
3436 }