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