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