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