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