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