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