]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
Regularize score handling (origin and copying)
[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                 unsmob_score ($3)->origin ()->set_spot (@$);
878                 $$ = $3;
879         }
880         ;
881
882 score_headers:
883         /* empty */
884         {
885                 $$ = SCM_EOL;
886         }
887         | score_headers
888         {
889                 if (!scm_is_pair ($1)
890                     || !ly_is_module (scm_car ($1)))
891                         $1 = scm_cons (ly_make_module (false), $1);
892                 parser->lexer_->add_scope (scm_car ($1));
893         } lilypond_header
894         {
895                 $$ = $1;
896         }
897         | score_headers output_def
898         {
899                 Output_def *od = unsmob_output_def ($2);
900                 if (od->lookup_variable (ly_symbol2scm ("is-paper")) == SCM_BOOL_T)
901                 {
902                         parser->parser_error (@2, _("\\paper cannot be used in \\score, use \\layout instead"));
903
904                 }
905                 else
906                 {
907                         if (scm_is_pair ($1) && ly_is_module (scm_car ($1)))
908                                 scm_set_cdr_x ($1, scm_cons ($2, scm_cdr ($1)));
909                         else
910                                 $$ = scm_cons ($2, $1);
911                 }
912         }
913         ;
914
915                 
916
917 score_body:
918         score_headers music {
919                 SCM scorify = ly_lily_module_constant ("scorify-music");
920                 $$ = scm_call_2 (scorify, $2, parser->self_scm ());
921
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                 if (!unsmob_score ($1))
936                 {
937                         $$ = (new Score)->unprotect ();
938                         parser->parser_error (@1, _("score expected"));
939                 }
940         }
941         | score_body
942         {
943                 Score *score = unsmob_score ($1);
944                 if (!ly_is_module (score->get_header ()))
945                         score->set_header (ly_make_module (false));
946                 parser->lexer_->add_scope (score->get_header ());
947         } lilypond_header
948         | score_body output_def {
949                 Output_def *od = unsmob_output_def ($2);
950                 if (od->lookup_variable (ly_symbol2scm ("is-paper")) == SCM_BOOL_T)
951                 {
952                         parser->parser_error (@2, _("\\paper cannot be used in \\score, use \\layout instead"));
953
954                 }
955                 else
956                 {
957                         unsmob_score ($1)->add_output_def (od);
958                 }
959         }
960         | score_body error {
961                 unsmob_score ($$)->error_found_ = true;
962         }
963         ;
964
965
966 /*
967         OUTPUT DEF
968 */
969
970 paper_block:
971         output_def {
972                 Output_def *od = unsmob_output_def ($1);
973
974                 if (od->lookup_variable (ly_symbol2scm ("is-paper")) != SCM_BOOL_T)
975                 {
976                         parser->parser_error (@1, _ ("need \\paper for paper block"));
977                         $$ = get_paper (parser)->unprotect ();
978                 }
979         }
980         ;
981
982
983 output_def:
984         output_def_body '}' {
985                 $$ = $1;
986
987                 parser->lexer_->remove_scope ();
988                 parser->lexer_->pop_state ();
989         }
990         ;
991
992 output_def_head:
993         PAPER {
994                 Output_def *p = get_paper (parser);
995                 p->input_origin_ = @$;
996                 parser->lexer_->add_scope (p->scope_);
997                 $$ = p->unprotect ();
998         }
999         | MIDI    {
1000                 Output_def *p = get_midi (parser);
1001                 $$ = p->unprotect ();
1002                 parser->lexer_->add_scope (p->scope_);
1003         }
1004         | LAYOUT        {
1005                 Output_def *p = get_layout (parser);
1006
1007                 parser->lexer_->add_scope (p->scope_);
1008                 $$ = p->unprotect ();
1009         }
1010         ;
1011
1012 output_def_head_with_mode_switch:
1013         output_def_head {
1014                 parser->lexer_->push_initial_state ();
1015                 $$ = $1;
1016         }
1017         ;
1018
1019 // We need this weird nonterminal because both music as well as a
1020 // context definition can start with \context and the difference is
1021 // only apparent after looking at the next token.  If it is '{', there
1022 // is still time to escape from notes mode.
1023
1024 music_or_context_def:
1025         music_arg
1026         | context_def_spec_block
1027         ;
1028
1029 output_def_body:
1030         output_def_head_with_mode_switch '{' {
1031                 $$ = $1;
1032                 unsmob_output_def ($$)->input_origin_.set_spot (@$);
1033         }
1034         | output_def_head_with_mode_switch '{' OUTPUT_DEF_IDENTIFIER    {
1035                 Output_def *o = unsmob_output_def ($3);
1036                 o->input_origin_.set_spot (@$);
1037                 $$ = o->self_scm ();
1038                 parser->lexer_->remove_scope ();
1039                 parser->lexer_->add_scope (o->scope_);
1040         }
1041         | output_def_body assignment  {
1042
1043         }
1044         | output_def_body embedded_scm  {
1045
1046         }
1047         | output_def_body
1048         {
1049                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
1050                 parser->lexer_->push_note_state (nn);
1051         } music_or_context_def
1052         {
1053                 parser->lexer_->pop_state ();
1054                 if (unsmob_context_def ($3))
1055                         assign_context_def (unsmob_output_def ($1), $3);
1056                 else {
1057
1058                         SCM proc = parser->lexer_->lookup_identifier
1059                                      ("output-def-music-handler");
1060                         scm_call_3 (proc, parser->self_scm (),
1061                                     $1, $3);
1062                 }
1063         }
1064         | output_def_body error {
1065
1066         }
1067         ;
1068
1069 tempo_event:
1070         TEMPO steno_duration '=' tempo_range    {
1071                 $$ = MAKE_SYNTAX ("tempo", @$, SCM_EOL, $2, $4);
1072         }
1073         | TEMPO scalar steno_duration '=' tempo_range   {
1074                 $$ = MAKE_SYNTAX ("tempo", @$, $2, $3, $5);
1075         }
1076         | TEMPO scalar {
1077                 $$ = MAKE_SYNTAX ("tempo", @$, $2);
1078         } %prec ':'
1079         ;
1080
1081 /*
1082 The representation of a  list is reversed to have efficient append.  */
1083
1084 music_list:
1085         /* empty */ {
1086                 $$ = SCM_EOL;
1087         }
1088         | music_list music_embedded {
1089                 if (unsmob_music ($2))
1090                         $$ = scm_cons ($2, $1);
1091         }
1092         | music_list error {
1093                 Music *m = MY_MAKE_MUSIC("Music", @$);
1094                 // ugh. code dup
1095                 m->set_property ("error-found", SCM_BOOL_T);
1096                 $$ = scm_cons (m->self_scm (), $1);
1097                 m->unprotect (); /* UGH */
1098         }
1099         ;
1100
1101 braced_music_list:
1102         '{' music_list '}'
1103         {
1104                 $$ = scm_reverse_x ($2, SCM_EOL);
1105         }
1106         ;
1107
1108 music:  music_arg
1109         | lyric_element_music
1110         ;
1111
1112 music_embedded:
1113         music
1114         {
1115                 if (unsmob_music ($1)->is_mus_type ("post-event")) {
1116                         parser->parser_error (@1, _ ("unexpected post-event"));
1117                         $$ = SCM_UNSPECIFIED;
1118                 }
1119         }
1120         | music_embedded_backup
1121         {
1122                 $$ = $1;
1123         }
1124         | music_embedded_backup BACKUP lyric_element_music
1125         {
1126                 $$ = $3;
1127         }
1128         | multiplied_duration post_events
1129         {
1130                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
1131
1132                 parser->default_duration_ = *unsmob_duration ($1);
1133                 n->set_property ("duration", $1);
1134
1135                 if (scm_is_pair ($2))
1136                         n->set_property ("articulations",
1137                                          scm_reverse_x ($2, SCM_EOL));
1138                 $$ = n->unprotect ();
1139         }
1140         ;
1141
1142 music_embedded_backup:
1143         embedded_scm
1144         {
1145                 if (scm_is_eq ($1, SCM_UNSPECIFIED))
1146                         $$ = $1;
1147                 else if (Music *m = unsmob_music ($1)) {
1148                         if (m->is_mus_type ("post-event")) {
1149                                 parser->parser_error
1150                                         (@1, _ ("unexpected post-event"));
1151                                 $$ = SCM_UNSPECIFIED;
1152                         } else
1153                                 $$ = $1;
1154                 } else if (parser->lexer_->is_lyric_state ()
1155                            && Text_interface::is_markup ($1))
1156                         MYBACKUP (LYRIC_ELEMENT, $1, @1);
1157                 else {
1158                         @$.warning (_ ("Ignoring non-music expression"));
1159                         $$ = $1;
1160                 }
1161         }
1162         ;
1163
1164 music_arg:
1165         simple_music
1166         {
1167                 $$ = make_music_from_simple (parser, @1, $1);
1168                 if (!unsmob_music ($$))
1169                 {
1170                         parser->parser_error (@1, _ ("music expected"));
1171                         $$ = MAKE_SYNTAX ("void-music", @$);
1172                 }
1173         }
1174         | composite_music %prec COMPOSITE
1175         ;
1176
1177 music_assign:
1178         simple_music
1179         | composite_music %prec COMPOSITE
1180         ;
1181
1182 repeated_music:
1183         REPEAT simple_string unsigned_number music
1184         {
1185                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, SCM_EOL);
1186         }
1187         | REPEAT_IDENTIFIER music
1188         {
1189                 $$ = MAKE_SYNTAX ("repeat", @$, scm_car ($1), scm_cdr ($1),
1190                                   $2, SCM_EOL);
1191         }
1192         | REPEAT simple_string unsigned_number music ALTERNATIVE braced_music_list
1193         {
1194                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, $6);
1195         }
1196         | REPEAT_IDENTIFIER music ALTERNATIVE braced_music_list
1197         {
1198                 $$ = MAKE_SYNTAX ("repeat", @$, scm_car ($1), scm_cdr ($1),
1199                                   $2, $4);
1200         }
1201         ;
1202
1203 sequential_music:
1204         SEQUENTIAL braced_music_list {
1205                 $$ = MAKE_SYNTAX ("sequential-music", @$, $2);
1206         }
1207         | braced_music_list {
1208                 $$ = MAKE_SYNTAX ("sequential-music", @$, $1);
1209         }
1210         ;
1211
1212 simultaneous_music:
1213         SIMULTANEOUS braced_music_list {
1214                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, $2);
1215         }
1216         | DOUBLE_ANGLE_OPEN music_list DOUBLE_ANGLE_CLOSE       {
1217                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, scm_reverse_x ($2, SCM_EOL));
1218         }
1219         ;
1220
1221 simple_music:
1222         event_chord
1223         | music_property_def
1224         | context_change
1225         ;
1226
1227 context_modification:
1228         WITH
1229         {
1230                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
1231                 parser->lexer_->push_note_state (nn);
1232         } '{' context_mod_list '}'
1233         {
1234                 parser->lexer_->pop_state ();
1235                 $$ = $4;
1236         }
1237         | WITH CONTEXT_MOD_IDENTIFIER
1238         {
1239                 $$ = $2;
1240         }
1241         | CONTEXT_MOD_IDENTIFIER
1242         {
1243                 $$ = $1;
1244         }
1245         | WITH context_modification_arg
1246         {
1247                 if (unsmob_music ($2)) {
1248                         SCM proc = parser->lexer_->lookup_identifier ("context-mod-music-handler");
1249                         $2 = scm_call_2 (proc, parser->self_scm (), $2);
1250                 }
1251                 if (unsmob_context_mod ($2))
1252                         $$ = $2;
1253                 else {
1254                         parser->parser_error (@2, _ ("not a context mod"));
1255                         $$ = Context_mod ().smobbed_copy ();
1256                 }
1257         }
1258         ;
1259
1260 context_modification_arg:
1261         embedded_scm_closed
1262         | MUSIC_IDENTIFIER
1263         ;
1264
1265 optional_context_mod:
1266         /**/ {
1267             $$ = SCM_EOL;
1268         }
1269         | context_modification
1270         {
1271               $$ = $1;
1272         }
1273         ;
1274
1275 context_mod_list:
1276         /**/ {
1277             $$ = Context_mod ().smobbed_copy ();
1278         }
1279         | context_mod_list context_mod  {
1280                 if (!SCM_UNBNDP ($2))
1281                         unsmob_context_mod ($1)->add_context_mod ($2);
1282         }
1283         | context_mod_list CONTEXT_MOD_IDENTIFIER {
1284                  Context_mod *md = unsmob_context_mod ($2);
1285                  if (md)
1286                      unsmob_context_mod ($1)->add_context_mods (md->get_mods ());
1287         }
1288         | context_mod_list context_mod_embedded {
1289                 unsmob_context_mod ($1)->add_context_mods
1290                         (unsmob_context_mod ($2)->get_mods ());
1291         }
1292         ;
1293
1294 composite_music:
1295         complex_music
1296         | music_bare
1297         ;
1298
1299 /* Music that can be parsed without lookahead */
1300 closed_music:
1301         music_bare
1302         | complex_music_prefix closed_music
1303         {
1304                 $$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
1305         }
1306         | music_function_call_closed
1307         ;
1308
1309 music_bare:
1310         mode_changed_music
1311         | MUSIC_IDENTIFIER
1312         | grouped_music_list
1313         ;
1314
1315 grouped_music_list:
1316         simultaneous_music              { $$ = $1; }
1317         | sequential_music              { $$ = $1; }
1318         ;
1319
1320 /* Function argument lists are arguably the most complex part in the
1321  * parser.  They are pretty tricky to understand because of the way
1322  * they are processed, and because of optional arguments that can be
1323  * omitted.  When there are several optional arguments in a row,
1324  * omitting one automatically omits all following arguments.  Optional
1325  * arguments can only be omitted when either
1326  *
1327  * a) the omission is explicitly started with \default
1328  * b) the omission is implicitly started by an argument not matching
1329  *    its predicate, and there is a mandatory argument later that can
1330  *    "catch" the argument that does not fit.
1331  *
1332  * When argument parsing starts, the lexer pushes EXPECT_SCM tokens
1333  * (corresponding to mandatory arguments and having a predicate
1334  * function as semantic value) or EXPECT_OPTIONAL EXPECT_SCM (where
1335  * the semantic value of the EXPECT_OPTIONAL token is the default to
1336  * use when the optional argument is omitted, and EXPECT_SCM again has
1337  * the argument predicate as semantic value) in reverse order to the
1338  * parser, followed by EXPECT_NO_MORE_ARGS.  The argument list is then
1339  * processed inside-out while actual tokens are consumed.
1340  *
1341  * This means that the argument list tokens determine the actions
1342  * taken as they arrive.  The structure of the argument list is known
1343  * to the parser and stored in its parse stack when the first argument
1344  * is being parsed.  What the parser does not know is which predicates
1345  * will match and whether or not \default will be appearing in the
1346  * argument list, and where.
1347  *
1348  * Many of the basic nonterminals used for argument list scanning come
1349  * in a "normal" and a "closed" flavor.  A closed expression is one
1350  * that can be parsed without a lookahead token.  That makes it
1351  * feasible for an optional argument that may need to be skipped:
1352  * skipping can only be accomplished by pushing back the token into
1353  * the lexer, and that only works when there is no lookahead token.
1354  *
1355  * Sequences of 0 or more optional arguments are scanned using either
1356  * function_arglist_backup or function_arglist_nonbackup.  The first
1357  * is used when optional arguments are followed by at least one
1358  * mandatory argument: in that case optional arguments may be skipped
1359  * by either a false predicate (in which case the expression will be
1360  * pushed back as one or more tokens, preceded by a BACKUP token) or
1361  * by using \default.
1362  *
1363  * If optional arguments are at the end of the argument list, they are
1364  * instead scanned using function_arglist_nonbackup: here the only
1365  * manner to enter into skipping of optional arguments is the use of
1366  * \default.
1367  *
1368  * The argument list of a normal function call is parsed using
1369  * function_arglist.  The part of an argument list before a mandatory
1370  * argument is parsed using function_arglist_optional.
1371  *
1372  * The difference is that leading optional arguments are scanned using
1373  * function_arglist_nonbackup and function_arglist_backup,
1374  * respectively.
1375  *
1376  * Most other details are obvious in the rules themselves.
1377  *
1378  */
1379
1380 function_arglist_nonbackup_common:
1381         EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup post_event_nofinger
1382         {
1383                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1384         }
1385         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' UNSIGNED
1386         {
1387                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1388                 if (scm_is_true (scm_call_1 ($2, n)))
1389                         $$ = scm_cons (n, $3);
1390                 else {
1391                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1392                         t->set_property ("digit", $5);
1393                         $$ = check_scheme_arg (parser, @4, t->unprotect (),
1394                                                $3, $2, n);
1395                 }
1396                 
1397         }
1398         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' REAL
1399         {
1400                 $$ = check_scheme_arg (parser, @4,
1401                                        scm_difference ($5, SCM_UNDEFINED),
1402                                        $3, $2);
1403         }
1404         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' NUMBER_IDENTIFIER
1405         {
1406                 $$ = check_scheme_arg (parser, @4,
1407                                        scm_difference ($5, SCM_UNDEFINED),
1408                                        $3, $2);
1409         }
1410         ;
1411
1412 function_arglist_closed_nonbackup:
1413         function_arglist_nonbackup_common
1414         | function_arglist_closed_common
1415         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup embedded_scm_arg_closed
1416         {
1417                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1418         }
1419         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup bare_number_closed
1420         {
1421                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1422         }
1423         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup SCM_IDENTIFIER
1424         {
1425                 $$ = check_scheme_arg (parser, @4,
1426                                        try_string_variants ($2, $4),
1427                                        $3, $2, $4);
1428         }
1429         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup STRING
1430         {
1431                 $$ = check_scheme_arg (parser, @4,
1432                                        try_string_variants ($2, $4),
1433                                        $3, $2, $4);
1434         }
1435         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup full_markup
1436         {
1437                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1438         }
1439         ;
1440
1441 symbol_list_arg:
1442         SYMBOL_LIST
1443         | SYMBOL_LIST '.' symbol_list_rev
1444         {
1445                 $$ = scm_append (scm_list_2 ($1, scm_reverse_x ($3, SCM_EOL)));
1446         }
1447         ;
1448
1449 symbol_list_rev:
1450         symbol_list_part
1451         | symbol_list_rev '.' symbol_list_part
1452         {
1453                 $$ = scm_append_x (scm_list_2 ($3, $1));
1454         }
1455         ;
1456
1457 // symbol_list_part delivers elements in reverse copy.
1458
1459 symbol_list_part:
1460         symbol_list_element
1461         {
1462                 SCM sym_l_p = ly_lily_module_constant ("symbol-list?");
1463                 $$ = try_string_variants (sym_l_p, $1);
1464                 if (SCM_UNBNDP ($$)) {
1465                         parser->parser_error (@1, _("not a symbol"));
1466                         $$ = SCM_EOL;
1467                 } else
1468                         $$ = scm_reverse ($$);
1469         }
1470         ;
1471
1472
1473 symbol_list_element:
1474         STRING
1475         | embedded_scm_bare
1476         ;
1477
1478
1479 function_arglist_nonbackup:
1480         function_arglist_nonbackup_common
1481         | function_arglist_common
1482         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup embedded_scm_arg
1483         {
1484                 if (scm_is_true (scm_call_1 ($2, $4)))
1485                         $$ = scm_cons ($4, $3);
1486                 else
1487                         $$ = check_scheme_arg (parser, @4,
1488                                                make_music_from_simple
1489                                                (parser, @4, $4),
1490                                                $3, $2);
1491         }
1492         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup bare_number_common
1493         {
1494                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1495         }
1496         | function_arglist_nonbackup_reparse REPARSE duration_length
1497         {
1498                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1499         }
1500         | function_arglist_nonbackup_reparse REPARSE bare_number_common
1501         {
1502                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1503         }
1504         | function_arglist_nonbackup_reparse REPARSE SCM_ARG
1505         {
1506                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1507         }
1508         | function_arglist_nonbackup_reparse REPARSE lyric_element_music
1509         {
1510                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1511         }
1512         | function_arglist_nonbackup_reparse REPARSE symbol_list_arg
1513         {
1514                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1515         }
1516         ;
1517
1518 function_arglist_nonbackup_reparse:
1519         EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup SCM_IDENTIFIER
1520         {
1521                 $$ = $3;
1522                 SCM res = try_string_variants ($2, $4);
1523                 if (!SCM_UNBNDP (res))
1524                         if (scm_is_pair (res))
1525                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1526                         else
1527                                 MYREPARSE (@4, $2, SCM_ARG, res);
1528                 else if (scm_is_true
1529                          (scm_call_1
1530                           ($2, make_music_from_simple
1531                            (parser, @4, $4))))
1532                         MYREPARSE (@4, $2, STRING, $4);
1533                 else
1534                         MYREPARSE (@4, $2, SCM_ARG, $4);
1535         }
1536         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup STRING
1537         {
1538                 $$ = $3;
1539                 SCM res = try_string_variants ($2, $4);
1540                 if (!SCM_UNBNDP (res))
1541                         if (scm_is_pair (res))
1542                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1543                         else
1544                                 MYREPARSE (@4, $2, SCM_ARG, res);
1545                 else if (scm_is_true
1546                          (scm_call_1
1547                           ($2, make_music_from_simple
1548                            (parser, @4, $4))))
1549                         MYREPARSE (@4, $2, STRING, $4);
1550                 else
1551                         MYREPARSE (@4, $2, SCM_ARG, $4);
1552         }
1553         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup full_markup
1554         {
1555                 $$ = $3;
1556                 if (scm_is_true (scm_call_1 ($2, $4)))
1557                         MYREPARSE (@4, $2, SCM_ARG, $4);
1558                 else if (scm_is_true
1559                          (scm_call_1
1560                           ($2, make_music_from_simple
1561                            (parser, @4, $4))))
1562                         MYREPARSE (@4, $2, STRING, $4);
1563                 else
1564                         MYREPARSE (@4, $2, SCM_ARG, $4);
1565         }
1566         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup UNSIGNED
1567         {
1568                 $$ = $3;
1569                 if (scm_is_true (scm_call_1 ($2, $4)))
1570                         MYREPARSE (@4, $2, REAL, $4);
1571                 else {
1572                         SCM d = make_duration ($4);
1573                         if (SCM_UNBNDP (d) || scm_is_false (scm_call_1 ($2, d)))
1574                                 MYREPARSE (@4, $2, REAL, $4); // trigger error
1575                         else
1576                                 MYREPARSE (@4, $2, DURATION_IDENTIFIER, d);
1577                 }
1578         }
1579         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup DURATION_IDENTIFIER {
1580                 $$ = $3;
1581                 MYREPARSE (@4, $2, DURATION_IDENTIFIER, $4);
1582         }
1583         ;
1584
1585
1586 // function_arglist_backup can't occur at the end of an argument
1587 // list.  It needs to be careful about avoiding lookahead only until
1588 // it has made a decision whether or not to accept the parsed entity.
1589 // At that point of time, music requiring lookahead to parse becomes
1590 // fine.
1591 function_arglist_backup:
1592         function_arglist_common
1593         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup embedded_scm_arg_closed
1594         {
1595                 if (scm_is_true (scm_call_1 ($2, $4)))
1596                 {
1597                         $$ = scm_cons ($4, $3);
1598                 } else {
1599                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1600                         MYBACKUP (SCM_ARG, $4, @4);
1601                 }
1602         }
1603         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup REPEAT simple_string unsigned_number
1604         {
1605                 $4 = MAKE_SYNTAX ("repeat", @4, $5, $6,
1606                                   MY_MAKE_MUSIC ("Music", @4)->unprotect (),
1607                                   SCM_EOL);
1608                 if (scm_is_true (scm_call_1 ($2, $4)))
1609                 {
1610                         $$ = $3;
1611                         MYREPARSE (@4, $2, REPEAT_IDENTIFIER, scm_cons ($5, $6));
1612                 } else {
1613                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1614                         MYBACKUP (REPEAT_IDENTIFIER, scm_cons ($5, $6), @4);
1615                 }
1616         }
1617         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup chord_body
1618         {
1619                 if (scm_is_true (scm_call_1 ($2, $4)))
1620                 {
1621                         $$ = $3;
1622                         MYREPARSE (@4, $2, CHORD_BODY_IDENTIFIER, $4);
1623                 } else {
1624                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1625                         MYBACKUP (CHORD_BODY_IDENTIFIER, $4, @4);
1626                 }
1627         }
1628         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup post_event_nofinger
1629         {
1630                 if (scm_is_true (scm_call_1 ($2, $4)))
1631                 {
1632                         $$ = scm_cons ($4, $3);
1633                 } else {
1634                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1635                         MYBACKUP (EVENT_IDENTIFIER, $4, @4);
1636                 }
1637         }
1638         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup full_markup
1639         {
1640                 if (scm_is_true (scm_call_1 ($2, $4)))
1641                         $$ = scm_cons ($4, $3);
1642                 else {
1643                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1644                         MYBACKUP (LYRIC_ELEMENT, $4, @4);
1645                 }
1646         }
1647         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup UNSIGNED
1648         {
1649                 if (scm_is_true (scm_call_1 ($2, $4)))
1650                 {
1651                         MYREPARSE (@4, $2, REAL, $4);
1652                         $$ = $3;
1653                 } else {
1654                         SCM d = make_duration ($4);
1655                         if (SCM_UNBNDP (d) || scm_is_false (scm_call_1 ($2, d)))
1656                         {
1657                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1658                                 MYBACKUP (UNSIGNED, $4, @4);
1659                         } else {
1660                                 MYREPARSE (@4, $2, DURATION_IDENTIFIER, d);
1661                                 $$ = $3;
1662                         }
1663                 }
1664         }
1665         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup REAL
1666         {
1667                 if (scm_is_true (scm_call_1 ($2, $4)))
1668                 {
1669                         $$ = $3;
1670                         MYREPARSE (@4, $2, REAL, $4);
1671                 } else {
1672                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1673                         MYBACKUP (REAL, $4, @4);
1674                 }
1675         }
1676         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup NUMBER_IDENTIFIER
1677         {
1678                 if (scm_is_true (scm_call_1 ($2, $4)))
1679                 {
1680                         $$ = scm_cons ($4, $3);
1681                 } else {
1682                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1683                         MYBACKUP (NUMBER_IDENTIFIER, $4, @4);
1684                 }
1685         }
1686         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' UNSIGNED
1687         {
1688                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1689                 if (scm_is_true (scm_call_1 ($2, n))) {
1690                         $$ = $3;
1691                         MYREPARSE (@5, $2, REAL, n);
1692                 } else {
1693                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1694                         t->set_property ("digit", $5);
1695                         $$ = t->unprotect ();
1696                         if (scm_is_true (scm_call_1 ($2, $$)))
1697                                 $$ = scm_cons ($$, $3);
1698                         else {
1699                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1700                                 MYBACKUP (UNSIGNED, $5, @5);
1701                                 parser->lexer_->push_extra_token ('-');
1702                         }
1703                 }
1704                 
1705         }
1706         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' REAL
1707         {
1708                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1709                 if (scm_is_true (scm_call_1 ($2, n))) {
1710                         MYREPARSE (@5, $2, REAL, n);
1711                         $$ = $3;
1712                 } else {
1713                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1714                         MYBACKUP (REAL, n, @5);
1715                 }
1716         }
1717         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' NUMBER_IDENTIFIER
1718         {
1719                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1720                 if (scm_is_true (scm_call_1 ($2, n))) {
1721                         $$ = scm_cons (n, $3);
1722                 } else {
1723                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1724                         MYBACKUP (NUMBER_IDENTIFIER, n, @5);
1725                 }
1726         }
1727         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup PITCH_IDENTIFIER
1728         {
1729                 SCM m = make_music_from_simple (parser, @4, $4);
1730                 if (unsmob_music (m) && scm_is_true (scm_call_1 ($2, m)))
1731                 {
1732                         MYREPARSE (@4, $2, PITCH_IDENTIFIER, $4);
1733                         $$ = $3;
1734                 } else if (scm_is_true (scm_call_1 ($2, $4)))
1735                 {
1736                         MYREPARSE (@4, $2, PITCH_ARG, $4);
1737                         $$ = $3;
1738                 } else {
1739                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1740                         MYBACKUP (PITCH_IDENTIFIER, $4, @4);
1741                 }
1742         }
1743         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup NOTENAME_PITCH
1744         {
1745                 SCM m = make_music_from_simple (parser, @4, $4);
1746                 if (unsmob_music (m) && scm_is_true (scm_call_1 ($2, m)))
1747                 {
1748                         MYREPARSE (@4, $2, NOTENAME_PITCH, $4);
1749                         $$ = $3;
1750                 } else if (scm_is_true (scm_call_1 ($2, $4)))
1751                 {
1752                         MYREPARSE (@4, $2, PITCH_ARG, $4);
1753                         $$ = $3;
1754                 } else {
1755                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1756                         MYBACKUP (NOTENAME_PITCH, $4, @4);
1757                 }
1758         }
1759         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup TONICNAME_PITCH
1760         {
1761                 SCM m = make_music_from_simple (parser, @4, $4);
1762                 if (unsmob_music (m) && scm_is_true (scm_call_1 ($2, m)))
1763                 {
1764                         MYREPARSE (@4, $2, TONICNAME_PITCH, $4);
1765                         $$ = $3;
1766                 } else if (scm_is_true (scm_call_1 ($2, $4)))
1767                 {
1768                         MYREPARSE (@4, $2, PITCH_ARG, $4);
1769                         $$ = $3;
1770                 } else {
1771                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1772                         MYBACKUP (TONICNAME_PITCH, $4, @4);
1773                 }
1774         }
1775         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup DURATION_IDENTIFIER
1776         {
1777                 if (scm_is_true (scm_call_1 ($2, $4)))
1778                 {
1779                         MYREPARSE (@4, $2, DURATION_IDENTIFIER, $4);
1780                         $$ = $3;
1781                 } else {
1782                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1783                         MYBACKUP (DURATION_IDENTIFIER, $4, @4);
1784                 }
1785         }
1786         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup SCM_IDENTIFIER
1787         {
1788                 SCM res = try_string_variants ($2, $4);
1789                 if (!SCM_UNBNDP (res))
1790                         if (scm_is_pair (res)) {
1791                                 $$ = $3;
1792                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1793                         }
1794                         else
1795                                 $$ = scm_cons (res, $3);
1796                 else {
1797                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1798                         MYBACKUP (SCM_IDENTIFIER, $4, @4);
1799                 }
1800         }
1801         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup STRING
1802         {
1803                 SCM res = try_string_variants ($2, $4);
1804                 if (!SCM_UNBNDP (res))
1805                         if (scm_is_pair (res)) {
1806                                 $$ = $3;
1807                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1808                         }
1809                         else
1810                                 $$ = scm_cons (res, $3);
1811                 else {
1812                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1813                         MYBACKUP (STRING, $4, @4);
1814                 }
1815         }
1816         | function_arglist_backup REPARSE music_assign
1817         {
1818                 if (scm_is_true (scm_call_1 ($2, $3)))
1819                         $$ = scm_cons ($3, $1);
1820                 else
1821                         $$ = check_scheme_arg (parser, @3,
1822                                                make_music_from_simple
1823                                                (parser, @3, $3),
1824                                                $1, $2);
1825         }
1826         | function_arglist_backup REPARSE pitch_arg
1827         {
1828                 $$ = check_scheme_arg (parser, @3,
1829                                        $3, $1, $2);
1830         }               
1831         | function_arglist_backup REPARSE bare_number_common
1832         {
1833                 $$ = check_scheme_arg (parser, @3,
1834                                        $3, $1, $2);
1835         }
1836         | function_arglist_backup REPARSE duration_length
1837         {
1838                 $$ = check_scheme_arg (parser, @3,
1839                                        $3, $1, $2);
1840         }
1841         | function_arglist_backup REPARSE symbol_list_arg
1842         {
1843                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1844         }
1845         ;
1846
1847 function_arglist:
1848         function_arglist_nonbackup
1849         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_nonbackup DEFAULT
1850         {
1851                 $$ = scm_cons (loc_on_music (@4, $1), $3);
1852         }
1853         ;
1854
1855 function_arglist_skip_nonbackup:
1856         function_arglist_nonbackup
1857         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_nonbackup
1858         {
1859                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1860         }
1861         ;
1862
1863 function_arglist_common:
1864         EXPECT_NO_MORE_ARGS {
1865                 $$ = SCM_EOL;
1866         }
1867         | EXPECT_SCM function_arglist_optional embedded_scm_arg
1868         {
1869                 if (scm_is_true (scm_call_1 ($1, $3)))
1870                         $$ = scm_cons ($3, $2);
1871                 else
1872                         $$ = check_scheme_arg (parser, @3,
1873                                                make_music_from_simple
1874                                                (parser, @3, $3),
1875                                                $2, $1);
1876         }
1877         | EXPECT_SCM function_arglist_optional bare_number_common
1878         {
1879                 $$ = check_scheme_arg (parser, @3,
1880                                        $3, $2, $1);
1881         }
1882         | EXPECT_SCM function_arglist_optional post_event_nofinger
1883         {
1884                 $$ = check_scheme_arg (parser, @3,
1885                                        $3, $2, $1);
1886         }
1887         | EXPECT_SCM function_arglist_optional '-' NUMBER_IDENTIFIER
1888         {
1889                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1890                 $$ = check_scheme_arg (parser, @4, n, $2, $1);
1891         }
1892         | function_arglist_common_reparse REPARSE SCM_ARG
1893         {
1894                 $$ = check_scheme_arg (parser, @3,
1895                                        $3, $1, $2);
1896         }
1897         | function_arglist_common_reparse REPARSE lyric_element_music
1898         {
1899                 $$ = check_scheme_arg (parser, @3,
1900                                        $3, $1, $2);
1901         }
1902         | function_arglist_common_reparse REPARSE bare_number_common
1903         {
1904                 $$ = check_scheme_arg (parser, @3,
1905                                        $3, $1, $2);
1906         }
1907         | function_arglist_common_reparse REPARSE duration_length
1908         {
1909                 $$ = check_scheme_arg (parser, @3,
1910                                        $3, $1, $2);
1911         }
1912         | function_arglist_common_reparse REPARSE symbol_list_arg
1913         {
1914                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1915         }
1916         ;
1917
1918 function_arglist_common_reparse:
1919         EXPECT_SCM function_arglist_optional SCM_IDENTIFIER
1920         {
1921                 $$ = $2;
1922                 SCM res = try_string_variants ($1, $3);
1923                 if (!SCM_UNBNDP (res))
1924                         if (scm_is_pair (res))
1925                                 MYREPARSE (@3, $1, SYMBOL_LIST, res);
1926                         else
1927                                 MYREPARSE (@3, $1, SCM_ARG, res);
1928                 else if (scm_is_true
1929                          (scm_call_1
1930                           ($1, make_music_from_simple (parser, @3, $3))))
1931                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1932                 else
1933                         // This is going to flag a syntax error, we
1934                         // know the predicate to be false.
1935                         MYREPARSE (@3, $1, SCM_ARG, $3);
1936         }
1937         | EXPECT_SCM function_arglist_optional STRING
1938         {
1939                 $$ = $2;
1940                 SCM res = try_string_variants ($1, $3);
1941                 if (!SCM_UNBNDP (res))
1942                         if (scm_is_pair (res))
1943                                 MYREPARSE (@3, $1, SYMBOL_LIST, res);
1944                         else
1945                                 MYREPARSE (@3, $1, SCM_ARG, res);
1946                 else if (scm_is_true
1947                          (scm_call_1
1948                           ($1, make_music_from_simple (parser, @3, $3))))
1949                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1950                 else
1951                         // This is going to flag a syntax error, we
1952                         // know the predicate to be false.
1953                         MYREPARSE (@3, $1, SCM_ARG, $3);
1954         }
1955         | EXPECT_SCM function_arglist_optional full_markup
1956         {
1957                 $$ = $2;
1958                 if (scm_is_true (scm_call_1 ($1, $3)))
1959                         MYREPARSE (@3, $1, SCM_ARG, $3);
1960                 else if (scm_is_true
1961                          (scm_call_1
1962                           ($1, make_music_from_simple (parser, @3, $3))))
1963                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1964                 else
1965                         // This is going to flag a syntax error, we
1966                         // know the predicate to be false.
1967                         MYREPARSE (@3, $1, SCM_ARG, $3);
1968         }
1969         | EXPECT_SCM function_arglist_optional UNSIGNED
1970         {
1971                 $$ = $2;
1972                 if (scm_is_true (scm_call_1 ($1, $3)))
1973                         MYREPARSE (@3, $1, REAL, $3);
1974                 else {
1975                         SCM d = make_duration ($3);
1976                         if (SCM_UNBNDP (d) || scm_is_false (scm_call_1 ($1, d)))
1977                                 MYREPARSE (@3, $1, REAL, $3);
1978                         else
1979                                 MYREPARSE (@3, $1, DURATION_IDENTIFIER, d);
1980                 }
1981         }
1982         | EXPECT_SCM function_arglist_optional DURATION_IDENTIFIER
1983         {
1984                 $$ = $2;
1985                 MYREPARSE (@3, $1, DURATION_IDENTIFIER, $3);
1986         }
1987         | EXPECT_SCM function_arglist_optional '-' UNSIGNED
1988         {
1989                 $$ = $2;
1990                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1991                 if (scm_is_true (scm_call_1 ($1, n)))
1992                         MYREPARSE (@4, $1, REAL, n);
1993                 else {
1994                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @4);
1995                         t->set_property ("digit", $4);
1996                         SCM m = t->unprotect ();
1997                         if (scm_is_true (scm_call_1 ($1, m)))
1998                                 MYREPARSE (@4, $1, SCM_ARG, m);
1999                         else
2000                                 MYREPARSE (@4, $1, SCM_ARG, $4);
2001                 }
2002                 
2003         }
2004         | EXPECT_SCM function_arglist_optional '-' REAL
2005         {
2006                 $$ = $2;
2007                 SCM n = scm_difference ($4, SCM_UNDEFINED);
2008                 MYREPARSE (@4, $1, REAL, n);
2009         }
2010         ;
2011
2012 function_arglist_closed:
2013         function_arglist_closed_nonbackup
2014         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_nonbackup DEFAULT
2015         {
2016                 $$ = scm_cons (loc_on_music (@4, $1), $3);
2017         }
2018         ;
2019
2020 function_arglist_closed_common:
2021         EXPECT_NO_MORE_ARGS {
2022                 $$ = SCM_EOL;
2023         }
2024         | EXPECT_SCM function_arglist_optional embedded_scm_arg_closed
2025         {
2026                 $$ = check_scheme_arg (parser, @3,
2027                                        $3, $2, $1);
2028         }
2029         | EXPECT_SCM function_arglist_optional bare_number_common_closed
2030         {
2031                 $$ = check_scheme_arg (parser, @3,
2032                                        $3, $2, $1);
2033         }
2034         | EXPECT_SCM function_arglist_optional '-' NUMBER_IDENTIFIER
2035         {
2036                 $$ = check_scheme_arg (parser, @3,
2037                                        scm_difference ($4, SCM_UNDEFINED),
2038                                        $2, $1);
2039         }
2040         | EXPECT_SCM function_arglist_optional post_event_nofinger
2041         {
2042                 $$ = check_scheme_arg (parser, @3,
2043                                        $3, $2, $1);
2044         }
2045         | function_arglist_common_reparse REPARSE SCM_ARG
2046         {
2047                 $$ = check_scheme_arg (parser, @3,
2048                                        $3, $1, $2);
2049         }
2050         | function_arglist_common_reparse REPARSE bare_number_common_closed
2051         {
2052                 $$ = check_scheme_arg (parser, @3,
2053                                        $3, $1, $2);
2054         }
2055         | function_arglist_common_reparse REPARSE symbol_list_arg
2056         {
2057                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
2058         }
2059         ;
2060
2061 function_arglist_optional:
2062         function_arglist_backup
2063         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_backup DEFAULT
2064         {
2065                 $$ = scm_cons (loc_on_music (@4, $1), $3);
2066         }
2067         | function_arglist_skip_backup BACKUP
2068         ;
2069
2070 function_arglist_skip_backup:
2071         function_arglist_backup
2072         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_backup
2073         {
2074                 $$ = scm_cons (loc_on_music (@3, $1), $3);
2075         }
2076         ;
2077
2078 embedded_scm_closed:
2079         embedded_scm_bare
2080         | scm_function_call_closed
2081         ;
2082
2083 embedded_scm_arg_closed:
2084         embedded_scm_bare_arg
2085         | scm_function_call_closed
2086         | closed_music
2087         ;
2088
2089 scm_function_call_closed:
2090         SCM_FUNCTION function_arglist_closed {
2091                 $$ = MAKE_SYNTAX ("music-function", @$,
2092                                          $1, $2);
2093         }
2094         ;
2095
2096 music_function_call:
2097         MUSIC_FUNCTION function_arglist {
2098                 $$ = MAKE_SYNTAX ("music-function", @$,
2099                                          $1, $2);
2100         }
2101         ;
2102
2103
2104 optional_id:
2105         /**/ { $$ = SCM_EOL; }
2106         | '=' simple_string {
2107                 $$ = $2;
2108         }
2109         ;
2110
2111 complex_music:
2112         music_function_call
2113         | repeated_music                { $$ = $1; }
2114         | re_rhythmed_music     { $$ = $1; }
2115         | complex_music_prefix music
2116         {
2117                 $$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
2118         }
2119         ;
2120
2121 complex_music_prefix:
2122         CONTEXT symbol optional_id optional_context_mod {
2123                 Context_mod *ctxmod = unsmob_context_mod ($4);
2124                 SCM mods = SCM_EOL;
2125                 if (ctxmod)
2126                         mods = ctxmod->get_mods ();
2127                 $$ = START_MAKE_SYNTAX ("context-specification", $2, $3, mods, SCM_BOOL_F);
2128         }
2129         | NEWCONTEXT symbol optional_id optional_context_mod {
2130                 Context_mod *ctxmod = unsmob_context_mod ($4);
2131                 SCM mods = SCM_EOL;
2132                 if (ctxmod)
2133                         mods = ctxmod->get_mods ();
2134                 $$ = START_MAKE_SYNTAX ("context-specification", $2, $3, mods, SCM_BOOL_T);
2135         }
2136         ;
2137
2138 mode_changed_music:
2139         mode_changing_head grouped_music_list {
2140                 if ($1 == ly_symbol2scm ("chords"))
2141                 {
2142                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $2);
2143                 }
2144                 else
2145                 {
2146                   $$ = $2;
2147                 }
2148                 parser->lexer_->pop_state ();
2149         }
2150         | mode_changing_head_with_context optional_context_mod grouped_music_list {
2151                 Context_mod *ctxmod = unsmob_context_mod ($2);
2152                 SCM mods = SCM_EOL;
2153                 if (ctxmod)
2154                         mods = ctxmod->get_mods ();
2155                 $$ = MAKE_SYNTAX ("context-specification", @$, $1, SCM_EOL, mods, SCM_BOOL_T, $3);
2156                 if ($1 == ly_symbol2scm ("ChordNames"))
2157                 {
2158                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $$);
2159                 }
2160                 parser->lexer_->pop_state ();
2161         }
2162         ;
2163
2164 mode_changing_head:
2165         NOTEMODE {
2166                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
2167                 parser->lexer_->push_note_state (nn);
2168
2169                 $$ = ly_symbol2scm ("notes");
2170         }
2171         | DRUMMODE
2172                 {
2173                 SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
2174                 parser->lexer_->push_note_state (nn);
2175
2176                 $$ = ly_symbol2scm ("drums");
2177         }
2178         | FIGUREMODE {
2179                 parser->lexer_->push_figuredbass_state ();
2180
2181                 $$ = ly_symbol2scm ("figures");
2182         }
2183         | CHORDMODE {
2184                 SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
2185                 parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
2186                 nn = parser->lexer_->lookup_identifier ("pitchnames");
2187                 parser->lexer_->push_chord_state (nn);
2188                 $$ = ly_symbol2scm ("chords");
2189
2190         }
2191         | LYRICMODE
2192                 { parser->lexer_->push_lyric_state ();
2193                 $$ = ly_symbol2scm ("lyrics");
2194         }
2195         ;
2196
2197 mode_changing_head_with_context:
2198         DRUMS {
2199                 SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
2200                 parser->lexer_->push_note_state (nn);
2201
2202                 $$ = ly_symbol2scm ("DrumStaff");
2203         }
2204         | FIGURES {
2205                 parser->lexer_->push_figuredbass_state ();
2206
2207                 $$ = ly_symbol2scm ("FiguredBass");
2208         }
2209         | CHORDS {
2210                 SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
2211                 parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
2212                 nn = parser->lexer_->lookup_identifier ("pitchnames");
2213                 parser->lexer_->push_chord_state (nn);
2214                 $$ = ly_symbol2scm ("ChordNames");
2215         }
2216         | LYRICS
2217                 { parser->lexer_->push_lyric_state ();
2218                 $$ = ly_symbol2scm ("Lyrics");
2219         }
2220         ;
2221
2222 new_lyrics:
2223         ADDLYRICS { parser->lexer_->push_lyric_state (); }
2224         /*cont */
2225         composite_music {
2226         /* Can also use music at the expensive of two S/Rs similar to
2227            \repeat \alternative */
2228                 parser->lexer_->pop_state ();
2229
2230                 $$ = scm_cons ($3, SCM_EOL);
2231         }
2232         | new_lyrics ADDLYRICS {
2233                 parser->lexer_->push_lyric_state ();
2234         } composite_music {
2235                 parser->lexer_->pop_state ();
2236                 $$ = scm_cons ($4, $1);
2237         }
2238         ;
2239
2240 re_rhythmed_music:
2241         composite_music new_lyrics {
2242                 $$ = MAKE_SYNTAX ("add-lyrics", @$, $1, scm_reverse_x ($2, SCM_EOL));
2243         } %prec COMPOSITE
2244         | LYRICSTO simple_string {
2245                 parser->lexer_->push_lyric_state ();
2246         } music {
2247                 parser->lexer_->pop_state ();
2248                 $$ = MAKE_SYNTAX ("lyric-combine", @$, $2, $4);
2249         }
2250         ;
2251
2252 context_change:
2253         CHANGE symbol '=' simple_string  {
2254                 $$ = MAKE_SYNTAX ("context-change", @$, $2, $4);
2255         }
2256         ;
2257
2258
2259 property_path:
2260         symbol_list_rev  {
2261                 $$ = scm_reverse_x ($1, SCM_EOL);
2262         }
2263         | symbol_list_rev property_path {
2264                 $$ = scm_reverse_x ($1, $2);
2265         }
2266         ;
2267
2268 property_operation:
2269         symbol '=' scalar {
2270                 $$ = scm_list_3 (ly_symbol2scm ("assign"), $1, $3);
2271         }
2272         | UNSET symbol {
2273                 $$ = scm_list_2 (ly_symbol2scm ("unset"), $2);
2274         }
2275         | OVERRIDE property_path '=' scalar {
2276                 if (scm_ilength ($2) < 2) {
2277                         parser->parser_error (@2, _("bad grob property path"));
2278                         $$ = SCM_UNDEFINED;
2279                 } else {
2280                         $$ = scm_cons (ly_symbol2scm ("push"),
2281                                        scm_cons2 (scm_car ($2),
2282                                                   $4,
2283                                                   scm_cdr ($2)));
2284                 }
2285         }
2286         | REVERT revert_arg {
2287                 $$ = scm_cons (ly_symbol2scm ("pop"), $2);
2288         }
2289         ;
2290
2291 // This is all quite awkward for the sake of substantial backward
2292 // compatibility while at the same time allowing a more "natural" form
2293 // of specification not separating grob specification from grob
2294 // property path.  The purpose of this definition of revert_arg is to
2295 // allow the symbol list which specifies grob and property to revert
2296 // to be optionally be split into two parts after the grob (which in
2297 // this case is just the first element of the list).  symbol_list_part
2298 // is only one path component, but it can be parsed without lookahead,
2299 // so we can follow it with a synthetic BACKUP token when needed.  If
2300 // the first symbol_list_part already contains multiple elements (only
2301 // possible if a Scheme expression provides them), we just parse for
2302 // additional elements introduced by '.', which is what the
2303 // SYMBOL_LIST backup in connection with the immediately following
2304 // rule using symbol_list_arg does.
2305 //
2306 // As long as we don't have our coffers filled with both grob and at
2307 // least one grob property specification, the rest of the required
2308 // symbol list chain may be provided either with or without a leading
2309 // dot.  This is for both allowing the traditional
2310 // \revert Accidental #'color
2311 // as well as well as the "naive" form
2312 // \revert Accidental.color
2313
2314 revert_arg:
2315         revert_arg_backup BACKUP symbol_list_arg
2316         {
2317                 $$ = $3;
2318         }
2319         ;
2320
2321 revert_arg_backup:
2322         revert_arg_part
2323         {
2324                 if (scm_is_null ($1)
2325                     || scm_is_null (scm_cdr ($1)))
2326                         MYBACKUP (SCM_ARG, $1, @1);
2327                 else
2328                         MYBACKUP (SYMBOL_LIST, scm_reverse_x ($1, SCM_EOL), @1);
2329         }
2330         ;
2331
2332 // revert_arg_part delivers results in reverse
2333 revert_arg_part:
2334         symbol_list_part
2335         | revert_arg_backup BACKUP SCM_ARG '.' symbol_list_part
2336         {
2337                 $$ = scm_append_x (scm_list_2 ($5, $3));
2338         }
2339         | revert_arg_backup BACKUP SCM_ARG symbol_list_part
2340         {
2341                 $$ = scm_append_x (scm_list_2 ($4, $3));
2342         }               
2343         ;
2344
2345 context_def_mod:
2346         CONSISTS { $$ = ly_symbol2scm ("consists"); }
2347         | REMOVE { $$ = ly_symbol2scm ("remove"); }
2348
2349         | ACCEPTS { $$ = ly_symbol2scm ("accepts"); }
2350         | DEFAULTCHILD { $$ = ly_symbol2scm ("default-child"); }
2351         | DENIES { $$ = ly_symbol2scm ("denies"); }
2352
2353         | ALIAS { $$ = ly_symbol2scm ("alias"); }
2354         | TYPE { $$ = ly_symbol2scm ("translator-type"); }
2355         | DESCRIPTION { $$ = ly_symbol2scm ("description"); }
2356         | NAME { $$ = ly_symbol2scm ("context-name"); }
2357         ;
2358
2359 context_mod:
2360         property_operation { $$ = $1; }
2361         | context_def_mod STRING {
2362                 $$ = scm_list_2 ($1, $2);
2363         }
2364         | context_def_mod embedded_scm
2365         {
2366                 if (!scm_is_string ($2)
2367                     && ly_symbol2scm ("consists") != $1
2368                     && ly_symbol2scm ("remove") != $1)
2369                 {
2370                         $$ = SCM_EOL;
2371                         parser->parser_error (@1, _ ("only \\consists and \\remove take non-string argument."));
2372                 }
2373                 else
2374                 {
2375                         $$ = scm_list_2 ($1, $2);
2376                 }
2377         }
2378         ;
2379
2380 // If defined, at least two members.
2381 grob_prop_spec:
2382         symbol_list_rev
2383         {
2384                 SCM l = scm_reverse_x ($1, SCM_EOL);
2385                 if (scm_is_pair (l)
2386                     && to_boolean
2387                     (scm_object_property (scm_car (l),
2388                                           ly_symbol2scm ("is-grob?"))))
2389                         l = scm_cons (ly_symbol2scm ("Bottom"), l);
2390                 if (scm_is_null (l) || scm_is_null (scm_cdr (l))) {
2391                         parser->parser_error (@1, _ ("bad grob property path"));
2392                         l = SCM_UNDEFINED;
2393                 }
2394                 $$ = l;
2395         }
2396         ;
2397
2398 // If defined, at least three members
2399 grob_prop_path:
2400         grob_prop_spec
2401         {
2402                 if (!SCM_UNBNDP ($1) && scm_is_null (scm_cddr ($1)))
2403                 {
2404                         parser->parser_error (@1, _ ("bad grob property path"));
2405                         $$ = SCM_UNDEFINED;
2406                 }
2407         }
2408         | grob_prop_spec property_path
2409         {
2410                 if (!SCM_UNBNDP ($1)) {
2411                         $$ = scm_append_x (scm_list_2 ($1, $2));
2412                         if (scm_is_null (scm_cddr ($$))) {
2413                                 parser->parser_error (@$, _ ("bad grob property path"));
2414                                 $$ = SCM_UNDEFINED;
2415                         }
2416                 }
2417
2418         }
2419         ;
2420
2421 // Exactly two elements or undefined
2422 context_prop_spec:
2423         symbol_list_rev
2424         {
2425                 SCM l = scm_reverse_x ($1, SCM_EOL);
2426                 switch (scm_ilength (l)) {
2427                 case 1:
2428                         l = scm_cons (ly_symbol2scm ("Bottom"), l);
2429                 case 2:
2430                         break;
2431                 default:
2432                         parser->parser_error (@1, _ ("bad context property path"));
2433                         l = SCM_UNDEFINED;
2434                 }
2435                 $$ = l;
2436         }
2437         ;
2438
2439 simple_music_property_def:
2440         OVERRIDE grob_prop_path '=' scalar {
2441                 if (SCM_UNBNDP ($2))
2442                         $$ = SCM_UNDEFINED;
2443                 else {
2444                         $$ = scm_list_5 (scm_car ($2),
2445                                          ly_symbol2scm ("OverrideProperty"),
2446                                          scm_cadr ($2),
2447                                          $4,
2448                                          scm_cddr ($2));
2449                 }
2450         }
2451         | REVERT simple_revert_context revert_arg {
2452                 $$ = scm_list_4 ($2,
2453                                  ly_symbol2scm ("RevertProperty"),
2454                                  scm_car ($3),
2455                                  scm_cdr ($3));
2456         }
2457         | SET context_prop_spec '=' scalar {
2458                 if (SCM_UNBNDP ($2))
2459                         $$ = SCM_UNDEFINED;
2460                 else
2461                         $$ = scm_list_4 (scm_car ($2),
2462                                          ly_symbol2scm ("PropertySet"),
2463                                          scm_cadr ($2),
2464                                          $4);
2465         }
2466         | UNSET context_prop_spec {
2467                 if (SCM_UNBNDP ($2))
2468                         $$ = SCM_UNDEFINED;
2469                 else
2470                         $$ = scm_list_3 (scm_car ($2),
2471                                          ly_symbol2scm ("PropertyUnset"),
2472                                          scm_cadr ($2));
2473         }
2474         ;
2475
2476
2477 // This is all quite awkward for the sake of substantial backward
2478 // compatibility while at the same time allowing a more "natural" form
2479 // of specification not separating grob specification from grob
2480 // property path.  The purpose of this definition of
2481 // simple_revert_context is to allow the symbol list which specifies
2482 // grob and property to revert to be optionally be split into two
2483 // parts after the grob (which may be preceded by a context
2484 // specification, a case which we distinguish by checking whether the
2485 // first symbol is a valid grob symbol instead).
2486 //
2487 // See revert_arg above for the main work horse of this arrangement.
2488 // simple_revert_context just caters for the context and delegates the
2489 // rest of the job to revert_arg.
2490
2491 simple_revert_context:
2492         symbol_list_part
2493         {
2494                 $1 = scm_reverse_x ($1, SCM_EOL);
2495                 if (scm_is_null ($1)
2496                     || to_boolean
2497                     (scm_object_property (scm_car ($1),
2498                                           ly_symbol2scm ("is-grob?")))) {
2499                         $$ = ly_symbol2scm ("Bottom");
2500                         parser->lexer_->push_extra_token (SCM_IDENTIFIER, $1);
2501                 } else {
2502                         $$ = scm_car ($1);
2503                         parser->lexer_->push_extra_token (SCM_IDENTIFIER,
2504                                                           scm_cdr ($1));
2505                 }
2506         }
2507         ;
2508
2509 music_property_def:
2510         simple_music_property_def {
2511                 if (SCM_UNBNDP ($1))
2512                         $$ = MAKE_SYNTAX ("void-music", @1);
2513                 else
2514                         $$ = LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("property-operation"), scm_cons2 (parser->self_scm (), make_input (@$), $1));
2515         }
2516         ;
2517
2518 string:
2519         STRING {
2520                 $$ = $1;
2521         }
2522         | full_markup
2523         ;
2524
2525 simple_string: STRING {
2526                 $$ = $1;
2527         }
2528         | embedded_scm_bare
2529         {
2530                 if (scm_is_string ($1)) {
2531                         $$ = $1;
2532                 } else {
2533                         parser->parser_error (@1, (_ ("simple string expected")));
2534                         $$ = scm_string (SCM_EOL);
2535                 }
2536         }
2537         ;
2538
2539 symbol:
2540         STRING {
2541                 $$ = scm_string_to_symbol ($1);
2542         }
2543         | embedded_scm_bare
2544         {
2545                 // This is a bit of overkill but makes the same
2546                 // routine responsible for all symbol interpretations.
2547                 $$ = try_string_variants (ly_lily_module_constant ("symbol?"),
2548                                           $1);
2549                 if (SCM_UNBNDP ($$))
2550                 {
2551                         parser->parser_error (@1, (_ ("symbol expected")));
2552                         // Generate a unique symbol in case it is used
2553                         // for an assignment or similar
2554                         $$ = scm_make_symbol (ly_string2scm ("undefined"));
2555                 }
2556         }
2557         ;
2558
2559 scalar:
2560         embedded_scm_arg
2561         | SCM_IDENTIFIER
2562         | bare_number
2563         // The following is a rather defensive variant of admitting
2564         // negative numbers: the grammar would permit number_factor or
2565         // even number_expression.  However, function arguments allow
2566         // only this simple kind of negative number, so to have things
2567         // like \tweak and \override behave reasonably similar, it
2568         // makes sense to rule out things like -- which are rather an
2569         // accent in function argument contexts.
2570         | '-' bare_number
2571         {
2572                 $$ = scm_difference ($2, SCM_UNDEFINED);
2573         }
2574         | STRING
2575         | full_markup
2576         ;
2577
2578 event_chord:
2579         simple_element post_events {
2580                 // Let the rhythmic music iterator sort this mess out.
2581                 if (scm_is_pair ($2)) {
2582                         $$ = make_music_from_simple (parser, @1, $1);
2583                         if (unsmob_music ($$))
2584                                 unsmob_music ($$)->set_property ("articulations",
2585                                                                  scm_reverse_x ($2, SCM_EOL));
2586                         else
2587                         {
2588                                 parser->parser_error (@1, _("music expected"));
2589                                 $$ = MAKE_SYNTAX ("void-music", @1);
2590                         }
2591                 }
2592         } %prec ':'
2593         | simple_chord_elements post_events     {
2594                 if (scm_is_pair ($2)) {
2595                         if (unsmob_pitch ($1))
2596                                 $1 = make_chord_elements (@1,
2597                                                           $1,
2598                                                           parser->default_duration_.smobbed_copy (),
2599                                                           SCM_EOL);
2600
2601                         SCM elts = ly_append2 ($1, scm_reverse_x ($2, SCM_EOL));
2602
2603                         $$ = MAKE_SYNTAX ("event-chord", @1, elts);
2604                 } else if (!unsmob_pitch ($1))
2605                         $$ = MAKE_SYNTAX ("event-chord", @1, $1);
2606                 // A mere pitch drops through.
2607         } %prec ':'
2608         | CHORD_REPETITION optional_notemode_duration post_events {
2609                 Input i;
2610                 i.set_location (@1, @3);
2611                 $$ = MAKE_SYNTAX ("repetition-chord", i,
2612                                   $2, scm_reverse_x ($3, SCM_EOL));
2613         } %prec ':'
2614         | MULTI_MEASURE_REST optional_notemode_duration post_events {
2615                 Input i;
2616                 i.set_location (@1, @3);
2617                 $$ = MAKE_SYNTAX ("multi-measure-rest", i, $2,
2618                                   scm_reverse_x ($3, SCM_EOL));
2619         } %prec ':'
2620         | command_element
2621         | note_chord_element
2622         ;
2623
2624
2625 note_chord_element:
2626         chord_body optional_notemode_duration post_events
2627         {
2628                 Music *m = unsmob_music ($1);
2629                 SCM dur = unsmob_duration ($2)->smobbed_copy ();
2630                 SCM es = m->get_property ("elements");
2631                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
2632
2633                 for (SCM s = es; scm_is_pair (s); s = scm_cdr (s))
2634                   unsmob_music (scm_car (s))->set_property ("duration", dur);
2635                 es = ly_append2 (es, postevs);
2636
2637                 m-> set_property ("elements", es);
2638                 m->set_spot (@$);
2639                 $$ = m->self_scm ();
2640         } %prec ':'
2641         ;
2642
2643 chord_body:
2644         ANGLE_OPEN chord_body_elements ANGLE_CLOSE
2645         {
2646                 $$ = MAKE_SYNTAX ("event-chord", @$, scm_reverse_x ($2, SCM_EOL));
2647         }
2648         | CHORD_BODY_IDENTIFIER
2649         ;
2650
2651 chord_body_elements:
2652         /* empty */             { $$ = SCM_EOL; }
2653         | chord_body_elements chord_body_element {
2654                 if (!SCM_UNBNDP ($2))
2655                         $$ = scm_cons ($2, $1);
2656         }
2657         ;
2658
2659 chord_body_element:
2660         pitch exclamations questions octave_check post_events
2661         {
2662                 bool q = to_boolean ($3);
2663                 bool ex = to_boolean ($2);
2664                 SCM check = $4;
2665                 SCM post = $5;
2666
2667                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2668                 n->set_property ("pitch", $1);
2669                 if (q)
2670                         n->set_property ("cautionary", SCM_BOOL_T);
2671                 if (ex || q)
2672                         n->set_property ("force-accidental", SCM_BOOL_T);
2673
2674                 if (scm_is_pair (post)) {
2675                         SCM arts = scm_reverse_x (post, SCM_EOL);
2676                         n->set_property ("articulations", arts);
2677                 }
2678                 if (scm_is_number (check))
2679                 {
2680                         int q = scm_to_int (check);
2681                         n->set_property ("absolute-octave", scm_from_int (q-1));
2682                 }
2683
2684                 $$ = n->unprotect ();
2685         }
2686         | DRUM_PITCH post_events {
2687                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2688                 n->set_property ("drum-type", $1);
2689
2690                 if (scm_is_pair ($2)) {
2691                         SCM arts = scm_reverse_x ($2, SCM_EOL);
2692                         n->set_property ("articulations", arts);
2693                 }
2694                 $$ = n->unprotect ();
2695         }
2696         | music_function_chord_body
2697         {
2698                 Music *m = unsmob_music ($1);
2699
2700                 while (m && m->is_mus_type ("music-wrapper-music")) {
2701                         $$ = m->get_property ("element");
2702                         m = unsmob_music ($$);
2703                 }
2704
2705                 if (!(m && m->is_mus_type ("rhythmic-event"))) {
2706                         parser->parser_error (@$, _ ("not a rhythmic event"));
2707                         $$ = SCM_UNDEFINED;
2708                 }
2709         }
2710         ;
2711
2712 music_function_chord_body:
2713         music_function_call
2714         | MUSIC_IDENTIFIER
2715         ;
2716
2717 // Event functions may only take closed arglists, otherwise it would
2718 // not be clear whether a following postevent should be associated
2719 // with the last argument of the event function or with the expression
2720 // for which the function call acts itself as event.
2721
2722 music_function_call_closed:
2723         MUSIC_FUNCTION function_arglist_closed {
2724                 $$ = MAKE_SYNTAX ("music-function", @$,
2725                                          $1, $2);
2726         }
2727         ;
2728
2729 event_function_event:
2730         EVENT_FUNCTION function_arglist_closed {
2731                 $$ = MAKE_SYNTAX ("music-function", @$,
2732                                          $1, $2);
2733         }
2734         ;
2735
2736 command_element:
2737         command_event {
2738                 $$ = $1;
2739         }
2740         ;
2741
2742 command_event:
2743         tempo_event {
2744                 $$ = $1;
2745         }
2746         ;
2747
2748
2749 post_events:
2750         /* empty */ {
2751                 $$ = SCM_EOL;
2752         }
2753         | post_events post_event {
2754                 $$ = $1;
2755                 if (Music *m = unsmob_music ($2))
2756                 {
2757                         if (m->is_mus_type ("post-event-wrapper"))
2758                         {
2759                                 for (SCM p = m->get_property ("elements");
2760                                      scm_is_pair (p);
2761                                      p = scm_cdr (p))
2762                                 {
2763                                         $$ = scm_cons (scm_car (p), $$);
2764                                 }
2765                         } else {
2766                                 m->set_spot (@2);
2767                                 $$ = scm_cons ($2, $$);
2768                         }
2769                 }
2770         }
2771         ;
2772
2773 post_event_nofinger:
2774         direction_less_event {
2775                 $$ = $1;
2776         }
2777         | script_dir music_function_call_closed {
2778                 $$ = $2;
2779                 if (!unsmob_music ($2)->is_mus_type ("post-event")) {
2780                         parser->parser_error (@2, _ ("post-event expected"));
2781                         $$ = SCM_UNSPECIFIED;
2782                 } else if (!SCM_UNBNDP ($1))
2783                 {
2784                         unsmob_music ($$)->set_property ("direction", $1);
2785                 }
2786         }
2787         | HYPHEN {
2788                 if (!parser->lexer_->is_lyric_state ())
2789                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2790                 $$ = MY_MAKE_MUSIC ("HyphenEvent", @$)->unprotect ();
2791         }
2792         | EXTENDER {
2793                 if (!parser->lexer_->is_lyric_state ())
2794                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2795                 $$ = MY_MAKE_MUSIC ("ExtenderEvent", @$)->unprotect ();
2796         }
2797         | script_dir direction_reqd_event {
2798                 if (!SCM_UNBNDP ($1))
2799                 {
2800                         Music *m = unsmob_music ($2);
2801                         m->set_property ("direction", $1);
2802                 }
2803                 $$ = $2;
2804         }
2805         | script_dir direction_less_event {
2806                 if (!SCM_UNBNDP ($1))
2807                 {
2808                         Music *m = unsmob_music ($2);
2809                         m->set_property ("direction", $1);
2810                 }
2811                 $$ = $2;
2812         }
2813         | '^' fingering
2814         {
2815                 $$ = $2;
2816                 unsmob_music ($$)->set_property ("direction", scm_from_int (UP));
2817         }
2818         | '_' fingering
2819         {
2820                 $$ = $2;
2821                 unsmob_music ($$)->set_property ("direction", scm_from_int (DOWN));
2822         }                       
2823         ;
2824
2825 post_event:
2826         post_event_nofinger
2827         | '-' fingering {
2828                 $$ = $2;
2829         }
2830         ;
2831
2832 string_number_event:
2833         E_UNSIGNED {
2834                 Music *s = MY_MAKE_MUSIC ("StringNumberEvent", @$);
2835                 s->set_property ("string-number", $1);
2836                 $$ = s->unprotect ();
2837         }
2838         ;
2839
2840 direction_less_event:
2841         string_number_event
2842         | EVENT_IDENTIFIER      {
2843                 $$ = $1;
2844         }
2845         | tremolo_type  {
2846                Music *a = MY_MAKE_MUSIC ("TremoloEvent", @$);
2847                a->set_property ("tremolo-type", $1);
2848                $$ = a->unprotect ();
2849         }
2850         | event_function_event  
2851         ;
2852
2853 direction_reqd_event:
2854         gen_text_def {
2855                 $$ = $1;
2856         }
2857         | script_abbreviation {
2858                 SCM s = parser->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
2859                 if (scm_is_string (s)) {
2860                         Music *a = MY_MAKE_MUSIC ("ArticulationEvent", @$);
2861                         a->set_property ("articulation-type", s);
2862                         $$ = a->unprotect ();
2863                 } else {
2864                         Music *original = unsmob_music (s);
2865                         if (original && original->is_mus_type ("post-event")) {
2866                                 Music *a = original->clone ();
2867                                 a->set_spot (parser->lexer_->override_input (@$));
2868                                 $$ = a->unprotect ();
2869                         } else {
2870                                 parser->parser_error (@1, _ ("expecting string or post-event as script definition"));
2871                                 $$ = MY_MAKE_MUSIC ("PostEvents", @$)->unprotect ();
2872                         }
2873                 }
2874         }
2875         ;
2876
2877 octave_check:
2878         /**/ { $$ = SCM_EOL; }
2879         | '=' quotes { $$ = $2; }
2880         ;
2881
2882 quotes:
2883         /* empty */
2884         {
2885                 $$ = SCM_INUM0;
2886         }
2887         | sub_quotes
2888         | sup_quotes
2889         ;
2890
2891 sup_quotes:
2892         '\'' {
2893                 $$ = scm_from_int (1);
2894         }
2895         | sup_quotes '\'' {
2896                 $$ = scm_oneplus ($1);
2897         }
2898         ;
2899
2900 sub_quotes:
2901         ',' {
2902                 $$ = scm_from_int (-1);
2903         }
2904         | sub_quotes ',' {
2905                 $$ = scm_oneminus ($1);
2906         }
2907         ;
2908
2909 steno_pitch:
2910         NOTENAME_PITCH quotes {
2911                 if (!scm_is_eq (SCM_INUM0, $2))
2912                 {
2913                         Pitch p = *unsmob_pitch ($1);
2914                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2915                         $$ = p.smobbed_copy ();
2916                 }
2917         }
2918         ;
2919
2920 /*
2921 ugh. duplication
2922 */
2923
2924 steno_tonic_pitch:
2925         TONICNAME_PITCH quotes {
2926                 if (!scm_is_eq (SCM_INUM0, $2))
2927                 {
2928                         Pitch p = *unsmob_pitch ($1);
2929                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2930                         $$ = p.smobbed_copy ();
2931                 }
2932         }
2933         ;
2934
2935 pitch:
2936         steno_pitch
2937         | PITCH_IDENTIFIER quotes {
2938                 if (!scm_is_eq (SCM_INUM0, $2))
2939                 {
2940                         Pitch p = *unsmob_pitch ($1);
2941                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2942                         $$ = p.smobbed_copy ();
2943                 }
2944         }
2945         ;
2946
2947 pitch_arg:
2948         PITCH_ARG quotes {
2949                 if (!scm_is_eq (SCM_INUM0, $2))
2950                 {
2951                         Pitch p = *unsmob_pitch ($1);
2952                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2953                         $$ = p.smobbed_copy ();
2954                 }
2955         }
2956         ;
2957
2958 gen_text_def:
2959         full_markup {
2960                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2961                 t->set_property ("text", $1);
2962                 $$ = t->unprotect ();
2963         }
2964         | STRING {
2965                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2966                 t->set_property ("text",
2967                         make_simple_markup ($1));
2968                 $$ = t->unprotect ();
2969         }
2970         | embedded_scm_closed
2971         {
2972                 Music *m = unsmob_music ($1);
2973                 if (m && m->is_mus_type ("post-event"))
2974                         $$ = $1;
2975                 else if (Text_interface::is_markup ($1)) {
2976                         Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2977                         t->set_property ("text", $1);
2978                         $$ = t->unprotect ();
2979                 } else
2980                         parser->parser_error (@1, _ ("not an articulation"));
2981         }
2982         ;
2983
2984 fingering:
2985         UNSIGNED {
2986                 Music *t = MY_MAKE_MUSIC ("FingeringEvent", @$);
2987                 t->set_property ("digit", $1);
2988                 $$ = t->unprotect ();
2989         }
2990         ;
2991
2992 script_abbreviation:
2993         '^'             {
2994                 $$ = scm_from_locale_string ("Hat");
2995         }
2996         | '+'           {
2997                 $$ = scm_from_locale_string ("Plus");
2998         }
2999         | '-'           {
3000                 $$ = scm_from_locale_string ("Dash");
3001         }
3002         | '!'           {
3003                 $$ = scm_from_locale_string ("Bang");
3004         }
3005         | ANGLE_CLOSE   {
3006                 $$ = scm_from_locale_string ("Larger");
3007         }
3008         | '.'           {
3009                 $$ = scm_from_locale_string ("Dot");
3010         }
3011         | '_' {
3012                 $$ = scm_from_locale_string ("Underscore");
3013         }
3014         ;
3015
3016 script_dir:
3017         '_'     { $$ = scm_from_int (DOWN); }
3018         | '^'   { $$ = scm_from_int (UP); }
3019         | '-'   { $$ = SCM_UNDEFINED; }
3020         ;
3021
3022 duration_length:
3023         multiplied_duration {
3024                 $$ = $1;
3025         }
3026         ;
3027
3028 maybe_notemode_duration:
3029         {
3030                 $$ = SCM_UNDEFINED;
3031         } %prec ':'
3032         | multiplied_duration   {
3033                 $$ = $1;
3034                 parser->default_duration_ = *unsmob_duration ($$);
3035         }
3036 ;
3037
3038
3039 optional_notemode_duration:
3040         maybe_notemode_duration
3041         {
3042                 if (SCM_UNBNDP ($$))
3043                         $$ = parser->default_duration_.smobbed_copy ();
3044         }
3045         ;
3046
3047 steno_duration:
3048         UNSIGNED dots           {
3049                 $$ = make_duration ($1, scm_to_int ($2));
3050                 if (SCM_UNBNDP ($$))
3051                 {
3052                         parser->parser_error (@1, _ ("not a duration"));
3053                         $$ = Duration ().smobbed_copy ();
3054                 }
3055         }
3056         | DURATION_IDENTIFIER dots      {
3057                 Duration *d = unsmob_duration ($1);
3058                 Duration k (d->duration_log (),
3059                             d->dot_count () + scm_to_int ($2));
3060                 k = k.compressed (d->factor ());
3061                 scm_remember_upto_here_1 ($1);
3062                 $$ = k.smobbed_copy ();
3063         }
3064         ;
3065
3066 multiplied_duration:
3067         steno_duration {
3068                 $$ = $1;
3069         }
3070         | multiplied_duration '*' UNSIGNED {
3071                 $$ = unsmob_duration ($$)->compressed (scm_to_int ($3)).smobbed_copy ();
3072         }
3073         | multiplied_duration '*' FRACTION {
3074                 Rational  m (scm_to_int (scm_car ($3)), scm_to_int (scm_cdr ($3)));
3075
3076                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
3077         }
3078         ;
3079
3080 dots:
3081         /* empty */     {
3082                 $$ = SCM_INUM0;
3083         }
3084         | dots '.' {
3085                 $$ = scm_oneplus ($1);
3086         }
3087         ;
3088
3089 tremolo_type:
3090         ':'     {
3091                 $$ = SCM_INUM0;
3092         }
3093         | ':' UNSIGNED {
3094                 if (SCM_UNBNDP (make_duration ($2)))
3095                         parser->parser_error (@2, _ ("not a duration"));
3096                 $$ = $2;
3097         }
3098         ;
3099
3100 bass_number:
3101         UNSIGNED { $$ = $1; }
3102         | STRING { $$ = $1; }
3103         | full_markup { $$ = $1; }
3104         | embedded_scm_bare
3105         {
3106                 // as an integer, it needs to be non-negative, and otherwise
3107                 // it needs to be suitable as a markup.
3108                 if (scm_is_integer ($1)
3109                     ? scm_is_true (scm_negative_p ($1))
3110                     : !Text_interface::is_markup ($1))
3111                 {
3112                         parser->parser_error (@1, _ ("bass number expected"));
3113                         $$ = SCM_INUM0;
3114                 }
3115         }
3116         ;
3117
3118 figured_bass_alteration:
3119         '-'     { $$ = ly_rational2scm (FLAT_ALTERATION); }
3120         | '+'   { $$ = ly_rational2scm (SHARP_ALTERATION); }
3121         | '!'   { $$ = scm_from_int (0); }
3122         ;
3123
3124 bass_figure:
3125         FIGURE_SPACE {
3126                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
3127                 $$ = bfr->unprotect ();
3128         }
3129         | bass_number  {
3130                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
3131                 $$ = bfr->self_scm ();
3132
3133                 if (scm_is_number ($1))
3134                         bfr->set_property ("figure", $1);
3135                 else if (Text_interface::is_markup ($1))
3136                         bfr->set_property ("text", $1);
3137
3138                 bfr->unprotect ();
3139         }
3140         | bass_figure ']' {
3141                 $$ = $1;
3142                 unsmob_music ($1)->set_property ("bracket-stop", SCM_BOOL_T);
3143         }
3144         | bass_figure figured_bass_alteration {
3145                 Music *m = unsmob_music ($1);
3146                 if (scm_to_double ($2)) {
3147                         SCM salter = m->get_property ("alteration");
3148                         SCM alter = scm_is_number (salter) ? salter : scm_from_int (0);
3149                         m->set_property ("alteration",
3150                                          scm_sum (alter, $2));
3151                 } else {
3152                         m->set_property ("alteration", scm_from_int (0));
3153                 }
3154         }
3155         | bass_figure figured_bass_modification  {
3156                 Music *m = unsmob_music ($1);
3157                 m->set_property ($2, SCM_BOOL_T);
3158         }
3159         ;
3160
3161
3162 figured_bass_modification:
3163         E_PLUS          {
3164                 $$ = ly_symbol2scm ("augmented");
3165         }
3166         | E_EXCLAMATION {
3167                 $$ = ly_symbol2scm ("no-continuation");
3168         }
3169         | '/'           {
3170                 $$ = ly_symbol2scm ("diminished");
3171         }
3172         | E_BACKSLASH {
3173                 $$ = ly_symbol2scm ("augmented-slash");
3174         }
3175         ;
3176
3177 br_bass_figure:
3178         bass_figure {
3179                 $$ = $1;
3180         }
3181         | '[' bass_figure {
3182                 $$ = $2;
3183                 unsmob_music ($$)->set_property ("bracket-start", SCM_BOOL_T);
3184         }
3185         ;
3186
3187 figure_list:
3188         /**/            {
3189                 $$ = SCM_EOL;
3190         }
3191         | figure_list br_bass_figure {
3192                 $$ = scm_cons ($2, $1);
3193         }
3194         ;
3195
3196 figure_spec:
3197         FIGURE_OPEN figure_list FIGURE_CLOSE {
3198                 $$ = scm_reverse_x ($2, SCM_EOL);
3199         }
3200         ;
3201
3202
3203 optional_rest:
3204         /**/   { $$ = SCM_BOOL_F; }
3205         | REST { $$ = SCM_BOOL_T; }
3206         ;
3207
3208 simple_element:
3209         pitch exclamations questions octave_check maybe_notemode_duration optional_rest {
3210                 if (!parser->lexer_->is_note_state ())
3211                         parser->parser_error (@1, _ ("have to be in Note mode for notes"));
3212                 if (!SCM_UNBNDP ($2)
3213                     || !SCM_UNBNDP ($3)
3214                     || scm_is_number ($4)
3215                     || !SCM_UNBNDP ($5)
3216                     || scm_is_true ($6))
3217                 {
3218                         Music *n = 0;
3219                         if (scm_is_true ($6))
3220                                 n = MY_MAKE_MUSIC ("RestEvent", @$);
3221                         else
3222                                 n = MY_MAKE_MUSIC ("NoteEvent", @$);
3223                         
3224                         n->set_property ("pitch", $1);
3225                         if (SCM_UNBNDP ($5))
3226                                 n->set_property ("duration",
3227                                                  parser->default_duration_.smobbed_copy ());
3228                         else
3229                                 n->set_property ("duration", $5);
3230                         
3231                         if (scm_is_number ($4))
3232                         {
3233                                 int q = scm_to_int ($4);
3234                                 n->set_property ("absolute-octave", scm_from_int (q-1));
3235                         }
3236                         
3237                         if (to_boolean ($3))
3238                                 n->set_property ("cautionary", SCM_BOOL_T);
3239                         if (to_boolean ($2) || to_boolean ($3))
3240                                 n->set_property ("force-accidental", SCM_BOOL_T);
3241                         
3242                         $$ = n->unprotect ();
3243                 }
3244         }
3245         | DRUM_PITCH optional_notemode_duration {
3246                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
3247                 n->set_property ("duration", $2);
3248                 n->set_property ("drum-type", $1);
3249
3250                 $$ = n->unprotect ();
3251         }
3252         | RESTNAME optional_notemode_duration           {
3253                 Music *ev = 0;
3254                 if (ly_scm2string ($1) == "s") {
3255                         /* Space */
3256                         ev = MY_MAKE_MUSIC ("SkipEvent", @$);
3257                   }
3258                 else {
3259                         ev = MY_MAKE_MUSIC ("RestEvent", @$);
3260
3261                     }
3262                 ev->set_property ("duration", $2);
3263                 $$ = ev->unprotect ();
3264         }
3265         ;
3266
3267 // Can return a single pitch rather than a list.
3268 simple_chord_elements:
3269         new_chord {
3270                 if (!parser->lexer_->is_chord_state ())
3271                         parser->parser_error (@1, _ ("have to be in Chord mode for chords"));
3272                 $$ = $1;
3273         }
3274         | figure_spec optional_notemode_duration {
3275                 for (SCM s = $1; scm_is_pair (s); s = scm_cdr (s))
3276                 {
3277                         unsmob_music (scm_car (s))->set_property ("duration", $2);
3278                 }
3279                 $$ = $1;
3280         }
3281         ;
3282
3283 lyric_element:
3284         full_markup {
3285                 if (!parser->lexer_->is_lyric_state ())
3286                         parser->parser_error (@1, _ ("markup outside of text script or \\lyricmode"));
3287                 $$ = $1;
3288         }
3289         | STRING {
3290                 if (!parser->lexer_->is_lyric_state ())
3291                         parser->parser_error (@1, _ ("unrecognized string, not in text script or \\lyricmode"));
3292                 $$ = $1;
3293         }
3294         | LYRIC_ELEMENT
3295         ;
3296
3297 lyric_element_music:
3298         lyric_element optional_notemode_duration post_events {
3299                 $$ = MAKE_SYNTAX ("lyric-event", @$, $1, $2);
3300                 if (scm_is_pair ($3))
3301                         unsmob_music ($$)->set_property
3302                                 ("articulations", scm_reverse_x ($3, SCM_EOL));
3303         } %prec ':'
3304         ;
3305
3306 // Can return a single pitch rather than a list.
3307 new_chord:
3308         steno_tonic_pitch maybe_notemode_duration   {
3309                 if (SCM_UNBNDP ($2))
3310                         $$ = $1;
3311                 else
3312                         $$ = make_chord_elements (@$, $1, $2, SCM_EOL);
3313         }
3314         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
3315                 SCM its = scm_reverse_x ($4, SCM_EOL);
3316                 $$ = make_chord_elements (@$, $1, $2, scm_cons ($3, its));
3317         } %prec ':'
3318         ;
3319
3320 chord_items:
3321         /**/ {
3322                 $$ = SCM_EOL;
3323         }
3324         | chord_items chord_item {
3325                 $$ = scm_cons ($2, $$);
3326         }
3327         ;
3328
3329 chord_separator:
3330         CHORD_COLON {
3331                 $$ = ly_symbol2scm ("chord-colon");
3332         }
3333         | CHORD_CARET {
3334                 $$ = ly_symbol2scm ("chord-caret");
3335         }
3336         | CHORD_SLASH steno_tonic_pitch {
3337                 $$ = scm_list_2 (ly_symbol2scm ("chord-slash"), $2);
3338         }
3339         | CHORD_BASS steno_tonic_pitch {
3340                 $$ = scm_list_2 (ly_symbol2scm ("chord-bass"), $2);
3341         }
3342         ;
3343
3344 chord_item:
3345         chord_separator {
3346                 $$ = $1;
3347         }
3348         | step_numbers {
3349                 $$ = scm_reverse_x ($1, SCM_EOL);
3350         }
3351         | CHORD_MODIFIER  {
3352                 $$ = $1;
3353         }
3354         ;
3355
3356 step_numbers:
3357         step_number { $$ = scm_cons ($1, SCM_EOL); }
3358         | step_numbers '.' step_number {
3359                 $$ = scm_cons ($3, $$);
3360         }
3361         ;
3362
3363 step_number:
3364         UNSIGNED {
3365                 $$ = make_chord_step ($1, 0);
3366         }
3367         | UNSIGNED '+' {
3368                 $$ = make_chord_step ($1, SHARP_ALTERATION);
3369         }
3370         | UNSIGNED CHORD_MINUS {
3371                 $$ = make_chord_step ($1, FLAT_ALTERATION);
3372         }
3373         ;
3374
3375 tempo_range:
3376         unsigned_number {
3377                 $$ = $1;
3378         } %prec ':'
3379         | unsigned_number '-' unsigned_number {
3380                 $$ = scm_cons ($1, $3);
3381         }
3382         ;
3383
3384 /*
3385         UTILITIES
3386
3387 TODO: should deprecate in favor of Scheme?
3388
3389  */
3390 number_expression:
3391         number_expression '+' number_term {
3392                 $$ = scm_sum ($1, $3);
3393         }
3394         | number_expression '-' number_term {
3395                 $$ = scm_difference ($1, $3);
3396         }
3397         | number_term
3398         ;
3399
3400 number_term:
3401         number_factor {
3402                 $$ = $1;
3403         }
3404         | number_factor '*' number_factor {
3405                 $$ = scm_product ($1, $3);
3406         }
3407         | number_factor '/' number_factor {
3408                 $$ = scm_divide ($1, $3);
3409         }
3410         ;
3411
3412 number_factor:
3413         '-'  number_factor { /* %prec UNARY_MINUS */
3414                 $$ = scm_difference ($2, SCM_UNDEFINED);
3415         }
3416         | bare_number
3417         ;
3418
3419 bare_number_common:
3420         bare_number_common_closed
3421         | REAL NUMBER_IDENTIFIER
3422         {
3423                 $$ = scm_product ($1, $2);
3424         }
3425         ;
3426
3427 bare_number_common_closed:
3428         REAL
3429         | NUMBER_IDENTIFIER
3430         ;
3431
3432 bare_number:
3433         bare_number_common
3434         | UNSIGNED
3435         | UNSIGNED NUMBER_IDENTIFIER    {
3436                 $$ = scm_product ($1, $2);
3437         }
3438         ;
3439
3440 bare_number_closed:
3441         UNSIGNED
3442         | bare_number_common_closed
3443         ;
3444
3445 unsigned_number:
3446         UNSIGNED
3447         | NUMBER_IDENTIFIER
3448         {
3449                 if (!scm_is_integer ($1)
3450                     || scm_is_true (scm_negative_p ($1)))
3451                 {
3452                         parser->parser_error (@1, _("not an unsigned integer"));
3453                         $$ = SCM_INUM0;
3454                 }
3455         }
3456         | embedded_scm
3457         {
3458                 if (!scm_is_integer ($1)
3459                     || scm_is_true (scm_negative_p ($1)))
3460                 {
3461                         parser->parser_error (@1, _("not an unsigned integer"));
3462                         $$ = SCM_INUM0;
3463                 }
3464         }
3465         ;
3466
3467 exclamations:
3468                 { $$ = SCM_UNDEFINED; }
3469         | exclamations '!'
3470         {
3471                 if (SCM_UNBNDP ($1))
3472                         $$ = SCM_BOOL_T;
3473                 else
3474                         $$ = scm_not ($1);
3475         }
3476         ;
3477
3478 questions:
3479         { $$ = SCM_UNDEFINED; }
3480         | questions '?'
3481         {
3482                 if (SCM_UNBNDP ($1))
3483                         $$ = SCM_BOOL_T;
3484                 else
3485                         $$ = scm_not ($1);
3486         }
3487         ;
3488
3489 full_markup_list:
3490         MARKUPLIST
3491                 { parser->lexer_->push_markup_state (); }
3492         markup_list {
3493                 $$ = $3;
3494                 parser->lexer_->pop_state ();
3495         }
3496         ;
3497
3498 full_markup:
3499         MARKUP
3500                 { parser->lexer_->push_markup_state (); }
3501         markup_top {
3502                 $$ = $3;
3503                 parser->lexer_->pop_state ();
3504         }
3505         ;
3506
3507 markup_top:
3508         markup_list {
3509                 $$ = scm_list_2 (ly_lily_module_constant ("line-markup"),  $1);
3510         }
3511         | markup_head_1_list simple_markup
3512         {
3513                 $$ = scm_car (MAKE_SYNTAX ("composed-markup-list",
3514                                            @2, $1, scm_list_1 ($2)));
3515         }
3516         | simple_markup {
3517                 $$ = $1;
3518         }
3519         ;
3520
3521 markup_scm:
3522         embedded_scm_bare
3523         {
3524                 if (Text_interface::is_markup ($1))
3525                         MYBACKUP (MARKUP_IDENTIFIER, $1, @1);
3526                 else if (Text_interface::is_markup_list ($1))
3527                         MYBACKUP (MARKUPLIST_IDENTIFIER, $1, @1);
3528                 else {
3529                         parser->parser_error (@1, _ ("not a markup"));
3530                         MYBACKUP (MARKUP_IDENTIFIER, scm_string (SCM_EOL), @1);
3531                 }
3532         } BACKUP
3533         ;
3534                         
3535
3536 markup_list:
3537         markup_composed_list {
3538                 $$ = $1;
3539         }
3540         | markup_uncomposed_list
3541         ;
3542
3543 markup_uncomposed_list:
3544         markup_braced_list {
3545                 $$ = $1;
3546         }
3547         | markup_command_list {
3548                 $$ = scm_list_1 ($1);
3549         }
3550         | markup_scm MARKUPLIST_IDENTIFIER
3551         {
3552                 $$ = $2;
3553         }
3554         | SCORELINES {
3555                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3556                 parser->lexer_->push_note_state (nn);
3557         } '{' score_body '}' {
3558                 unsmob_score ($4)->origin ()->set_spot (@$);
3559                 $$ = scm_list_1 (scm_list_2 (ly_lily_module_constant ("score-lines-markup-list"), $4));
3560                 parser->lexer_->pop_state ();
3561         }
3562         ;
3563
3564 markup_composed_list:
3565         markup_head_1_list markup_uncomposed_list {
3566                 $$ = MAKE_SYNTAX ("composed-markup-list",
3567                                   @2, $1, $2);
3568         }
3569         ;
3570
3571 markup_braced_list:
3572         '{' markup_braced_list_body '}' {
3573                 $$ = scm_reverse_x ($2, SCM_EOL);
3574         }
3575         ;
3576
3577 markup_braced_list_body:
3578         /* empty */     {  $$ = SCM_EOL; }
3579         | markup_braced_list_body markup {
3580                 $$ = scm_cons ($2, $1);
3581         }
3582         | markup_braced_list_body markup_list {
3583                 $$ = scm_reverse_x ($2, $1);
3584         }
3585         ;
3586
3587 markup_command_list:
3588         MARKUP_LIST_FUNCTION markup_command_list_arguments {
3589           $$ = scm_cons ($1, scm_reverse_x($2, SCM_EOL));
3590         }
3591         ;
3592
3593 markup_command_basic_arguments:
3594         EXPECT_MARKUP_LIST markup_command_list_arguments markup_list {
3595           $$ = scm_cons ($3, $2);
3596         }
3597         | EXPECT_SCM markup_command_list_arguments embedded_scm_closed {
3598           $$ = check_scheme_arg (parser, @3, $3, $2, $1);
3599         }
3600         | EXPECT_NO_MORE_ARGS {
3601           $$ = SCM_EOL;
3602         }
3603         ;
3604
3605 markup_command_list_arguments:
3606         markup_command_basic_arguments { $$ = $1; }
3607         | EXPECT_MARKUP markup_command_list_arguments markup {
3608           $$ = scm_cons ($3, $2);
3609         }
3610         ;
3611
3612 markup_head_1_item:
3613         MARKUP_FUNCTION EXPECT_MARKUP markup_command_list_arguments {
3614           $$ = scm_cons ($1, scm_reverse_x ($3, SCM_EOL));
3615         }
3616         ;
3617
3618 markup_head_1_list:
3619         markup_head_1_item      {
3620                 $$ = scm_list_1 ($1);
3621         }
3622         | markup_head_1_list markup_head_1_item {
3623                 $$ = scm_cons ($2, $1);
3624         }
3625         ;
3626
3627 simple_markup:
3628         STRING {
3629                 $$ = make_simple_markup ($1);
3630         }
3631         | SCORE {
3632                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3633                 parser->lexer_->push_note_state (nn);
3634         } '{' score_body '}' {
3635                 unsmob_score ($4)->origin ()->set_spot (@$);
3636                 $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), $4);
3637                 parser->lexer_->pop_state ();
3638         }
3639         | MARKUP_FUNCTION markup_command_basic_arguments {
3640                 $$ = scm_cons ($1, scm_reverse_x ($2, SCM_EOL));
3641         }
3642         | markup_scm MARKUP_IDENTIFIER
3643         {
3644                 $$ = $2;
3645         }
3646         ;
3647
3648 markup:
3649         markup_head_1_list simple_markup
3650         {
3651                 $$ = scm_car (MAKE_SYNTAX ("composed-markup-list",
3652                                            @2, $1, scm_list_1 ($2)));
3653         }
3654         | simple_markup {
3655                 $$ = $1;
3656         }
3657         ;
3658
3659 %%
3660
3661 void
3662 Lily_parser::set_yydebug (bool x)
3663 {
3664         yydebug = x;
3665 }
3666
3667 SCM
3668 Lily_parser::do_yyparse ()
3669 {
3670         SCM retval = SCM_UNDEFINED;
3671         yyparse (this, &retval);
3672         return retval;
3673 }
3674
3675
3676
3677
3678
3679 /*
3680
3681 It is a little strange to have this function in this file, but
3682 otherwise, we have to import music classes into the lexer.
3683
3684 */
3685 int
3686 Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
3687 {
3688         if (unsmob_book (sid)) {
3689                 Book *book =  unsmob_book (sid)->clone ();
3690                 *destination = book->self_scm ();
3691                 book->unprotect ();
3692
3693                 return BOOK_IDENTIFIER;
3694         } else if (scm_is_number (sid)) {
3695                 *destination = sid;
3696                 return NUMBER_IDENTIFIER;
3697         } else if (unsmob_context_def (sid)) {
3698                 Context_def *def= unsmob_context_def (sid)->clone ();
3699
3700                 *destination = def->self_scm ();
3701                 def->unprotect ();
3702
3703                 return CONTEXT_DEF_IDENTIFIER;
3704         } else if (unsmob_context_mod (sid)) {
3705                 *destination = unsmob_context_mod (sid)->smobbed_copy ();
3706
3707                 return CONTEXT_MOD_IDENTIFIER;
3708         } else if (Music *mus = unsmob_music (sid)) {
3709                 mus = mus->clone ();
3710                 *destination = mus->self_scm ();
3711                 bool is_event = mus->is_mus_type ("post-event");
3712                 mus->unprotect ();
3713                 return is_event ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
3714         } else if (unsmob_pitch (sid)) {
3715                 *destination = unsmob_pitch (sid)->smobbed_copy ();
3716                 return PITCH_IDENTIFIER;
3717         } else if (unsmob_duration (sid)) {
3718                 *destination = unsmob_duration (sid)->smobbed_copy ();
3719                 return DURATION_IDENTIFIER;
3720         } else if (unsmob_output_def (sid)) {
3721                 Output_def *p = unsmob_output_def (sid);
3722                 p = p->clone ();
3723
3724                 *destination = p->self_scm ();
3725                 p->unprotect ();
3726                 return OUTPUT_DEF_IDENTIFIER;
3727         } else if (unsmob_score (sid)) {
3728                 *destination = unsmob_score (sid)->clone ()->unprotect ();
3729                 return SCM_IDENTIFIER;
3730         }
3731
3732         return -1;
3733 }
3734
3735 SCM
3736 get_next_unique_context_id ()
3737 {
3738         return scm_from_locale_string ("$uniqueContextId");
3739 }
3740
3741
3742 SCM
3743 get_next_unique_lyrics_context_id ()
3744 {
3745         static int new_context_count;
3746         char s[128];
3747         snprintf (s, sizeof (s)-1, "uniqueContext%d", new_context_count++);
3748         return scm_from_locale_string (s);
3749 }
3750
3751 // check_scheme_arg checks one argument with a given predicate for use
3752 // in an argument list and throws a syntax error if it is unusable.
3753 // The argument is prepended to the argument list in any case.  After
3754 // throwing a syntax error, the argument list is terminated with #f as
3755 // its last cdr in order to mark it as uncallable while not losing
3756 // track of its total length.
3757 //
3758 // There are a few special considerations: if optional argument disp
3759 // is given (otherwise it defaults to SCM_UNDEFINED), it will be used
3760 // instead of arg in a prospective error message.  This is useful if
3761 // arg is not the actual argument but rather a transformation of it.
3762 //
3763 // If arg itself is SCM_UNDEFINED, the predicate is considered false
3764 // and an error message using disp is produced unconditionally.
3765
3766 SCM check_scheme_arg (Lily_parser *parser, Input loc,
3767                       SCM arg, SCM args, SCM pred, SCM disp)
3768 {
3769         if (SCM_UNBNDP (arg))
3770                 args = scm_cons (disp, args);
3771         else {
3772                 args = scm_cons (arg, args);
3773                 if (scm_is_true (scm_call_1 (pred, arg)))
3774                         return args;
3775         }
3776         scm_set_cdr_x (scm_last_pair (args), SCM_EOL);
3777         MAKE_SYNTAX ("argument-error", loc, scm_length (args), pred,
3778                      SCM_UNBNDP (disp) ? arg : disp);
3779         scm_set_cdr_x (scm_last_pair (args), SCM_BOOL_F);
3780         return args;
3781 }
3782
3783 SCM loc_on_music (Input loc, SCM arg)
3784 {
3785         if (Music *m = unsmob_music (arg))
3786         {
3787                 m = m->clone ();
3788                 m->set_spot (loc);
3789                 return m->unprotect ();
3790         }
3791         return arg;
3792 }
3793
3794 SCM
3795 try_string_variants (SCM pred, SCM str)
3796 {
3797         // a matching predicate is always ok
3798         if (scm_is_true (scm_call_1 (pred, str)))
3799                 return str;
3800         // a symbol may be interpreted as a list of symbols if it helps
3801         if (scm_is_symbol (str)) {
3802                 str = scm_list_1 (str);
3803                 if (scm_is_true (scm_call_1 (pred, str)))
3804                         return str;
3805                 return SCM_UNDEFINED;
3806         }
3807
3808         // If this cannot be a string representation of a symbol list,
3809         // we are through.
3810
3811         if (!is_regular_identifier (str, true))
3812                 return SCM_UNDEFINED;
3813
3814         str = scm_string_split (str, SCM_MAKE_CHAR ('.'));
3815         for (SCM p = str; scm_is_pair (p); p = scm_cdr (p))
3816                 scm_set_car_x (p, scm_string_to_symbol (scm_car (p)));
3817
3818         // Let's attempt the symbol list interpretation first.
3819
3820         if (scm_is_true (scm_call_1 (pred, str)))
3821                 return str;
3822
3823         // If there is just one symbol in the list, we might interpret
3824         // it as a single symbol
3825
3826         if (scm_is_null (scm_cdr (str)))
3827         {
3828                 str = scm_car (str);
3829                 if (scm_is_true (scm_call_1 (pred, str)))
3830                         return str;
3831         }
3832
3833         return SCM_UNDEFINED;
3834 }
3835
3836 bool
3837 is_regular_identifier (SCM id, bool multiple)
3838 {
3839   if (!scm_is_string (id))
3840           return false;
3841
3842   string str = ly_scm2string (id);
3843
3844   bool middle = false;
3845
3846   for (string::iterator it=str.begin(); it != str.end (); it++)
3847   {
3848           int c = *it & 0xff;
3849           if ((c >= 'a' && c <= 'z')
3850               || (c >= 'A' && c <= 'Z')
3851               || c > 0x7f)
3852                   middle = true;
3853           else if (middle && (c == '-' || c == '_' || (multiple && c == '.')))
3854                   middle = false;
3855           else
3856                   return false;
3857   }
3858   return middle;
3859 }
3860
3861 SCM
3862 make_music_from_simple (Lily_parser *parser, Input loc, SCM simple)
3863 {
3864         if (unsmob_music (simple))
3865                 return simple;
3866         if (parser->lexer_->is_note_state ()) {
3867                 if (scm_is_symbol (simple)) {
3868                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3869                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3870                         n->set_property ("drum-type", simple);
3871                         return n->unprotect ();
3872                 }
3873                 if (unsmob_pitch (simple)) {
3874                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3875                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3876                         n->set_property ("pitch", simple);
3877                         return n->unprotect ();
3878                 }
3879                 return simple;
3880         } else if (parser->lexer_->is_lyric_state ()) {
3881                 if (Text_interface::is_markup (simple))
3882                         return MAKE_SYNTAX ("lyric-event", loc, simple,
3883                                             parser->default_duration_.smobbed_copy ());
3884         } else if (parser->lexer_->is_chord_state ()) {
3885                 if (unsmob_pitch (simple))
3886                         return MAKE_SYNTAX
3887                                 ("event-chord",
3888                                  loc,
3889                                  make_chord_elements (loc, simple,
3890                                                       parser->default_duration_.smobbed_copy (),
3891                                                       SCM_EOL));
3892         }
3893         return simple;
3894 }
3895
3896 Music *
3897 make_music_with_input (SCM name, Input where)
3898 {
3899        Music *m = make_music_by_name (name);
3900        m->set_spot (where);
3901        return m;
3902 }
3903
3904 SCM
3905 make_simple_markup (SCM a)
3906 {
3907         return a;
3908 }
3909
3910 SCM
3911 make_duration (SCM d, int dots)
3912 {
3913         int t = scm_to_int (d);
3914         if (t > 0 && (t & (t-1)) == 0)
3915                 return Duration (intlog2 (t), dots).smobbed_copy ();
3916         else
3917                 return SCM_UNDEFINED;
3918 }
3919
3920 SCM
3921 make_chord_step (SCM step_scm, Rational alter)
3922 {
3923         int step = scm_to_int (step_scm);
3924
3925         if (step == 7)
3926                 alter += FLAT_ALTERATION;
3927
3928         while (step < 0)
3929                 step += 7;
3930         Pitch m ((step -1) / 7, (step - 1) % 7, alter);
3931         return m.smobbed_copy ();
3932 }
3933
3934
3935 SCM
3936 make_chord_elements (Input loc, SCM pitch, SCM dur, SCM modification_list)
3937 {
3938         SCM chord_ctor = ly_lily_module_constant ("construct-chord-elements");
3939         SCM res = scm_call_3 (chord_ctor, pitch, dur, modification_list);
3940         for (SCM s = res; scm_is_pair (s); s = scm_cdr (s))
3941         {
3942                 unsmob_music (scm_car (s))->set_spot (loc);
3943         }
3944         return res;
3945 }
3946
3947 int
3948 yylex (YYSTYPE *s, YYLTYPE *loc, Lily_parser *parser)
3949 {
3950         Lily_lexer *lex = parser->lexer_;
3951
3952         lex->lexval_ = s;
3953         lex->lexloc_ = loc;
3954         lex->prepare_for_next_token ();
3955         return lex->yylex ();
3956 }