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