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