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