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