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