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