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