]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
Merge branch 'issue4096' into HEAD
[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 (), parser->lexer_->override_input (location).smobbed_copy (), ##__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 (), parser->lexer_->override_input (location).smobbed_copy (), 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                         (Book::unsmob($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 (Score::unsmob ($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 = Output_def::unsmob ($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 = Output_def::unsmob ($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 = Music::unsmob ($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 (Music::unsmob ($2))
587                         $3 = scm_cons ($2, $3);
588                 if (Music::unsmob ($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 = Music::unsmob ($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 = Context_def::unsmob ($$);
691                 if (!td) {
692                         $$ = Context_def::make_scm ();
693                         td = Context_def::unsmob ($$);
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 = Context_def::unsmob ($$);
721                         if (!td) {
722                                 $$ = Context_def::make_scm ();
723                                 td = Context_def::unsmob ($$);
724                         }
725                         Context_def::unsmob ($$)->add_context_mod ($2);
726                 }
727         }
728         | context_def_spec_body context_modification {
729                 Context_def *td = Context_def::unsmob ($$);
730                 if (!td) {
731                         $$ = Context_def::make_scm ();
732                         td = Context_def::unsmob ($$);
733                 }
734                 SCM new_mods = Context_mod::unsmob ($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 = Context_def::unsmob ($1);
741                 if (scm_is_eq ($2, SCM_UNSPECIFIED))
742                         ;
743                 else if (!td && Context_def::unsmob ($2))
744                         $$ = $2;
745                 else {
746                         if (!td) {
747                                 $$ = Context_def::make_scm ();
748                                 td = Context_def::unsmob ($$);
749                         }
750                         if (Music::unsmob ($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 = Context_mod::unsmob ($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                 Book::unsmob ($$)->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*> (Output_def::unsmob (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                 Book::unsmob ($1)->paper_ = Output_def::unsmob ($2);
794                 set_paper (parser, Output_def::unsmob ($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 (Score::unsmob ($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 = Output_def::unsmob ($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 (Book::unsmob ($1)->header_);
852         } lilypond_header
853         | book_body error {
854                 Book *book = Book::unsmob ($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                 Book::unsmob ($$)->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                 Book::unsmob ($$)->paper_ = Output_def::unsmob ($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 (Score::unsmob ($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 = Output_def::unsmob ($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 = Book::unsmob ($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 = Book::unsmob ($1);
939                 book->paper_ = 0;
940                 book->scores_ = SCM_EOL;
941         }
942         ;
943
944 score_block:
945         SCORE '{' score_body '}'        {
946                 Score::unsmob ($3)->origin ()->set_spot (@$);
947                 $$ = $3;
948         }
949         ;
950
951 score_body:
952         score_items {
953                 if (!Score::unsmob ($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                                 Score::unsmob ($$)->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                                 Score::unsmob ($$)->
965                                         add_output_def (Output_def::unsmob (scm_car (p)));
966                         }
967                 }
968         }
969         | score_body error {
970                 Score::unsmob ($$)->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 = Output_def::unsmob ($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 (!Score::unsmob ($$)) {
996                         if (Music::unsmob ($2)) {
997                                 SCM scorify = ly_lily_module_constant ("scorify-music");
998                                 $2 = scm_call_2 (scorify, $2, parser->self_scm ());
999                         }
1000                         if (Score::unsmob ($2))
1001                         {
1002                                 $$ = $2;
1003                                 $2 = SCM_UNSPECIFIED;
1004                         }
1005                 }
1006                 Score *score = Score::unsmob ($$);
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 (Output_def::unsmob (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 = Score::unsmob ($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 = Output_def::unsmob ($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                 Output_def::unsmob ($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 = Output_def::unsmob ($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 (Context_def::unsmob ($2))
1141                         assign_context_def (Output_def::unsmob ($1), $2);
1142                 // Seems unlikely, but let's be complete:
1143                 else if (Music::unsmob ($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 (Context_def::unsmob ($3))
1169                         assign_context_def (Output_def::unsmob ($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 (Music::unsmob ($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_as_music
1226         ;
1227
1228 pitch_as_music:
1229         pitch_or_music
1230         {
1231                 $$ = make_music_from_simple (parser, @1, $1);
1232                 if (!Music::unsmob ($$))
1233                 {
1234                         parser->parser_error (@1, _ ("music expected"));
1235                         $$ = MAKE_SYNTAX ("void-music", @$);
1236                 }
1237         }
1238         ;
1239
1240 music_embedded:
1241         music
1242         {
1243                 if (Music::unsmob ($1)->is_mus_type ("post-event")) {
1244                         parser->parser_error (@1, _ ("unexpected post-event"));
1245                         $$ = SCM_UNSPECIFIED;
1246                 }
1247         }
1248         | music_embedded_backup
1249         {
1250                 $$ = $1;
1251         }
1252         | music_embedded_backup BACKUP lyric_element_music
1253         {
1254                 $$ = $3;
1255         }
1256         | multiplied_duration post_events
1257         {
1258                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
1259
1260                 parser->default_duration_ = *Duration::unsmob ($1);
1261                 n->set_property ("duration", $1);
1262
1263                 if (scm_is_pair ($2))
1264                         n->set_property ("articulations",
1265                                          scm_reverse_x ($2, SCM_EOL));
1266                 $$ = n->unprotect ();
1267         }
1268         ;
1269
1270 music_embedded_backup:
1271         embedded_scm
1272         {
1273                 if (scm_is_eq ($1, SCM_UNSPECIFIED))
1274                         $$ = $1;
1275                 else if (Music *m = Music::unsmob ($1)) {
1276                         if (m->is_mus_type ("post-event")) {
1277                                 parser->parser_error
1278                                         (@1, _ ("unexpected post-event"));
1279                                 $$ = SCM_UNSPECIFIED;
1280                         } else
1281                                 $$ = $1;
1282                 } else if (parser->lexer_->is_lyric_state ()
1283                            && Text_interface::is_markup ($1))
1284                         MYBACKUP (LYRIC_ELEMENT, $1, @1);
1285                 else {
1286                         @$.warning (_ ("Ignoring non-music expression"));
1287                         $$ = $1;
1288                 }
1289         }
1290         ;
1291
1292 // music_assign does not need to contain lyrics: there are no
1293 // assignments in lyricmode.
1294 music_assign:
1295         simple_music
1296         | composite_music %prec COMPOSITE
1297         ;
1298
1299 repeated_music:
1300         REPEAT simple_string unsigned_number music
1301         {
1302                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, SCM_EOL);
1303         }
1304         | REPEAT simple_string unsigned_number music ALTERNATIVE braced_music_list
1305         {
1306                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, $6);
1307         }
1308         ;
1309
1310 sequential_music:
1311         SEQUENTIAL braced_music_list {
1312                 $$ = MAKE_SYNTAX ("sequential-music", @$, $2);
1313         }
1314         | braced_music_list {
1315                 $$ = MAKE_SYNTAX ("sequential-music", @$, $1);
1316         }
1317         ;
1318
1319 simultaneous_music:
1320         SIMULTANEOUS braced_music_list {
1321                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, $2);
1322         }
1323         | DOUBLE_ANGLE_OPEN music_list DOUBLE_ANGLE_CLOSE       {
1324                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, scm_reverse_x ($2, SCM_EOL));
1325         }
1326         ;
1327
1328 simple_music:
1329         event_chord
1330         | music_property_def
1331         | context_change
1332         ;
1333
1334 context_modification:
1335         WITH
1336         {
1337                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
1338                 parser->lexer_->push_note_state (nn);
1339         } '{' context_mod_list '}'
1340         {
1341                 parser->lexer_->pop_state ();
1342                 $$ = $4;
1343         }
1344         | WITH CONTEXT_MOD_IDENTIFIER
1345         {
1346                 $$ = $2;
1347         }
1348         | CONTEXT_MOD_IDENTIFIER
1349         {
1350                 $$ = $1;
1351         }
1352         | WITH context_modification_arg
1353         {
1354                 if (Music::unsmob ($2)) {
1355                         SCM proc = parser->lexer_->lookup_identifier ("context-mod-music-handler");
1356                         $2 = scm_call_2 (proc, parser->self_scm (), $2);
1357                 }
1358                 if (Context_mod::unsmob ($2))
1359                         $$ = $2;
1360                 else {
1361                         parser->parser_error (@2, _ ("not a context mod"));
1362                         $$ = Context_mod ().smobbed_copy ();
1363                 }
1364         }
1365         ;
1366
1367 context_modification_arg:
1368         embedded_scm
1369         | MUSIC_IDENTIFIER
1370         ;
1371
1372 optional_context_mod:
1373         /**/ {
1374             $$ = SCM_EOL;
1375         }
1376         | context_modification
1377         {
1378               $$ = $1;
1379         }
1380         ;
1381
1382 context_mod_list:
1383         /**/ {
1384             $$ = Context_mod ().smobbed_copy ();
1385         }
1386         | context_mod_list context_mod  {
1387                 if (!SCM_UNBNDP ($2))
1388                         Context_mod::unsmob ($1)->add_context_mod ($2);
1389         }
1390         | context_mod_list CONTEXT_MOD_IDENTIFIER {
1391                  Context_mod *md = Context_mod::unsmob ($2);
1392                  if (md)
1393                      Context_mod::unsmob ($1)->add_context_mods (md->get_mods ());
1394         }
1395         | context_mod_list context_mod_arg {
1396                 if (scm_is_eq ($2, SCM_UNSPECIFIED))
1397                         ;
1398                 else if (Music::unsmob ($2)) {
1399                         SCM proc = parser->lexer_->lookup_identifier ("context-mod-music-handler");
1400                         $2 = scm_call_2 (proc, parser->self_scm (), $2);
1401                 }
1402                 if (Context_mod::unsmob ($2))
1403                         Context_mod::unsmob ($$)->add_context_mods
1404                                 (Context_mod::unsmob ($2)->get_mods ());
1405                 else {
1406                         parser->parser_error (@2, _ ("not a context mod"));
1407                 }
1408         }
1409         ;
1410
1411 context_prefix:
1412         CONTEXT symbol optional_id optional_context_mod {
1413                 Context_mod *ctxmod = Context_mod::unsmob ($4);
1414                 SCM mods = SCM_EOL;
1415                 if (ctxmod)
1416                         mods = ctxmod->get_mods ();
1417                 $$ = START_MAKE_SYNTAX ("context-specification", $2, $3, mods, SCM_BOOL_F);
1418         }
1419         | NEWCONTEXT symbol optional_id optional_context_mod {
1420                 Context_mod *ctxmod = Context_mod::unsmob ($4);
1421                 SCM mods = SCM_EOL;
1422                 if (ctxmod)
1423                         mods = ctxmod->get_mods ();
1424                 $$ = START_MAKE_SYNTAX ("context-specification", $2, $3, mods, SCM_BOOL_T);
1425         }
1426         ;
1427
1428 new_lyrics:
1429         ADDLYRICS lyric_mode_music {
1430                 $$ = scm_list_1 ($2);
1431         }
1432         | new_lyrics ADDLYRICS lyric_mode_music {
1433                 $$ = scm_cons ($3, $1);
1434         }
1435         ;
1436
1437 /* basic_music is basically the same as composite_music but with
1438  * context-prefixed music and lyricized music explicitly removed.  The
1439  * reason is that in a sequence
1440  *
1441  *   \new Staff \new Voice { ... } \addlyrics { ... } \addlyrics { ... }
1442  *
1443  * we need to group both \addlyrics together (as they go with the same
1444  * voice) but then combine them with \new Voice { ... }, meaning that
1445  * combining \new Voice { ... } needs higher priority than
1446  * { ... } \addlyrics, and *not* have \new Staff \new Voice { ... }
1447  * combine before combining \new Voice { ... } \addlyrics: only one
1448  * layer of context-prefixed music should assemble before \addlyrics
1449  * is integrated.  Total mess, and we sort this mess out with explicit
1450  * rules preferring a single context-prefix.
1451  */
1452
1453 basic_music:
1454         music_function_call
1455         | repeated_music
1456         | music_bare
1457         | LYRICSTO simple_string lyric_mode_music {
1458                 $$ = MAKE_SYNTAX ("lyric-combine", @$, $2, $3);
1459         }
1460         ;
1461
1462 contextable_music:
1463         basic_music
1464         | pitch_as_music
1465         | event_chord
1466         ;
1467
1468 contexted_basic_music:
1469         context_prefix contextable_music new_lyrics
1470         {
1471                 Input i;
1472                 i.set_location (@1, @2);
1473                 $$ = FINISH_MAKE_SYNTAX ($1, i, $2);
1474                 $$ = MAKE_SYNTAX ("add-lyrics", @$, $$, scm_reverse_x ($3, SCM_EOL));
1475         } %prec COMPOSITE
1476         | context_prefix contextable_music
1477         {
1478                 $$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
1479         } %prec COMPOSITE
1480         | context_prefix contexted_basic_music
1481         {
1482                 $$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
1483         }
1484         ;
1485
1486 composite_music:
1487         basic_music %prec COMPOSITE
1488         | contexted_basic_music
1489         | basic_music new_lyrics
1490         {
1491                 $$ = MAKE_SYNTAX ("add-lyrics", @$, $1, scm_reverse_x ($2, SCM_EOL));
1492         } %prec COMPOSITE
1493         ;
1494
1495 music_bare:
1496         mode_changed_music
1497         | MUSIC_IDENTIFIER
1498         | grouped_music_list
1499         ;
1500
1501 grouped_music_list:
1502         simultaneous_music              { $$ = $1; }
1503         | sequential_music              { $$ = $1; }
1504         ;
1505
1506 /* Function argument lists are arguably the most complex part in the
1507  * parser.  They are pretty tricky to understand because of the way
1508  * they are processed, and because of optional arguments that can be
1509  * omitted.  When there are several optional arguments in a row,
1510  * omitting one automatically omits all following arguments.  Optional
1511  * arguments can only be omitted when either
1512  *
1513  * a) the omission is explicitly started with \default
1514  * b) the omission is implicitly started by an argument not matching
1515  *    its predicate, and there is a mandatory argument later that can
1516  *    "catch" the argument that does not fit.
1517  *
1518  * When argument parsing starts, the lexer pushes EXPECT_SCM tokens
1519  * (corresponding to mandatory arguments and having a predicate
1520  * function as semantic value) or EXPECT_OPTIONAL EXPECT_SCM (where
1521  * the semantic value of the EXPECT_OPTIONAL token is the default to
1522  * use when the optional argument is omitted, and EXPECT_SCM again has
1523  * the argument predicate as semantic value) in reverse order to the
1524  * parser, followed by EXPECT_NO_MORE_ARGS.  The argument list is then
1525  * processed inside-out while actual tokens are consumed.
1526  *
1527  * This means that the argument list tokens determine the actions
1528  * taken as they arrive.  The structure of the argument list is known
1529  * to the parser and stored in its parse stack when the first argument
1530  * is being parsed.  What the parser does not know is which predicates
1531  * will match and whether or not \default will be appearing in the
1532  * argument list, and where.
1533  *
1534  * Sequences of 0 or more optional arguments are scanned using either
1535  * function_arglist_backup or function_arglist_nonbackup.  The first
1536  * is used when optional arguments are followed by at least one
1537  * mandatory argument: in that case optional arguments may be skipped
1538  * by either a false predicate (in which case the expression will be
1539  * pushed back as one or more tokens, preceded by a BACKUP token) or
1540  * by using \default.
1541  *
1542  * If optional arguments are at the end of the argument list, they are
1543  * instead scanned using function_arglist_nonbackup: here the only
1544  * manner to enter into skipping of optional arguments is the use of
1545  * \default.
1546  *
1547  * The argument list of a normal function call is parsed using
1548  * function_arglist.  The part of an argument list before a mandatory
1549  * argument is parsed using function_arglist_optional.
1550  *
1551  * The difference is that leading optional arguments are scanned using
1552  * function_arglist_nonbackup and function_arglist_backup,
1553  * respectively.
1554  *
1555  * Most other details are obvious in the rules themselves.
1556  *
1557  */
1558
1559 symbol_list_arg:
1560         SYMBOL_LIST
1561         | SYMBOL_LIST '.' symbol_list_rev
1562         {
1563                 $$ = scm_append (scm_list_2 ($1, scm_reverse_x ($3, SCM_EOL)));
1564         }
1565         ;
1566
1567 symbol_list_rev:
1568         symbol_list_part
1569         | symbol_list_rev '.' symbol_list_part
1570         {
1571                 $$ = scm_append_x (scm_list_2 ($3, $1));
1572         }
1573         ;
1574
1575 // symbol_list_part delivers elements in reverse copy.
1576
1577 symbol_list_part:
1578         symbol_list_element
1579         {
1580                 SCM sym_l_p = ly_lily_module_constant ("symbol-list?");
1581                 $$ = try_string_variants (sym_l_p, $1);
1582                 if (SCM_UNBNDP ($$)) {
1583                         parser->parser_error (@1, _("not a symbol"));
1584                         $$ = SCM_EOL;
1585                 } else
1586                         $$ = scm_reverse ($$);
1587         }
1588         ;
1589
1590
1591 symbol_list_element:
1592         STRING
1593         | embedded_scm_bare
1594         ;
1595
1596
1597 function_arglist_nonbackup:
1598         function_arglist_common
1599         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup post_event_nofinger
1600         {
1601                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1602         }
1603         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' UNSIGNED
1604         {
1605                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1606                 if (scm_is_true (scm_call_1 ($2, n)))
1607                         $$ = scm_cons (n, $3);
1608                 else {
1609                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1610                         t->set_property ("digit", $5);
1611                         $$ = check_scheme_arg (parser, @4, t->unprotect (),
1612                                                $3, $2, n);
1613                 }
1614         }
1615         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' REAL
1616         {
1617                 $$ = check_scheme_arg (parser, @4,
1618                                        scm_difference ($5, SCM_UNDEFINED),
1619                                        $3, $2);
1620         }
1621         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' NUMBER_IDENTIFIER
1622         {
1623                 $$ = check_scheme_arg (parser, @4,
1624                                        scm_difference ($5, SCM_UNDEFINED),
1625                                        $3, $2);
1626         }
1627         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup embedded_scm_arg
1628         {
1629                 if (scm_is_true (scm_call_1 ($2, $4)))
1630                         $$ = scm_cons ($4, $3);
1631                 else
1632                         $$ = check_scheme_arg (parser, @4,
1633                                                make_music_from_simple
1634                                                (parser, @4, $4),
1635                                                $3, $2);
1636         }
1637         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup bare_number_common
1638         {
1639                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1640         }
1641         | function_arglist_nonbackup_reparse REPARSE pitch_or_music
1642         {
1643                 if (scm_is_true (scm_call_1 ($2, $3)))
1644                         $$ = scm_cons ($3, $1);
1645                 else
1646                         $$ = check_scheme_arg (parser, @3,
1647                                                make_music_from_simple
1648                                                (parser, @3, $3),
1649                                                $1, $2);
1650         }
1651         | function_arglist_nonbackup_reparse REPARSE duration_length
1652         {
1653                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1654         }
1655         | function_arglist_nonbackup_reparse REPARSE bare_number_common
1656         {
1657                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1658         }
1659         | function_arglist_nonbackup_reparse REPARSE SCM_ARG
1660         {
1661                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1662         }
1663         | function_arglist_nonbackup_reparse REPARSE lyric_element_music
1664         {
1665                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1666         }
1667         | function_arglist_nonbackup_reparse REPARSE symbol_list_arg
1668         {
1669                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1670         }
1671         ;
1672
1673 function_arglist_nonbackup_reparse:
1674         EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup SCM_IDENTIFIER
1675         {
1676                 $$ = $3;
1677                 SCM res = try_string_variants ($2, $4);
1678                 if (!SCM_UNBNDP (res))
1679                         if (scm_is_pair (res))
1680                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1681                         else
1682                                 MYREPARSE (@4, $2, SCM_ARG, res);
1683                 else if (scm_is_true
1684                          (scm_call_1
1685                           ($2, make_music_from_simple
1686                            (parser, @4, $4))))
1687                         MYREPARSE (@4, $2, STRING, $4);
1688                 else
1689                         MYREPARSE (@4, $2, SCM_ARG, $4);
1690         }
1691         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup pitch
1692         {
1693                 $$ = $3;
1694                 if (scm_is_true
1695                     (scm_call_1
1696                      ($2, make_music_from_simple
1697                       (parser, @4, $4))))
1698                         MYREPARSE (@4, $2, PITCH_IDENTIFIER, $4);
1699                 else
1700                         MYREPARSE (@4, $2, SCM_ARG, $4);
1701         }
1702         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup steno_tonic_pitch
1703         {
1704                 $$ = $3;
1705                 if (scm_is_true
1706                     (scm_call_1
1707                      ($2, make_music_from_simple
1708                       (parser, @4, $4))))
1709                         MYREPARSE (@4, $2, TONICNAME_PITCH, $4);
1710                 else
1711                         MYREPARSE (@4, $2, SCM_ARG, $4);
1712         }
1713         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup STRING
1714         {
1715                 $$ = $3;
1716                 SCM res = try_string_variants ($2, $4);
1717                 if (!SCM_UNBNDP (res))
1718                         if (scm_is_pair (res))
1719                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1720                         else
1721                                 MYREPARSE (@4, $2, SCM_ARG, res);
1722                 else if (scm_is_true
1723                          (scm_call_1
1724                           ($2, make_music_from_simple
1725                            (parser, @4, $4))))
1726                         MYREPARSE (@4, $2, STRING, $4);
1727                 else
1728                         MYREPARSE (@4, $2, SCM_ARG, $4);
1729         }
1730         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup full_markup
1731         {
1732                 $$ = $3;
1733                 if (scm_is_true (scm_call_1 ($2, $4)))
1734                         MYREPARSE (@4, $2, SCM_ARG, $4);
1735                 else if (scm_is_true
1736                          (scm_call_1
1737                           ($2, make_music_from_simple
1738                            (parser, @4, $4))))
1739                         MYREPARSE (@4, $2, STRING, $4);
1740                 else
1741                         MYREPARSE (@4, $2, SCM_ARG, $4);
1742         }
1743         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup UNSIGNED
1744         {
1745                 $$ = $3;
1746                 if (scm_is_true (scm_call_1 ($2, $4)))
1747                         MYREPARSE (@4, $2, REAL, $4);
1748                 else {
1749                         SCM d = make_duration ($4);
1750                         if (SCM_UNBNDP (d) || scm_is_false (scm_call_1 ($2, d)))
1751                                 MYREPARSE (@4, $2, REAL, $4); // trigger error
1752                         else
1753                                 MYREPARSE (@4, $2, DURATION_IDENTIFIER, d);
1754                 }
1755         }
1756         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup DURATION_IDENTIFIER {
1757                 $$ = $3;
1758                 MYREPARSE (@4, $2, DURATION_IDENTIFIER, $4);
1759         }
1760         ;
1761
1762
1763 // function_arglist_backup can't occur at the end of an argument
1764 // list.
1765 function_arglist_backup:
1766         function_arglist_common
1767         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup embedded_scm_arg
1768         {
1769                 if (scm_is_true (scm_call_1 ($2, $4)))
1770                         $$ = scm_cons ($4, $3);
1771                 else {
1772                         $$ = make_music_from_simple (parser, @4, $4);
1773                         if (scm_is_true (scm_call_1 ($2, $$)))
1774                                 $$ = scm_cons ($$, $3);
1775                         else
1776                         {
1777                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1778                                 MYBACKUP (SCM_ARG, $4, @4);
1779                         }
1780                 }
1781         }
1782         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup post_event_nofinger
1783         {
1784                 if (scm_is_true (scm_call_1 ($2, $4)))
1785                 {
1786                         $$ = scm_cons ($4, $3);
1787                 } else {
1788                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1789                         MYBACKUP (EVENT_IDENTIFIER, $4, @4);
1790                 }
1791         }
1792         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup pitch
1793         {
1794                 if (scm_is_true
1795                     (scm_call_1
1796                      ($2, make_music_from_simple
1797                       (parser, @4, $4))))
1798                 {
1799                         $$ = $3;
1800                         MYREPARSE (@4, $2, PITCH_IDENTIFIER, $4);
1801                 } else if (scm_is_true (scm_call_1 ($2, $4)))
1802                         $$ = scm_cons ($4, $3);
1803                 else {
1804                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1805                         MYBACKUP (PITCH_IDENTIFIER, $4, @4);
1806                 }
1807         }
1808         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup steno_tonic_pitch
1809         {
1810                 if (scm_is_true
1811                     (scm_call_1
1812                      ($2, make_music_from_simple
1813                       (parser, @4, $4))))
1814                 {
1815                         $$ = $3;
1816                         MYREPARSE (@4, $2, TONICNAME_PITCH, $4);
1817                 } else if (scm_is_true (scm_call_1 ($2, $4)))
1818                         $$ = scm_cons ($4, $3);
1819                 else {
1820                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1821                         MYBACKUP (TONICNAME_PITCH, $4, @4);
1822                 }
1823         }
1824         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup full_markup
1825         {
1826                 if (scm_is_true (scm_call_1 ($2, $4)))
1827                         $$ = scm_cons ($4, $3);
1828                 else {
1829                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1830                         MYBACKUP (SCM_IDENTIFIER, $4, @4);
1831                 }
1832         }
1833         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup UNSIGNED
1834         {
1835                 if (scm_is_true (scm_call_1 ($2, $4)))
1836                 {
1837                         MYREPARSE (@4, $2, REAL, $4);
1838                         $$ = $3;
1839                 } else {
1840                         SCM d = make_duration ($4);
1841                         if (SCM_UNBNDP (d) || scm_is_false (scm_call_1 ($2, d)))
1842                         {
1843                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1844                                 MYBACKUP (UNSIGNED, $4, @4);
1845                         } else {
1846                                 MYREPARSE (@4, $2, DURATION_IDENTIFIER, d);
1847                                 $$ = $3;
1848                         }
1849                 }
1850         }
1851         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup REAL
1852         {
1853                 if (scm_is_true (scm_call_1 ($2, $4)))
1854                 {
1855                         $$ = $3;
1856                         MYREPARSE (@4, $2, REAL, $4);
1857                 } else {
1858                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1859                         MYBACKUP (REAL, $4, @4);
1860                 }
1861         }
1862         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup NUMBER_IDENTIFIER
1863         {
1864                 if (scm_is_true (scm_call_1 ($2, $4)))
1865                 {
1866                         $$ = scm_cons ($4, $3);
1867                 } else {
1868                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1869                         MYBACKUP (NUMBER_IDENTIFIER, $4, @4);
1870                 }
1871         }
1872         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' UNSIGNED
1873         {
1874                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1875                 if (scm_is_true (scm_call_1 ($2, n))) {
1876                         $$ = $3;
1877                         MYREPARSE (@5, $2, REAL, n);
1878                 } else {
1879                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1880                         t->set_property ("digit", $5);
1881                         $$ = t->unprotect ();
1882                         if (scm_is_true (scm_call_1 ($2, $$)))
1883                                 $$ = scm_cons ($$, $3);
1884                         else {
1885                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1886                                 MYBACKUP (UNSIGNED, $5, @5);
1887                                 parser->lexer_->push_extra_token (@4, '-');
1888                         }
1889                 }
1890         }
1891         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' REAL
1892         {
1893                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1894                 if (scm_is_true (scm_call_1 ($2, n))) {
1895                         MYREPARSE (@5, $2, REAL, n);
1896                         $$ = $3;
1897                 } else {
1898                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1899                         MYBACKUP (REAL, n, @5);
1900                 }
1901         }
1902         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' NUMBER_IDENTIFIER
1903         {
1904                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1905                 if (scm_is_true (scm_call_1 ($2, n))) {
1906                         $$ = scm_cons (n, $3);
1907                 } else {
1908                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1909                         MYBACKUP (NUMBER_IDENTIFIER, n, @5);
1910                 }
1911         }
1912         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup DURATION_IDENTIFIER
1913         {
1914                 if (scm_is_true (scm_call_1 ($2, $4)))
1915                 {
1916                         MYREPARSE (@4, $2, DURATION_IDENTIFIER, $4);
1917                         $$ = $3;
1918                 } else {
1919                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1920                         MYBACKUP (DURATION_IDENTIFIER, $4, @4);
1921                 }
1922         }
1923         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup SCM_IDENTIFIER
1924         {
1925                 SCM res = try_string_variants ($2, $4);
1926                 if (!SCM_UNBNDP (res))
1927                         if (scm_is_pair (res)) {
1928                                 $$ = $3;
1929                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1930                         }
1931                         else
1932                                 $$ = scm_cons (res, $3);
1933                 else {
1934                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1935                         MYBACKUP (SCM_IDENTIFIER, $4, @4);
1936                 }
1937         }
1938         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup STRING
1939         {
1940                 SCM res = try_string_variants ($2, $4);
1941                 if (!SCM_UNBNDP (res))
1942                         if (scm_is_pair (res)) {
1943                                 $$ = $3;
1944                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1945                         }
1946                         else
1947                                 $$ = scm_cons (res, $3);
1948                 else {
1949                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1950                         MYBACKUP (STRING, $4, @4);
1951                 }
1952         }
1953         | function_arglist_backup REPARSE pitch_or_music
1954         {
1955                 if (scm_is_true (scm_call_1 ($2, $3)))
1956                         $$ = scm_cons ($3, $1);
1957                 else
1958                         $$ = check_scheme_arg (parser, @3,
1959                                                make_music_from_simple
1960                                                (parser, @3, $3),
1961                                                $1, $2);
1962         }
1963         | function_arglist_backup REPARSE bare_number_common
1964         {
1965                 $$ = check_scheme_arg (parser, @3,
1966                                        $3, $1, $2);
1967         }
1968         | function_arglist_backup REPARSE duration_length
1969         {
1970                 $$ = check_scheme_arg (parser, @3,
1971                                        $3, $1, $2);
1972         }
1973         | function_arglist_backup REPARSE symbol_list_arg
1974         {
1975                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1976         }
1977         ;
1978
1979 function_arglist:
1980         function_arglist_nonbackup
1981         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_nonbackup DEFAULT
1982         {
1983                 $$ = scm_cons (loc_on_music (@4, $1), $3);
1984         }
1985         ;
1986
1987 function_arglist_skip_nonbackup:
1988         function_arglist_nonbackup
1989         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_nonbackup
1990         {
1991                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1992         }
1993         ;
1994
1995 function_arglist_common:
1996         EXPECT_NO_MORE_ARGS {
1997                 $$ = SCM_EOL;
1998         }
1999         | EXPECT_SCM function_arglist_optional embedded_scm_arg
2000         {
2001                 if (scm_is_true (scm_call_1 ($1, $3)))
2002                         $$ = scm_cons ($3, $2);
2003                 else
2004                         $$ = check_scheme_arg (parser, @3,
2005                                                make_music_from_simple
2006                                                (parser, @3, $3),
2007                                                $2, $1);
2008         }
2009         | EXPECT_SCM function_arglist_optional bare_number_common
2010         {
2011                 $$ = check_scheme_arg (parser, @3,
2012                                        $3, $2, $1);
2013         }
2014         | EXPECT_SCM function_arglist_optional post_event_nofinger
2015         {
2016                 $$ = check_scheme_arg (parser, @3,
2017                                        $3, $2, $1);
2018         }
2019         | EXPECT_SCM function_arglist_optional '-' NUMBER_IDENTIFIER
2020         {
2021                 SCM n = scm_difference ($4, SCM_UNDEFINED);
2022                 $$ = check_scheme_arg (parser, @4, n, $2, $1);
2023         }
2024         | function_arglist_common_reparse REPARSE SCM_ARG
2025         {
2026                 $$ = check_scheme_arg (parser, @3,
2027                                        $3, $1, $2);
2028         }
2029         | function_arglist_common_reparse REPARSE lyric_element_music
2030         {
2031                 $$ = check_scheme_arg (parser, @3,
2032                                        $3, $1, $2);
2033         }
2034         | function_arglist_common_reparse REPARSE pitch_or_music
2035         {
2036                 if (scm_is_true (scm_call_1 ($2, $3)))
2037                         $$ = scm_cons ($3, $1);
2038                 else
2039                         $$ = check_scheme_arg (parser, @3,
2040                                                make_music_from_simple
2041                                                (parser, @3, $3),
2042                                                $1, $2);
2043         }
2044         | function_arglist_common_reparse REPARSE bare_number_common
2045         {
2046                 $$ = check_scheme_arg (parser, @3,
2047                                        $3, $1, $2);
2048         }
2049         | function_arglist_common_reparse REPARSE duration_length
2050         {
2051                 $$ = check_scheme_arg (parser, @3,
2052                                        $3, $1, $2);
2053         }
2054         | function_arglist_common_reparse REPARSE symbol_list_arg
2055         {
2056                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
2057         }
2058         ;
2059
2060 function_arglist_common_reparse:
2061         EXPECT_SCM function_arglist_optional SCM_IDENTIFIER
2062         {
2063                 $$ = $2;
2064                 SCM res = try_string_variants ($1, $3);
2065                 if (!SCM_UNBNDP (res))
2066                         if (scm_is_pair (res))
2067                                 MYREPARSE (@3, $1, SYMBOL_LIST, res);
2068                         else
2069                                 MYREPARSE (@3, $1, SCM_ARG, res);
2070                 else if (scm_is_true
2071                          (scm_call_1
2072                           ($1, make_music_from_simple (parser, @3, $3))))
2073                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
2074                 else
2075                         // This is going to flag a syntax error, we
2076                         // know the predicate to be false.
2077                         MYREPARSE (@3, $1, SCM_ARG, $3);
2078         }
2079         | EXPECT_SCM function_arglist_optional pitch
2080         {
2081                 $$ = $2;
2082                 if (scm_is_true
2083                     (scm_call_1
2084                      ($1, make_music_from_simple
2085                       (parser, @3, $3))))
2086                         MYREPARSE (@3, $1, PITCH_IDENTIFIER, $3);
2087                 else
2088                         MYREPARSE (@3, $1, SCM_ARG, $3);
2089         }
2090         | EXPECT_SCM function_arglist_optional steno_tonic_pitch
2091         {
2092                 $$ = $2;
2093                 if (scm_is_true
2094                     (scm_call_1
2095                      ($1, make_music_from_simple
2096                       (parser, @3, $3))))
2097                         MYREPARSE (@3, $1, TONICNAME_PITCH, $3);
2098                 else
2099                         MYREPARSE (@3, $1, SCM_ARG, $3);
2100         }
2101         | EXPECT_SCM function_arglist_optional STRING
2102         {
2103                 $$ = $2;
2104                 SCM res = try_string_variants ($1, $3);
2105                 if (!SCM_UNBNDP (res))
2106                         if (scm_is_pair (res))
2107                                 MYREPARSE (@3, $1, SYMBOL_LIST, res);
2108                         else
2109                                 MYREPARSE (@3, $1, SCM_ARG, res);
2110                 else if (scm_is_true
2111                          (scm_call_1
2112                           ($1, make_music_from_simple (parser, @3, $3))))
2113                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
2114                 else
2115                         // This is going to flag a syntax error, we
2116                         // know the predicate to be false.
2117                         MYREPARSE (@3, $1, SCM_ARG, $3);
2118         }
2119         | EXPECT_SCM function_arglist_optional full_markup
2120         {
2121                 $$ = $2;
2122                 if (scm_is_true (scm_call_1 ($1, $3)))
2123                         MYREPARSE (@3, $1, SCM_ARG, $3);
2124                 else if (scm_is_true
2125                          (scm_call_1
2126                           ($1, make_music_from_simple (parser, @3, $3))))
2127                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
2128                 else
2129                         // This is going to flag a syntax error, we
2130                         // know the predicate to be false.
2131                         MYREPARSE (@3, $1, SCM_ARG, $3);
2132         }
2133         | EXPECT_SCM function_arglist_optional UNSIGNED
2134         {
2135                 $$ = $2;
2136                 if (scm_is_true (scm_call_1 ($1, $3)))
2137                         MYREPARSE (@3, $1, REAL, $3);
2138                 else {
2139                         SCM d = make_duration ($3);
2140                         if (SCM_UNBNDP (d) || scm_is_false (scm_call_1 ($1, d)))
2141                                 MYREPARSE (@3, $1, REAL, $3);
2142                         else
2143                                 MYREPARSE (@3, $1, DURATION_IDENTIFIER, d);
2144                 }
2145         }
2146         | EXPECT_SCM function_arglist_optional DURATION_IDENTIFIER
2147         {
2148                 $$ = $2;
2149                 MYREPARSE (@3, $1, DURATION_IDENTIFIER, $3);
2150         }
2151         | EXPECT_SCM function_arglist_optional '-' UNSIGNED
2152         {
2153                 $$ = $2;
2154                 SCM n = scm_difference ($4, SCM_UNDEFINED);
2155                 if (scm_is_true (scm_call_1 ($1, n)))
2156                         MYREPARSE (@4, $1, REAL, n);
2157                 else {
2158                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @4);
2159                         t->set_property ("digit", $4);
2160                         SCM m = t->unprotect ();
2161                         if (scm_is_true (scm_call_1 ($1, m)))
2162                                 MYREPARSE (@4, $1, SCM_ARG, m);
2163                         else
2164                                 MYREPARSE (@4, $1, SCM_ARG, $4);
2165                 }
2166         }
2167         | EXPECT_SCM function_arglist_optional '-' REAL
2168         {
2169                 $$ = $2;
2170                 SCM n = scm_difference ($4, SCM_UNDEFINED);
2171                 MYREPARSE (@4, $1, REAL, n);
2172         }
2173         ;
2174
2175 function_arglist_optional:
2176         function_arglist_backup
2177         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_backup DEFAULT
2178         {
2179                 $$ = scm_cons (loc_on_music (@4, $1), $3);
2180         }
2181         | function_arglist_skip_backup BACKUP
2182         ;
2183
2184 function_arglist_skip_backup:
2185         function_arglist_backup
2186         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_backup
2187         {
2188                 $$ = scm_cons (loc_on_music (@3, $1), $3);
2189         }
2190         ;
2191
2192 music_function_call:
2193         MUSIC_FUNCTION function_arglist {
2194                 $$ = MAKE_SYNTAX ("music-function", @$,
2195                                          $1, $2);
2196         }
2197         ;
2198
2199
2200 optional_id:
2201         /**/ { $$ = SCM_EOL; }
2202         | '=' simple_string {
2203                 $$ = $2;
2204         }
2205         ;
2206
2207 // We must not have lookahead tokens parsed in lyric mode.  In order
2208 // to save confusion, we take almost the same set as permitted with
2209 // \lyricmode and/or \lyrics.  However, music identifiers are also
2210 // allowed, and they obviously do not require switching into lyrics
2211 // mode for parsing.
2212
2213 lyric_mode_music:
2214         {
2215                 parser->lexer_->push_lyric_state ();
2216         } grouped_music_list
2217         {
2218                 parser->lexer_->pop_state ();
2219                 $$ = $2;
2220         }
2221         | MUSIC_IDENTIFIER
2222         ;
2223
2224 mode_changed_music:
2225         mode_changing_head grouped_music_list {
2226                 if ($1 == ly_symbol2scm ("chords"))
2227                 {
2228                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $2);
2229                 }
2230                 else
2231                 {
2232                   $$ = $2;
2233                 }
2234                 parser->lexer_->pop_state ();
2235         }
2236         | mode_changing_head_with_context optional_context_mod grouped_music_list {
2237                 Context_mod *ctxmod = Context_mod::unsmob ($2);
2238                 SCM mods = SCM_EOL;
2239                 if (ctxmod)
2240                         mods = ctxmod->get_mods ();
2241                 $$ = MAKE_SYNTAX ("context-specification", @$, $1, SCM_EOL, mods, SCM_BOOL_T, $3);
2242                 if ($1 == ly_symbol2scm ("ChordNames"))
2243                 {
2244                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $$);
2245                 }
2246                 parser->lexer_->pop_state ();
2247         }
2248         ;
2249
2250 mode_changing_head:
2251         NOTEMODE {
2252                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
2253                 parser->lexer_->push_note_state (nn);
2254
2255                 $$ = ly_symbol2scm ("notes");
2256         }
2257         | DRUMMODE
2258                 {
2259                 SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
2260                 parser->lexer_->push_note_state (nn);
2261
2262                 $$ = ly_symbol2scm ("drums");
2263         }
2264         | FIGUREMODE {
2265                 parser->lexer_->push_figuredbass_state ();
2266
2267                 $$ = ly_symbol2scm ("figures");
2268         }
2269         | CHORDMODE {
2270                 SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
2271                 parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
2272                 nn = parser->lexer_->lookup_identifier ("pitchnames");
2273                 parser->lexer_->push_chord_state (nn);
2274                 $$ = ly_symbol2scm ("chords");
2275
2276         }
2277         | LYRICMODE
2278                 { parser->lexer_->push_lyric_state ();
2279                 $$ = ly_symbol2scm ("lyrics");
2280         }
2281         ;
2282
2283 mode_changing_head_with_context:
2284         DRUMS {
2285                 SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
2286                 parser->lexer_->push_note_state (nn);
2287
2288                 $$ = ly_symbol2scm ("DrumStaff");
2289         }
2290         | FIGURES {
2291                 parser->lexer_->push_figuredbass_state ();
2292
2293                 $$ = ly_symbol2scm ("FiguredBass");
2294         }
2295         | CHORDS {
2296                 SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
2297                 parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
2298                 nn = parser->lexer_->lookup_identifier ("pitchnames");
2299                 parser->lexer_->push_chord_state (nn);
2300                 $$ = ly_symbol2scm ("ChordNames");
2301         }
2302         | LYRICS
2303                 { parser->lexer_->push_lyric_state ();
2304                 $$ = ly_symbol2scm ("Lyrics");
2305         }
2306         ;
2307
2308 context_change:
2309         CHANGE symbol '=' simple_string  {
2310                 $$ = MAKE_SYNTAX ("context-change", @$, $2, $4);
2311         }
2312         ;
2313
2314
2315 property_path:
2316         symbol_list_rev  {
2317                 $$ = scm_reverse_x ($1, SCM_EOL);
2318         }
2319         | symbol_list_rev property_path {
2320                 $$ = scm_reverse_x ($1, $2);
2321         }
2322         ;
2323
2324 property_operation:
2325         symbol '=' scalar {
2326                 $$ = scm_list_3 (ly_symbol2scm ("assign"), $1, $3);
2327         }
2328         | UNSET symbol {
2329                 $$ = scm_list_2 (ly_symbol2scm ("unset"), $2);
2330         }
2331         | OVERRIDE property_path '=' scalar {
2332                 if (scm_ilength ($2) < 2) {
2333                         parser->parser_error (@2, _("bad grob property path"));
2334                         $$ = SCM_UNDEFINED;
2335                 } else {
2336                         $$ = scm_cons (ly_symbol2scm ("push"),
2337                                        scm_cons2 (scm_car ($2),
2338                                                   $4,
2339                                                   scm_cdr ($2)));
2340                 }
2341         }
2342         | REVERT revert_arg {
2343                 $$ = scm_cons (ly_symbol2scm ("pop"), $2);
2344         }
2345         ;
2346
2347 // This is all quite awkward for the sake of substantial backward
2348 // compatibility while at the same time allowing a more "natural" form
2349 // of specification not separating grob specification from grob
2350 // property path.  The purpose of this definition of revert_arg is to
2351 // allow the symbol list which specifies grob and property to revert
2352 // to be optionally be split into two parts after the grob (which in
2353 // this case is just the first element of the list).  symbol_list_part
2354 // is only one path component, but it can be parsed without lookahead,
2355 // so we can follow it with a synthetic BACKUP token when needed.  If
2356 // the first symbol_list_part already contains multiple elements (only
2357 // possible if a Scheme expression provides them), we just parse for
2358 // additional elements introduced by '.', which is what the
2359 // SYMBOL_LIST backup in connection with the immediately following
2360 // rule using symbol_list_arg does.
2361 //
2362 // As long as we don't have our coffers filled with both grob and at
2363 // least one grob property specification, the rest of the required
2364 // symbol list chain may be provided either with or without a leading
2365 // dot.  This is for both allowing the traditional
2366 // \revert Accidental #'color
2367 // as well as well as the "naive" form
2368 // \revert Accidental.color
2369
2370 revert_arg:
2371         revert_arg_backup BACKUP symbol_list_arg
2372         {
2373                 $$ = $3;
2374         }
2375         ;
2376
2377 revert_arg_backup:
2378         revert_arg_part
2379         {
2380                 if (scm_is_null ($1)
2381                     || scm_is_null (scm_cdr ($1)))
2382                         MYBACKUP (SCM_ARG, $1, @1);
2383                 else
2384                         MYBACKUP (SYMBOL_LIST, scm_reverse_x ($1, SCM_EOL), @1);
2385         }
2386         ;
2387
2388 // revert_arg_part delivers results in reverse
2389 revert_arg_part:
2390         symbol_list_part
2391         | revert_arg_backup BACKUP SCM_ARG '.' symbol_list_part
2392         {
2393                 $$ = scm_append_x (scm_list_2 ($5, $3));
2394         }
2395         | revert_arg_backup BACKUP SCM_ARG symbol_list_part
2396         {
2397                 $$ = scm_append_x (scm_list_2 ($4, $3));
2398         }
2399         ;
2400
2401 context_def_mod:
2402         CONSISTS { $$ = ly_symbol2scm ("consists"); }
2403         | REMOVE { $$ = ly_symbol2scm ("remove"); }
2404
2405         | ACCEPTS { $$ = ly_symbol2scm ("accepts"); }
2406         | DEFAULTCHILD { $$ = ly_symbol2scm ("default-child"); }
2407         | DENIES { $$ = ly_symbol2scm ("denies"); }
2408
2409         | ALIAS { $$ = ly_symbol2scm ("alias"); }
2410         | TYPE { $$ = ly_symbol2scm ("translator-type"); }
2411         | DESCRIPTION { $$ = ly_symbol2scm ("description"); }
2412         | NAME { $$ = ly_symbol2scm ("context-name"); }
2413         ;
2414
2415 context_mod:
2416         property_operation { $$ = $1; }
2417         | context_def_mod STRING {
2418                 $$ = scm_list_2 ($1, $2);
2419         }
2420         | context_def_mod embedded_scm
2421         {
2422                 if (!scm_is_string ($2)
2423                     && ly_symbol2scm ("consists") != $1
2424                     && ly_symbol2scm ("remove") != $1)
2425                 {
2426                         $$ = SCM_EOL;
2427                         parser->parser_error (@1, _ ("only \\consists and \\remove take non-string argument."));
2428                 }
2429                 else
2430                 {
2431                         $$ = scm_list_2 ($1, $2);
2432                 }
2433         }
2434         ;
2435
2436 // If defined, at least two members.
2437 grob_prop_spec:
2438         symbol_list_rev
2439         {
2440                 SCM l = scm_reverse_x ($1, SCM_EOL);
2441                 if (scm_is_pair (l)
2442                     && to_boolean
2443                     (scm_object_property (scm_car (l),
2444                                           ly_symbol2scm ("is-grob?"))))
2445                         l = scm_cons (ly_symbol2scm ("Bottom"), l);
2446                 if (scm_is_null (l) || scm_is_null (scm_cdr (l))) {
2447                         parser->parser_error (@1, _ ("bad grob property path"));
2448                         l = SCM_UNDEFINED;
2449                 }
2450                 $$ = l;
2451         }
2452         ;
2453
2454 // If defined, at least three members
2455 grob_prop_path:
2456         grob_prop_spec
2457         {
2458                 if (!SCM_UNBNDP ($1) && scm_is_null (scm_cddr ($1)))
2459                 {
2460                         parser->parser_error (@1, _ ("bad grob property path"));
2461                         $$ = SCM_UNDEFINED;
2462                 }
2463         }
2464         | grob_prop_spec property_path
2465         {
2466                 if (!SCM_UNBNDP ($1)) {
2467                         $$ = scm_append_x (scm_list_2 ($1, $2));
2468                         if (scm_is_null (scm_cddr ($$))) {
2469                                 parser->parser_error (@$, _ ("bad grob property path"));
2470                                 $$ = SCM_UNDEFINED;
2471                         }
2472                 }
2473
2474         }
2475         ;
2476
2477 // Exactly two elements or undefined
2478 context_prop_spec:
2479         symbol_list_rev
2480         {
2481                 SCM l = scm_reverse_x ($1, SCM_EOL);
2482                 switch (scm_ilength (l)) {
2483                 case 1:
2484                         l = scm_cons (ly_symbol2scm ("Bottom"), l);
2485                 case 2:
2486                         break;
2487                 default:
2488                         parser->parser_error (@1, _ ("bad context property path"));
2489                         l = SCM_UNDEFINED;
2490                 }
2491                 $$ = l;
2492         }
2493         ;
2494
2495 simple_music_property_def:
2496         OVERRIDE grob_prop_path '=' scalar {
2497                 if (SCM_UNBNDP ($2))
2498                         $$ = SCM_UNDEFINED;
2499                 else {
2500                         $$ = scm_list_5 (scm_car ($2),
2501                                          ly_symbol2scm ("OverrideProperty"),
2502                                          scm_cadr ($2),
2503                                          $4,
2504                                          scm_cddr ($2));
2505                 }
2506         }
2507         | REVERT simple_revert_context revert_arg {
2508                 $$ = scm_list_4 ($2,
2509                                  ly_symbol2scm ("RevertProperty"),
2510                                  scm_car ($3),
2511                                  scm_cdr ($3));
2512         }
2513         | SET context_prop_spec '=' scalar {
2514                 if (SCM_UNBNDP ($2))
2515                         $$ = SCM_UNDEFINED;
2516                 else
2517                         $$ = scm_list_4 (scm_car ($2),
2518                                          ly_symbol2scm ("PropertySet"),
2519                                          scm_cadr ($2),
2520                                          $4);
2521         }
2522         | UNSET context_prop_spec {
2523                 if (SCM_UNBNDP ($2))
2524                         $$ = SCM_UNDEFINED;
2525                 else
2526                         $$ = scm_list_3 (scm_car ($2),
2527                                          ly_symbol2scm ("PropertyUnset"),
2528                                          scm_cadr ($2));
2529         }
2530         ;
2531
2532
2533 // This is all quite awkward for the sake of substantial backward
2534 // compatibility while at the same time allowing a more "natural" form
2535 // of specification not separating grob specification from grob
2536 // property path.  The purpose of this definition of
2537 // simple_revert_context is to allow the symbol list which specifies
2538 // grob and property to revert to be optionally be split into two
2539 // parts after the grob (which may be preceded by a context
2540 // specification, a case which we distinguish by checking whether the
2541 // first symbol is a valid grob symbol instead).
2542 //
2543 // See revert_arg above for the main work horse of this arrangement.
2544 // simple_revert_context just caters for the context and delegates the
2545 // rest of the job to revert_arg.
2546
2547 simple_revert_context:
2548         symbol_list_part
2549         {
2550                 $1 = scm_reverse_x ($1, SCM_EOL);
2551                 if (scm_is_null ($1)
2552                     || to_boolean
2553                     (scm_object_property (scm_car ($1),
2554                                           ly_symbol2scm ("is-grob?")))) {
2555                         $$ = ly_symbol2scm ("Bottom");
2556                         parser->lexer_->push_extra_token (@1, SCM_IDENTIFIER, $1);
2557                 } else {
2558                         $$ = scm_car ($1);
2559                         parser->lexer_->push_extra_token (@1, SCM_IDENTIFIER,
2560                                                           scm_cdr ($1));
2561                 }
2562         }
2563         ;
2564
2565 music_property_def:
2566         simple_music_property_def {
2567                 if (SCM_UNBNDP ($1))
2568                         $$ = MAKE_SYNTAX ("void-music", @1);
2569                 else
2570                         $$ = LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("property-operation"), scm_cons2 (parser->self_scm (), @$.smobbed_copy (), $1));
2571         }
2572         ;
2573
2574 string:
2575         STRING {
2576                 $$ = $1;
2577         }
2578         | full_markup
2579         ;
2580
2581 simple_string: STRING {
2582                 $$ = $1;
2583         }
2584         | embedded_scm_bare
2585         {
2586                 if (scm_is_string ($1)) {
2587                         $$ = $1;
2588                 } else {
2589                         parser->parser_error (@1, (_ ("simple string expected")));
2590                         $$ = scm_string (SCM_EOL);
2591                 }
2592         }
2593         ;
2594
2595 symbol:
2596         STRING {
2597                 $$ = scm_string_to_symbol ($1);
2598         }
2599         | embedded_scm_bare
2600         {
2601                 // This is a bit of overkill but makes the same
2602                 // routine responsible for all symbol interpretations.
2603                 $$ = try_string_variants (ly_lily_module_constant ("symbol?"),
2604                                           $1);
2605                 if (SCM_UNBNDP ($$))
2606                 {
2607                         parser->parser_error (@1, (_ ("symbol expected")));
2608                         // Generate a unique symbol in case it is used
2609                         // for an assignment or similar
2610                         $$ = scm_make_symbol (ly_string2scm ("undefined"));
2611                 }
2612         }
2613         ;
2614
2615 scalar:
2616         embedded_scm_arg
2617         | pitch_or_music
2618         | SCM_IDENTIFIER
2619         | bare_number
2620         // The following is a rather defensive variant of admitting
2621         // negative numbers: the grammar would permit number_factor or
2622         // even number_expression.  However, function arguments allow
2623         // only this simple kind of negative number, so to have things
2624         // like \tweak and \override behave reasonably similar, it
2625         // makes sense to rule out things like -- which are rather an
2626         // accent in function argument contexts.
2627         | '-' bare_number
2628         {
2629                 $$ = scm_difference ($2, SCM_UNDEFINED);
2630         }
2631         | string
2632         ;
2633
2634 event_chord:
2635         simple_element post_events {
2636                 // Let the rhythmic music iterator sort this mess out.
2637                 if (scm_is_pair ($2)) {
2638                         Music::unsmob ($$)->set_property ("articulations",
2639                                                          scm_reverse_x ($2, SCM_EOL));
2640                 }
2641         } %prec ':'
2642         | CHORD_REPETITION optional_notemode_duration post_events {
2643                 Input i;
2644                 i.set_location (@1, @3);
2645                 $$ = MAKE_SYNTAX ("repetition-chord", i,
2646                                   $2, scm_reverse_x ($3, SCM_EOL));
2647         } %prec ':'
2648         | MULTI_MEASURE_REST optional_notemode_duration post_events {
2649                 Input i;
2650                 i.set_location (@1, @3);
2651                 $$ = MAKE_SYNTAX ("multi-measure-rest", i, $2,
2652                                   scm_reverse_x ($3, SCM_EOL));
2653         } %prec ':'
2654         | tempo_event
2655         | note_chord_element
2656         ;
2657
2658
2659 note_chord_element:
2660         chord_body optional_notemode_duration post_events
2661         {
2662                 Music *m = Music::unsmob ($1);
2663                 SCM dur = Duration::unsmob ($2)->smobbed_copy ();
2664                 SCM es = m->get_property ("elements");
2665                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
2666
2667                 for (SCM s = es; scm_is_pair (s); s = scm_cdr (s))
2668                   Music::unsmob (scm_car (s))->set_property ("duration", dur);
2669                 es = ly_append2 (es, postevs);
2670
2671                 m-> set_property ("elements", es);
2672                 m->set_spot (@$);
2673                 $$ = m->self_scm ();
2674         } %prec ':'
2675         ;
2676
2677 chord_body:
2678         ANGLE_OPEN chord_body_elements ANGLE_CLOSE
2679         {
2680                 $$ = MAKE_SYNTAX ("event-chord", @$, scm_reverse_x ($2, SCM_EOL));
2681         }
2682         | FIGURE_OPEN figure_list FIGURE_CLOSE
2683         {
2684                 $$ = MAKE_SYNTAX ("event-chord", @$, scm_reverse_x ($2, SCM_EOL));
2685         }
2686         ;
2687
2688 chord_body_elements:
2689         /* empty */             { $$ = SCM_EOL; }
2690         | chord_body_elements chord_body_element {
2691                 if (!SCM_UNBNDP ($2))
2692                         $$ = scm_cons ($2, $1);
2693         }
2694         ;
2695
2696 chord_body_element:
2697         pitch_or_tonic_pitch exclamations questions octave_check post_events
2698         {
2699                 bool q = to_boolean ($3);
2700                 bool ex = to_boolean ($2);
2701                 SCM check = $4;
2702                 SCM post = $5;
2703
2704                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2705                 n->set_property ("pitch", $1);
2706                 if (q)
2707                         n->set_property ("cautionary", SCM_BOOL_T);
2708                 if (ex || q)
2709                         n->set_property ("force-accidental", SCM_BOOL_T);
2710
2711                 if (scm_is_pair (post)) {
2712                         SCM arts = scm_reverse_x (post, SCM_EOL);
2713                         n->set_property ("articulations", arts);
2714                 }
2715                 if (scm_is_number (check))
2716                 {
2717                         int q = scm_to_int (check);
2718                         n->set_property ("absolute-octave", scm_from_int (q-1));
2719                 }
2720
2721                 $$ = n->unprotect ();
2722         }
2723         | DRUM_PITCH post_events {
2724                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2725                 n->set_property ("drum-type", $1);
2726
2727                 if (scm_is_pair ($2)) {
2728                         SCM arts = scm_reverse_x ($2, SCM_EOL);
2729                         n->set_property ("articulations", arts);
2730                 }
2731                 $$ = n->unprotect ();
2732         }
2733         | music_function_chord_body
2734         {
2735                 Music *m = Music::unsmob ($1);
2736
2737                 while (m && m->is_mus_type ("music-wrapper-music")) {
2738                         $$ = m->get_property ("element");
2739                         m = Music::unsmob ($$);
2740                 }
2741
2742                 if (!(m && m->is_mus_type ("rhythmic-event"))) {
2743                         parser->parser_error (@$, _ ("not a rhythmic event"));
2744                         $$ = SCM_UNDEFINED;
2745                 }
2746         }
2747         ;
2748
2749 music_function_chord_body:
2750         music_function_call
2751         | MUSIC_IDENTIFIER
2752         | embedded_scm
2753         ;
2754
2755 event_function_event:
2756         EVENT_FUNCTION function_arglist {
2757                 $$ = MAKE_SYNTAX ("music-function", @$,
2758                                          $1, $2);
2759         }
2760         ;
2761
2762 post_events:
2763         /* empty */ {
2764                 $$ = SCM_EOL;
2765         }
2766         | post_events post_event {
2767                 $$ = $1;
2768                 if (Music *m = Music::unsmob ($2))
2769                 {
2770                         if (m->is_mus_type ("post-event-wrapper"))
2771                         {
2772                                 for (SCM p = m->get_property ("elements");
2773                                      scm_is_pair (p);
2774                                      p = scm_cdr (p))
2775                                 {
2776                                         $$ = scm_cons (scm_car (p), $$);
2777                                 }
2778                         } else {
2779                                 m->set_spot (@2);
2780                                 $$ = scm_cons ($2, $$);
2781                         }
2782                 }
2783         }
2784         ;
2785
2786 post_event_nofinger:
2787         direction_less_event {
2788                 $$ = $1;
2789         }
2790         | script_dir music_function_call {
2791                 $$ = $2;
2792                 if (!Music::unsmob ($2)->is_mus_type ("post-event")) {
2793                         parser->parser_error (@2, _ ("post-event expected"));
2794                         $$ = SCM_UNSPECIFIED;
2795                 } else if (!SCM_UNBNDP ($1))
2796                 {
2797                         Music::unsmob ($$)->set_property ("direction", $1);
2798                 }
2799         }
2800         | HYPHEN {
2801                 if (!parser->lexer_->is_lyric_state ())
2802                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2803                 $$ = MY_MAKE_MUSIC ("HyphenEvent", @$)->unprotect ();
2804         }
2805         | EXTENDER {
2806                 if (!parser->lexer_->is_lyric_state ())
2807                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2808                 $$ = MY_MAKE_MUSIC ("ExtenderEvent", @$)->unprotect ();
2809         }
2810         | script_dir direction_reqd_event {
2811                 if (!SCM_UNBNDP ($1))
2812                 {
2813                         Music *m = Music::unsmob ($2);
2814                         m->set_property ("direction", $1);
2815                 }
2816                 $$ = $2;
2817         }
2818         | script_dir direction_less_event {
2819                 if (!SCM_UNBNDP ($1))
2820                 {
2821                         Music *m = Music::unsmob ($2);
2822                         m->set_property ("direction", $1);
2823                 }
2824                 $$ = $2;
2825         }
2826         | '^' fingering
2827         {
2828                 $$ = $2;
2829                 Music::unsmob ($$)->set_property ("direction", scm_from_int (UP));
2830         }
2831         | '_' fingering
2832         {
2833                 $$ = $2;
2834                 Music::unsmob ($$)->set_property ("direction", scm_from_int (DOWN));
2835         }
2836         ;
2837
2838 post_event:
2839         post_event_nofinger
2840         | '-' fingering {
2841                 $$ = $2;
2842         }
2843         ;
2844
2845 string_number_event:
2846         E_UNSIGNED {
2847                 Music *s = MY_MAKE_MUSIC ("StringNumberEvent", @$);
2848                 s->set_property ("string-number", $1);
2849                 $$ = s->unprotect ();
2850         }
2851         ;
2852
2853 direction_less_event:
2854         string_number_event
2855         | EVENT_IDENTIFIER      {
2856                 $$ = $1;
2857         }
2858         | tremolo_type  {
2859                Music *a = MY_MAKE_MUSIC ("TremoloEvent", @$);
2860                a->set_property ("tremolo-type", $1);
2861                $$ = a->unprotect ();
2862         }
2863         | event_function_event
2864         ;
2865
2866 direction_reqd_event:
2867         gen_text_def {
2868                 $$ = $1;
2869         }
2870         | script_abbreviation {
2871                 SCM s = parser->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
2872                 if (scm_is_string (s)) {
2873                         Music *a = MY_MAKE_MUSIC ("ArticulationEvent", @$);
2874                         a->set_property ("articulation-type", s);
2875                         $$ = a->unprotect ();
2876                 } else {
2877                         Music *original = Music::unsmob (s);
2878                         if (original && original->is_mus_type ("post-event")) {
2879                                 Music *a = original->clone ();
2880                                 a->set_spot (parser->lexer_->override_input (@$));
2881                                 $$ = a->unprotect ();
2882                         } else {
2883                                 parser->parser_error (@1, _ ("expecting string or post-event as script definition"));
2884                                 $$ = MY_MAKE_MUSIC ("PostEvents", @$)->unprotect ();
2885                         }
2886                 }
2887         }
2888         ;
2889
2890 octave_check:
2891         /**/ { $$ = SCM_EOL; }
2892         | '=' quotes { $$ = $2; }
2893         ;
2894
2895 quotes:
2896         /* empty */
2897         {
2898                 $$ = SCM_INUM0;
2899         }
2900         | sub_quotes
2901         | sup_quotes
2902         ;
2903
2904 sup_quotes:
2905         '\'' {
2906                 $$ = scm_from_int (1);
2907         }
2908         | sup_quotes '\'' {
2909                 $$ = scm_oneplus ($1);
2910         }
2911         ;
2912
2913 sub_quotes:
2914         ',' {
2915                 $$ = scm_from_int (-1);
2916         }
2917         | sub_quotes ',' {
2918                 $$ = scm_oneminus ($1);
2919         }
2920         ;
2921
2922 steno_pitch:
2923         NOTENAME_PITCH quotes {
2924                 if (!scm_is_eq (SCM_INUM0, $2))
2925                 {
2926                         Pitch p = *Pitch::unsmob ($1);
2927                         p = p.transposed (Pitch (scm_to_int ($2), 0));
2928                         $$ = p.smobbed_copy ();
2929                 }
2930         }
2931         ;
2932
2933 /*
2934 ugh. duplication
2935 */
2936
2937 steno_tonic_pitch:
2938         TONICNAME_PITCH quotes {
2939                 if (!scm_is_eq (SCM_INUM0, $2))
2940                 {
2941                         Pitch p = *Pitch::unsmob ($1);
2942                         p = p.transposed (Pitch (scm_to_int ($2), 0));
2943                         $$ = p.smobbed_copy ();
2944                 }
2945         }
2946         ;
2947
2948 pitch:
2949         steno_pitch
2950         | PITCH_IDENTIFIER quotes {
2951                 if (!scm_is_eq (SCM_INUM0, $2))
2952                 {
2953                         Pitch p = *Pitch::unsmob ($1);
2954                         p = p.transposed (Pitch (scm_to_int ($2), 0));
2955                         $$ = p.smobbed_copy ();
2956                 }
2957         }
2958         ;
2959
2960 pitch_or_tonic_pitch:
2961         pitch
2962         | steno_tonic_pitch
2963         ;
2964
2965 gen_text_def:
2966         full_markup {
2967                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2968                 t->set_property ("text", $1);
2969                 $$ = t->unprotect ();
2970         }
2971         | STRING {
2972                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2973                 t->set_property ("text",
2974                         make_simple_markup ($1));
2975                 $$ = t->unprotect ();
2976         }
2977         | embedded_scm
2978         {
2979                 Music *m = Music::unsmob ($1);
2980                 if (m && m->is_mus_type ("post-event"))
2981                         $$ = $1;
2982                 else if (Text_interface::is_markup ($1)) {
2983                         Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2984                         t->set_property ("text", $1);
2985                         $$ = t->unprotect ();
2986                 } else
2987                         parser->parser_error (@1, _ ("not an articulation"));
2988         }
2989         ;
2990
2991 fingering:
2992         UNSIGNED {
2993                 Music *t = MY_MAKE_MUSIC ("FingeringEvent", @$);
2994                 t->set_property ("digit", $1);
2995                 $$ = t->unprotect ();
2996         }
2997         ;
2998
2999 script_abbreviation:
3000         '^'             {
3001                 $$ = scm_from_locale_string ("Hat");
3002         }
3003         | '+'           {
3004                 $$ = scm_from_locale_string ("Plus");
3005         }
3006         | '-'           {
3007                 $$ = scm_from_locale_string ("Dash");
3008         }
3009         | '!'           {
3010                 $$ = scm_from_locale_string ("Bang");
3011         }
3012         | ANGLE_CLOSE   {
3013                 $$ = scm_from_locale_string ("Larger");
3014         }
3015         | '.'           {
3016                 $$ = scm_from_locale_string ("Dot");
3017         }
3018         | '_' {
3019                 $$ = scm_from_locale_string ("Underscore");
3020         }
3021         ;
3022
3023 script_dir:
3024         '_'     { $$ = scm_from_int (DOWN); }
3025         | '^'   { $$ = scm_from_int (UP); }
3026         | '-'   { $$ = SCM_UNDEFINED; }
3027         ;
3028
3029 duration_length:
3030         multiplied_duration {
3031                 $$ = $1;
3032         }
3033         ;
3034
3035 maybe_notemode_duration:
3036         {
3037                 $$ = SCM_UNDEFINED;
3038         } %prec ':'
3039         | multiplied_duration   {
3040                 $$ = $1;
3041                 parser->default_duration_ = *Duration::unsmob ($$);
3042         }
3043 ;
3044
3045
3046 optional_notemode_duration:
3047         maybe_notemode_duration
3048         {
3049                 if (SCM_UNBNDP ($$))
3050                         $$ = parser->default_duration_.smobbed_copy ();
3051         }
3052         ;
3053
3054 steno_duration:
3055         UNSIGNED dots           {
3056                 $$ = make_duration ($1, scm_to_int ($2));
3057                 if (SCM_UNBNDP ($$))
3058                 {
3059                         parser->parser_error (@1, _ ("not a duration"));
3060                         $$ = Duration ().smobbed_copy ();
3061                 }
3062         }
3063         | DURATION_IDENTIFIER dots      {
3064                 Duration *d = Duration::unsmob ($1);
3065                 Duration k (d->duration_log (),
3066                             d->dot_count () + scm_to_int ($2));
3067                 k = k.compressed (d->factor ());
3068                 scm_remember_upto_here_1 ($1);
3069                 $$ = k.smobbed_copy ();
3070         }
3071         ;
3072
3073 multiplied_duration:
3074         steno_duration {
3075                 $$ = $1;
3076         }
3077         | multiplied_duration '*' UNSIGNED {
3078                 $$ = Duration::unsmob ($$)->compressed (scm_to_int ($3)).smobbed_copy ();
3079         }
3080         | multiplied_duration '*' FRACTION {
3081                 Rational  m (scm_to_int (scm_car ($3)), scm_to_int (scm_cdr ($3)));
3082
3083                 $$ = Duration::unsmob ($$)->compressed (m).smobbed_copy ();
3084         }
3085         ;
3086
3087 dots:
3088         /* empty */     {
3089                 $$ = SCM_INUM0;
3090         }
3091         | dots '.' {
3092                 $$ = scm_oneplus ($1);
3093         }
3094         ;
3095
3096 tremolo_type:
3097         ':'     {
3098                 $$ = scm_from_int (parser->default_tremolo_type_);
3099         }
3100         | ':' UNSIGNED {
3101                 if (SCM_UNBNDP (make_duration ($2))) {
3102                         parser->parser_error (@2, _ ("not a duration"));
3103                         $$ = scm_from_int (parser->default_tremolo_type_);
3104                 } else {
3105                         $$ = $2;
3106                         parser->default_tremolo_type_ = scm_to_int ($2);
3107                 }
3108         }
3109         ;
3110
3111 bass_number:
3112         UNSIGNED { $$ = $1; }
3113         | STRING { $$ = $1; }
3114         | full_markup { $$ = $1; }
3115         | embedded_scm_bare
3116         {
3117                 // as an integer, it needs to be non-negative, and otherwise
3118                 // it needs to be suitable as a markup.
3119                 if (scm_is_integer ($1)
3120                     ? scm_is_true (scm_negative_p ($1))
3121                     : !Text_interface::is_markup ($1))
3122                 {
3123                         parser->parser_error (@1, _ ("bass number expected"));
3124                         $$ = SCM_INUM0;
3125                 }
3126         }
3127         ;
3128
3129 figured_bass_alteration:
3130         '-'     { $$ = ly_rational2scm (FLAT_ALTERATION); }
3131         | '+'   { $$ = ly_rational2scm (SHARP_ALTERATION); }
3132         | '!'   { $$ = scm_from_int (0); }
3133         ;
3134
3135 bass_figure:
3136         FIGURE_SPACE {
3137                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
3138                 $$ = bfr->unprotect ();
3139         }
3140         | bass_number  {
3141                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
3142                 $$ = bfr->self_scm ();
3143
3144                 if (scm_is_number ($1))
3145                         bfr->set_property ("figure", $1);
3146                 else if (Text_interface::is_markup ($1))
3147                         bfr->set_property ("text", $1);
3148
3149                 bfr->unprotect ();
3150         }
3151         | bass_figure ']' {
3152                 $$ = $1;
3153                 Music::unsmob ($1)->set_property ("bracket-stop", SCM_BOOL_T);
3154         }
3155         | bass_figure figured_bass_alteration {
3156                 Music *m = Music::unsmob ($1);
3157                 if (scm_to_double ($2)) {
3158                         SCM salter = m->get_property ("alteration");
3159                         SCM alter = scm_is_number (salter) ? salter : scm_from_int (0);
3160                         m->set_property ("alteration",
3161                                          scm_sum (alter, $2));
3162                 } else {
3163                         m->set_property ("alteration", scm_from_int (0));
3164                 }
3165         }
3166         | bass_figure figured_bass_modification  {
3167                 Music *m = Music::unsmob ($1);
3168                 m->set_property ($2, SCM_BOOL_T);
3169         }
3170         ;
3171
3172
3173 figured_bass_modification:
3174         E_PLUS          {
3175                 $$ = ly_symbol2scm ("augmented");
3176         }
3177         | E_EXCLAMATION {
3178                 $$ = ly_symbol2scm ("no-continuation");
3179         }
3180         | '/'           {
3181                 $$ = ly_symbol2scm ("diminished");
3182         }
3183         | E_BACKSLASH {
3184                 $$ = ly_symbol2scm ("augmented-slash");
3185         }
3186         ;
3187
3188 br_bass_figure:
3189         bass_figure {
3190                 $$ = $1;
3191         }
3192         | '[' bass_figure {
3193                 $$ = $2;
3194                 Music::unsmob ($$)->set_property ("bracket-start", SCM_BOOL_T);
3195         }
3196         ;
3197
3198 figure_list:
3199         /**/            {
3200                 $$ = SCM_EOL;
3201         }
3202         | figure_list br_bass_figure {
3203                 $$ = scm_cons ($2, $1);
3204         }
3205         ;
3206
3207 optional_rest:
3208         /**/   { $$ = SCM_BOOL_F; }
3209         | REST { $$ = SCM_BOOL_T; }
3210         ;
3211
3212 pitch_or_music:
3213         pitch exclamations questions octave_check maybe_notemode_duration optional_rest post_events {
3214                 if (!parser->lexer_->is_note_state ())
3215                         parser->parser_error (@1, _ ("have to be in Note mode for notes"));
3216                 if (!SCM_UNBNDP ($2)
3217                     || !SCM_UNBNDP ($3)
3218                     || scm_is_number ($4)
3219                     || !SCM_UNBNDP ($5)
3220                     || scm_is_true ($6)
3221                     || scm_is_pair ($7))
3222                 {
3223                         Music *n = 0;
3224                         if (scm_is_true ($6))
3225                                 n = MY_MAKE_MUSIC ("RestEvent", @$);
3226                         else
3227                                 n = MY_MAKE_MUSIC ("NoteEvent", @$);
3228
3229                         n->set_property ("pitch", $1);
3230                         if (SCM_UNBNDP ($5))
3231                                 n->set_property ("duration",
3232                                                  parser->default_duration_.smobbed_copy ());
3233                         else
3234                                 n->set_property ("duration", $5);
3235
3236                         if (scm_is_number ($4))
3237                         {
3238                                 int q = scm_to_int ($4);
3239                                 n->set_property ("absolute-octave", scm_from_int (q-1));
3240                         }
3241
3242                         if (to_boolean ($3))
3243                                 n->set_property ("cautionary", SCM_BOOL_T);
3244                         if (to_boolean ($2) || to_boolean ($3))
3245                                 n->set_property ("force-accidental", SCM_BOOL_T);
3246                         if (scm_is_pair ($7))
3247                                 n->set_property ("articulations",
3248                                                  scm_reverse_x ($7, SCM_EOL));
3249                         $$ = n->unprotect ();
3250                 }
3251         } %prec ':'
3252         | new_chord post_events {
3253                 if (!parser->lexer_->is_chord_state ())
3254                         parser->parser_error (@1, _ ("have to be in Chord mode for chords"));
3255                 if (scm_is_pair ($2)) {
3256                         if (Pitch::unsmob ($1))
3257                                 $1 = make_chord_elements (@1,
3258                                                           $1,
3259                                                           parser->default_duration_.smobbed_copy (),
3260                                                           SCM_EOL);
3261
3262                         SCM elts = ly_append2 ($1, scm_reverse_x ($2, SCM_EOL));
3263
3264                         $$ = MAKE_SYNTAX ("event-chord", @1, elts);
3265                 } else if (!Pitch::unsmob ($1))
3266                         $$ = MAKE_SYNTAX ("event-chord", @1, $1);
3267                 // A mere pitch drops through.
3268         } %prec ':'
3269         ;
3270
3271 simple_element:
3272         DRUM_PITCH optional_notemode_duration {
3273                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
3274                 n->set_property ("duration", $2);
3275                 n->set_property ("drum-type", $1);
3276
3277                 $$ = n->unprotect ();
3278         }
3279         | RESTNAME optional_notemode_duration           {
3280                 Music *ev = 0;
3281                 if (ly_scm2string ($1) == "s") {
3282                         /* Space */
3283                         ev = MY_MAKE_MUSIC ("SkipEvent", @$);
3284                   }
3285                 else {
3286                         ev = MY_MAKE_MUSIC ("RestEvent", @$);
3287
3288                     }
3289                 ev->set_property ("duration", $2);
3290                 $$ = ev->unprotect ();
3291         }
3292         ;
3293
3294 lyric_element:
3295         full_markup {
3296                 if (!parser->lexer_->is_lyric_state ())
3297                         parser->parser_error (@1, _ ("markup outside of text script or \\lyricmode"));
3298                 $$ = $1;
3299         }
3300         | STRING {
3301                 if (!parser->lexer_->is_lyric_state ())
3302                         parser->parser_error (@1, _ ("unrecognized string, not in text script or \\lyricmode"));
3303                 $$ = $1;
3304         }
3305         | LYRIC_ELEMENT
3306         ;
3307
3308 lyric_element_music:
3309         lyric_element optional_notemode_duration post_events {
3310                 $$ = MAKE_SYNTAX ("lyric-event", @$, $1, $2);
3311                 if (scm_is_pair ($3))
3312                         Music::unsmob ($$)->set_property
3313                                 ("articulations", scm_reverse_x ($3, SCM_EOL));
3314         } %prec ':'
3315         ;
3316
3317 // Can return a single pitch rather than a list.
3318 new_chord:
3319         steno_tonic_pitch maybe_notemode_duration   {
3320                 if (SCM_UNBNDP ($2))
3321                         $$ = $1;
3322                 else
3323                         $$ = make_chord_elements (@$, $1, $2, SCM_EOL);
3324         }
3325         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
3326                 SCM its = scm_reverse_x ($4, SCM_EOL);
3327                 $$ = make_chord_elements (@$, $1, $2, scm_cons ($3, its));
3328         } %prec ':'
3329         ;
3330
3331 chord_items:
3332         /**/ {
3333                 $$ = SCM_EOL;
3334         }
3335         | chord_items chord_item {
3336                 $$ = scm_cons ($2, $$);
3337         }
3338         ;
3339
3340 chord_separator:
3341         CHORD_COLON {
3342                 $$ = ly_symbol2scm ("chord-colon");
3343         }
3344         | CHORD_CARET {
3345                 $$ = ly_symbol2scm ("chord-caret");
3346         }
3347         | CHORD_SLASH steno_tonic_pitch {
3348                 $$ = scm_list_2 (ly_symbol2scm ("chord-slash"), $2);
3349         }
3350         | CHORD_BASS steno_tonic_pitch {
3351                 $$ = scm_list_2 (ly_symbol2scm ("chord-bass"), $2);
3352         }
3353         ;
3354
3355 chord_item:
3356         chord_separator {
3357                 $$ = $1;
3358         }
3359         | step_numbers {
3360                 $$ = scm_reverse_x ($1, SCM_EOL);
3361         }
3362         | CHORD_MODIFIER  {
3363                 $$ = $1;
3364         }
3365         ;
3366
3367 step_numbers:
3368         step_number { $$ = scm_cons ($1, SCM_EOL); }
3369         | step_numbers '.' step_number {
3370                 $$ = scm_cons ($3, $$);
3371         }
3372         ;
3373
3374 step_number:
3375         UNSIGNED {
3376                 $$ = make_chord_step ($1, 0);
3377         }
3378         | UNSIGNED '+' {
3379                 $$ = make_chord_step ($1, SHARP_ALTERATION);
3380         }
3381         | UNSIGNED CHORD_MINUS {
3382                 $$ = make_chord_step ($1, FLAT_ALTERATION);
3383         }
3384         ;
3385
3386 tempo_range:
3387         unsigned_number {
3388                 $$ = $1;
3389         } %prec ':'
3390         | unsigned_number '-' unsigned_number {
3391                 $$ = scm_cons ($1, $3);
3392         }
3393         ;
3394
3395 /*
3396         UTILITIES
3397
3398 TODO: should deprecate in favor of Scheme?
3399
3400  */
3401 number_expression:
3402         number_expression '+' number_term {
3403                 $$ = scm_sum ($1, $3);
3404         }
3405         | number_expression '-' number_term {
3406                 $$ = scm_difference ($1, $3);
3407         }
3408         | number_term
3409         ;
3410
3411 number_term:
3412         number_factor {
3413                 $$ = $1;
3414         }
3415         | number_factor '*' number_factor {
3416                 $$ = scm_product ($1, $3);
3417         }
3418         | number_factor '/' number_factor {
3419                 $$ = scm_divide ($1, $3);
3420         }
3421         ;
3422
3423 number_factor:
3424         '-'  number_factor { /* %prec UNARY_MINUS */
3425                 $$ = scm_difference ($2, SCM_UNDEFINED);
3426         }
3427         | bare_number
3428         ;
3429
3430 bare_number_common:
3431         REAL
3432         | NUMBER_IDENTIFIER
3433         | REAL NUMBER_IDENTIFIER
3434         {
3435                 $$ = scm_product ($1, $2);
3436         }
3437         ;
3438
3439 bare_number:
3440         bare_number_common
3441         | UNSIGNED
3442         | UNSIGNED NUMBER_IDENTIFIER    {
3443                 $$ = scm_product ($1, $2);
3444         }
3445         ;
3446
3447 unsigned_number:
3448         UNSIGNED
3449         | NUMBER_IDENTIFIER
3450         {
3451                 if (!scm_is_integer ($1)
3452                     || scm_is_true (scm_negative_p ($1)))
3453                 {
3454                         parser->parser_error (@1, _("not an unsigned integer"));
3455                         $$ = SCM_INUM0;
3456                 }
3457         }
3458         | embedded_scm
3459         {
3460                 if (!scm_is_integer ($1)
3461                     || scm_is_true (scm_negative_p ($1)))
3462                 {
3463                         parser->parser_error (@1, _("not an unsigned integer"));
3464                         $$ = SCM_INUM0;
3465                 }
3466         }
3467         ;
3468
3469 exclamations:
3470                 { $$ = SCM_UNDEFINED; }
3471         | exclamations '!'
3472         {
3473                 if (SCM_UNBNDP ($1))
3474                         $$ = SCM_BOOL_T;
3475                 else
3476                         $$ = scm_not ($1);
3477         }
3478         ;
3479
3480 questions:
3481 // This precedence rule is rather weird.  It triggers when '!' is
3482 // encountered after a pitch, and is used for deciding whether to save
3483 // this instead for a figure modification.  This should not actually
3484 // occur in practice as pitches and figures are generated in different
3485 // modes.  Using a greedy (%right) precedence makes sure that we don't
3486 // get stuck in a wrong interpretation.
3487         { $$ = SCM_UNDEFINED; } %prec ':'
3488         | questions '?'
3489         {
3490                 if (SCM_UNBNDP ($1))
3491                         $$ = SCM_BOOL_T;
3492                 else
3493                         $$ = scm_not ($1);
3494         }
3495         ;
3496
3497 full_markup_list:
3498         MARKUPLIST
3499                 { parser->lexer_->push_markup_state (); }
3500         markup_list {
3501                 $$ = $3;
3502                 parser->lexer_->pop_state ();
3503         }
3504         ;
3505
3506 full_markup:
3507         MARKUP
3508                 { parser->lexer_->push_markup_state (); }
3509         markup_top {
3510                 $$ = $3;
3511                 parser->lexer_->pop_state ();
3512         }
3513         ;
3514
3515 markup_top:
3516         markup_list {
3517                 $$ = scm_list_2 (ly_lily_module_constant ("line-markup"),  $1);
3518         }
3519         | markup_head_1_list simple_markup
3520         {
3521                 $$ = scm_car (MAKE_SYNTAX ("composed-markup-list",
3522                                            @2, $1, scm_list_1 ($2)));
3523         }
3524         | simple_markup {
3525                 $$ = $1;
3526         }
3527         ;
3528
3529 markup_scm:
3530         embedded_scm_bare
3531         {
3532                 if (Text_interface::is_markup ($1))
3533                         MYBACKUP (MARKUP_IDENTIFIER, $1, @1);
3534                 else if (Text_interface::is_markup_list ($1))
3535                         MYBACKUP (MARKUPLIST_IDENTIFIER, $1, @1);
3536                 else {
3537                         parser->parser_error (@1, _ ("not a markup"));
3538                         MYBACKUP (MARKUP_IDENTIFIER, scm_string (SCM_EOL), @1);
3539                 }
3540         } BACKUP
3541         ;
3542
3543
3544 markup_list:
3545         markup_composed_list {
3546                 $$ = $1;
3547         }
3548         | markup_uncomposed_list
3549         ;
3550
3551 markup_uncomposed_list:
3552         markup_braced_list {
3553                 $$ = $1;
3554         }
3555         | markup_command_list {
3556                 $$ = scm_list_1 ($1);
3557         }
3558         | markup_scm MARKUPLIST_IDENTIFIER
3559         {
3560                 $$ = $2;
3561         }
3562         | SCORELINES {
3563                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3564                 parser->lexer_->push_note_state (nn);
3565         } '{' score_body '}' {
3566                 Score *sc = Score::unsmob ($4);
3567                 sc->origin ()->set_spot (@$);
3568                 if (sc->defs_.empty ()) {
3569                         Output_def *od = get_layout (parser);
3570                         sc->add_output_def (od);
3571                         od->unprotect ();
3572                 }
3573                 $$ = scm_list_1 (scm_list_2 (ly_lily_module_constant ("score-lines-markup-list"), $4));
3574                 parser->lexer_->pop_state ();
3575         }
3576         ;
3577
3578 markup_composed_list:
3579         markup_head_1_list markup_uncomposed_list {
3580                 $$ = MAKE_SYNTAX ("composed-markup-list",
3581                                   @2, $1, $2);
3582         }
3583         ;
3584
3585 markup_braced_list:
3586         '{' markup_braced_list_body '}' {
3587                 $$ = scm_reverse_x ($2, SCM_EOL);
3588         }
3589         ;
3590
3591 markup_braced_list_body:
3592         /* empty */     {  $$ = SCM_EOL; }
3593         | markup_braced_list_body markup {
3594                 $$ = scm_cons ($2, $1);
3595         }
3596         | markup_braced_list_body markup_list {
3597                 $$ = scm_reverse_x ($2, $1);
3598         }
3599         ;
3600
3601 markup_command_list:
3602         MARKUP_LIST_FUNCTION markup_command_list_arguments {
3603           $$ = scm_cons ($1, scm_reverse_x($2, SCM_EOL));
3604         }
3605         ;
3606
3607 markup_command_basic_arguments:
3608         EXPECT_MARKUP_LIST markup_command_list_arguments markup_list {
3609           $$ = scm_cons ($3, $2);
3610         }
3611         | EXPECT_SCM markup_command_list_arguments embedded_scm {
3612           $$ = check_scheme_arg (parser, @3, $3, $2, $1);
3613         }
3614         | EXPECT_NO_MORE_ARGS {
3615           $$ = SCM_EOL;
3616         }
3617         ;
3618
3619 markup_command_list_arguments:
3620         markup_command_basic_arguments { $$ = $1; }
3621         | EXPECT_MARKUP markup_command_list_arguments markup {
3622           $$ = scm_cons ($3, $2);
3623         }
3624         ;
3625
3626 markup_head_1_item:
3627         MARKUP_FUNCTION EXPECT_MARKUP markup_command_list_arguments {
3628           $$ = scm_cons ($1, scm_reverse_x ($3, SCM_EOL));
3629         }
3630         ;
3631
3632 markup_head_1_list:
3633         markup_head_1_item      {
3634                 $$ = scm_list_1 ($1);
3635         }
3636         | markup_head_1_list markup_head_1_item {
3637                 $$ = scm_cons ($2, $1);
3638         }
3639         ;
3640
3641 simple_markup:
3642         STRING {
3643                 $$ = make_simple_markup ($1);
3644         }
3645         | SCORE {
3646                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3647                 parser->lexer_->push_note_state (nn);
3648         } '{' score_body '}' {
3649                 Score *sc = Score::unsmob ($4);
3650                 sc->origin ()->set_spot (@$);
3651                 if (sc->defs_.empty ()) {
3652                         Output_def *od = get_layout (parser);
3653                         sc->add_output_def (od);
3654                         od->unprotect ();
3655                 }
3656                 $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), $4);
3657                 parser->lexer_->pop_state ();
3658         }
3659         | MARKUP_FUNCTION markup_command_basic_arguments {
3660                 $$ = scm_cons ($1, scm_reverse_x ($2, SCM_EOL));
3661         }
3662         | markup_scm MARKUP_IDENTIFIER
3663         {
3664                 $$ = $2;
3665         }
3666         ;
3667
3668 markup:
3669         markup_head_1_list simple_markup
3670         {
3671                 $$ = scm_car (MAKE_SYNTAX ("composed-markup-list",
3672                                            @2, $1, scm_list_1 ($2)));
3673         }
3674         | simple_markup {
3675                 $$ = $1;
3676         }
3677         ;
3678
3679 %%
3680
3681 void
3682 Lily_parser::set_yydebug (bool x)
3683 {
3684         yydebug = x;
3685 }
3686
3687 SCM
3688 Lily_parser::do_yyparse ()
3689 {
3690         SCM retval = SCM_UNDEFINED;
3691         yyparse (this, &retval);
3692         return retval;
3693 }
3694
3695
3696
3697
3698
3699 /*
3700
3701 It is a little strange to have this function in this file, but
3702 otherwise, we have to import music classes into the lexer.
3703
3704 */
3705 int
3706 Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
3707 {
3708         if (Book::unsmob (sid)) {
3709                 Book *book =  Book::unsmob (sid)->clone ();
3710                 *destination = book->self_scm ();
3711                 book->unprotect ();
3712
3713                 return BOOK_IDENTIFIER;
3714         } else if (scm_is_number (sid)) {
3715                 *destination = sid;
3716                 return NUMBER_IDENTIFIER;
3717         } else if (Context_def::unsmob (sid))
3718         {
3719                 *destination = Context_def::unsmob (sid)->clone ()->unprotect ();
3720                 return SCM_IDENTIFIER;
3721         } else if (Context_mod::unsmob (sid)) {
3722                 *destination = Context_mod::unsmob (sid)->smobbed_copy ();
3723                 return CONTEXT_MOD_IDENTIFIER;
3724         } else if (Music *mus = Music::unsmob (sid)) {
3725                 mus = mus->clone ();
3726                 *destination = mus->self_scm ();
3727                 bool is_event = mus->is_mus_type ("post-event");
3728                 mus->unprotect ();
3729                 return is_event ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
3730         } else if (Pitch::unsmob (sid)) {
3731                 *destination = Pitch::unsmob (sid)->smobbed_copy ();
3732                 return PITCH_IDENTIFIER;
3733         } else if (Duration::unsmob (sid)) {
3734                 *destination = Duration::unsmob (sid)->smobbed_copy ();
3735                 return DURATION_IDENTIFIER;
3736         } else if (Output_def::unsmob (sid)) {
3737                 *destination = Output_def::unsmob (sid)->clone ()->unprotect ();
3738                 return SCM_IDENTIFIER;
3739         } else if (Score::unsmob (sid)) {
3740                 *destination = Score::unsmob (sid)->clone ()->unprotect ();
3741                 return SCM_IDENTIFIER;
3742         }
3743
3744         return -1;
3745 }
3746
3747 SCM
3748 get_next_unique_context_id ()
3749 {
3750         return scm_from_locale_string ("$uniqueContextId");
3751 }
3752
3753
3754 SCM
3755 get_next_unique_lyrics_context_id ()
3756 {
3757         static int new_context_count;
3758         char s[128];
3759         snprintf (s, sizeof (s)-1, "uniqueContext%d", new_context_count++);
3760         return scm_from_locale_string (s);
3761 }
3762
3763 // check_scheme_arg checks one argument with a given predicate for use
3764 // in an argument list and throws a syntax error if it is unusable.
3765 // The argument is prepended to the argument list in any case.  After
3766 // throwing a syntax error, the argument list is terminated with #f as
3767 // its last cdr in order to mark it as uncallable while not losing
3768 // track of its total length.
3769 //
3770 // There are a few special considerations: if optional argument disp
3771 // is given (otherwise it defaults to SCM_UNDEFINED), it will be used
3772 // instead of arg in a prospective error message.  This is useful if
3773 // arg is not the actual argument but rather a transformation of it.
3774 //
3775 // If arg itself is SCM_UNDEFINED, the predicate is considered false
3776 // and an error message using disp is produced unconditionally.
3777
3778 SCM check_scheme_arg (Lily_parser *parser, Input loc,
3779                       SCM arg, SCM args, SCM pred, SCM disp)
3780 {
3781         if (SCM_UNBNDP (arg))
3782                 args = scm_cons (disp, args);
3783         else {
3784                 args = scm_cons (arg, args);
3785                 if (scm_is_true (scm_call_1 (pred, arg)))
3786                         return args;
3787         }
3788         scm_set_cdr_x (scm_last_pair (args), SCM_EOL);
3789         MAKE_SYNTAX ("argument-error", loc, scm_length (args), pred,
3790                      SCM_UNBNDP (disp) ? arg : disp);
3791         scm_set_cdr_x (scm_last_pair (args), SCM_BOOL_F);
3792         return args;
3793 }
3794
3795 SCM loc_on_music (Input loc, SCM arg)
3796 {
3797         if (Music *m = Music::unsmob (arg))
3798         {
3799                 m = m->clone ();
3800                 m->set_spot (loc);
3801                 return m->unprotect ();
3802         }
3803         return arg;
3804 }
3805
3806 SCM
3807 try_string_variants (SCM pred, SCM str)
3808 {
3809         // a matching predicate is always ok
3810         if (scm_is_true (scm_call_1 (pred, str)))
3811                 return str;
3812         // a symbol may be interpreted as a list of symbols if it helps
3813         if (scm_is_symbol (str)) {
3814                 str = scm_list_1 (str);
3815                 if (scm_is_true (scm_call_1 (pred, str)))
3816                         return str;
3817                 return SCM_UNDEFINED;
3818         }
3819
3820         // If this cannot be a string representation of a symbol list,
3821         // we are through.
3822
3823         if (!is_regular_identifier (str, true))
3824                 return SCM_UNDEFINED;
3825
3826         str = scm_string_split (str, SCM_MAKE_CHAR ('.'));
3827         for (SCM p = str; scm_is_pair (p); p = scm_cdr (p))
3828                 scm_set_car_x (p, scm_string_to_symbol (scm_car (p)));
3829
3830         // Let's attempt the symbol list interpretation first.
3831
3832         if (scm_is_true (scm_call_1 (pred, str)))
3833                 return str;
3834
3835         // If there is just one symbol in the list, we might interpret
3836         // it as a single symbol
3837
3838         if (scm_is_null (scm_cdr (str)))
3839         {
3840                 str = scm_car (str);
3841                 if (scm_is_true (scm_call_1 (pred, str)))
3842                         return str;
3843         }
3844
3845         return SCM_UNDEFINED;
3846 }
3847
3848 bool
3849 is_regular_identifier (SCM id, bool multiple)
3850 {
3851   if (!scm_is_string (id))
3852           return false;
3853
3854   string str = ly_scm2string (id);
3855
3856   bool middle = false;
3857
3858   for (string::iterator it=str.begin(); it != str.end (); it++)
3859   {
3860           int c = *it & 0xff;
3861           if ((c >= 'a' && c <= 'z')
3862               || (c >= 'A' && c <= 'Z')
3863               || c > 0x7f)
3864                   middle = true;
3865           else if (middle && (c == '-' || c == '_' || (multiple && c == '.')))
3866                   middle = false;
3867           else
3868                   return false;
3869   }
3870   return middle;
3871 }
3872
3873 SCM
3874 make_music_from_simple (Lily_parser *parser, Input loc, SCM simple)
3875 {
3876         if (Music::unsmob (simple))
3877                 return simple;
3878         if (parser->lexer_->is_note_state ()) {
3879                 if (scm_is_symbol (simple)) {
3880                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3881                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3882                         n->set_property ("drum-type", simple);
3883                         return n->unprotect ();
3884                 }
3885                 if (Pitch::unsmob (simple)) {
3886                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3887                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3888                         n->set_property ("pitch", simple);
3889                         return n->unprotect ();
3890                 }
3891                 return simple;
3892         } else if (parser->lexer_->is_lyric_state ()) {
3893                 if (Text_interface::is_markup (simple))
3894                         return MAKE_SYNTAX ("lyric-event", loc, simple,
3895                                             parser->default_duration_.smobbed_copy ());
3896         } else if (parser->lexer_->is_chord_state ()) {
3897                 if (Pitch::unsmob (simple))
3898                         return MAKE_SYNTAX
3899                                 ("event-chord",
3900                                  loc,
3901                                  make_chord_elements (loc, simple,
3902                                                       parser->default_duration_.smobbed_copy (),
3903                                                       SCM_EOL));
3904         }
3905         return simple;
3906 }
3907
3908 Music *
3909 make_music_with_input (SCM name, Input where)
3910 {
3911        Music *m = make_music_by_name (name);
3912        m->set_spot (where);
3913        return m;
3914 }
3915
3916 SCM
3917 make_simple_markup (SCM a)
3918 {
3919         return a;
3920 }
3921
3922 SCM
3923 make_duration (SCM d, int dots)
3924 {
3925         int t = scm_to_int (d);
3926         if (t > 0 && (t & (t-1)) == 0)
3927                 return Duration (intlog2 (t), dots).smobbed_copy ();
3928         else
3929                 return SCM_UNDEFINED;
3930 }
3931
3932 SCM
3933 make_chord_step (SCM step_scm, Rational alter)
3934 {
3935         int step = scm_to_int (step_scm);
3936
3937         if (step == 7)
3938                 alter += FLAT_ALTERATION;
3939
3940         while (step < 0)
3941                 step += 7;
3942         Pitch m ((step -1) / 7, (step - 1) % 7, alter);
3943         return m.smobbed_copy ();
3944 }
3945
3946
3947 SCM
3948 make_chord_elements (Input loc, SCM pitch, SCM dur, SCM modification_list)
3949 {
3950         SCM chord_ctor = ly_lily_module_constant ("construct-chord-elements");
3951         SCM res = scm_call_3 (chord_ctor, pitch, dur, modification_list);
3952         for (SCM s = res; scm_is_pair (s); s = scm_cdr (s))
3953         {
3954                 Music::unsmob (scm_car (s))->set_spot (loc);
3955         }
3956         return res;
3957 }
3958
3959 int
3960 yylex (YYSTYPE *s, YYLTYPE *loc, Lily_parser *parser)
3961 {
3962         Lily_lexer *lex = parser->lexer_;
3963
3964         lex->lexval_ = s;
3965         lex->lexloc_ = loc;
3966         int tok = lex->pop_extra_token ();
3967         if (tok >= 0)
3968                 return tok;
3969         lex->prepare_for_next_token ();
3970         return lex->yylex ();
3971 }