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