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