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