]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
Issue 3727: Allow header blocks and output definitions in \score to precede music
[lilypond.git] / lily / parser.yy
1 /* -*- mode: c++; c-file-style: "linux"; indent-tabs-mode: t -*- */
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 YYSTYPE SCM
37 #define YYLLOC_DEFAULT(Current,Rhs,N) \
38         ((Current).set_location ((Rhs)[1], (Rhs)[N]))
39
40 #define YYPRINT(file, type, value)                                      \
41         do {                                                            \
42                 if (scm_is_eq (value, SCM_UNSPECIFIED))                 \
43                         break;                                          \
44                 char *p = scm_to_locale_string                          \
45                         (scm_simple_format (SCM_BOOL_F,                 \
46                                             scm_from_locale_string ("~S"), \
47                                             scm_list_1 (value)));       \
48                 fputs (p, file);                                        \
49                 free (p);                                               \
50         } while (0)
51
52 %}
53
54 %parse-param {Lily_parser *parser}
55 %parse-param {SCM *retval}
56 %lex-param {Lily_parser *parser}
57 %error-verbose
58 %debug
59
60 /* We use SCMs to do strings, because it saves us the trouble of
61 deleting them.  Let's hope that a stack overflow doesn't trigger a move
62 of the parse stack onto the heap. */
63
64 %left PREC_BOT
65 %nonassoc REPEAT REPEAT_IDENTIFIER
66 %nonassoc ALTERNATIVE
67
68 /* The above precedences tackle the shift/reduce problem
69
70 1.  \repeat
71         \repeat .. \alternative
72
73     \repeat { \repeat .. \alternative }
74
75 or
76
77     \repeat { \repeat } \alternative
78 */
79
80 %nonassoc COMPOSITE
81 %left ADDLYRICS
82
83 %right ':' UNSIGNED REAL E_UNSIGNED EVENT_IDENTIFIER EVENT_FUNCTION '^' '_'
84        HYPHEN EXTENDER DURATION_IDENTIFIER
85
86  /* The above are needed for collecting tremoli and other items (that
87     could otherwise be interpreted as belonging to the next function
88     argument) greedily, and together with the next rule will serve to
89     join numbers and units greedily instead of allowing them into
90     separate function arguments
91  */
92
93 %nonassoc NUMBER_IDENTIFIER
94
95 %left PREC_TOP
96
97
98
99
100 %pure-parser
101 %locations
102
103
104
105 %{ // -*-Fundamental-*-
106
107 /*
108 FIXME:
109
110    * The rules for who is protecting what are very shady.  Uniformise
111      this.
112
113    * There are too many lexical modes?
114 */
115
116 #include "config.hh"
117
118 #include <cctype>
119 #include <cstdlib>
120 #include <cstdio>
121 using namespace std;
122
123 #include "book.hh"
124 #include "context-def.hh"
125 #include "context-mod.hh"
126 #include "dimensions.hh"
127 #include "file-path.hh"
128 #include "input.hh"
129 #include "international.hh"
130 #include "lily-guile.hh"
131 #include "lily-lexer.hh"
132 #include "lily-parser.hh"
133 #include "main.hh"
134 #include "misc.hh"
135 #include "music.hh"
136 #include "output-def.hh"
137 #include "paper-book.hh"
138 #include "scm-hash.hh"
139 #include "score.hh"
140 #include "text-interface.hh"
141 #include "warn.hh"
142
143 void
144 Lily_parser::parser_error (Input const *i, Lily_parser *parser, SCM *, const string &s)
145 {
146         parser->parser_error (*i, s);
147 }
148
149 #define MYBACKUP(Token, Value, Location)                                \
150 do                                                                      \
151         if (yychar == YYEMPTY)                                          \
152         {                                                               \
153                 if (Token)                                              \
154                         parser->lexer_->push_extra_token (Token, Value); \
155                 parser->lexer_->push_extra_token (BACKUP);              \
156         } else {                                                        \
157                 parser->parser_error                                    \
158                         (Location, _("Too much lookahead"));            \
159         }                                                               \
160 while (0)
161
162
163 #define MYREPARSE(Location, Pred, Token, Value)                         \
164 do                                                                      \
165         if (yychar == YYEMPTY)                                          \
166         {                                                               \
167                 parser->lexer_->push_extra_token (Token, Value);        \
168                 parser->lexer_->push_extra_token (REPARSE,              \
169                                                   Pred);                \
170         } else {                                                        \
171                 parser->parser_error                                    \
172                         (Location, _("Too much lookahead"));            \
173         }                                                               \
174 while (0)
175
176 %}
177
178
179 %{
180
181 #define MY_MAKE_MUSIC(x, spot) \
182         make_music_with_input (ly_symbol2scm (x), \
183                                parser->lexer_->override_input (spot))
184
185 /* ES TODO:
186 - Don't use lily module, create a new module instead.
187 - delay application of the function
188 */
189 #define LOWLEVEL_MAKE_SYNTAX(proc, args)        \
190   scm_apply_0 (proc, args)
191 /* Syntactic Sugar. */
192 #define MAKE_SYNTAX(name, location, ...)                                \
193         LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant (name), scm_list_n (parser->self_scm (), make_input (parser->lexer_->override_input (location)), ##__VA_ARGS__, SCM_UNDEFINED))
194 #define START_MAKE_SYNTAX(name, ...)                                    \
195         scm_list_n (ly_lily_module_constant (name) , ##__VA_ARGS__, SCM_UNDEFINED)
196 #define FINISH_MAKE_SYNTAX(start, location, ...)                        \
197         LOWLEVEL_MAKE_SYNTAX (scm_car (start), scm_cons2 (parser->self_scm (), make_input (parser->lexer_->override_input (location)), scm_append_x (scm_list_2 (scm_cdr (start), scm_list_n (__VA_ARGS__, SCM_UNDEFINED)))))
198
199 SCM get_next_unique_context_id ();
200 SCM get_next_unique_lyrics_context_id ();
201
202 #undef _
203 #if !HAVE_GETTEXT
204 #define _(x) x
205 #else
206 #include <libintl.h>
207 #define _(x) gettext (x)
208 #endif
209
210
211 static Music *make_music_with_input (SCM name, Input where);
212 SCM check_scheme_arg (Lily_parser *parser, Input loc,
213                       SCM arg, SCM args, SCM pred, SCM disp = SCM_UNDEFINED);
214 SCM make_music_from_simple (Lily_parser *parser, Input loc, SCM pitch);
215 SCM loc_on_music (Input loc, SCM arg);
216 SCM make_chord_elements (Input loc, SCM pitch, SCM dur, SCM modification_list);
217 SCM make_chord_step (SCM step, Rational alter);
218 SCM make_simple_markup (SCM a);
219 SCM make_duration (SCM t, int dots = 0);
220 bool is_regular_identifier (SCM id, bool multiple=false);
221 SCM try_string_variants (SCM pred, SCM str);
222 int yylex (YYSTYPE *s, YYLTYPE *loc, Lily_parser *parser);
223
224 %}
225
226 /* The third option is an alias that will be used to display the
227    syntax error.  Bison CVS now correctly handles backslash escapes.
228
229    FIXME: Bison needs to translate some of these, eg, STRING.
230
231 */
232
233 /* Keyword tokens with plain escaped name.  */
234 %token END_OF_FILE 0 "end of input"
235 %token ACCEPTS "\\accepts"
236 %token ADDLYRICS "\\addlyrics"
237 %token ALIAS "\\alias"
238 %token ALTERNATIVE "\\alternative"
239 %token BOOK "\\book"
240 %token BOOKPART "\\bookpart"
241 %token CHANGE "\\change"
242 %token CHORDMODE "\\chordmode"
243 %token CHORDS "\\chords"
244 %token CONSISTS "\\consists"
245 %token CONTEXT "\\context"
246 %token DEFAULT "\\default"
247 %token DEFAULTCHILD "\\defaultchild"
248 %token DENIES "\\denies"
249 %token DESCRIPTION "\\description"
250 %token DRUMMODE "\\drummode"
251 %token DRUMS "\\drums"
252 %token FIGUREMODE "\\figuremode"
253 %token FIGURES "\\figures"
254 %token HEADER "\\header"
255 %token INVALID "\\version-error"
256 %token LAYOUT "\\layout"
257 %token LYRICMODE "\\lyricmode"
258 %token LYRICS "\\lyrics"
259 %token LYRICSTO "\\lyricsto"
260 %token MARKUP "\\markup"
261 %token MARKUPLIST "\\markuplist"
262 %token MIDI "\\midi"
263 %token NAME "\\name"
264 %token NOTEMODE "\\notemode"
265 %token OVERRIDE "\\override"
266 %token PAPER "\\paper"
267 %token REMOVE "\\remove"
268 %token REPEAT "\\repeat"
269 %token REST "\\rest"
270 %token REVERT "\\revert"
271 %token SCORE "\\score"
272 %token SCORELINES "\\score-lines"
273 %token SEQUENTIAL "\\sequential"
274 %token SET "\\set"
275 %token SIMULTANEOUS "\\simultaneous"
276 %token TEMPO "\\tempo"
277 %token TYPE "\\type"
278 %token UNSET "\\unset"
279 %token WITH "\\with"
280
281 /* Keyword token exceptions.  */
282 %token NEWCONTEXT "\\new"
283
284
285 /* Other string tokens.  */
286
287 %token CHORD_BASS "/+"
288 %token CHORD_CARET "^"
289 %token CHORD_COLON ":"
290 %token CHORD_MINUS "-"
291 %token CHORD_SLASH "/"
292 %token ANGLE_OPEN "<"
293 %token ANGLE_CLOSE ">"
294 %token DOUBLE_ANGLE_OPEN "<<"
295 %token DOUBLE_ANGLE_CLOSE ">>"
296 %token E_BACKSLASH "\\"
297 %token E_EXCLAMATION "\\!"
298 %token E_PLUS "\\+"
299 %token EXTENDER "__"
300
301 /*
302 If we give names, Bison complains.
303 */
304 %token FIGURE_CLOSE /* "\\>" */
305 %token FIGURE_OPEN /* "\\<" */
306 %token FIGURE_SPACE "_"
307 %token HYPHEN "--"
308
309 %token MULTI_MEASURE_REST
310
311
312 %token E_UNSIGNED
313 %token UNSIGNED
314
315 /* Artificial tokens, for more generic function syntax */
316 %token EXPECT_MARKUP "markup?"
317 %token EXPECT_SCM "scheme?"
318 %token BACKUP "(backed-up?)"
319 %token REPARSE "(reparsed?)"
320 %token EXPECT_MARKUP_LIST "markup-list?"
321 %token EXPECT_OPTIONAL "optional?"
322 /* After the last argument. */
323 %token EXPECT_NO_MORE_ARGS;
324
325 /* An artificial token for parsing embedded Lilypond */
326 %token EMBEDDED_LILY "#{"
327
328 %token BOOK_IDENTIFIER
329 %token CHORD_BODY_IDENTIFIER
330 %token CHORD_MODIFIER
331 %token CHORD_REPETITION
332 %token CONTEXT_DEF_IDENTIFIER
333 %token CONTEXT_MOD_IDENTIFIER
334 %token DRUM_PITCH
335 %token PITCH_IDENTIFIER
336 %token PITCH_ARG
337 %token DURATION_IDENTIFIER
338 %token EVENT_IDENTIFIER
339 %token EVENT_FUNCTION
340 %token FRACTION
341 %token LYRIC_ELEMENT
342 %token MARKUP_FUNCTION
343 %token MARKUP_LIST_FUNCTION
344 %token MARKUP_IDENTIFIER
345 %token MARKUPLIST_IDENTIFIER
346 %token MUSIC_FUNCTION
347 %token MUSIC_IDENTIFIER
348 %token NOTENAME_PITCH
349 %token NUMBER_IDENTIFIER
350 %token OUTPUT_DEF_IDENTIFIER
351 %token REAL
352 %token REPEAT_IDENTIFIER
353 %token RESTNAME
354 %token SCM_ARG
355 %token SCM_FUNCTION
356 %token SCM_IDENTIFIER
357 %token SCM_TOKEN
358 %token STRING
359 %token SYMBOL_LIST
360 %token TONICNAME_PITCH
361
362 %left '-' '+'
363
364 /* We don't assign precedence to / and *, because we might need varied
365 prec levels in different prods */
366
367 %left UNARY_MINUS
368
369 %%
370
371 start_symbol:
372         lilypond
373         | EMBEDDED_LILY {
374                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
375                 parser->lexer_->push_note_state (nn);
376         } embedded_lilypond {
377                 parser->lexer_->pop_state ();
378                 *retval = $3;
379         }
380         ;
381
382 lilypond:       /* empty */ { $$ = SCM_UNSPECIFIED; }
383         | lilypond toplevel_expression {
384         }
385         | lilypond assignment {
386         }
387         | lilypond error {
388                 parser->error_level_ = 1;
389         }
390         | lilypond INVALID      {
391                 parser->error_level_ = 1;
392         }
393         ;
394
395
396 toplevel_expression:
397         {
398                 parser->lexer_->add_scope (get_header (parser));
399         } lilypond_header {
400                 parser->lexer_->set_identifier (ly_symbol2scm ("$defaultheader"), $2);
401         }
402         | book_block {
403                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-book-handler");
404                 scm_call_2 (proc, parser->self_scm (), $1);
405         }
406         | bookpart_block {
407                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-bookpart-handler");
408                 scm_call_2 (proc, parser->self_scm (), $1);
409         }
410         | score_block {
411                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-score-handler");
412                 scm_call_2 (proc, parser->self_scm (), $1);
413         }
414         | composite_music {
415                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-music-handler");
416                 scm_call_2 (proc, parser->self_scm (), $1);
417         }
418         | full_markup {
419                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-text-handler");
420                 scm_call_2 (proc, parser->self_scm (), scm_list_1 ($1));
421         }
422         | full_markup_list {
423                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-text-handler");
424                 scm_call_2 (proc, parser->self_scm (), $1);
425         }
426         | SCM_TOKEN {
427                 // Evaluate and ignore #xxx, as opposed to \xxx
428                 parser->lexer_->eval_scm_token ($1);
429         }
430         | embedded_scm_active
431         {
432                 SCM out = SCM_UNDEFINED;
433                 if (Text_interface::is_markup ($1))
434                         out = scm_list_1 ($1);
435                 else if (Text_interface::is_markup_list ($1))
436                         out = $1;
437                 if (scm_is_pair (out))
438                 {
439                         SCM proc = parser->lexer_->lookup_identifier ("toplevel-text-handler");
440                         scm_call_2 (proc, parser->self_scm (), out);
441                 } else if (!scm_is_eq ($1, SCM_UNSPECIFIED))
442                         parser->parser_error (@1, _("bad expression type"));
443         }
444         | output_def {
445                 SCM id = SCM_EOL;
446                 Output_def * od = unsmob_output_def ($1);
447
448                 if (od->c_variable ("is-paper") == SCM_BOOL_T)
449                         id = ly_symbol2scm ("$defaultpaper");
450                 else if (od->c_variable ("is-midi") == SCM_BOOL_T)
451                         id = ly_symbol2scm ("$defaultmidi");
452                 else if (od->c_variable ("is-layout") == SCM_BOOL_T)
453                         id = ly_symbol2scm ("$defaultlayout");
454
455                 parser->lexer_->set_identifier (id, $1);
456         }
457         ;
458
459 embedded_scm_bare:
460         SCM_TOKEN
461         {
462                 $$ = parser->lexer_->eval_scm_token ($1);
463         }
464         | SCM_IDENTIFIER
465         ;
466
467 embedded_scm_active:
468         SCM_IDENTIFIER
469         | scm_function_call
470         ;
471
472 embedded_scm_bare_arg:
473         SCM_ARG
474         | SCM_TOKEN
475         {
476                 $$ = parser->lexer_->eval_scm_token ($1);
477         }
478         | FRACTION
479         | full_markup_list
480         | context_modification
481         | score_block
482         | context_def_spec_block
483         | book_block
484         | bookpart_block
485         | output_def
486         ;
487
488 /* The generic version may end in music, or not */
489
490 embedded_scm:
491         embedded_scm_bare
492         | scm_function_call
493         ;
494
495 /* embedded_scm_arg is _not_ casting pitches to music by default, this
496  * has to be done by the function itself.  Note that this may cause
497  * the results of scm_function_call or embedded_scm_bare_arg to be
498  * turned into music from pitches as well.  Note that this creates a
499  * distinctly awkward situation for calculated drum pitches.  Those
500  * are at the current point of time rejected as music constituents as
501  * they can't be distinguished from "proper" symbols.
502  */
503
504 embedded_scm_arg:
505         embedded_scm_bare_arg
506         | scm_function_call
507         | music_assign
508         ;
509
510 scm_function_call:
511         SCM_FUNCTION function_arglist {
512                 $$ = MAKE_SYNTAX ("music-function", @$,
513                                          $1, $2);
514         }
515         ;
516
517 embedded_lilypond_number:
518         '-' embedded_lilypond_number
519         {
520                 $$ = scm_difference ($2, SCM_UNDEFINED);
521         }
522         | bare_number_common
523         | UNSIGNED NUMBER_IDENTIFIER
524         {
525                 $$ = scm_product ($1, $2);
526         }
527         ;
528
529 embedded_lilypond:
530         /* empty */
531         {
532                 // FIXME: @$ does not contain a useful source location
533                 // for empty rules, and the only token in the whole
534                 // production, EMBEDDED_LILY, is synthetic and also
535                 // contains no source location.
536                 $$ = MAKE_SYNTAX ("void-music", @$);
537         }
538         | identifier_init_nonumber
539         | embedded_lilypond_number
540         | post_event post_events
541         {
542                 $$ = scm_reverse_x ($2, SCM_EOL);
543                 if (Music *m = unsmob_music ($1))
544                 {
545                         if (m->is_mus_type ("post-event-wrapper"))
546                                 $$ = scm_append
547                                         (scm_list_2 (m->get_property ("elements"),
548                                                      $$));
549                         else
550                                 $$ = scm_cons ($1, $$);
551                 }
552                 if (scm_is_pair ($$)
553                     && scm_is_null (scm_cdr ($$)))
554                         $$ = scm_car ($$);
555                 else
556                 {
557                         Music * m = MY_MAKE_MUSIC ("PostEvents", @$);
558                         m->set_property ("elements", $$);
559                         $$ = m->unprotect ();
560                 }
561         }
562         | multiplied_duration
563         | music_embedded music_embedded music_list {
564                 $3 = scm_reverse_x ($3, SCM_EOL);
565                 if (unsmob_music ($2))
566                         $3 = scm_cons ($2, $3);
567                 if (unsmob_music ($1))
568                         $3 = scm_cons ($1, $3);
569                 $$ = MAKE_SYNTAX ("sequential-music", @$, $3);
570         }
571         | error {
572                 parser->error_level_ = 1;
573                 $$ = SCM_UNSPECIFIED;
574         }
575         | INVALID embedded_lilypond {
576                 parser->error_level_ = 1;
577                 $$ = $2;
578         }
579         ;
580
581
582 lilypond_header_body:
583         /* empty */ { $$ = SCM_UNSPECIFIED; }
584         | lilypond_header_body assignment  {
585
586         }
587         | lilypond_header_body embedded_scm  {
588
589         }
590         ;
591
592 lilypond_header:
593         HEADER '{' lilypond_header_body '}'     {
594                 $$ = parser->lexer_->remove_scope ();
595         }
596         ;
597
598 /*
599         DECLARATIONS
600 */
601 assignment_id:
602         STRING          { $$ = $1; }
603         ;
604
605 assignment:
606         assignment_id '=' identifier_init  {
607                 parser->lexer_->set_identifier ($1, $3);
608                 $$ = SCM_UNSPECIFIED;
609         }
610         | assignment_id property_path '=' identifier_init {
611                 SCM path = scm_cons (scm_string_to_symbol ($1), $2);
612                 parser->lexer_->set_identifier (path, $4);
613                 $$ = SCM_UNSPECIFIED;
614         }
615         ;
616
617
618 identifier_init:
619         identifier_init_nonumber
620         | number_expression
621         | post_event_nofinger post_events
622         {
623                 $$ = scm_reverse_x ($2, SCM_EOL);
624                 if (Music *m = unsmob_music ($1))
625                 {
626                         if (m->is_mus_type ("post-event-wrapper"))
627                                 $$ = scm_append
628                                         (scm_list_2 (m->get_property ("elements"),
629                                                      $$));
630                         else
631                                 $$ = scm_cons ($1, $$);
632                 }
633                 if (scm_is_pair ($$)
634                     && scm_is_null (scm_cdr ($$)))
635                         $$ = scm_car ($$);
636                 else
637                 {
638                         Music * m = MY_MAKE_MUSIC ("PostEvents", @$);
639                         m->set_property ("elements", $$);
640                         $$ = m->unprotect ();
641                 }
642         }
643         ;
644
645 identifier_init_nonumber:
646         score_block
647         | book_block
648         | bookpart_block
649         | output_def
650         | context_def_spec_block
651         | music_assign
652         | FRACTION
653         | string
654         | embedded_scm
655         | full_markup_list
656         | context_modification
657         ;
658
659 context_def_spec_block:
660         CONTEXT '{' context_def_spec_body '}'
661                 {
662                 $$ = $3;
663                 unsmob_context_def ($$)->origin ()->set_spot (@$);
664         }
665         ;
666
667 context_mod_arg:
668         embedded_scm
669         |
670         {
671                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
672                 parser->lexer_->push_note_state (nn);
673         }
674         composite_music
675         {
676                 parser->lexer_->pop_state ();
677                 $$ = $2;
678         }
679         ;
680
681 context_mod_embedded:
682         context_mod_arg
683         {
684                 if (unsmob_music ($1)) {
685                         SCM proc = parser->lexer_->lookup_identifier ("context-mod-music-handler");
686                         $1 = scm_call_2 (proc, parser->self_scm (), $1);
687                 }
688                 if (unsmob_context_mod ($1))
689                         $$ = $1;
690                 else {
691                         parser->parser_error (@1, _ ("not a context mod"));
692                         $$ = Context_mod ().smobbed_copy ();
693                 }
694         }
695         ;
696
697
698 context_def_spec_body:
699         /**/ {
700                 $$ = Context_def::make_scm ();
701         }
702         | CONTEXT_DEF_IDENTIFIER {
703                 $$ = $1;
704         }
705         | context_def_spec_body context_mod {
706                 if (!SCM_UNBNDP ($2))
707                         unsmob_context_def ($$)->add_context_mod ($2);
708         }
709         | context_def_spec_body context_modification {
710                 Context_def *td = unsmob_context_def ($$);
711                 SCM new_mods = unsmob_context_mod ($2)->get_mods ();
712                 for (SCM m = new_mods; scm_is_pair (m); m = scm_cdr (m)) {
713                     td->add_context_mod (scm_car (m));
714                 }
715         }
716         | context_def_spec_body context_mod_embedded {
717                 Context_def *td = unsmob_context_def ($$);
718                 SCM new_mods = unsmob_context_mod ($2)->get_mods ();
719                 for (SCM m = new_mods; scm_is_pair (m); m = scm_cdr (m)) {
720                     td->add_context_mod (scm_car (m));
721                 }
722         }
723         ;
724
725
726
727 book_block:
728         BOOK '{' book_body '}'  {
729                 $$ = $3;
730                 unsmob_book ($$)->origin ()->set_spot (@$);
731                 pop_paper (parser);
732                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-book"), SCM_BOOL_F);
733         }
734         ;
735
736 /* FIXME:
737    * Use 'handlers' like for toplevel-* stuff?
738    * grok \layout and \midi?  */
739 book_body:
740         {
741                 Book *book = new Book;
742                 init_papers (parser);
743                 book->paper_ = dynamic_cast<Output_def*> (unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultpaper"))->clone ());
744                 book->paper_->unprotect ();
745                 push_paper (parser, book->paper_);
746                 book->header_ = get_header (parser);
747                 $$ = book->unprotect ();
748                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-book"), $$);
749         }
750         | BOOK_IDENTIFIER {
751                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-book"), $1);
752         }
753         | book_body paper_block {
754                 unsmob_book ($1)->paper_ = unsmob_output_def ($2);
755                 set_paper (parser, unsmob_output_def ($2));
756         }
757         | book_body bookpart_block {
758                 SCM proc = parser->lexer_->lookup_identifier ("book-bookpart-handler");
759                 scm_call_2 (proc, $1, $2);
760         }
761         | book_body score_block {
762                 SCM proc = parser->lexer_->lookup_identifier ("book-score-handler");
763                 scm_call_2 (proc, $1, $2);
764         }
765         | book_body composite_music {
766                 SCM proc = parser->lexer_->lookup_identifier ("book-music-handler");
767                 scm_call_3 (proc, parser->self_scm (), $1, $2);
768         }
769         | book_body full_markup {
770                 SCM proc = parser->lexer_->lookup_identifier ("book-text-handler");
771                 scm_call_2 (proc, $1, scm_list_1 ($2));
772         }
773         | book_body full_markup_list {
774                 SCM proc = parser->lexer_->lookup_identifier ("book-text-handler");
775                 scm_call_2 (proc, $1, $2);
776         }
777         | book_body SCM_TOKEN {
778                 // Evaluate and ignore #xxx, as opposed to \xxx
779                 parser->lexer_->eval_scm_token ($2);
780         }
781         | book_body embedded_scm_active
782         {
783                 SCM out = SCM_UNDEFINED;
784                 if (Text_interface::is_markup ($2))
785                         out = scm_list_1 ($2);
786                 else if (Text_interface::is_markup_list ($2))
787                         out = $2;
788                 if (scm_is_pair (out))
789                 {
790                         SCM proc = parser->lexer_->lookup_identifier ("book-text-handler");
791                         scm_call_2 (proc, $1, out);
792                 } else if (!scm_is_eq ($2, SCM_UNSPECIFIED))
793                         parser->parser_error (@2, _("bad expression type"));
794         }
795         | book_body
796         {
797                 parser->lexer_->add_scope (unsmob_book ($1)->header_);
798         } lilypond_header
799         | book_body error {
800                 Book *book = unsmob_book ($1);
801                 book->paper_ = 0;
802                 book->scores_ = SCM_EOL;
803                 book->bookparts_ = SCM_EOL;
804         }
805         ;
806
807 bookpart_block:
808         BOOKPART '{' bookpart_body '}' {
809                 $$ = $3;
810                 unsmob_book ($$)->origin ()->set_spot (@$);
811                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), SCM_BOOL_F);
812         }
813         ;
814
815 bookpart_body:
816         {
817                 Book *book = new Book;
818                 $$ = book->unprotect ();
819                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), $$);
820         }
821         | BOOK_IDENTIFIER {
822                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), $1);
823         }
824         | bookpart_body paper_block {
825                 unsmob_book ($$)->paper_ = unsmob_output_def ($2);
826         }
827         | bookpart_body score_block {
828                 SCM proc = parser->lexer_->lookup_identifier ("bookpart-score-handler");
829                 scm_call_2 (proc, $1, $2);
830         }
831         | bookpart_body composite_music {
832                 SCM proc = parser->lexer_->lookup_identifier ("bookpart-music-handler");
833                 scm_call_3 (proc, parser->self_scm (), $1, $2);
834         }
835         | bookpart_body full_markup {
836                 SCM proc = parser->lexer_->lookup_identifier ("bookpart-text-handler");
837                 scm_call_2 (proc, $1, scm_list_1 ($2));
838         }
839         | bookpart_body full_markup_list {
840                 SCM proc = parser->lexer_->lookup_identifier ("bookpart-text-handler");
841                 scm_call_2 (proc, $1, $2);
842         }
843         | bookpart_body SCM_TOKEN {
844                 // Evaluate and ignore #xxx, as opposed to \xxx
845                 parser->lexer_->eval_scm_token ($2);
846         }
847         | bookpart_body embedded_scm_active
848         {
849                 SCM out = SCM_UNDEFINED;
850                 if (Text_interface::is_markup ($2))
851                         out = scm_list_1 ($2);
852                 else if (Text_interface::is_markup_list ($2))
853                         out = $2;
854                 if (scm_is_pair (out))
855                 {
856                         SCM proc = parser->lexer_->lookup_identifier ("bookpart-text-handler");
857                         scm_call_2 (proc, $1, out);
858                 } else if (!scm_is_eq ($2, SCM_UNSPECIFIED))
859                         parser->parser_error (@2, _("bad expression type"));
860         }
861         | bookpart_body
862         {
863                 Book *book = unsmob_book ($1);
864                 if (!ly_is_module (book->header_))
865                         book->header_ = ly_make_module (false);
866                 parser->lexer_->add_scope (book->header_);
867         } lilypond_header
868         | bookpart_body error {
869                 Book *book = unsmob_book ($1);
870                 book->paper_ = 0;
871                 book->scores_ = SCM_EOL;
872         }
873         ;
874
875 score_block:
876         SCORE '{' score_body '}'        {
877                 $$ = $3;
878         }
879         ;
880
881 score_headers:
882         /* empty */
883         {
884                 $$ = SCM_EOL;
885         }
886         | score_headers
887         {
888                 if (!scm_is_pair ($1)
889                     || !ly_is_module (scm_car ($1)))
890                         $1 = scm_cons (ly_make_module (false), $1);
891                 parser->lexer_->add_scope (scm_car ($1));
892         } lilypond_header
893         {
894                 $$ = $1;
895         }
896         | score_headers output_def
897         {
898                 Output_def *od = unsmob_output_def ($2);
899                 if (od->lookup_variable (ly_symbol2scm ("is-paper")) == SCM_BOOL_T)
900                 {
901                         parser->parser_error (@2, _("\\paper cannot be used in \\score, use \\layout instead"));
902
903                 }
904                 else
905                 {
906                         if (scm_is_pair ($1) && ly_is_module (scm_car ($1)))
907                                 scm_set_cdr_x ($1, scm_cons ($2, scm_cdr ($1)));
908                         else
909                                 $$ = scm_cons ($2, $1);
910                 }
911         }
912         ;
913
914                 
915
916 score_body:
917         score_headers music {
918                 SCM scorify = ly_lily_module_constant ("scorify-music");
919                 $$ = scm_call_2 (scorify, $2, parser->self_scm ());
920
921                 unsmob_score ($$)->origin ()->set_spot (@2);
922                 if (scm_is_pair ($1) && ly_is_module (scm_car ($1)))
923                 {
924                         unsmob_score ($$)->set_header (scm_car ($1));
925                         $1 = scm_cdr ($1);
926                 }
927                 for (SCM p = scm_reverse_x ($1, SCM_EOL);
928                      scm_is_pair (p); p = scm_cdr (p))
929                 {
930                         unsmob_score ($$)->
931                                 add_output_def (unsmob_output_def (scm_car (p)));
932                 }
933         }
934         | embedded_scm_active {
935                 Score *score;
936                 if (unsmob_score ($1))
937                         score = new Score (*unsmob_score ($1));
938                 else {
939                         score = new Score;
940                         parser->parser_error (@1, _("score expected"));
941                 }
942                 unsmob_score ($$)->origin ()->set_spot (@$);
943                 $$ = score->unprotect ();
944         }
945         | score_body
946         {
947                 Score *score = unsmob_score ($1);
948                 if (!ly_is_module (score->get_header ()))
949                         score->set_header (ly_make_module (false));
950                 parser->lexer_->add_scope (score->get_header ());
951         } lilypond_header
952         | score_body output_def {
953                 Output_def *od = unsmob_output_def ($2);
954                 if (od->lookup_variable (ly_symbol2scm ("is-paper")) == SCM_BOOL_T)
955                 {
956                         parser->parser_error (@2, _("\\paper cannot be used in \\score, use \\layout instead"));
957
958                 }
959                 else
960                 {
961                         unsmob_score ($1)->add_output_def (od);
962                 }
963         }
964         | score_body error {
965                 unsmob_score ($$)->error_found_ = true;
966         }
967         ;
968
969
970 /*
971         OUTPUT DEF
972 */
973
974 paper_block:
975         output_def {
976                 Output_def *od = unsmob_output_def ($1);
977
978                 if (od->lookup_variable (ly_symbol2scm ("is-paper")) != SCM_BOOL_T)
979                 {
980                         parser->parser_error (@1, _ ("need \\paper for paper block"));
981                         $$ = get_paper (parser)->unprotect ();
982                 }
983         }
984         ;
985
986
987 output_def:
988         output_def_body '}' {
989                 $$ = $1;
990
991                 parser->lexer_->remove_scope ();
992                 parser->lexer_->pop_state ();
993         }
994         ;
995
996 output_def_head:
997         PAPER {
998                 Output_def *p = get_paper (parser);
999                 p->input_origin_ = @$;
1000                 parser->lexer_->add_scope (p->scope_);
1001                 $$ = p->unprotect ();
1002         }
1003         | MIDI    {
1004                 Output_def *p = get_midi (parser);
1005                 $$ = p->unprotect ();
1006                 parser->lexer_->add_scope (p->scope_);
1007         }
1008         | LAYOUT        {
1009                 Output_def *p = get_layout (parser);
1010
1011                 parser->lexer_->add_scope (p->scope_);
1012                 $$ = p->unprotect ();
1013         }
1014         ;
1015
1016 output_def_head_with_mode_switch:
1017         output_def_head {
1018                 parser->lexer_->push_initial_state ();
1019                 $$ = $1;
1020         }
1021         ;
1022
1023 // We need this weird nonterminal because both music as well as a
1024 // context definition can start with \context and the difference is
1025 // only apparent after looking at the next token.  If it is '{', there
1026 // is still time to escape from notes mode.
1027
1028 music_or_context_def:
1029         music_arg
1030         | context_def_spec_block
1031         ;
1032
1033 output_def_body:
1034         output_def_head_with_mode_switch '{' {
1035                 $$ = $1;
1036                 unsmob_output_def ($$)->input_origin_.set_spot (@$);
1037         }
1038         | output_def_head_with_mode_switch '{' OUTPUT_DEF_IDENTIFIER    {
1039                 Output_def *o = unsmob_output_def ($3);
1040                 o->input_origin_.set_spot (@$);
1041                 $$ = o->self_scm ();
1042                 parser->lexer_->remove_scope ();
1043                 parser->lexer_->add_scope (o->scope_);
1044         }
1045         | output_def_body assignment  {
1046
1047         }
1048         | output_def_body embedded_scm  {
1049
1050         }
1051         | output_def_body
1052         {
1053                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
1054                 parser->lexer_->push_note_state (nn);
1055         } music_or_context_def
1056         {
1057                 parser->lexer_->pop_state ();
1058                 if (unsmob_context_def ($3))
1059                         assign_context_def (unsmob_output_def ($1), $3);
1060                 else {
1061
1062                         SCM proc = parser->lexer_->lookup_identifier
1063                                      ("output-def-music-handler");
1064                         scm_call_3 (proc, parser->self_scm (),
1065                                     $1, $3);
1066                 }
1067         }
1068         | output_def_body error {
1069
1070         }
1071         ;
1072
1073 tempo_event:
1074         TEMPO steno_duration '=' tempo_range    {
1075                 $$ = MAKE_SYNTAX ("tempo", @$, SCM_EOL, $2, $4);
1076         }
1077         | TEMPO scalar steno_duration '=' tempo_range   {
1078                 $$ = MAKE_SYNTAX ("tempo", @$, $2, $3, $5);
1079         }
1080         | TEMPO scalar {
1081                 $$ = MAKE_SYNTAX ("tempo", @$, $2);
1082         } %prec ':'
1083         ;
1084
1085 /*
1086 The representation of a  list is reversed to have efficient append.  */
1087
1088 music_list:
1089         /* empty */ {
1090                 $$ = SCM_EOL;
1091         }
1092         | music_list music_embedded {
1093                 if (unsmob_music ($2))
1094                         $$ = scm_cons ($2, $1);
1095         }
1096         | music_list error {
1097                 Music *m = MY_MAKE_MUSIC("Music", @$);
1098                 // ugh. code dup
1099                 m->set_property ("error-found", SCM_BOOL_T);
1100                 $$ = scm_cons (m->self_scm (), $1);
1101                 m->unprotect (); /* UGH */
1102         }
1103         ;
1104
1105 braced_music_list:
1106         '{' music_list '}'
1107         {
1108                 $$ = scm_reverse_x ($2, SCM_EOL);
1109         }
1110         ;
1111
1112 music:  music_arg
1113         | lyric_element_music
1114         ;
1115
1116 music_embedded:
1117         music
1118         {
1119                 if (unsmob_music ($1)->is_mus_type ("post-event")) {
1120                         parser->parser_error (@1, _ ("unexpected post-event"));
1121                         $$ = SCM_UNSPECIFIED;
1122                 }
1123         }
1124         | music_embedded_backup
1125         {
1126                 $$ = $1;
1127         }
1128         | music_embedded_backup BACKUP lyric_element_music
1129         {
1130                 $$ = $3;
1131         }
1132         | multiplied_duration post_events
1133         {
1134                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
1135
1136                 parser->default_duration_ = *unsmob_duration ($1);
1137                 n->set_property ("duration", $1);
1138
1139                 if (scm_is_pair ($2))
1140                         n->set_property ("articulations",
1141                                          scm_reverse_x ($2, SCM_EOL));
1142                 $$ = n->unprotect ();
1143         }
1144         ;
1145
1146 music_embedded_backup:
1147         embedded_scm
1148         {
1149                 if (scm_is_eq ($1, SCM_UNSPECIFIED))
1150                         $$ = $1;
1151                 else if (Music *m = unsmob_music ($1)) {
1152                         if (m->is_mus_type ("post-event")) {
1153                                 parser->parser_error
1154                                         (@1, _ ("unexpected post-event"));
1155                                 $$ = SCM_UNSPECIFIED;
1156                         } else
1157                                 $$ = $1;
1158                 } else if (parser->lexer_->is_lyric_state ()
1159                            && Text_interface::is_markup ($1))
1160                         MYBACKUP (LYRIC_ELEMENT, $1, @1);
1161                 else {
1162                         @$.warning (_ ("Ignoring non-music expression"));
1163                         $$ = $1;
1164                 }
1165         }
1166         ;
1167
1168 music_arg:
1169         simple_music
1170         {
1171                 $$ = make_music_from_simple (parser, @1, $1);
1172                 if (!unsmob_music ($$))
1173                 {
1174                         parser->parser_error (@1, _ ("music expected"));
1175                         $$ = MAKE_SYNTAX ("void-music", @$);
1176                 }
1177         }
1178         | composite_music %prec COMPOSITE
1179         ;
1180
1181 music_assign:
1182         simple_music
1183         | composite_music %prec COMPOSITE
1184         ;
1185
1186 repeated_music:
1187         REPEAT simple_string unsigned_number music
1188         {
1189                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, SCM_EOL);
1190         }
1191         | REPEAT_IDENTIFIER music
1192         {
1193                 $$ = MAKE_SYNTAX ("repeat", @$, scm_car ($1), scm_cdr ($1),
1194                                   $2, SCM_EOL);
1195         }
1196         | REPEAT simple_string unsigned_number music ALTERNATIVE braced_music_list
1197         {
1198                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, $6);
1199         }
1200         | REPEAT_IDENTIFIER music ALTERNATIVE braced_music_list
1201         {
1202                 $$ = MAKE_SYNTAX ("repeat", @$, scm_car ($1), scm_cdr ($1),
1203                                   $2, $4);
1204         }
1205         ;
1206
1207 sequential_music:
1208         SEQUENTIAL braced_music_list {
1209                 $$ = MAKE_SYNTAX ("sequential-music", @$, $2);
1210         }
1211         | braced_music_list {
1212                 $$ = MAKE_SYNTAX ("sequential-music", @$, $1);
1213         }
1214         ;
1215
1216 simultaneous_music:
1217         SIMULTANEOUS braced_music_list {
1218                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, $2);
1219         }
1220         | DOUBLE_ANGLE_OPEN music_list DOUBLE_ANGLE_CLOSE       {
1221                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, scm_reverse_x ($2, SCM_EOL));
1222         }
1223         ;
1224
1225 simple_music:
1226         event_chord
1227         | music_property_def
1228         | context_change
1229         ;
1230
1231 context_modification:
1232         WITH
1233         {
1234                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
1235                 parser->lexer_->push_note_state (nn);
1236         } '{' context_mod_list '}'
1237         {
1238                 parser->lexer_->pop_state ();
1239                 $$ = $4;
1240         }
1241         | WITH CONTEXT_MOD_IDENTIFIER
1242         {
1243                 $$ = $2;
1244         }
1245         | CONTEXT_MOD_IDENTIFIER
1246         {
1247                 $$ = $1;
1248         }
1249         | WITH context_modification_arg
1250         {
1251                 if (unsmob_music ($2)) {
1252                         SCM proc = parser->lexer_->lookup_identifier ("context-mod-music-handler");
1253                         $2 = scm_call_2 (proc, parser->self_scm (), $2);
1254                 }
1255                 if (unsmob_context_mod ($2))
1256                         $$ = $2;
1257                 else {
1258                         parser->parser_error (@2, _ ("not a context mod"));
1259                         $$ = Context_mod ().smobbed_copy ();
1260                 }
1261         }
1262         ;
1263
1264 context_modification_arg:
1265         embedded_scm_closed
1266         | MUSIC_IDENTIFIER
1267         ;
1268
1269 optional_context_mod:
1270         /**/ {
1271             $$ = SCM_EOL;
1272         }
1273         | context_modification
1274         {
1275               $$ = $1;
1276         }
1277         ;
1278
1279 context_mod_list:
1280         /**/ {
1281             $$ = Context_mod ().smobbed_copy ();
1282         }
1283         | context_mod_list context_mod  {
1284                 if (!SCM_UNBNDP ($2))
1285                         unsmob_context_mod ($1)->add_context_mod ($2);
1286         }
1287         | context_mod_list CONTEXT_MOD_IDENTIFIER {
1288                  Context_mod *md = unsmob_context_mod ($2);
1289                  if (md)
1290                      unsmob_context_mod ($1)->add_context_mods (md->get_mods ());
1291         }
1292         | context_mod_list context_mod_embedded {
1293                 unsmob_context_mod ($1)->add_context_mods
1294                         (unsmob_context_mod ($2)->get_mods ());
1295         }
1296         ;
1297
1298 composite_music:
1299         complex_music
1300         | music_bare
1301         ;
1302
1303 /* Music that can be parsed without lookahead */
1304 closed_music:
1305         music_bare
1306         | complex_music_prefix closed_music
1307         {
1308                 $$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
1309         }
1310         | music_function_call_closed
1311         ;
1312
1313 music_bare:
1314         mode_changed_music
1315         | MUSIC_IDENTIFIER
1316         | grouped_music_list
1317         ;
1318
1319 grouped_music_list:
1320         simultaneous_music              { $$ = $1; }
1321         | sequential_music              { $$ = $1; }
1322         ;
1323
1324 /* Function argument lists are arguably the most complex part in the
1325  * parser.  They are pretty tricky to understand because of the way
1326  * they are processed, and because of optional arguments that can be
1327  * omitted.  When there are several optional arguments in a row,
1328  * omitting one automatically omits all following arguments.  Optional
1329  * arguments can only be omitted when either
1330  *
1331  * a) the omission is explicitly started with \default
1332  * b) the omission is implicitly started by an argument not matching
1333  *    its predicate, and there is a mandatory argument later that can
1334  *    "catch" the argument that does not fit.
1335  *
1336  * When argument parsing starts, the lexer pushes EXPECT_SCM tokens
1337  * (corresponding to mandatory arguments and having a predicate
1338  * function as semantic value) or EXPECT_OPTIONAL EXPECT_SCM (where
1339  * the semantic value of the EXPECT_OPTIONAL token is the default to
1340  * use when the optional argument is omitted, and EXPECT_SCM again has
1341  * the argument predicate as semantic value) in reverse order to the
1342  * parser, followed by EXPECT_NO_MORE_ARGS.  The argument list is then
1343  * processed inside-out while actual tokens are consumed.
1344  *
1345  * This means that the argument list tokens determine the actions
1346  * taken as they arrive.  The structure of the argument list is known
1347  * to the parser and stored in its parse stack when the first argument
1348  * is being parsed.  What the parser does not know is which predicates
1349  * will match and whether or not \default will be appearing in the
1350  * argument list, and where.
1351  *
1352  * Many of the basic nonterminals used for argument list scanning come
1353  * in a "normal" and a "closed" flavor.  A closed expression is one
1354  * that can be parsed without a lookahead token.  That makes it
1355  * feasible for an optional argument that may need to be skipped:
1356  * skipping can only be accomplished by pushing back the token into
1357  * the lexer, and that only works when there is no lookahead token.
1358  *
1359  * Sequences of 0 or more optional arguments are scanned using either
1360  * function_arglist_backup or function_arglist_nonbackup.  The first
1361  * is used when optional arguments are followed by at least one
1362  * mandatory argument: in that case optional arguments may be skipped
1363  * by either a false predicate (in which case the expression will be
1364  * pushed back as one or more tokens, preceded by a BACKUP token) or
1365  * by using \default.
1366  *
1367  * If optional arguments are at the end of the argument list, they are
1368  * instead scanned using function_arglist_nonbackup: here the only
1369  * manner to enter into skipping of optional arguments is the use of
1370  * \default.
1371  *
1372  * The argument list of a normal function call is parsed using
1373  * function_arglist.  The part of an argument list before a mandatory
1374  * argument is parsed using function_arglist_optional.
1375  *
1376  * The difference is that leading optional arguments are scanned using
1377  * function_arglist_nonbackup and function_arglist_backup,
1378  * respectively.
1379  *
1380  * Most other details are obvious in the rules themselves.
1381  *
1382  */
1383
1384 function_arglist_nonbackup_common:
1385         EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup post_event_nofinger
1386         {
1387                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1388         }
1389         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' UNSIGNED
1390         {
1391                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1392                 if (scm_is_true (scm_call_1 ($2, n)))
1393                         $$ = scm_cons (n, $3);
1394                 else {
1395                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1396                         t->set_property ("digit", $5);
1397                         $$ = check_scheme_arg (parser, @4, t->unprotect (),
1398                                                $3, $2, n);
1399                 }
1400                 
1401         }
1402         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' REAL
1403         {
1404                 $$ = check_scheme_arg (parser, @4,
1405                                        scm_difference ($5, SCM_UNDEFINED),
1406                                        $3, $2);
1407         }
1408         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' NUMBER_IDENTIFIER
1409         {
1410                 $$ = check_scheme_arg (parser, @4,
1411                                        scm_difference ($5, SCM_UNDEFINED),
1412                                        $3, $2);
1413         }
1414         ;
1415
1416 function_arglist_closed_nonbackup:
1417         function_arglist_nonbackup_common
1418         | function_arglist_closed_common
1419         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup embedded_scm_arg_closed
1420         {
1421                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1422         }
1423         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup bare_number_closed
1424         {
1425                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1426         }
1427         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup SCM_IDENTIFIER
1428         {
1429                 $$ = check_scheme_arg (parser, @4,
1430                                        try_string_variants ($2, $4),
1431                                        $3, $2, $4);
1432         }
1433         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup STRING
1434         {
1435                 $$ = check_scheme_arg (parser, @4,
1436                                        try_string_variants ($2, $4),
1437                                        $3, $2, $4);
1438         }
1439         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup full_markup
1440         {
1441                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1442         }
1443         ;
1444
1445 symbol_list_arg:
1446         SYMBOL_LIST
1447         | SYMBOL_LIST '.' symbol_list_rev
1448         {
1449                 $$ = scm_append (scm_list_2 ($1, scm_reverse_x ($3, SCM_EOL)));
1450         }
1451         ;
1452
1453 symbol_list_rev:
1454         symbol_list_part
1455         | symbol_list_rev '.' symbol_list_part
1456         {
1457                 $$ = scm_append_x (scm_list_2 ($3, $1));
1458         }
1459         ;
1460
1461 // symbol_list_part delivers elements in reverse copy.
1462
1463 symbol_list_part:
1464         symbol_list_element
1465         {
1466                 SCM sym_l_p = ly_lily_module_constant ("symbol-list?");
1467                 $$ = try_string_variants (sym_l_p, $1);
1468                 if (SCM_UNBNDP ($$)) {
1469                         parser->parser_error (@1, _("not a symbol"));
1470                         $$ = SCM_EOL;
1471                 } else
1472                         $$ = scm_reverse ($$);
1473         }
1474         ;
1475
1476
1477 symbol_list_element:
1478         STRING
1479         | embedded_scm_bare
1480         ;
1481
1482
1483 function_arglist_nonbackup:
1484         function_arglist_nonbackup_common
1485         | function_arglist_common
1486         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup embedded_scm_arg
1487         {
1488                 if (scm_is_true (scm_call_1 ($2, $4)))
1489                         $$ = scm_cons ($4, $3);
1490                 else
1491                         $$ = check_scheme_arg (parser, @4,
1492                                                make_music_from_simple
1493                                                (parser, @4, $4),
1494                                                $3, $2);
1495         }
1496         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup bare_number_common
1497         {
1498                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1499         }
1500         | function_arglist_nonbackup_reparse REPARSE duration_length
1501         {
1502                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1503         }
1504         | function_arglist_nonbackup_reparse REPARSE bare_number_common
1505         {
1506                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1507         }
1508         | function_arglist_nonbackup_reparse REPARSE SCM_ARG
1509         {
1510                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1511         }
1512         | function_arglist_nonbackup_reparse REPARSE lyric_element_music
1513         {
1514                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1515         }
1516         | function_arglist_nonbackup_reparse REPARSE symbol_list_arg
1517         {
1518                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1519         }
1520         ;
1521
1522 function_arglist_nonbackup_reparse:
1523         EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup SCM_IDENTIFIER
1524         {
1525                 $$ = $3;
1526                 SCM res = try_string_variants ($2, $4);
1527                 if (!SCM_UNBNDP (res))
1528                         if (scm_is_pair (res))
1529                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1530                         else
1531                                 MYREPARSE (@4, $2, SCM_ARG, res);
1532                 else if (scm_is_true
1533                          (scm_call_1
1534                           ($2, make_music_from_simple
1535                            (parser, @4, $4))))
1536                         MYREPARSE (@4, $2, STRING, $4);
1537                 else
1538                         MYREPARSE (@4, $2, SCM_ARG, $4);
1539         }
1540         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup STRING
1541         {
1542                 $$ = $3;
1543                 SCM res = try_string_variants ($2, $4);
1544                 if (!SCM_UNBNDP (res))
1545                         if (scm_is_pair (res))
1546                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1547                         else
1548                                 MYREPARSE (@4, $2, SCM_ARG, res);
1549                 else if (scm_is_true
1550                          (scm_call_1
1551                           ($2, make_music_from_simple
1552                            (parser, @4, $4))))
1553                         MYREPARSE (@4, $2, STRING, $4);
1554                 else
1555                         MYREPARSE (@4, $2, SCM_ARG, $4);
1556         }
1557         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup full_markup
1558         {
1559                 $$ = $3;
1560                 if (scm_is_true (scm_call_1 ($2, $4)))
1561                         MYREPARSE (@4, $2, SCM_ARG, $4);
1562                 else if (scm_is_true
1563                          (scm_call_1
1564                           ($2, make_music_from_simple
1565                            (parser, @4, $4))))
1566                         MYREPARSE (@4, $2, STRING, $4);
1567                 else
1568                         MYREPARSE (@4, $2, SCM_ARG, $4);
1569         }
1570         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup UNSIGNED
1571         {
1572                 $$ = $3;
1573                 if (scm_is_true (scm_call_1 ($2, $4)))
1574                         MYREPARSE (@4, $2, REAL, $4);
1575                 else {
1576                         SCM d = make_duration ($4);
1577                         if (SCM_UNBNDP (d) || scm_is_false (scm_call_1 ($2, d)))
1578                                 MYREPARSE (@4, $2, REAL, $4); // trigger error
1579                         else
1580                                 MYREPARSE (@4, $2, DURATION_IDENTIFIER, d);
1581                 }
1582         }
1583         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup DURATION_IDENTIFIER {
1584                 $$ = $3;
1585                 MYREPARSE (@4, $2, DURATION_IDENTIFIER, $4);
1586         }
1587         ;
1588
1589
1590 // function_arglist_backup can't occur at the end of an argument
1591 // list.  It needs to be careful about avoiding lookahead only until
1592 // it has made a decision whether or not to accept the parsed entity.
1593 // At that point of time, music requiring lookahead to parse becomes
1594 // fine.
1595 function_arglist_backup:
1596         function_arglist_common
1597         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup embedded_scm_arg_closed
1598         {
1599                 if (scm_is_true (scm_call_1 ($2, $4)))
1600                 {
1601                         $$ = scm_cons ($4, $3);
1602                 } else {
1603                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1604                         MYBACKUP (SCM_ARG, $4, @4);
1605                 }
1606         }
1607         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup REPEAT simple_string unsigned_number
1608         {
1609                 $4 = MAKE_SYNTAX ("repeat", @4, $5, $6,
1610                                   MY_MAKE_MUSIC ("Music", @4)->unprotect (),
1611                                   SCM_EOL);
1612                 if (scm_is_true (scm_call_1 ($2, $4)))
1613                 {
1614                         $$ = $3;
1615                         MYREPARSE (@4, $2, REPEAT_IDENTIFIER, scm_cons ($5, $6));
1616                 } else {
1617                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1618                         MYBACKUP (REPEAT_IDENTIFIER, scm_cons ($5, $6), @4);
1619                 }
1620         }
1621         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup chord_body
1622         {
1623                 if (scm_is_true (scm_call_1 ($2, $4)))
1624                 {
1625                         $$ = $3;
1626                         MYREPARSE (@4, $2, CHORD_BODY_IDENTIFIER, $4);
1627                 } else {
1628                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1629                         MYBACKUP (CHORD_BODY_IDENTIFIER, $4, @4);
1630                 }
1631         }
1632         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup post_event_nofinger
1633         {
1634                 if (scm_is_true (scm_call_1 ($2, $4)))
1635                 {
1636                         $$ = scm_cons ($4, $3);
1637                 } else {
1638                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1639                         MYBACKUP (EVENT_IDENTIFIER, $4, @4);
1640                 }
1641         }
1642         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup full_markup
1643         {
1644                 if (scm_is_true (scm_call_1 ($2, $4)))
1645                         $$ = scm_cons ($4, $3);
1646                 else {
1647                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1648                         MYBACKUP (LYRIC_ELEMENT, $4, @4);
1649                 }
1650         }
1651         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup UNSIGNED
1652         {
1653                 if (scm_is_true (scm_call_1 ($2, $4)))
1654                 {
1655                         MYREPARSE (@4, $2, REAL, $4);
1656                         $$ = $3;
1657                 } else {
1658                         SCM d = make_duration ($4);
1659                         if (SCM_UNBNDP (d) || scm_is_false (scm_call_1 ($2, d)))
1660                         {
1661                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1662                                 MYBACKUP (UNSIGNED, $4, @4);
1663                         } else {
1664                                 MYREPARSE (@4, $2, DURATION_IDENTIFIER, d);
1665                                 $$ = $3;
1666                         }
1667                 }
1668         }
1669         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup REAL
1670         {
1671                 if (scm_is_true (scm_call_1 ($2, $4)))
1672                 {
1673                         $$ = $3;
1674                         MYREPARSE (@4, $2, REAL, $4);
1675                 } else {
1676                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1677                         MYBACKUP (REAL, $4, @4);
1678                 }
1679         }
1680         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup NUMBER_IDENTIFIER
1681         {
1682                 if (scm_is_true (scm_call_1 ($2, $4)))
1683                 {
1684                         $$ = scm_cons ($4, $3);
1685                 } else {
1686                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1687                         MYBACKUP (NUMBER_IDENTIFIER, $4, @4);
1688                 }
1689         }
1690         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' UNSIGNED
1691         {
1692                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1693                 if (scm_is_true (scm_call_1 ($2, n))) {
1694                         $$ = $3;
1695                         MYREPARSE (@5, $2, REAL, n);
1696                 } else {
1697                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1698                         t->set_property ("digit", $5);
1699                         $$ = t->unprotect ();
1700                         if (scm_is_true (scm_call_1 ($2, $$)))
1701                                 $$ = scm_cons ($$, $3);
1702                         else {
1703                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1704                                 MYBACKUP (UNSIGNED, $5, @5);
1705                                 parser->lexer_->push_extra_token ('-');
1706                         }
1707                 }
1708                 
1709         }
1710         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' REAL
1711         {
1712                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1713                 if (scm_is_true (scm_call_1 ($2, n))) {
1714                         MYREPARSE (@5, $2, REAL, n);
1715                         $$ = $3;
1716                 } else {
1717                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1718                         MYBACKUP (REAL, n, @5);
1719                 }
1720         }
1721         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' NUMBER_IDENTIFIER
1722         {
1723                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1724                 if (scm_is_true (scm_call_1 ($2, n))) {
1725                         $$ = scm_cons (n, $3);
1726                 } else {
1727                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1728                         MYBACKUP (NUMBER_IDENTIFIER, n, @5);
1729                 }
1730         }
1731         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup PITCH_IDENTIFIER
1732         {
1733                 SCM m = make_music_from_simple (parser, @4, $4);
1734                 if (unsmob_music (m) && scm_is_true (scm_call_1 ($2, m)))
1735                 {
1736                         MYREPARSE (@4, $2, PITCH_IDENTIFIER, $4);
1737                         $$ = $3;
1738                 } else if (scm_is_true (scm_call_1 ($2, $4)))
1739                 {
1740                         MYREPARSE (@4, $2, PITCH_ARG, $4);
1741                         $$ = $3;
1742                 } else {
1743                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1744                         MYBACKUP (PITCH_IDENTIFIER, $4, @4);
1745                 }
1746         }
1747         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup NOTENAME_PITCH
1748         {
1749                 SCM m = make_music_from_simple (parser, @4, $4);
1750                 if (unsmob_music (m) && scm_is_true (scm_call_1 ($2, m)))
1751                 {
1752                         MYREPARSE (@4, $2, NOTENAME_PITCH, $4);
1753                         $$ = $3;
1754                 } else if (scm_is_true (scm_call_1 ($2, $4)))
1755                 {
1756                         MYREPARSE (@4, $2, PITCH_ARG, $4);
1757                         $$ = $3;
1758                 } else {
1759                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1760                         MYBACKUP (NOTENAME_PITCH, $4, @4);
1761                 }
1762         }
1763         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup TONICNAME_PITCH
1764         {
1765                 SCM m = make_music_from_simple (parser, @4, $4);
1766                 if (unsmob_music (m) && scm_is_true (scm_call_1 ($2, m)))
1767                 {
1768                         MYREPARSE (@4, $2, TONICNAME_PITCH, $4);
1769                         $$ = $3;
1770                 } else if (scm_is_true (scm_call_1 ($2, $4)))
1771                 {
1772                         MYREPARSE (@4, $2, PITCH_ARG, $4);
1773                         $$ = $3;
1774                 } else {
1775                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1776                         MYBACKUP (TONICNAME_PITCH, $4, @4);
1777                 }
1778         }
1779         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup DURATION_IDENTIFIER
1780         {
1781                 if (scm_is_true (scm_call_1 ($2, $4)))
1782                 {
1783                         MYREPARSE (@4, $2, DURATION_IDENTIFIER, $4);
1784                         $$ = $3;
1785                 } else {
1786                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1787                         MYBACKUP (DURATION_IDENTIFIER, $4, @4);
1788                 }
1789         }
1790         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup SCM_IDENTIFIER
1791         {
1792                 SCM res = try_string_variants ($2, $4);
1793                 if (!SCM_UNBNDP (res))
1794                         if (scm_is_pair (res)) {
1795                                 $$ = $3;
1796                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1797                         }
1798                         else
1799                                 $$ = scm_cons (res, $3);
1800                 else {
1801                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1802                         MYBACKUP (SCM_IDENTIFIER, $4, @4);
1803                 }
1804         }
1805         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup STRING
1806         {
1807                 SCM res = try_string_variants ($2, $4);
1808                 if (!SCM_UNBNDP (res))
1809                         if (scm_is_pair (res)) {
1810                                 $$ = $3;
1811                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1812                         }
1813                         else
1814                                 $$ = scm_cons (res, $3);
1815                 else {
1816                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1817                         MYBACKUP (STRING, $4, @4);
1818                 }
1819         }
1820         | function_arglist_backup REPARSE music_assign
1821         {
1822                 if (scm_is_true (scm_call_1 ($2, $3)))
1823                         $$ = scm_cons ($3, $1);
1824                 else
1825                         $$ = check_scheme_arg (parser, @3,
1826                                                make_music_from_simple
1827                                                (parser, @3, $3),
1828                                                $1, $2);
1829         }
1830         | function_arglist_backup REPARSE pitch_arg
1831         {
1832                 $$ = check_scheme_arg (parser, @3,
1833                                        $3, $1, $2);
1834         }               
1835         | function_arglist_backup REPARSE bare_number_common
1836         {
1837                 $$ = check_scheme_arg (parser, @3,
1838                                        $3, $1, $2);
1839         }
1840         | function_arglist_backup REPARSE duration_length
1841         {
1842                 $$ = check_scheme_arg (parser, @3,
1843                                        $3, $1, $2);
1844         }
1845         | function_arglist_backup REPARSE symbol_list_arg
1846         {
1847                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1848         }
1849         ;
1850
1851 function_arglist:
1852         function_arglist_nonbackup
1853         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_nonbackup DEFAULT
1854         {
1855                 $$ = scm_cons (loc_on_music (@4, $1), $3);
1856         }
1857         ;
1858
1859 function_arglist_skip_nonbackup:
1860         function_arglist_nonbackup
1861         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_nonbackup
1862         {
1863                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1864         }
1865         ;
1866
1867 function_arglist_common:
1868         EXPECT_NO_MORE_ARGS {
1869                 $$ = SCM_EOL;
1870         }
1871         | EXPECT_SCM function_arglist_optional embedded_scm_arg
1872         {
1873                 if (scm_is_true (scm_call_1 ($1, $3)))
1874                         $$ = scm_cons ($3, $2);
1875                 else
1876                         $$ = check_scheme_arg (parser, @3,
1877                                                make_music_from_simple
1878                                                (parser, @3, $3),
1879                                                $2, $1);
1880         }
1881         | EXPECT_SCM function_arglist_optional bare_number_common
1882         {
1883                 $$ = check_scheme_arg (parser, @3,
1884                                        $3, $2, $1);
1885         }
1886         | EXPECT_SCM function_arglist_optional post_event_nofinger
1887         {
1888                 $$ = check_scheme_arg (parser, @3,
1889                                        $3, $2, $1);
1890         }
1891         | EXPECT_SCM function_arglist_optional '-' NUMBER_IDENTIFIER
1892         {
1893                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1894                 $$ = check_scheme_arg (parser, @4, n, $2, $1);
1895         }
1896         | function_arglist_common_reparse REPARSE SCM_ARG
1897         {
1898                 $$ = check_scheme_arg (parser, @3,
1899                                        $3, $1, $2);
1900         }
1901         | function_arglist_common_reparse REPARSE lyric_element_music
1902         {
1903                 $$ = check_scheme_arg (parser, @3,
1904                                        $3, $1, $2);
1905         }
1906         | function_arglist_common_reparse REPARSE bare_number_common
1907         {
1908                 $$ = check_scheme_arg (parser, @3,
1909                                        $3, $1, $2);
1910         }
1911         | function_arglist_common_reparse REPARSE duration_length
1912         {
1913                 $$ = check_scheme_arg (parser, @3,
1914                                        $3, $1, $2);
1915         }
1916         | function_arglist_common_reparse REPARSE symbol_list_arg
1917         {
1918                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1919         }
1920         ;
1921
1922 function_arglist_common_reparse:
1923         EXPECT_SCM function_arglist_optional SCM_IDENTIFIER
1924         {
1925                 $$ = $2;
1926                 SCM res = try_string_variants ($1, $3);
1927                 if (!SCM_UNBNDP (res))
1928                         if (scm_is_pair (res))
1929                                 MYREPARSE (@3, $1, SYMBOL_LIST, res);
1930                         else
1931                                 MYREPARSE (@3, $1, SCM_ARG, res);
1932                 else if (scm_is_true
1933                          (scm_call_1
1934                           ($1, make_music_from_simple (parser, @3, $3))))
1935                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1936                 else
1937                         // This is going to flag a syntax error, we
1938                         // know the predicate to be false.
1939                         MYREPARSE (@3, $1, SCM_ARG, $3);
1940         }
1941         | EXPECT_SCM function_arglist_optional STRING
1942         {
1943                 $$ = $2;
1944                 SCM res = try_string_variants ($1, $3);
1945                 if (!SCM_UNBNDP (res))
1946                         if (scm_is_pair (res))
1947                                 MYREPARSE (@3, $1, SYMBOL_LIST, res);
1948                         else
1949                                 MYREPARSE (@3, $1, SCM_ARG, res);
1950                 else if (scm_is_true
1951                          (scm_call_1
1952                           ($1, make_music_from_simple (parser, @3, $3))))
1953                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1954                 else
1955                         // This is going to flag a syntax error, we
1956                         // know the predicate to be false.
1957                         MYREPARSE (@3, $1, SCM_ARG, $3);
1958         }
1959         | EXPECT_SCM function_arglist_optional full_markup
1960         {
1961                 $$ = $2;
1962                 if (scm_is_true (scm_call_1 ($1, $3)))
1963                         MYREPARSE (@3, $1, SCM_ARG, $3);
1964                 else if (scm_is_true
1965                          (scm_call_1
1966                           ($1, make_music_from_simple (parser, @3, $3))))
1967                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1968                 else
1969                         // This is going to flag a syntax error, we
1970                         // know the predicate to be false.
1971                         MYREPARSE (@3, $1, SCM_ARG, $3);
1972         }
1973         | EXPECT_SCM function_arglist_optional UNSIGNED
1974         {
1975                 $$ = $2;
1976                 if (scm_is_true (scm_call_1 ($1, $3)))
1977                         MYREPARSE (@3, $1, REAL, $3);
1978                 else {
1979                         SCM d = make_duration ($3);
1980                         if (SCM_UNBNDP (d) || scm_is_false (scm_call_1 ($1, d)))
1981                                 MYREPARSE (@3, $1, REAL, $3);
1982                         else
1983                                 MYREPARSE (@3, $1, DURATION_IDENTIFIER, d);
1984                 }
1985         }
1986         | EXPECT_SCM function_arglist_optional DURATION_IDENTIFIER
1987         {
1988                 $$ = $2;
1989                 MYREPARSE (@3, $1, DURATION_IDENTIFIER, $3);
1990         }
1991         | EXPECT_SCM function_arglist_optional '-' UNSIGNED
1992         {
1993                 $$ = $2;
1994                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1995                 if (scm_is_true (scm_call_1 ($1, n)))
1996                         MYREPARSE (@4, $1, REAL, n);
1997                 else {
1998                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @4);
1999                         t->set_property ("digit", $4);
2000                         SCM m = t->unprotect ();
2001                         if (scm_is_true (scm_call_1 ($1, m)))
2002                                 MYREPARSE (@4, $1, SCM_ARG, m);
2003                         else
2004                                 MYREPARSE (@4, $1, SCM_ARG, $4);
2005                 }
2006                 
2007         }
2008         | EXPECT_SCM function_arglist_optional '-' REAL
2009         {
2010                 $$ = $2;
2011                 SCM n = scm_difference ($4, SCM_UNDEFINED);
2012                 MYREPARSE (@4, $1, REAL, n);
2013         }
2014         ;
2015
2016 function_arglist_closed:
2017         function_arglist_closed_nonbackup
2018         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_nonbackup DEFAULT
2019         {
2020                 $$ = scm_cons (loc_on_music (@4, $1), $3);
2021         }
2022         ;
2023
2024 function_arglist_closed_common:
2025         EXPECT_NO_MORE_ARGS {
2026                 $$ = SCM_EOL;
2027         }
2028         | EXPECT_SCM function_arglist_optional embedded_scm_arg_closed
2029         {
2030                 $$ = check_scheme_arg (parser, @3,
2031                                        $3, $2, $1);
2032         }
2033         | EXPECT_SCM function_arglist_optional bare_number_common_closed
2034         {
2035                 $$ = check_scheme_arg (parser, @3,
2036                                        $3, $2, $1);
2037         }
2038         | EXPECT_SCM function_arglist_optional '-' NUMBER_IDENTIFIER
2039         {
2040                 $$ = check_scheme_arg (parser, @3,
2041                                        scm_difference ($4, SCM_UNDEFINED),
2042                                        $2, $1);
2043         }
2044         | EXPECT_SCM function_arglist_optional post_event_nofinger
2045         {
2046                 $$ = check_scheme_arg (parser, @3,
2047                                        $3, $2, $1);
2048         }
2049         | function_arglist_common_reparse REPARSE SCM_ARG
2050         {
2051                 $$ = check_scheme_arg (parser, @3,
2052                                        $3, $1, $2);
2053         }
2054         | function_arglist_common_reparse REPARSE bare_number_common_closed
2055         {
2056                 $$ = check_scheme_arg (parser, @3,
2057                                        $3, $1, $2);
2058         }
2059         | function_arglist_common_reparse REPARSE symbol_list_arg
2060         {
2061                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
2062         }
2063         ;
2064
2065 function_arglist_optional:
2066         function_arglist_backup
2067         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_backup DEFAULT
2068         {
2069                 $$ = scm_cons (loc_on_music (@4, $1), $3);
2070         }
2071         | function_arglist_skip_backup BACKUP
2072         ;
2073
2074 function_arglist_skip_backup:
2075         function_arglist_backup
2076         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_backup
2077         {
2078                 $$ = scm_cons (loc_on_music (@3, $1), $3);
2079         }
2080         ;
2081
2082 embedded_scm_closed:
2083         embedded_scm_bare
2084         | scm_function_call_closed
2085         ;
2086
2087 embedded_scm_arg_closed:
2088         embedded_scm_bare_arg
2089         | scm_function_call_closed
2090         | closed_music
2091         ;
2092
2093 scm_function_call_closed:
2094         SCM_FUNCTION function_arglist_closed {
2095                 $$ = MAKE_SYNTAX ("music-function", @$,
2096                                          $1, $2);
2097         }
2098         ;
2099
2100 music_function_call:
2101         MUSIC_FUNCTION function_arglist {
2102                 $$ = MAKE_SYNTAX ("music-function", @$,
2103                                          $1, $2);
2104         }
2105         ;
2106
2107
2108 optional_id:
2109         /**/ { $$ = SCM_EOL; }
2110         | '=' simple_string {
2111                 $$ = $2;
2112         }
2113         ;
2114
2115 complex_music:
2116         music_function_call
2117         | repeated_music                { $$ = $1; }
2118         | re_rhythmed_music     { $$ = $1; }
2119         | complex_music_prefix music
2120         {
2121                 $$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
2122         }
2123         ;
2124
2125 complex_music_prefix:
2126         CONTEXT symbol optional_id optional_context_mod {
2127                 Context_mod *ctxmod = unsmob_context_mod ($4);
2128                 SCM mods = SCM_EOL;
2129                 if (ctxmod)
2130                         mods = ctxmod->get_mods ();
2131                 $$ = START_MAKE_SYNTAX ("context-specification", $2, $3, mods, SCM_BOOL_F);
2132         }
2133         | NEWCONTEXT symbol optional_id optional_context_mod {
2134                 Context_mod *ctxmod = unsmob_context_mod ($4);
2135                 SCM mods = SCM_EOL;
2136                 if (ctxmod)
2137                         mods = ctxmod->get_mods ();
2138                 $$ = START_MAKE_SYNTAX ("context-specification", $2, $3, mods, SCM_BOOL_T);
2139         }
2140         ;
2141
2142 mode_changed_music:
2143         mode_changing_head grouped_music_list {
2144                 if ($1 == ly_symbol2scm ("chords"))
2145                 {
2146                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $2);
2147                 }
2148                 else
2149                 {
2150                   $$ = $2;
2151                 }
2152                 parser->lexer_->pop_state ();
2153         }
2154         | mode_changing_head_with_context optional_context_mod grouped_music_list {
2155                 Context_mod *ctxmod = unsmob_context_mod ($2);
2156                 SCM mods = SCM_EOL;
2157                 if (ctxmod)
2158                         mods = ctxmod->get_mods ();
2159                 $$ = MAKE_SYNTAX ("context-specification", @$, $1, SCM_EOL, mods, SCM_BOOL_T, $3);
2160                 if ($1 == ly_symbol2scm ("ChordNames"))
2161                 {
2162                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $$);
2163                 }
2164                 parser->lexer_->pop_state ();
2165         }
2166         ;
2167
2168 mode_changing_head:
2169         NOTEMODE {
2170                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
2171                 parser->lexer_->push_note_state (nn);
2172
2173                 $$ = ly_symbol2scm ("notes");
2174         }
2175         | DRUMMODE
2176                 {
2177                 SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
2178                 parser->lexer_->push_note_state (nn);
2179
2180                 $$ = ly_symbol2scm ("drums");
2181         }
2182         | FIGUREMODE {
2183                 parser->lexer_->push_figuredbass_state ();
2184
2185                 $$ = ly_symbol2scm ("figures");
2186         }
2187         | CHORDMODE {
2188                 SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
2189                 parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
2190                 nn = parser->lexer_->lookup_identifier ("pitchnames");
2191                 parser->lexer_->push_chord_state (nn);
2192                 $$ = ly_symbol2scm ("chords");
2193
2194         }
2195         | LYRICMODE
2196                 { parser->lexer_->push_lyric_state ();
2197                 $$ = ly_symbol2scm ("lyrics");
2198         }
2199         ;
2200
2201 mode_changing_head_with_context:
2202         DRUMS {
2203                 SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
2204                 parser->lexer_->push_note_state (nn);
2205
2206                 $$ = ly_symbol2scm ("DrumStaff");
2207         }
2208         | FIGURES {
2209                 parser->lexer_->push_figuredbass_state ();
2210
2211                 $$ = ly_symbol2scm ("FiguredBass");
2212         }
2213         | CHORDS {
2214                 SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
2215                 parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
2216                 nn = parser->lexer_->lookup_identifier ("pitchnames");
2217                 parser->lexer_->push_chord_state (nn);
2218                 $$ = ly_symbol2scm ("ChordNames");
2219         }
2220         | LYRICS
2221                 { parser->lexer_->push_lyric_state ();
2222                 $$ = ly_symbol2scm ("Lyrics");
2223         }
2224         ;
2225
2226 new_lyrics:
2227         ADDLYRICS { parser->lexer_->push_lyric_state (); }
2228         /*cont */
2229         composite_music {
2230         /* Can also use music at the expensive of two S/Rs similar to
2231            \repeat \alternative */
2232                 parser->lexer_->pop_state ();
2233
2234                 $$ = scm_cons ($3, SCM_EOL);
2235         }
2236         | new_lyrics ADDLYRICS {
2237                 parser->lexer_->push_lyric_state ();
2238         } composite_music {
2239                 parser->lexer_->pop_state ();
2240                 $$ = scm_cons ($4, $1);
2241         }
2242         ;
2243
2244 re_rhythmed_music:
2245         composite_music new_lyrics {
2246                 $$ = MAKE_SYNTAX ("add-lyrics", @$, $1, scm_reverse_x ($2, SCM_EOL));
2247         } %prec COMPOSITE
2248         | LYRICSTO simple_string {
2249                 parser->lexer_->push_lyric_state ();
2250         } music {
2251                 parser->lexer_->pop_state ();
2252                 $$ = MAKE_SYNTAX ("lyric-combine", @$, $2, $4);
2253         }
2254         ;
2255
2256 context_change:
2257         CHANGE symbol '=' simple_string  {
2258                 $$ = MAKE_SYNTAX ("context-change", @$, $2, $4);
2259         }
2260         ;
2261
2262
2263 property_path:
2264         symbol_list_rev  {
2265                 $$ = scm_reverse_x ($1, SCM_EOL);
2266         }
2267         | symbol_list_rev property_path {
2268                 $$ = scm_reverse_x ($1, $2);
2269         }
2270         ;
2271
2272 property_operation:
2273         symbol '=' scalar {
2274                 $$ = scm_list_3 (ly_symbol2scm ("assign"), $1, $3);
2275         }
2276         | UNSET symbol {
2277                 $$ = scm_list_2 (ly_symbol2scm ("unset"), $2);
2278         }
2279         | OVERRIDE property_path '=' scalar {
2280                 if (scm_ilength ($2) < 2) {
2281                         parser->parser_error (@2, _("bad grob property path"));
2282                         $$ = SCM_UNDEFINED;
2283                 } else {
2284                         $$ = scm_cons (ly_symbol2scm ("push"),
2285                                        scm_cons2 (scm_car ($2),
2286                                                   $4,
2287                                                   scm_cdr ($2)));
2288                 }
2289         }
2290         | REVERT revert_arg {
2291                 $$ = scm_cons (ly_symbol2scm ("pop"), $2);
2292         }
2293         ;
2294
2295 // This is all quite awkward for the sake of substantial backward
2296 // compatibility while at the same time allowing a more "natural" form
2297 // of specification not separating grob specification from grob
2298 // property path.  The purpose of this definition of revert_arg is to
2299 // allow the symbol list which specifies grob and property to revert
2300 // to be optionally be split into two parts after the grob (which in
2301 // this case is just the first element of the list).  symbol_list_part
2302 // is only one path component, but it can be parsed without lookahead,
2303 // so we can follow it with a synthetic BACKUP token when needed.  If
2304 // the first symbol_list_part already contains multiple elements (only
2305 // possible if a Scheme expression provides them), we just parse for
2306 // additional elements introduced by '.', which is what the
2307 // SYMBOL_LIST backup in connection with the immediately following
2308 // rule using symbol_list_arg does.
2309 //
2310 // As long as we don't have our coffers filled with both grob and at
2311 // least one grob property specification, the rest of the required
2312 // symbol list chain may be provided either with or without a leading
2313 // dot.  This is for both allowing the traditional
2314 // \revert Accidental #'color
2315 // as well as well as the "naive" form
2316 // \revert Accidental.color
2317
2318 revert_arg:
2319         revert_arg_backup BACKUP symbol_list_arg
2320         {
2321                 $$ = $3;
2322         }
2323         ;
2324
2325 revert_arg_backup:
2326         revert_arg_part
2327         {
2328                 if (scm_is_null ($1)
2329                     || scm_is_null (scm_cdr ($1)))
2330                         MYBACKUP (SCM_ARG, $1, @1);
2331                 else
2332                         MYBACKUP (SYMBOL_LIST, scm_reverse_x ($1, SCM_EOL), @1);
2333         }
2334         ;
2335
2336 // revert_arg_part delivers results in reverse
2337 revert_arg_part:
2338         symbol_list_part
2339         | revert_arg_backup BACKUP SCM_ARG '.' symbol_list_part
2340         {
2341                 $$ = scm_append_x (scm_list_2 ($5, $3));
2342         }
2343         | revert_arg_backup BACKUP SCM_ARG symbol_list_part
2344         {
2345                 $$ = scm_append_x (scm_list_2 ($4, $3));
2346         }               
2347         ;
2348
2349 context_def_mod:
2350         CONSISTS { $$ = ly_symbol2scm ("consists"); }
2351         | REMOVE { $$ = ly_symbol2scm ("remove"); }
2352
2353         | ACCEPTS { $$ = ly_symbol2scm ("accepts"); }
2354         | DEFAULTCHILD { $$ = ly_symbol2scm ("default-child"); }
2355         | DENIES { $$ = ly_symbol2scm ("denies"); }
2356
2357         | ALIAS { $$ = ly_symbol2scm ("alias"); }
2358         | TYPE { $$ = ly_symbol2scm ("translator-type"); }
2359         | DESCRIPTION { $$ = ly_symbol2scm ("description"); }
2360         | NAME { $$ = ly_symbol2scm ("context-name"); }
2361         ;
2362
2363 context_mod:
2364         property_operation { $$ = $1; }
2365         | context_def_mod STRING {
2366                 $$ = scm_list_2 ($1, $2);
2367         }
2368         | context_def_mod embedded_scm
2369         {
2370                 if (!scm_is_string ($2)
2371                     && ly_symbol2scm ("consists") != $1
2372                     && ly_symbol2scm ("remove") != $1)
2373                 {
2374                         $$ = SCM_EOL;
2375                         parser->parser_error (@1, _ ("only \\consists and \\remove take non-string argument."));
2376                 }
2377                 else
2378                 {
2379                         $$ = scm_list_2 ($1, $2);
2380                 }
2381         }
2382         ;
2383
2384 // If defined, at least two members.
2385 grob_prop_spec:
2386         symbol_list_rev
2387         {
2388                 SCM l = scm_reverse_x ($1, SCM_EOL);
2389                 if (scm_is_pair (l)
2390                     && to_boolean
2391                     (scm_object_property (scm_car (l),
2392                                           ly_symbol2scm ("is-grob?"))))
2393                         l = scm_cons (ly_symbol2scm ("Bottom"), l);
2394                 if (scm_is_null (l) || scm_is_null (scm_cdr (l))) {
2395                         parser->parser_error (@1, _ ("bad grob property path"));
2396                         l = SCM_UNDEFINED;
2397                 }
2398                 $$ = l;
2399         }
2400         ;
2401
2402 // If defined, at least three members
2403 grob_prop_path:
2404         grob_prop_spec
2405         {
2406                 if (!SCM_UNBNDP ($1) && scm_is_null (scm_cddr ($1)))
2407                 {
2408                         parser->parser_error (@1, _ ("bad grob property path"));
2409                         $$ = SCM_UNDEFINED;
2410                 }
2411         }
2412         | grob_prop_spec property_path
2413         {
2414                 if (!SCM_UNBNDP ($1)) {
2415                         $$ = scm_append_x (scm_list_2 ($1, $2));
2416                         if (scm_is_null (scm_cddr ($$))) {
2417                                 parser->parser_error (@$, _ ("bad grob property path"));
2418                                 $$ = SCM_UNDEFINED;
2419                         }
2420                 }
2421
2422         }
2423         ;
2424
2425 // Exactly two elements or undefined
2426 context_prop_spec:
2427         symbol_list_rev
2428         {
2429                 SCM l = scm_reverse_x ($1, SCM_EOL);
2430                 switch (scm_ilength (l)) {
2431                 case 1:
2432                         l = scm_cons (ly_symbol2scm ("Bottom"), l);
2433                 case 2:
2434                         break;
2435                 default:
2436                         parser->parser_error (@1, _ ("bad context property path"));
2437                         l = SCM_UNDEFINED;
2438                 }
2439                 $$ = l;
2440         }
2441         ;
2442
2443 simple_music_property_def:
2444         OVERRIDE grob_prop_path '=' scalar {
2445                 if (SCM_UNBNDP ($2))
2446                         $$ = SCM_UNDEFINED;
2447                 else {
2448                         $$ = scm_list_5 (scm_car ($2),
2449                                          ly_symbol2scm ("OverrideProperty"),
2450                                          scm_cadr ($2),
2451                                          $4,
2452                                          scm_cddr ($2));
2453                 }
2454         }
2455         | REVERT simple_revert_context revert_arg {
2456                 $$ = scm_list_4 ($2,
2457                                  ly_symbol2scm ("RevertProperty"),
2458                                  scm_car ($3),
2459                                  scm_cdr ($3));
2460         }
2461         | SET context_prop_spec '=' scalar {
2462                 if (SCM_UNBNDP ($2))
2463                         $$ = SCM_UNDEFINED;
2464                 else
2465                         $$ = scm_list_4 (scm_car ($2),
2466                                          ly_symbol2scm ("PropertySet"),
2467                                          scm_cadr ($2),
2468                                          $4);
2469         }
2470         | UNSET context_prop_spec {
2471                 if (SCM_UNBNDP ($2))
2472                         $$ = SCM_UNDEFINED;
2473                 else
2474                         $$ = scm_list_3 (scm_car ($2),
2475                                          ly_symbol2scm ("PropertyUnset"),
2476                                          scm_cadr ($2));
2477         }
2478         ;
2479
2480
2481 // This is all quite awkward for the sake of substantial backward
2482 // compatibility while at the same time allowing a more "natural" form
2483 // of specification not separating grob specification from grob
2484 // property path.  The purpose of this definition of
2485 // simple_revert_context is to allow the symbol list which specifies
2486 // grob and property to revert to be optionally be split into two
2487 // parts after the grob (which may be preceded by a context
2488 // specification, a case which we distinguish by checking whether the
2489 // first symbol is a valid grob symbol instead).
2490 //
2491 // See revert_arg above for the main work horse of this arrangement.
2492 // simple_revert_context just caters for the context and delegates the
2493 // rest of the job to revert_arg.
2494
2495 simple_revert_context:
2496         symbol_list_part
2497         {
2498                 $1 = scm_reverse_x ($1, SCM_EOL);
2499                 if (scm_is_null ($1)
2500                     || to_boolean
2501                     (scm_object_property (scm_car ($1),
2502                                           ly_symbol2scm ("is-grob?")))) {
2503                         $$ = ly_symbol2scm ("Bottom");
2504                         parser->lexer_->push_extra_token (SCM_IDENTIFIER, $1);
2505                 } else {
2506                         $$ = scm_car ($1);
2507                         parser->lexer_->push_extra_token (SCM_IDENTIFIER,
2508                                                           scm_cdr ($1));
2509                 }
2510         }
2511         ;
2512
2513 music_property_def:
2514         simple_music_property_def {
2515                 if (SCM_UNBNDP ($1))
2516                         $$ = MAKE_SYNTAX ("void-music", @1);
2517                 else
2518                         $$ = LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("property-operation"), scm_cons2 (parser->self_scm (), make_input (@$), $1));
2519         }
2520         ;
2521
2522 string:
2523         STRING {
2524                 $$ = $1;
2525         }
2526         | full_markup
2527         ;
2528
2529 simple_string: STRING {
2530                 $$ = $1;
2531         }
2532         | embedded_scm_bare
2533         {
2534                 if (scm_is_string ($1)) {
2535                         $$ = $1;
2536                 } else {
2537                         parser->parser_error (@1, (_ ("simple string expected")));
2538                         $$ = scm_string (SCM_EOL);
2539                 }
2540         }
2541         ;
2542
2543 symbol:
2544         STRING {
2545                 $$ = scm_string_to_symbol ($1);
2546         }
2547         | embedded_scm_bare
2548         {
2549                 // This is a bit of overkill but makes the same
2550                 // routine responsible for all symbol interpretations.
2551                 $$ = try_string_variants (ly_lily_module_constant ("symbol?"),
2552                                           $1);
2553                 if (SCM_UNBNDP ($$))
2554                 {
2555                         parser->parser_error (@1, (_ ("symbol expected")));
2556                         // Generate a unique symbol in case it is used
2557                         // for an assignment or similar
2558                         $$ = scm_make_symbol (ly_string2scm ("undefined"));
2559                 }
2560         }
2561         ;
2562
2563 scalar:
2564         embedded_scm_arg
2565         | SCM_IDENTIFIER
2566         | bare_number
2567         // The following is a rather defensive variant of admitting
2568         // negative numbers: the grammar would permit number_factor or
2569         // even number_expression.  However, function arguments allow
2570         // only this simple kind of negative number, so to have things
2571         // like \tweak and \override behave reasonably similar, it
2572         // makes sense to rule out things like -- which are rather an
2573         // accent in function argument contexts.
2574         | '-' bare_number
2575         {
2576                 $$ = scm_difference ($2, SCM_UNDEFINED);
2577         }
2578         | STRING
2579         | full_markup
2580         ;
2581
2582 event_chord:
2583         simple_element post_events {
2584                 // Let the rhythmic music iterator sort this mess out.
2585                 if (scm_is_pair ($2)) {
2586                         $$ = make_music_from_simple (parser, @1, $1);
2587                         if (unsmob_music ($$))
2588                                 unsmob_music ($$)->set_property ("articulations",
2589                                                                  scm_reverse_x ($2, SCM_EOL));
2590                         else
2591                         {
2592                                 parser->parser_error (@1, _("music expected"));
2593                                 $$ = MAKE_SYNTAX ("void-music", @1);
2594                         }
2595                 }
2596         } %prec ':'
2597         | simple_chord_elements post_events     {
2598                 if (scm_is_pair ($2)) {
2599                         if (unsmob_pitch ($1))
2600                                 $1 = make_chord_elements (@1,
2601                                                           $1,
2602                                                           parser->default_duration_.smobbed_copy (),
2603                                                           SCM_EOL);
2604
2605                         SCM elts = ly_append2 ($1, scm_reverse_x ($2, SCM_EOL));
2606
2607                         $$ = MAKE_SYNTAX ("event-chord", @1, elts);
2608                 } else if (!unsmob_pitch ($1))
2609                         $$ = MAKE_SYNTAX ("event-chord", @1, $1);
2610                 // A mere pitch drops through.
2611         } %prec ':'
2612         | CHORD_REPETITION optional_notemode_duration post_events {
2613                 Input i;
2614                 i.set_location (@1, @3);
2615                 $$ = MAKE_SYNTAX ("repetition-chord", i,
2616                                   $2, scm_reverse_x ($3, SCM_EOL));
2617         } %prec ':'
2618         | MULTI_MEASURE_REST optional_notemode_duration post_events {
2619                 Input i;
2620                 i.set_location (@1, @3);
2621                 $$ = MAKE_SYNTAX ("multi-measure-rest", i, $2,
2622                                   scm_reverse_x ($3, SCM_EOL));
2623         } %prec ':'
2624         | command_element
2625         | note_chord_element
2626         ;
2627
2628
2629 note_chord_element:
2630         chord_body optional_notemode_duration post_events
2631         {
2632                 Music *m = unsmob_music ($1);
2633                 SCM dur = unsmob_duration ($2)->smobbed_copy ();
2634                 SCM es = m->get_property ("elements");
2635                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
2636
2637                 for (SCM s = es; scm_is_pair (s); s = scm_cdr (s))
2638                   unsmob_music (scm_car (s))->set_property ("duration", dur);
2639                 es = ly_append2 (es, postevs);
2640
2641                 m-> set_property ("elements", es);
2642                 m->set_spot (@$);
2643                 $$ = m->self_scm ();
2644         } %prec ':'
2645         ;
2646
2647 chord_body:
2648         ANGLE_OPEN chord_body_elements ANGLE_CLOSE
2649         {
2650                 $$ = MAKE_SYNTAX ("event-chord", @$, scm_reverse_x ($2, SCM_EOL));
2651         }
2652         | CHORD_BODY_IDENTIFIER
2653         ;
2654
2655 chord_body_elements:
2656         /* empty */             { $$ = SCM_EOL; }
2657         | chord_body_elements chord_body_element {
2658                 if (!SCM_UNBNDP ($2))
2659                         $$ = scm_cons ($2, $1);
2660         }
2661         ;
2662
2663 chord_body_element:
2664         pitch exclamations questions octave_check post_events
2665         {
2666                 bool q = to_boolean ($3);
2667                 bool ex = to_boolean ($2);
2668                 SCM check = $4;
2669                 SCM post = $5;
2670
2671                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2672                 n->set_property ("pitch", $1);
2673                 if (q)
2674                         n->set_property ("cautionary", SCM_BOOL_T);
2675                 if (ex || q)
2676                         n->set_property ("force-accidental", SCM_BOOL_T);
2677
2678                 if (scm_is_pair (post)) {
2679                         SCM arts = scm_reverse_x (post, SCM_EOL);
2680                         n->set_property ("articulations", arts);
2681                 }
2682                 if (scm_is_number (check))
2683                 {
2684                         int q = scm_to_int (check);
2685                         n->set_property ("absolute-octave", scm_from_int (q-1));
2686                 }
2687
2688                 $$ = n->unprotect ();
2689         }
2690         | DRUM_PITCH post_events {
2691                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2692                 n->set_property ("drum-type", $1);
2693
2694                 if (scm_is_pair ($2)) {
2695                         SCM arts = scm_reverse_x ($2, SCM_EOL);
2696                         n->set_property ("articulations", arts);
2697                 }
2698                 $$ = n->unprotect ();
2699         }
2700         | music_function_chord_body
2701         {
2702                 Music *m = unsmob_music ($1);
2703
2704                 while (m && m->is_mus_type ("music-wrapper-music")) {
2705                         $$ = m->get_property ("element");
2706                         m = unsmob_music ($$);
2707                 }
2708
2709                 if (!(m && m->is_mus_type ("rhythmic-event"))) {
2710                         parser->parser_error (@$, _ ("not a rhythmic event"));
2711                         $$ = SCM_UNDEFINED;
2712                 }
2713         }
2714         ;
2715
2716 music_function_chord_body:
2717         music_function_call
2718         | MUSIC_IDENTIFIER
2719         ;
2720
2721 // Event functions may only take closed arglists, otherwise it would
2722 // not be clear whether a following postevent should be associated
2723 // with the last argument of the event function or with the expression
2724 // for which the function call acts itself as event.
2725
2726 music_function_call_closed:
2727         MUSIC_FUNCTION function_arglist_closed {
2728                 $$ = MAKE_SYNTAX ("music-function", @$,
2729                                          $1, $2);
2730         }
2731         ;
2732
2733 event_function_event:
2734         EVENT_FUNCTION function_arglist_closed {
2735                 $$ = MAKE_SYNTAX ("music-function", @$,
2736                                          $1, $2);
2737         }
2738         ;
2739
2740 command_element:
2741         command_event {
2742                 $$ = $1;
2743         }
2744         ;
2745
2746 command_event:
2747         tempo_event {
2748                 $$ = $1;
2749         }
2750         ;
2751
2752
2753 post_events:
2754         /* empty */ {
2755                 $$ = SCM_EOL;
2756         }
2757         | post_events post_event {
2758                 $$ = $1;
2759                 if (Music *m = unsmob_music ($2))
2760                 {
2761                         if (m->is_mus_type ("post-event-wrapper"))
2762                         {
2763                                 for (SCM p = m->get_property ("elements");
2764                                      scm_is_pair (p);
2765                                      p = scm_cdr (p))
2766                                 {
2767                                         $$ = scm_cons (scm_car (p), $$);
2768                                 }
2769                         } else {
2770                                 m->set_spot (@2);
2771                                 $$ = scm_cons ($2, $$);
2772                         }
2773                 }
2774         }
2775         ;
2776
2777 post_event_nofinger:
2778         direction_less_event {
2779                 $$ = $1;
2780         }
2781         | script_dir music_function_call_closed {
2782                 $$ = $2;
2783                 if (!unsmob_music ($2)->is_mus_type ("post-event")) {
2784                         parser->parser_error (@2, _ ("post-event expected"));
2785                         $$ = SCM_UNSPECIFIED;
2786                 } else if (!SCM_UNBNDP ($1))
2787                 {
2788                         unsmob_music ($$)->set_property ("direction", $1);
2789                 }
2790         }
2791         | HYPHEN {
2792                 if (!parser->lexer_->is_lyric_state ())
2793                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2794                 $$ = MY_MAKE_MUSIC ("HyphenEvent", @$)->unprotect ();
2795         }
2796         | EXTENDER {
2797                 if (!parser->lexer_->is_lyric_state ())
2798                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2799                 $$ = MY_MAKE_MUSIC ("ExtenderEvent", @$)->unprotect ();
2800         }
2801         | script_dir direction_reqd_event {
2802                 if (!SCM_UNBNDP ($1))
2803                 {
2804                         Music *m = unsmob_music ($2);
2805                         m->set_property ("direction", $1);
2806                 }
2807                 $$ = $2;
2808         }
2809         | script_dir direction_less_event {
2810                 if (!SCM_UNBNDP ($1))
2811                 {
2812                         Music *m = unsmob_music ($2);
2813                         m->set_property ("direction", $1);
2814                 }
2815                 $$ = $2;
2816         }
2817         | '^' fingering
2818         {
2819                 $$ = $2;
2820                 unsmob_music ($$)->set_property ("direction", scm_from_int (UP));
2821         }
2822         | '_' fingering
2823         {
2824                 $$ = $2;
2825                 unsmob_music ($$)->set_property ("direction", scm_from_int (DOWN));
2826         }                       
2827         ;
2828
2829 post_event:
2830         post_event_nofinger
2831         | '-' fingering {
2832                 $$ = $2;
2833         }
2834         ;
2835
2836 string_number_event:
2837         E_UNSIGNED {
2838                 Music *s = MY_MAKE_MUSIC ("StringNumberEvent", @$);
2839                 s->set_property ("string-number", $1);
2840                 $$ = s->unprotect ();
2841         }
2842         ;
2843
2844 direction_less_event:
2845         string_number_event
2846         | EVENT_IDENTIFIER      {
2847                 $$ = $1;
2848         }
2849         | tremolo_type  {
2850                Music *a = MY_MAKE_MUSIC ("TremoloEvent", @$);
2851                a->set_property ("tremolo-type", $1);
2852                $$ = a->unprotect ();
2853         }
2854         | event_function_event  
2855         ;
2856
2857 direction_reqd_event:
2858         gen_text_def {
2859                 $$ = $1;
2860         }
2861         | script_abbreviation {
2862                 SCM s = parser->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
2863                 if (scm_is_string (s)) {
2864                         Music *a = MY_MAKE_MUSIC ("ArticulationEvent", @$);
2865                         a->set_property ("articulation-type", s);
2866                         $$ = a->unprotect ();
2867                 } else {
2868                         Music *original = unsmob_music (s);
2869                         if (original && original->is_mus_type ("post-event")) {
2870                                 Music *a = original->clone ();
2871                                 a->set_spot (parser->lexer_->override_input (@$));
2872                                 $$ = a->unprotect ();
2873                         } else {
2874                                 parser->parser_error (@1, _ ("expecting string or post-event as script definition"));
2875                                 $$ = MY_MAKE_MUSIC ("PostEvents", @$)->unprotect ();
2876                         }
2877                 }
2878         }
2879         ;
2880
2881 octave_check:
2882         /**/ { $$ = SCM_EOL; }
2883         | '=' quotes { $$ = $2; }
2884         ;
2885
2886 quotes:
2887         /* empty */
2888         {
2889                 $$ = SCM_INUM0;
2890         }
2891         | sub_quotes
2892         | sup_quotes
2893         ;
2894
2895 sup_quotes:
2896         '\'' {
2897                 $$ = scm_from_int (1);
2898         }
2899         | sup_quotes '\'' {
2900                 $$ = scm_oneplus ($1);
2901         }
2902         ;
2903
2904 sub_quotes:
2905         ',' {
2906                 $$ = scm_from_int (-1);
2907         }
2908         | sub_quotes ',' {
2909                 $$ = scm_oneminus ($1);
2910         }
2911         ;
2912
2913 steno_pitch:
2914         NOTENAME_PITCH quotes {
2915                 if (!scm_is_eq (SCM_INUM0, $2))
2916                 {
2917                         Pitch p = *unsmob_pitch ($1);
2918                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2919                         $$ = p.smobbed_copy ();
2920                 }
2921         }
2922         ;
2923
2924 /*
2925 ugh. duplication
2926 */
2927
2928 steno_tonic_pitch:
2929         TONICNAME_PITCH quotes {
2930                 if (!scm_is_eq (SCM_INUM0, $2))
2931                 {
2932                         Pitch p = *unsmob_pitch ($1);
2933                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2934                         $$ = p.smobbed_copy ();
2935                 }
2936         }
2937         ;
2938
2939 pitch:
2940         steno_pitch
2941         | PITCH_IDENTIFIER quotes {
2942                 if (!scm_is_eq (SCM_INUM0, $2))
2943                 {
2944                         Pitch p = *unsmob_pitch ($1);
2945                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2946                         $$ = p.smobbed_copy ();
2947                 }
2948         }
2949         ;
2950
2951 pitch_arg:
2952         PITCH_ARG quotes {
2953                 if (!scm_is_eq (SCM_INUM0, $2))
2954                 {
2955                         Pitch p = *unsmob_pitch ($1);
2956                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2957                         $$ = p.smobbed_copy ();
2958                 }
2959         }
2960         ;
2961
2962 gen_text_def:
2963         full_markup {
2964                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2965                 t->set_property ("text", $1);
2966                 $$ = t->unprotect ();
2967         }
2968         | STRING {
2969                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2970                 t->set_property ("text",
2971                         make_simple_markup ($1));
2972                 $$ = t->unprotect ();
2973         }
2974         | embedded_scm_closed
2975         {
2976                 Music *m = unsmob_music ($1);
2977                 if (m && m->is_mus_type ("post-event"))
2978                         $$ = $1;
2979                 else if (Text_interface::is_markup ($1)) {
2980                         Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2981                         t->set_property ("text", $1);
2982                         $$ = t->unprotect ();
2983                 } else
2984                         parser->parser_error (@1, _ ("not an articulation"));
2985         }
2986         ;
2987
2988 fingering:
2989         UNSIGNED {
2990                 Music *t = MY_MAKE_MUSIC ("FingeringEvent", @$);
2991                 t->set_property ("digit", $1);
2992                 $$ = t->unprotect ();
2993         }
2994         ;
2995
2996 script_abbreviation:
2997         '^'             {
2998                 $$ = scm_from_locale_string ("Hat");
2999         }
3000         | '+'           {
3001                 $$ = scm_from_locale_string ("Plus");
3002         }
3003         | '-'           {
3004                 $$ = scm_from_locale_string ("Dash");
3005         }
3006         | '!'           {
3007                 $$ = scm_from_locale_string ("Bang");
3008         }
3009         | ANGLE_CLOSE   {
3010                 $$ = scm_from_locale_string ("Larger");
3011         }
3012         | '.'           {
3013                 $$ = scm_from_locale_string ("Dot");
3014         }
3015         | '_' {
3016                 $$ = scm_from_locale_string ("Underscore");
3017         }
3018         ;
3019
3020 script_dir:
3021         '_'     { $$ = scm_from_int (DOWN); }
3022         | '^'   { $$ = scm_from_int (UP); }
3023         | '-'   { $$ = SCM_UNDEFINED; }
3024         ;
3025
3026 duration_length:
3027         multiplied_duration {
3028                 $$ = $1;
3029         }
3030         ;
3031
3032 maybe_notemode_duration:
3033         {
3034                 $$ = SCM_UNDEFINED;
3035         } %prec ':'
3036         | multiplied_duration   {
3037                 $$ = $1;
3038                 parser->default_duration_ = *unsmob_duration ($$);
3039         }
3040 ;
3041
3042
3043 optional_notemode_duration:
3044         maybe_notemode_duration
3045         {
3046                 if (SCM_UNBNDP ($$))
3047                         $$ = parser->default_duration_.smobbed_copy ();
3048         }
3049         ;
3050
3051 steno_duration:
3052         UNSIGNED dots           {
3053                 $$ = make_duration ($1, scm_to_int ($2));
3054                 if (SCM_UNBNDP ($$))
3055                 {
3056                         parser->parser_error (@1, _ ("not a duration"));
3057                         $$ = Duration ().smobbed_copy ();
3058                 }
3059         }
3060         | DURATION_IDENTIFIER dots      {
3061                 Duration *d = unsmob_duration ($1);
3062                 Duration k (d->duration_log (),
3063                             d->dot_count () + scm_to_int ($2));
3064                 k = k.compressed (d->factor ());
3065                 scm_remember_upto_here_1 ($1);
3066                 $$ = k.smobbed_copy ();
3067         }
3068         ;
3069
3070 multiplied_duration:
3071         steno_duration {
3072                 $$ = $1;
3073         }
3074         | multiplied_duration '*' UNSIGNED {
3075                 $$ = unsmob_duration ($$)->compressed (scm_to_int ($3)).smobbed_copy ();
3076         }
3077         | multiplied_duration '*' FRACTION {
3078                 Rational  m (scm_to_int (scm_car ($3)), scm_to_int (scm_cdr ($3)));
3079
3080                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
3081         }
3082         ;
3083
3084 dots:
3085         /* empty */     {
3086                 $$ = SCM_INUM0;
3087         }
3088         | dots '.' {
3089                 $$ = scm_oneplus ($1);
3090         }
3091         ;
3092
3093 tremolo_type:
3094         ':'     {
3095                 $$ = SCM_INUM0;
3096         }
3097         | ':' UNSIGNED {
3098                 if (SCM_UNBNDP (make_duration ($2)))
3099                         parser->parser_error (@2, _ ("not a duration"));
3100                 $$ = $2;
3101         }
3102         ;
3103
3104 bass_number:
3105         UNSIGNED { $$ = $1; }
3106         | STRING { $$ = $1; }
3107         | full_markup { $$ = $1; }
3108         | embedded_scm_bare
3109         {
3110                 // as an integer, it needs to be non-negative, and otherwise
3111                 // it needs to be suitable as a markup.
3112                 if (scm_is_integer ($1)
3113                     ? scm_is_true (scm_negative_p ($1))
3114                     : !Text_interface::is_markup ($1))
3115                 {
3116                         parser->parser_error (@1, _ ("bass number expected"));
3117                         $$ = SCM_INUM0;
3118                 }
3119         }
3120         ;
3121
3122 figured_bass_alteration:
3123         '-'     { $$ = ly_rational2scm (FLAT_ALTERATION); }
3124         | '+'   { $$ = ly_rational2scm (SHARP_ALTERATION); }
3125         | '!'   { $$ = scm_from_int (0); }
3126         ;
3127
3128 bass_figure:
3129         FIGURE_SPACE {
3130                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
3131                 $$ = bfr->unprotect ();
3132         }
3133         | bass_number  {
3134                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
3135                 $$ = bfr->self_scm ();
3136
3137                 if (scm_is_number ($1))
3138                         bfr->set_property ("figure", $1);
3139                 else if (Text_interface::is_markup ($1))
3140                         bfr->set_property ("text", $1);
3141
3142                 bfr->unprotect ();
3143         }
3144         | bass_figure ']' {
3145                 $$ = $1;
3146                 unsmob_music ($1)->set_property ("bracket-stop", SCM_BOOL_T);
3147         }
3148         | bass_figure figured_bass_alteration {
3149                 Music *m = unsmob_music ($1);
3150                 if (scm_to_double ($2)) {
3151                         SCM salter = m->get_property ("alteration");
3152                         SCM alter = scm_is_number (salter) ? salter : scm_from_int (0);
3153                         m->set_property ("alteration",
3154                                          scm_sum (alter, $2));
3155                 } else {
3156                         m->set_property ("alteration", scm_from_int (0));
3157                 }
3158         }
3159         | bass_figure figured_bass_modification  {
3160                 Music *m = unsmob_music ($1);
3161                 m->set_property ($2, SCM_BOOL_T);
3162         }
3163         ;
3164
3165
3166 figured_bass_modification:
3167         E_PLUS          {
3168                 $$ = ly_symbol2scm ("augmented");
3169         }
3170         | E_EXCLAMATION {
3171                 $$ = ly_symbol2scm ("no-continuation");
3172         }
3173         | '/'           {
3174                 $$ = ly_symbol2scm ("diminished");
3175         }
3176         | E_BACKSLASH {
3177                 $$ = ly_symbol2scm ("augmented-slash");
3178         }
3179         ;
3180
3181 br_bass_figure:
3182         bass_figure {
3183                 $$ = $1;
3184         }
3185         | '[' bass_figure {
3186                 $$ = $2;
3187                 unsmob_music ($$)->set_property ("bracket-start", SCM_BOOL_T);
3188         }
3189         ;
3190
3191 figure_list:
3192         /**/            {
3193                 $$ = SCM_EOL;
3194         }
3195         | figure_list br_bass_figure {
3196                 $$ = scm_cons ($2, $1);
3197         }
3198         ;
3199
3200 figure_spec:
3201         FIGURE_OPEN figure_list FIGURE_CLOSE {
3202                 $$ = scm_reverse_x ($2, SCM_EOL);
3203         }
3204         ;
3205
3206
3207 optional_rest:
3208         /**/   { $$ = SCM_BOOL_F; }
3209         | REST { $$ = SCM_BOOL_T; }
3210         ;
3211
3212 simple_element:
3213         pitch exclamations questions octave_check maybe_notemode_duration optional_rest {
3214                 if (!parser->lexer_->is_note_state ())
3215                         parser->parser_error (@1, _ ("have to be in Note mode for notes"));
3216                 if (!SCM_UNBNDP ($2)
3217                     || !SCM_UNBNDP ($3)
3218                     || scm_is_number ($4)
3219                     || !SCM_UNBNDP ($5)
3220                     || scm_is_true ($6))
3221                 {
3222                         Music *n = 0;
3223                         if (scm_is_true ($6))
3224                                 n = MY_MAKE_MUSIC ("RestEvent", @$);
3225                         else
3226                                 n = MY_MAKE_MUSIC ("NoteEvent", @$);
3227                         
3228                         n->set_property ("pitch", $1);
3229                         if (SCM_UNBNDP ($5))
3230                                 n->set_property ("duration",
3231                                                  parser->default_duration_.smobbed_copy ());
3232                         else
3233                                 n->set_property ("duration", $5);
3234                         
3235                         if (scm_is_number ($4))
3236                         {
3237                                 int q = scm_to_int ($4);
3238                                 n->set_property ("absolute-octave", scm_from_int (q-1));
3239                         }
3240                         
3241                         if (to_boolean ($3))
3242                                 n->set_property ("cautionary", SCM_BOOL_T);
3243                         if (to_boolean ($2) || to_boolean ($3))
3244                                 n->set_property ("force-accidental", SCM_BOOL_T);
3245                         
3246                         $$ = n->unprotect ();
3247                 }
3248         }
3249         | DRUM_PITCH optional_notemode_duration {
3250                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
3251                 n->set_property ("duration", $2);
3252                 n->set_property ("drum-type", $1);
3253
3254                 $$ = n->unprotect ();
3255         }
3256         | RESTNAME optional_notemode_duration           {
3257                 Music *ev = 0;
3258                 if (ly_scm2string ($1) == "s") {
3259                         /* Space */
3260                         ev = MY_MAKE_MUSIC ("SkipEvent", @$);
3261                   }
3262                 else {
3263                         ev = MY_MAKE_MUSIC ("RestEvent", @$);
3264
3265                     }
3266                 ev->set_property ("duration", $2);
3267                 $$ = ev->unprotect ();
3268         }
3269         ;
3270
3271 // Can return a single pitch rather than a list.
3272 simple_chord_elements:
3273         new_chord {
3274                 if (!parser->lexer_->is_chord_state ())
3275                         parser->parser_error (@1, _ ("have to be in Chord mode for chords"));
3276                 $$ = $1;
3277         }
3278         | figure_spec optional_notemode_duration {
3279                 for (SCM s = $1; scm_is_pair (s); s = scm_cdr (s))
3280                 {
3281                         unsmob_music (scm_car (s))->set_property ("duration", $2);
3282                 }
3283                 $$ = $1;
3284         }
3285         ;
3286
3287 lyric_element:
3288         full_markup {
3289                 if (!parser->lexer_->is_lyric_state ())
3290                         parser->parser_error (@1, _ ("markup outside of text script or \\lyricmode"));
3291                 $$ = $1;
3292         }
3293         | STRING {
3294                 if (!parser->lexer_->is_lyric_state ())
3295                         parser->parser_error (@1, _ ("unrecognized string, not in text script or \\lyricmode"));
3296                 $$ = $1;
3297         }
3298         | LYRIC_ELEMENT
3299         ;
3300
3301 lyric_element_music:
3302         lyric_element optional_notemode_duration post_events {
3303                 $$ = MAKE_SYNTAX ("lyric-event", @$, $1, $2);
3304                 if (scm_is_pair ($3))
3305                         unsmob_music ($$)->set_property
3306                                 ("articulations", scm_reverse_x ($3, SCM_EOL));
3307         } %prec ':'
3308         ;
3309
3310 // Can return a single pitch rather than a list.
3311 new_chord:
3312         steno_tonic_pitch maybe_notemode_duration   {
3313                 if (SCM_UNBNDP ($2))
3314                         $$ = $1;
3315                 else
3316                         $$ = make_chord_elements (@$, $1, $2, SCM_EOL);
3317         }
3318         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
3319                 SCM its = scm_reverse_x ($4, SCM_EOL);
3320                 $$ = make_chord_elements (@$, $1, $2, scm_cons ($3, its));
3321         } %prec ':'
3322         ;
3323
3324 chord_items:
3325         /**/ {
3326                 $$ = SCM_EOL;
3327         }
3328         | chord_items chord_item {
3329                 $$ = scm_cons ($2, $$);
3330         }
3331         ;
3332
3333 chord_separator:
3334         CHORD_COLON {
3335                 $$ = ly_symbol2scm ("chord-colon");
3336         }
3337         | CHORD_CARET {
3338                 $$ = ly_symbol2scm ("chord-caret");
3339         }
3340         | CHORD_SLASH steno_tonic_pitch {
3341                 $$ = scm_list_2 (ly_symbol2scm ("chord-slash"), $2);
3342         }
3343         | CHORD_BASS steno_tonic_pitch {
3344                 $$ = scm_list_2 (ly_symbol2scm ("chord-bass"), $2);
3345         }
3346         ;
3347
3348 chord_item:
3349         chord_separator {
3350                 $$ = $1;
3351         }
3352         | step_numbers {
3353                 $$ = scm_reverse_x ($1, SCM_EOL);
3354         }
3355         | CHORD_MODIFIER  {
3356                 $$ = $1;
3357         }
3358         ;
3359
3360 step_numbers:
3361         step_number { $$ = scm_cons ($1, SCM_EOL); }
3362         | step_numbers '.' step_number {
3363                 $$ = scm_cons ($3, $$);
3364         }
3365         ;
3366
3367 step_number:
3368         UNSIGNED {
3369                 $$ = make_chord_step ($1, 0);
3370         }
3371         | UNSIGNED '+' {
3372                 $$ = make_chord_step ($1, SHARP_ALTERATION);
3373         }
3374         | UNSIGNED CHORD_MINUS {
3375                 $$ = make_chord_step ($1, FLAT_ALTERATION);
3376         }
3377         ;
3378
3379 tempo_range:
3380         unsigned_number {
3381                 $$ = $1;
3382         } %prec ':'
3383         | unsigned_number '-' unsigned_number {
3384                 $$ = scm_cons ($1, $3);
3385         }
3386         ;
3387
3388 /*
3389         UTILITIES
3390
3391 TODO: should deprecate in favor of Scheme?
3392
3393  */
3394 number_expression:
3395         number_expression '+' number_term {
3396                 $$ = scm_sum ($1, $3);
3397         }
3398         | number_expression '-' number_term {
3399                 $$ = scm_difference ($1, $3);
3400         }
3401         | number_term
3402         ;
3403
3404 number_term:
3405         number_factor {
3406                 $$ = $1;
3407         }
3408         | number_factor '*' number_factor {
3409                 $$ = scm_product ($1, $3);
3410         }
3411         | number_factor '/' number_factor {
3412                 $$ = scm_divide ($1, $3);
3413         }
3414         ;
3415
3416 number_factor:
3417         '-'  number_factor { /* %prec UNARY_MINUS */
3418                 $$ = scm_difference ($2, SCM_UNDEFINED);
3419         }
3420         | bare_number
3421         ;
3422
3423 bare_number_common:
3424         bare_number_common_closed
3425         | REAL NUMBER_IDENTIFIER
3426         {
3427                 $$ = scm_product ($1, $2);
3428         }
3429         ;
3430
3431 bare_number_common_closed:
3432         REAL
3433         | NUMBER_IDENTIFIER
3434         ;
3435
3436 bare_number:
3437         bare_number_common
3438         | UNSIGNED
3439         | UNSIGNED NUMBER_IDENTIFIER    {
3440                 $$ = scm_product ($1, $2);
3441         }
3442         ;
3443
3444 bare_number_closed:
3445         UNSIGNED
3446         | bare_number_common_closed
3447         ;
3448
3449 unsigned_number:
3450         UNSIGNED
3451         | NUMBER_IDENTIFIER
3452         {
3453                 if (!scm_is_integer ($1)
3454                     || scm_is_true (scm_negative_p ($1)))
3455                 {
3456                         parser->parser_error (@1, _("not an unsigned integer"));
3457                         $$ = SCM_INUM0;
3458                 }
3459         }
3460         | embedded_scm
3461         {
3462                 if (!scm_is_integer ($1)
3463                     || scm_is_true (scm_negative_p ($1)))
3464                 {
3465                         parser->parser_error (@1, _("not an unsigned integer"));
3466                         $$ = SCM_INUM0;
3467                 }
3468         }
3469         ;
3470
3471 exclamations:
3472                 { $$ = SCM_UNDEFINED; }
3473         | exclamations '!'
3474         {
3475                 if (SCM_UNBNDP ($1))
3476                         $$ = SCM_BOOL_T;
3477                 else
3478                         $$ = scm_not ($1);
3479         }
3480         ;
3481
3482 questions:
3483         { $$ = SCM_UNDEFINED; }
3484         | questions '?'
3485         {
3486                 if (SCM_UNBNDP ($1))
3487                         $$ = SCM_BOOL_T;
3488                 else
3489                         $$ = scm_not ($1);
3490         }
3491         ;
3492
3493 full_markup_list:
3494         MARKUPLIST
3495                 { parser->lexer_->push_markup_state (); }
3496         markup_list {
3497                 $$ = $3;
3498                 parser->lexer_->pop_state ();
3499         }
3500         ;
3501
3502 full_markup:
3503         MARKUP
3504                 { parser->lexer_->push_markup_state (); }
3505         markup_top {
3506                 $$ = $3;
3507                 parser->lexer_->pop_state ();
3508         }
3509         ;
3510
3511 markup_top:
3512         markup_list {
3513                 $$ = scm_list_2 (ly_lily_module_constant ("line-markup"),  $1);
3514         }
3515         | markup_head_1_list simple_markup
3516         {
3517                 $$ = scm_car (MAKE_SYNTAX ("composed-markup-list",
3518                                            @2, $1, scm_list_1 ($2)));
3519         }
3520         | simple_markup {
3521                 $$ = $1;
3522         }
3523         ;
3524
3525 markup_scm:
3526         embedded_scm_bare
3527         {
3528                 if (Text_interface::is_markup ($1))
3529                         MYBACKUP (MARKUP_IDENTIFIER, $1, @1);
3530                 else if (Text_interface::is_markup_list ($1))
3531                         MYBACKUP (MARKUPLIST_IDENTIFIER, $1, @1);
3532                 else {
3533                         parser->parser_error (@1, _ ("not a markup"));
3534                         MYBACKUP (MARKUP_IDENTIFIER, scm_string (SCM_EOL), @1);
3535                 }
3536         } BACKUP
3537         ;
3538                         
3539
3540 markup_list:
3541         markup_composed_list {
3542                 $$ = $1;
3543         }
3544         | markup_uncomposed_list
3545         ;
3546
3547 markup_uncomposed_list:
3548         markup_braced_list {
3549                 $$ = $1;
3550         }
3551         | markup_command_list {
3552                 $$ = scm_list_1 ($1);
3553         }
3554         | markup_scm MARKUPLIST_IDENTIFIER
3555         {
3556                 $$ = $2;
3557         }
3558         | SCORELINES {
3559                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3560                 parser->lexer_->push_note_state (nn);
3561         } '{' score_body '}' {
3562                 $$ = scm_list_1 (scm_list_2 (ly_lily_module_constant ("score-lines-markup-list"), $4));
3563                 parser->lexer_->pop_state ();
3564         }
3565         ;
3566
3567 markup_composed_list:
3568         markup_head_1_list markup_uncomposed_list {
3569                 $$ = MAKE_SYNTAX ("composed-markup-list",
3570                                   @2, $1, $2);
3571         }
3572         ;
3573
3574 markup_braced_list:
3575         '{' markup_braced_list_body '}' {
3576                 $$ = scm_reverse_x ($2, SCM_EOL);
3577         }
3578         ;
3579
3580 markup_braced_list_body:
3581         /* empty */     {  $$ = SCM_EOL; }
3582         | markup_braced_list_body markup {
3583                 $$ = scm_cons ($2, $1);
3584         }
3585         | markup_braced_list_body markup_list {
3586                 $$ = scm_reverse_x ($2, $1);
3587         }
3588         ;
3589
3590 markup_command_list:
3591         MARKUP_LIST_FUNCTION markup_command_list_arguments {
3592           $$ = scm_cons ($1, scm_reverse_x($2, SCM_EOL));
3593         }
3594         ;
3595
3596 markup_command_basic_arguments:
3597         EXPECT_MARKUP_LIST markup_command_list_arguments markup_list {
3598           $$ = scm_cons ($3, $2);
3599         }
3600         | EXPECT_SCM markup_command_list_arguments embedded_scm_closed {
3601           $$ = check_scheme_arg (parser, @3, $3, $2, $1);
3602         }
3603         | EXPECT_NO_MORE_ARGS {
3604           $$ = SCM_EOL;
3605         }
3606         ;
3607
3608 markup_command_list_arguments:
3609         markup_command_basic_arguments { $$ = $1; }
3610         | EXPECT_MARKUP markup_command_list_arguments markup {
3611           $$ = scm_cons ($3, $2);
3612         }
3613         ;
3614
3615 markup_head_1_item:
3616         MARKUP_FUNCTION EXPECT_MARKUP markup_command_list_arguments {
3617           $$ = scm_cons ($1, scm_reverse_x ($3, SCM_EOL));
3618         }
3619         ;
3620
3621 markup_head_1_list:
3622         markup_head_1_item      {
3623                 $$ = scm_list_1 ($1);
3624         }
3625         | markup_head_1_list markup_head_1_item {
3626                 $$ = scm_cons ($2, $1);
3627         }
3628         ;
3629
3630 simple_markup:
3631         STRING {
3632                 $$ = make_simple_markup ($1);
3633         }
3634         | SCORE {
3635                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3636                 parser->lexer_->push_note_state (nn);
3637         } '{' score_body '}' {
3638                 $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), $4);
3639                 parser->lexer_->pop_state ();
3640         }
3641         | MARKUP_FUNCTION markup_command_basic_arguments {
3642                 $$ = scm_cons ($1, scm_reverse_x ($2, SCM_EOL));
3643         }
3644         | markup_scm MARKUP_IDENTIFIER
3645         {
3646                 $$ = $2;
3647         }
3648         ;
3649
3650 markup:
3651         markup_head_1_list simple_markup
3652         {
3653                 $$ = scm_car (MAKE_SYNTAX ("composed-markup-list",
3654                                            @2, $1, scm_list_1 ($2)));
3655         }
3656         | simple_markup {
3657                 $$ = $1;
3658         }
3659         ;
3660
3661 %%
3662
3663 void
3664 Lily_parser::set_yydebug (bool x)
3665 {
3666         yydebug = x;
3667 }
3668
3669 SCM
3670 Lily_parser::do_yyparse ()
3671 {
3672         SCM retval = SCM_UNDEFINED;
3673         yyparse (this, &retval);
3674         return retval;
3675 }
3676
3677
3678
3679
3680
3681 /*
3682
3683 It is a little strange to have this function in this file, but
3684 otherwise, we have to import music classes into the lexer.
3685
3686 */
3687 int
3688 Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
3689 {
3690         if (unsmob_book (sid)) {
3691                 Book *book =  unsmob_book (sid)->clone ();
3692                 *destination = book->self_scm ();
3693                 book->unprotect ();
3694
3695                 return BOOK_IDENTIFIER;
3696         } else if (scm_is_number (sid)) {
3697                 *destination = sid;
3698                 return NUMBER_IDENTIFIER;
3699         } else if (unsmob_context_def (sid)) {
3700                 Context_def *def= unsmob_context_def (sid)->clone ();
3701
3702                 *destination = def->self_scm ();
3703                 def->unprotect ();
3704
3705                 return CONTEXT_DEF_IDENTIFIER;
3706         } else if (unsmob_context_mod (sid)) {
3707                 *destination = unsmob_context_mod (sid)->smobbed_copy ();
3708
3709                 return CONTEXT_MOD_IDENTIFIER;
3710         } else if (Music *mus = unsmob_music (sid)) {
3711                 mus = mus->clone ();
3712                 *destination = mus->self_scm ();
3713                 bool is_event = mus->is_mus_type ("post-event");
3714                 mus->unprotect ();
3715                 return is_event ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
3716         } else if (unsmob_pitch (sid)) {
3717                 *destination = unsmob_pitch (sid)->smobbed_copy ();
3718                 return PITCH_IDENTIFIER;
3719         } else if (unsmob_duration (sid)) {
3720                 *destination = unsmob_duration (sid)->smobbed_copy ();
3721                 return DURATION_IDENTIFIER;
3722         } else if (unsmob_output_def (sid)) {
3723                 Output_def *p = unsmob_output_def (sid);
3724                 p = p->clone ();
3725
3726                 *destination = p->self_scm ();
3727                 p->unprotect ();
3728                 return OUTPUT_DEF_IDENTIFIER;
3729         }
3730
3731         return -1;
3732 }
3733
3734 SCM
3735 get_next_unique_context_id ()
3736 {
3737         return scm_from_locale_string ("$uniqueContextId");
3738 }
3739
3740
3741 SCM
3742 get_next_unique_lyrics_context_id ()
3743 {
3744         static int new_context_count;
3745         char s[128];
3746         snprintf (s, sizeof (s)-1, "uniqueContext%d", new_context_count++);
3747         return scm_from_locale_string (s);
3748 }
3749
3750 // check_scheme_arg checks one argument with a given predicate for use
3751 // in an argument list and throws a syntax error if it is unusable.
3752 // The argument is prepended to the argument list in any case.  After
3753 // throwing a syntax error, the argument list is terminated with #f as
3754 // its last cdr in order to mark it as uncallable while not losing
3755 // track of its total length.
3756 //
3757 // There are a few special considerations: if optional argument disp
3758 // is given (otherwise it defaults to SCM_UNDEFINED), it will be used
3759 // instead of arg in a prospective error message.  This is useful if
3760 // arg is not the actual argument but rather a transformation of it.
3761 //
3762 // If arg itself is SCM_UNDEFINED, the predicate is considered false
3763 // and an error message using disp is produced unconditionally.
3764
3765 SCM check_scheme_arg (Lily_parser *parser, Input loc,
3766                       SCM arg, SCM args, SCM pred, SCM disp)
3767 {
3768         if (SCM_UNBNDP (arg))
3769                 args = scm_cons (disp, args);
3770         else {
3771                 args = scm_cons (arg, args);
3772                 if (scm_is_true (scm_call_1 (pred, arg)))
3773                         return args;
3774         }
3775         scm_set_cdr_x (scm_last_pair (args), SCM_EOL);
3776         MAKE_SYNTAX ("argument-error", loc, scm_length (args), pred,
3777                      SCM_UNBNDP (disp) ? arg : disp);
3778         scm_set_cdr_x (scm_last_pair (args), SCM_BOOL_F);
3779         return args;
3780 }
3781
3782 SCM loc_on_music (Input loc, SCM arg)
3783 {
3784         if (Music *m = unsmob_music (arg))
3785         {
3786                 m = m->clone ();
3787                 m->set_spot (loc);
3788                 return m->unprotect ();
3789         }
3790         return arg;
3791 }
3792
3793 SCM
3794 try_string_variants (SCM pred, SCM str)
3795 {
3796         // a matching predicate is always ok
3797         if (scm_is_true (scm_call_1 (pred, str)))
3798                 return str;
3799         // a symbol may be interpreted as a list of symbols if it helps
3800         if (scm_is_symbol (str)) {
3801                 str = scm_list_1 (str);
3802                 if (scm_is_true (scm_call_1 (pred, str)))
3803                         return str;
3804                 return SCM_UNDEFINED;
3805         }
3806
3807         // If this cannot be a string representation of a symbol list,
3808         // we are through.
3809
3810         if (!is_regular_identifier (str, true))
3811                 return SCM_UNDEFINED;
3812
3813         str = scm_string_split (str, SCM_MAKE_CHAR ('.'));
3814         for (SCM p = str; scm_is_pair (p); p = scm_cdr (p))
3815                 scm_set_car_x (p, scm_string_to_symbol (scm_car (p)));
3816
3817         // Let's attempt the symbol list interpretation first.
3818
3819         if (scm_is_true (scm_call_1 (pred, str)))
3820                 return str;
3821
3822         // If there is just one symbol in the list, we might interpret
3823         // it as a single symbol
3824
3825         if (scm_is_null (scm_cdr (str)))
3826         {
3827                 str = scm_car (str);
3828                 if (scm_is_true (scm_call_1 (pred, str)))
3829                         return str;
3830         }
3831
3832         return SCM_UNDEFINED;
3833 }
3834
3835 bool
3836 is_regular_identifier (SCM id, bool multiple)
3837 {
3838   if (!scm_is_string (id))
3839           return false;
3840
3841   string str = ly_scm2string (id);
3842
3843   bool middle = false;
3844
3845   for (string::iterator it=str.begin(); it != str.end (); it++)
3846   {
3847           int c = *it & 0xff;
3848           if ((c >= 'a' && c <= 'z')
3849               || (c >= 'A' && c <= 'Z')
3850               || c > 0x7f)
3851                   middle = true;
3852           else if (middle && (c == '-' || c == '_' || (multiple && c == '.')))
3853                   middle = false;
3854           else
3855                   return false;
3856   }
3857   return middle;
3858 }
3859
3860 SCM
3861 make_music_from_simple (Lily_parser *parser, Input loc, SCM simple)
3862 {
3863         if (unsmob_music (simple))
3864                 return simple;
3865         if (parser->lexer_->is_note_state ()) {
3866                 if (scm_is_symbol (simple)) {
3867                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3868                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3869                         n->set_property ("drum-type", simple);
3870                         return n->unprotect ();
3871                 }
3872                 if (unsmob_pitch (simple)) {
3873                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3874                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3875                         n->set_property ("pitch", simple);
3876                         return n->unprotect ();
3877                 }
3878                 return simple;
3879         } else if (parser->lexer_->is_lyric_state ()) {
3880                 if (Text_interface::is_markup (simple))
3881                         return MAKE_SYNTAX ("lyric-event", loc, simple,
3882                                             parser->default_duration_.smobbed_copy ());
3883         } else if (parser->lexer_->is_chord_state ()) {
3884                 if (unsmob_pitch (simple))
3885                         return MAKE_SYNTAX
3886                                 ("event-chord",
3887                                  loc,
3888                                  make_chord_elements (loc, simple,
3889                                                       parser->default_duration_.smobbed_copy (),
3890                                                       SCM_EOL));
3891         }
3892         return simple;
3893 }
3894
3895 Music *
3896 make_music_with_input (SCM name, Input where)
3897 {
3898        Music *m = make_music_by_name (name);
3899        m->set_spot (where);
3900        return m;
3901 }
3902
3903 SCM
3904 make_simple_markup (SCM a)
3905 {
3906         return a;
3907 }
3908
3909 SCM
3910 make_duration (SCM d, int dots)
3911 {
3912         int t = scm_to_int (d);
3913         if (t > 0 && (t & (t-1)) == 0)
3914                 return Duration (intlog2 (t), dots).smobbed_copy ();
3915         else
3916                 return SCM_UNDEFINED;
3917 }
3918
3919 SCM
3920 make_chord_step (SCM step_scm, Rational alter)
3921 {
3922         int step = scm_to_int (step_scm);
3923
3924         if (step == 7)
3925                 alter += FLAT_ALTERATION;
3926
3927         while (step < 0)
3928                 step += 7;
3929         Pitch m ((step -1) / 7, (step - 1) % 7, alter);
3930         return m.smobbed_copy ();
3931 }
3932
3933
3934 SCM
3935 make_chord_elements (Input loc, SCM pitch, SCM dur, SCM modification_list)
3936 {
3937         SCM chord_ctor = ly_lily_module_constant ("construct-chord-elements");
3938         SCM res = scm_call_3 (chord_ctor, pitch, dur, modification_list);
3939         for (SCM s = res; scm_is_pair (s); s = scm_cdr (s))
3940         {
3941                 unsmob_music (scm_car (s))->set_spot (loc);
3942         }
3943         return res;
3944 }
3945
3946 int
3947 yylex (YYSTYPE *s, YYLTYPE *loc, Lily_parser *parser)
3948 {
3949         Lily_lexer *lex = parser->lexer_;
3950
3951         lex->lexval_ = s;
3952         lex->lexloc_ = loc;
3953         lex->prepare_for_next_token ();
3954         return lex->yylex ();
3955 }