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