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