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