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