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