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