]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
Merge branch 'translation' into staging
[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         | context_def_spec_block
951         ;
952
953 output_def_body:
954         output_def_head_with_mode_switch '{' {
955                 $$ = $1;
956                 unsmob_output_def ($$)->input_origin_.set_spot (@$);
957         }
958         | output_def_head_with_mode_switch '{' OUTPUT_DEF_IDENTIFIER    {
959                 Output_def *o = unsmob_output_def ($3);
960                 o->input_origin_.set_spot (@$);
961                 $$ = o->self_scm ();
962                 parser->lexer_->remove_scope ();
963                 parser->lexer_->add_scope (o->scope_);
964         }
965         | output_def_body assignment  {
966
967         }
968         | output_def_body embedded_scm  {
969
970         }
971         | output_def_body
972         {
973                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
974                 parser->lexer_->push_note_state (nn);
975         } music_or_context_def
976         {
977                 parser->lexer_->pop_state ();
978                 if (unsmob_context_def ($3))
979                         assign_context_def (unsmob_output_def ($1), $3);
980                 else {
981
982                         SCM proc = parser->lexer_->lookup_identifier
983                                      ("output-def-music-handler");
984                         scm_call_3 (proc, parser->self_scm (),
985                                     $1, $3);
986                 }
987         }
988         | output_def_body error {
989
990         }
991         ;
992
993 tempo_event:
994         TEMPO steno_duration '=' tempo_range    {
995                 $$ = MAKE_SYNTAX ("tempo", @$, SCM_EOL, $2, $4);
996         }
997         | TEMPO scalar_closed steno_duration '=' tempo_range    {
998                 $$ = MAKE_SYNTAX ("tempo", @$, $2, $3, $5);
999         }
1000         | TEMPO scalar {
1001                 $$ = MAKE_SYNTAX ("tempo", @$, $2);
1002         }
1003         ;
1004
1005 /*
1006 The representation of a  list is reversed to have efficient append.  */
1007
1008 music_list:
1009         /* empty */ {
1010                 $$ = SCM_EOL;
1011         }
1012         | music_list music_embedded {
1013                 if (unsmob_music ($2))
1014                         $$ = scm_cons ($2, $1);
1015         }
1016         | music_list error {
1017                 Music *m = MY_MAKE_MUSIC("Music", @$);
1018                 // ugh. code dup
1019                 m->set_property ("error-found", SCM_BOOL_T);
1020                 $$ = scm_cons (m->self_scm (), $1);
1021                 m->unprotect (); /* UGH */
1022         }
1023         ;
1024
1025 braced_music_list:
1026         '{' music_list '}'
1027         {
1028                 $$ = scm_reverse_x ($2, SCM_EOL);
1029         }
1030         ;
1031
1032 music:  music_arg
1033         | lyric_element_music
1034         ;
1035
1036 music_embedded:
1037         music
1038         {
1039                 if (unsmob_music ($1)->is_mus_type ("post-event")) {
1040                         parser->parser_error (@1, _ ("unexpected post-event"));
1041                         $$ = SCM_UNSPECIFIED;
1042                 }
1043         }
1044         | music_embedded_backup
1045         {
1046                 $$ = $1;
1047         }
1048         | music_embedded_backup BACKUP lyric_element_music
1049         {
1050                 $$ = $3;
1051         }
1052         ;
1053
1054 music_embedded_backup:
1055         embedded_scm
1056         {
1057                 if (scm_is_eq ($1, SCM_UNSPECIFIED))
1058                         $$ = $1;
1059                 else if (Music *m = unsmob_music ($1)) {
1060                         if (m->is_mus_type ("post-event")) {
1061                                 parser->parser_error
1062                                         (@1, _ ("unexpected post-event"));
1063                                 $$ = SCM_UNSPECIFIED;
1064                         } else
1065                                 $$ = $1;
1066                 } else if (parser->lexer_->is_lyric_state ()
1067                            && Text_interface::is_markup ($1))
1068                         MYBACKUP (LYRIC_ELEMENT, $1, @1);
1069                 else {
1070                         @$.warning (_ ("Ignoring non-music expression"));
1071                         $$ = $1;
1072                 }
1073         }
1074         ;
1075
1076 music_arg:
1077         simple_music
1078         {
1079                 $$ = make_music_from_simple (parser, @1, $1);
1080                 if (!unsmob_music ($$))
1081                         parser->parser_error (@1, _ ("music expected"));
1082         }
1083         | composite_music %prec COMPOSITE
1084         ;
1085
1086 music_assign:
1087         simple_music
1088         | composite_music %prec COMPOSITE
1089         ;
1090
1091 repeated_music:
1092         REPEAT simple_string unsigned_number music
1093         {
1094                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, SCM_EOL);
1095         }
1096         | REPEAT simple_string unsigned_number music ALTERNATIVE braced_music_list
1097         {
1098                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, $6);
1099         }
1100         ;
1101
1102 sequential_music:
1103         SEQUENTIAL braced_music_list {
1104                 $$ = MAKE_SYNTAX ("sequential-music", @$, $2);
1105         }
1106         | braced_music_list {
1107                 $$ = MAKE_SYNTAX ("sequential-music", @$, $1);
1108         }
1109         ;
1110
1111 simultaneous_music:
1112         SIMULTANEOUS braced_music_list {
1113                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, $2);
1114         }
1115         | DOUBLE_ANGLE_OPEN music_list DOUBLE_ANGLE_CLOSE       {
1116                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, scm_reverse_x ($2, SCM_EOL));
1117         }
1118         ;
1119
1120 simple_music:
1121         event_chord
1122         | music_property_def
1123         | context_change
1124         ;
1125
1126 context_modification:
1127         WITH
1128         {
1129                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
1130                 parser->lexer_->push_note_state (nn);
1131         } '{' context_mod_list '}'
1132         {
1133                 parser->lexer_->pop_state ();
1134                 $$ = $4;
1135         }
1136         | WITH CONTEXT_MOD_IDENTIFIER
1137         {
1138                 $$ = $2;
1139         }
1140         | CONTEXT_MOD_IDENTIFIER
1141         {
1142                 $$ = $1;
1143         }
1144         | WITH context_modification_arg
1145         {
1146                 if (unsmob_music ($2)) {
1147                         SCM proc = parser->lexer_->lookup_identifier ("context-mod-music-handler");
1148                         $2 = scm_call_2 (proc, parser->self_scm (), $2);
1149                 }
1150                 if (unsmob_context_mod ($2))
1151                         $$ = $2;
1152                 else {
1153                         parser->parser_error (@2, _ ("not a context mod"));
1154                         $$ = Context_mod ().smobbed_copy ();
1155                 }
1156         }
1157         ;
1158
1159 context_modification_arg:
1160         embedded_scm_closed
1161         | MUSIC_IDENTIFIER
1162         ;
1163
1164 optional_context_mod:
1165         /**/ {
1166             $$ = SCM_EOL;
1167         }
1168         | context_modification
1169         {
1170               $$ = $1;
1171         }
1172         ;
1173
1174 context_mod_list:
1175         /**/ {
1176             $$ = Context_mod ().smobbed_copy ();
1177         }
1178         | context_mod_list context_mod  {
1179                 if (!SCM_UNBNDP ($2))
1180                         unsmob_context_mod ($1)->add_context_mod ($2);
1181         }
1182         | context_mod_list CONTEXT_MOD_IDENTIFIER {
1183                  Context_mod *md = unsmob_context_mod ($2);
1184                  if (md)
1185                      unsmob_context_mod ($1)->add_context_mods (md->get_mods ());
1186         }
1187         | context_mod_list context_mod_embedded {
1188                 unsmob_context_mod ($1)->add_context_mods
1189                         (unsmob_context_mod ($2)->get_mods ());
1190         }
1191         ;
1192
1193 composite_music:
1194         complex_music
1195         | music_bare
1196         ;
1197
1198 /* Music that can be parsed without lookahead */
1199 closed_music:
1200         music_bare
1201         | complex_music_prefix closed_music
1202         {
1203                 $$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
1204         }
1205         | music_function_call_closed
1206         ;
1207
1208 music_bare:
1209         mode_changed_music
1210         | MUSIC_IDENTIFIER
1211         | grouped_music_list
1212         ;
1213
1214 grouped_music_list:
1215         simultaneous_music              { $$ = $1; }
1216         | sequential_music              { $$ = $1; }
1217         ;
1218
1219 /* An argument list. If a function \foo expects scm scm pitch, then the lexer expands \foo into the token sequence:
1220  MUSIC_FUNCTION EXPECT_PITCH EXPECT_SCM EXPECT_SCM EXPECT_NO_MORE_ARGS
1221 and this rule returns the reversed list of arguments. */
1222
1223 /* Function argument lists come in a number of flavors.  Whenever
1224  * LilyPond has to pick between different flavors, the decision is
1225  * either made because of tokens it has already seen, or it is
1226  * postponed until tokens suitable for making the decision come up.
1227  * For postponing decisions, it may be necessary that the competing
1228  * rules are written in a way making them compatible until a decision
1229  * can be made.  Sometimes this is done by putting common traits into
1230  * a separate "common" rule set.
1231  *
1232  * function_arglist: a full argument list.  Optional arguments at the
1233  * end of the list can only be skipped by writing \default in their
1234  * place.
1235  *
1236  * function_arglist_backup: an argument list ending in an optional
1237  * argument that may be skipped depending on its predicate.
1238  *
1239  * function_arglist_skip: an argument list _not_ ending in an optional
1240  * argument that is actually taken.
1241  *
1242  * function_arglist_nonbackup: an argument list ending in an optional
1243  * argument that may not be skipped because it is in end position and
1244  * has not been shortcircuited with \default.
1245  *
1246  * function_arglist* / function_arglist_closed*: The closed variants
1247  * don't end in simple music expressions that might still accept
1248  * things like a duration or a postevent.
1249  */
1250
1251 function_arglist_skip:
1252         function_arglist_common
1253         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_skip
1254         {
1255                 $$ = scm_cons ($1, $3);
1256         } %prec FUNCTION_ARGLIST
1257         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_skip
1258         {
1259                 $$ = scm_cons ($1, $3);
1260         } %prec FUNCTION_ARGLIST
1261         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip
1262         {
1263                 $$ = scm_cons ($1, $3);
1264         } %prec FUNCTION_ARGLIST
1265         ;
1266
1267
1268 function_arglist_nonbackup_common:
1269         EXPECT_OPTIONAL EXPECT_PITCH function_arglist pitch_also_in_chords {
1270                 $$ = scm_cons ($4, $3);
1271         }
1272         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_closed duration_length {
1273                 $$ = scm_cons ($4, $3);
1274         }
1275         | EXPECT_OPTIONAL EXPECT_SCM function_arglist FRACTION
1276         {
1277                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1278         }
1279         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed post_event_nofinger
1280         {
1281                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1282         }
1283         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed '-' UNSIGNED
1284         {
1285                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1286                 if (scm_is_true (scm_call_1 ($2, n)))
1287                         $$ = scm_cons (n, $3);
1288                 else {
1289                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1290                         t->set_property ("digit", $5);
1291                         $$ = check_scheme_arg (parser, @4, t->unprotect (),
1292                                                $3, $2, n);
1293                 }
1294                 
1295         }
1296         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed '-' REAL
1297         {
1298                 $$ = check_scheme_arg (parser, @4,
1299                                        scm_difference ($5, SCM_UNDEFINED),
1300                                        $3, $2);
1301         }
1302         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed '-' NUMBER_IDENTIFIER
1303         {
1304                 $$ = check_scheme_arg (parser, @4,
1305                                        scm_difference ($5, SCM_UNDEFINED),
1306                                        $3, $2);
1307         }
1308         ;
1309
1310 function_arglist_closed_nonbackup:
1311         function_arglist_nonbackup_common
1312         | EXPECT_OPTIONAL EXPECT_SCM function_arglist embedded_scm_arg_closed
1313         {
1314                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1315         }
1316         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed bare_number_closed
1317         {
1318                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1319         }
1320         | EXPECT_OPTIONAL EXPECT_SCM function_arglist SCM_IDENTIFIER
1321         {
1322                 $$ = check_scheme_arg (parser, @4,
1323                                        try_string_variants ($2, $4),
1324                                        $3, $2, $4);
1325         }
1326         | EXPECT_OPTIONAL EXPECT_SCM function_arglist STRING
1327         {
1328                 $$ = check_scheme_arg (parser, @4,
1329                                        try_string_variants ($2, $4),
1330                                        $3, $2, $4);
1331         }
1332         | EXPECT_OPTIONAL EXPECT_SCM function_arglist full_markup
1333         {
1334                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1335         }
1336         ;
1337
1338 symbol_list_arg:
1339         SYMBOL_LIST
1340         | SYMBOL_LIST '.' symbol_list_rev
1341         {
1342                 $$ = scm_append (scm_list_2 ($1, scm_reverse_x ($3, SCM_EOL)));
1343         }
1344         ;
1345
1346 symbol_list_rev:
1347         symbol_list_part
1348         | symbol_list_rev '.' symbol_list_part
1349         {
1350                 $$ = scm_append_x (scm_list_2 ($3, $1));
1351         }
1352         ;
1353
1354 // symbol_list_part delivers elements in reverse copy.
1355
1356 symbol_list_part:
1357         symbol_list_element
1358         {
1359                 SCM sym_l_p = ly_lily_module_constant ("symbol-list?");
1360                 $$ = try_string_variants (sym_l_p, $1);
1361                 if (SCM_UNBNDP ($$)) {
1362                         parser->parser_error (@1, _("not a symbol"));
1363                         $$ = SCM_EOL;
1364                 } else
1365                         $$ = scm_reverse ($$);
1366         }
1367         ;
1368
1369
1370 symbol_list_element:
1371         STRING
1372         | embedded_scm_bare
1373         ;
1374
1375
1376 function_arglist_nonbackup:
1377         function_arglist_nonbackup_common
1378         | EXPECT_OPTIONAL EXPECT_SCM function_arglist embedded_scm_arg
1379         {
1380                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1381         }
1382         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed bare_number
1383         {
1384                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1385         }
1386         | function_arglist_nonbackup_reparse REPARSE SCM_ARG
1387         {
1388                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1389         }
1390         | function_arglist_nonbackup_reparse REPARSE lyric_element_music
1391         {
1392                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1393         }
1394         | function_arglist_nonbackup_reparse REPARSE symbol_list_arg
1395         {
1396                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1397         }
1398         ;
1399
1400 function_arglist_nonbackup_reparse:
1401         EXPECT_OPTIONAL EXPECT_SCM function_arglist SCM_IDENTIFIER
1402         {
1403                 $$ = $3;
1404                 SCM res = try_string_variants ($2, $4);
1405                 if (!SCM_UNBNDP (res))
1406                         if (scm_is_pair (res))
1407                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1408                         else
1409                                 MYREPARSE (@4, $2, SCM_ARG, res);
1410                 else if (scm_is_true
1411                          (scm_call_1
1412                           ($2, make_music_from_simple
1413                            (parser, @4, $4))))
1414                         MYREPARSE (@4, $2, STRING, $4);
1415                 else
1416                         MYREPARSE (@4, $2, SCM_ARG, $4);
1417         }
1418         | EXPECT_OPTIONAL EXPECT_SCM function_arglist STRING
1419         {
1420                 $$ = $3;
1421                 SCM res = try_string_variants ($2, $4);
1422                 if (!SCM_UNBNDP (res))
1423                         if (scm_is_pair (res))
1424                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1425                         else
1426                                 MYREPARSE (@4, $2, SCM_ARG, res);
1427                 else if (scm_is_true
1428                          (scm_call_1
1429                           ($2, make_music_from_simple
1430                            (parser, @4, $4))))
1431                         MYREPARSE (@4, $2, STRING, $4);
1432                 else
1433                         MYREPARSE (@4, $2, SCM_ARG, $4);
1434         }
1435         | EXPECT_OPTIONAL EXPECT_SCM function_arglist full_markup
1436         {
1437                 $$ = $3;
1438                 if (scm_is_true (scm_call_1 ($2, $4)))
1439                         MYREPARSE (@4, $2, SCM_ARG, $4);
1440                 else if (scm_is_true
1441                          (scm_call_1
1442                           ($2, make_music_from_simple
1443                            (parser, @4, $4))))
1444                         MYREPARSE (@4, $2, STRING, $4);
1445                 else
1446                         MYREPARSE (@4, $2, SCM_ARG, $4);
1447         }
1448         ;
1449
1450 function_arglist_keep:
1451         function_arglist_common
1452         | function_arglist_backup
1453         ;
1454
1455 function_arglist_closed_keep:
1456         function_arglist_closed_common
1457         | function_arglist_backup
1458         ;
1459
1460 function_arglist_backup:
1461         EXPECT_OPTIONAL EXPECT_SCM function_arglist_keep embedded_scm_arg_closed
1462         {
1463                 if (scm_is_true (scm_call_1 ($2, $4)))
1464                 {
1465                         $$ = scm_cons ($4, $3);
1466                 } else {
1467                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1468                         MYBACKUP (SCM_ARG, $4, @4);
1469                 }
1470         }
1471         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep post_event_nofinger
1472         {
1473                 if (scm_is_true (scm_call_1 ($2, $4)))
1474                 {
1475                         $$ = scm_cons ($4, $3);
1476                 } else {
1477                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1478                         MYBACKUP (EVENT_IDENTIFIER, $4, @4);
1479                 }
1480         }
1481         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_keep full_markup
1482         {
1483                 if (scm_is_true (scm_call_1 ($2, $4)))
1484                         $$ = scm_cons ($4, $3);
1485                 else {
1486                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1487                         MYBACKUP (LYRIC_ELEMENT, $4, @4);
1488                 }
1489         }
1490         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep UNSIGNED
1491         {
1492                 if (scm_is_true (scm_call_1 ($2, $4)))
1493                 {
1494                         $$ = $3;
1495                         MYREPARSE (@4, $2, UNSIGNED, $4);
1496                 } else {
1497                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1498                         MYBACKUP (UNSIGNED, $4, @4);
1499                 }
1500         }
1501         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep REAL
1502         {
1503                 if (scm_is_true (scm_call_1 ($2, $4)))
1504                 {
1505                         $$ = $3;
1506                         MYREPARSE (@4, $2, REAL, $4);
1507                 } else {
1508                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1509                         MYBACKUP (REAL, $4, @4);
1510                 }
1511         }
1512         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep NUMBER_IDENTIFIER
1513         {
1514                 if (scm_is_true (scm_call_1 ($2, $4)))
1515                 {
1516                         $$ = scm_cons ($4, $3);
1517                 } else {
1518                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1519                         MYBACKUP (NUMBER_IDENTIFIER, $4, @4);
1520                 }
1521         }
1522         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_keep FRACTION
1523         {
1524                 if (scm_is_true (scm_call_1 ($2, $4)))
1525                 {
1526                         $$ = scm_cons ($4, $3);
1527                 } else {
1528                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1529                         MYBACKUP (FRACTION, $4, @4);
1530                 }
1531         }
1532         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep '-' UNSIGNED
1533         {
1534                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1535                 if (scm_is_true (scm_call_1 ($2, n))) {
1536                         $$ = $3;
1537                         MYREPARSE (@5, $2, REAL, n);
1538                 } else {
1539                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1540                         t->set_property ("digit", $5);
1541                         $$ = t->unprotect ();
1542                         if (scm_is_true (scm_call_1 ($2, $$)))
1543                                 $$ = scm_cons ($$, $3);
1544                         else {
1545                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1546                                 MYBACKUP (UNSIGNED, $5, @5);
1547                                 parser->lexer_->push_extra_token ('-');
1548                         }
1549                 }
1550                 
1551         }
1552         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep '-' REAL
1553         {
1554                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1555                 if (scm_is_true (scm_call_1 ($2, n))) {
1556                         MYREPARSE (@5, $2, REAL, n);
1557                         $$ = $3;
1558                 } else {
1559                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1560                         MYBACKUP (REAL, n, @5);
1561                 }
1562         }
1563         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep '-' NUMBER_IDENTIFIER
1564         {
1565                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1566                 if (scm_is_true (scm_call_1 ($2, n))) {
1567                         $$ = scm_cons (n, $3);
1568                 } else {
1569                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1570                         MYBACKUP (NUMBER_IDENTIFIER, n, @5);
1571                 }
1572         }
1573         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_keep pitch_also_in_chords
1574         {
1575                 $$ = scm_cons ($4, $3);
1576         }
1577         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_closed_keep duration_length
1578         {
1579                 $$ = scm_cons ($4, $3);
1580         }
1581         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_keep SCM_IDENTIFIER
1582         {
1583                 SCM res = try_string_variants ($2, $4);
1584                 if (!SCM_UNBNDP (res))
1585                         if (scm_is_pair (res)) {
1586                                 $$ = $3;
1587                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1588                         }
1589                         else
1590                                 $$ = scm_cons (res, $3);
1591                 else {
1592                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1593                         MYBACKUP (SCM_IDENTIFIER, $4, @4);
1594                 }
1595         }
1596         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_keep STRING
1597         {
1598                 SCM res = try_string_variants ($2, $4);
1599                 if (!SCM_UNBNDP (res))
1600                         if (scm_is_pair (res)) {
1601                                 $$ = $3;
1602                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1603                         }
1604                         else
1605                                 $$ = scm_cons (res, $3);
1606                 else {
1607                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1608                         MYBACKUP (STRING, $4, @4);
1609                 }
1610         }
1611         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup BACKUP
1612         {
1613                 $$ = scm_cons ($1, $3);
1614                 MYBACKUP(0, SCM_UNDEFINED, @3);
1615         }
1616         | function_arglist_backup REPARSE bare_number
1617         {
1618                 $$ = check_scheme_arg (parser, @3,
1619                                        $3, $1, $2);
1620         }
1621         | function_arglist_backup REPARSE symbol_list_arg
1622         {
1623                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1624         }
1625         ;
1626
1627 function_arglist:
1628         function_arglist_common
1629         | function_arglist_nonbackup
1630         ;
1631
1632 function_arglist_common:
1633         function_arglist_bare
1634         | EXPECT_SCM function_arglist_optional embedded_scm_arg
1635         {
1636                 $$ = check_scheme_arg (parser, @3,
1637                                        $3, $2, $1);
1638         }
1639         | EXPECT_SCM function_arglist_closed_optional bare_number
1640         {
1641                 $$ = check_scheme_arg (parser, @3,
1642                                        $3, $2, $1);
1643         }
1644         | EXPECT_SCM function_arglist_optional FRACTION
1645         {
1646                 $$ = check_scheme_arg (parser, @3,
1647                                        $3, $2, $1);
1648         }
1649         | EXPECT_SCM function_arglist_closed_optional post_event_nofinger
1650         {
1651                 $$ = check_scheme_arg (parser, @3,
1652                                        $3, $2, $1);
1653         }
1654         | EXPECT_SCM function_arglist_closed_optional '-' NUMBER_IDENTIFIER
1655         {
1656                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1657                 $$ = check_scheme_arg (parser, @4, n, $2, $1);
1658         }
1659         | function_arglist_common_reparse REPARSE SCM_ARG
1660         {
1661                 $$ = check_scheme_arg (parser, @3,
1662                                        $3, $1, $2);
1663         }
1664         | function_arglist_common_reparse REPARSE lyric_element_music
1665         {
1666                 $$ = check_scheme_arg (parser, @3,
1667                                        $3, $1, $2);
1668         }
1669         | function_arglist_common_reparse REPARSE bare_number
1670         {
1671                 $$ = check_scheme_arg (parser, @3,
1672                                        $3, $1, $2);
1673         }
1674         | function_arglist_common_reparse REPARSE symbol_list_arg
1675         {
1676                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1677         }
1678         ;
1679
1680 function_arglist_common_reparse:
1681         EXPECT_SCM function_arglist_optional SCM_IDENTIFIER
1682         {
1683                 $$ = $2;
1684                 SCM res = try_string_variants ($1, $3);
1685                 if (!SCM_UNBNDP (res))
1686                         if (scm_is_pair (res))
1687                                 MYREPARSE (@3, $1, SYMBOL_LIST, res);
1688                         else
1689                                 MYREPARSE (@3, $1, SCM_ARG, res);
1690                 else if (scm_is_true
1691                          (scm_call_1
1692                           ($1, make_music_from_simple (parser, @3, $3))))
1693                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1694                 else
1695                         // This is going to flag a syntax error, we
1696                         // know the predicate to be false.
1697                         MYREPARSE (@3, $1, SCM_ARG, $3);
1698         }
1699         | EXPECT_SCM function_arglist_optional STRING
1700         {
1701                 $$ = $2;
1702                 SCM res = try_string_variants ($1, $3);
1703                 if (!SCM_UNBNDP (res))
1704                         if (scm_is_pair (res))
1705                                 MYREPARSE (@3, $1, SYMBOL_LIST, res);
1706                         else
1707                                 MYREPARSE (@3, $1, SCM_ARG, res);
1708                 else if (scm_is_true
1709                          (scm_call_1
1710                           ($1, make_music_from_simple (parser, @3, $3))))
1711                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1712                 else
1713                         // This is going to flag a syntax error, we
1714                         // know the predicate to be false.
1715                         MYREPARSE (@3, $1, SCM_ARG, $3);
1716         }
1717         | EXPECT_SCM function_arglist_optional full_markup
1718         {
1719                 $$ = $2;
1720                 if (scm_is_true (scm_call_1 ($1, $3)))
1721                         MYREPARSE (@3, $1, SCM_ARG, $3);
1722                 else if (scm_is_true
1723                          (scm_call_1
1724                           ($1, make_music_from_simple (parser, @3, $3))))
1725                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1726                 else
1727                         // This is going to flag a syntax error, we
1728                         // know the predicate to be false.
1729                         MYREPARSE (@3, $1, SCM_ARG, $3);
1730         }
1731         | EXPECT_SCM function_arglist_closed_optional '-' UNSIGNED
1732         {
1733                 $$ = $2;
1734                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1735                 if (scm_is_true (scm_call_1 ($1, n)))
1736                         MYREPARSE (@4, $1, REAL, n);
1737                 else {
1738                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @4);
1739                         t->set_property ("digit", $4);
1740                         SCM m = t->unprotect ();
1741                         if (scm_is_true (scm_call_1 ($1, m)))
1742                                 MYREPARSE (@4, $1, SCM_ARG, m);
1743                         else
1744                                 MYREPARSE (@4, $1, SCM_ARG, $4);
1745                 }
1746                 
1747         }
1748         | EXPECT_SCM function_arglist_closed_optional '-' REAL
1749         {
1750                 $$ = $2;
1751                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1752                 MYREPARSE (@4, $1, REAL, n);
1753         }
1754         ;
1755
1756 function_arglist_closed:
1757         function_arglist_closed_common
1758         | function_arglist_closed_nonbackup
1759         ;
1760
1761 function_arglist_closed_common:
1762         function_arglist_bare
1763         | EXPECT_SCM function_arglist_optional embedded_scm_arg_closed
1764         {
1765                 $$ = check_scheme_arg (parser, @3,
1766                                        $3, $2, $1);
1767         }
1768         | EXPECT_SCM function_arglist_closed_optional bare_number
1769         {
1770                 $$ = check_scheme_arg (parser, @3,
1771                                        $3, $2, $1);
1772         }
1773         | EXPECT_SCM function_arglist_closed_optional '-' NUMBER_IDENTIFIER
1774         {
1775                 $$ = check_scheme_arg (parser, @3,
1776                                        scm_difference ($4, SCM_UNDEFINED),
1777                                        $2, $1);
1778         }
1779         | EXPECT_SCM function_arglist_closed_optional post_event_nofinger
1780         {
1781                 $$ = check_scheme_arg (parser, @3,
1782                                        $3, $2, $1);
1783         }
1784         | EXPECT_SCM function_arglist_optional FRACTION
1785         {
1786                 $$ = check_scheme_arg (parser, @3,
1787                                        $3, $2, $1);
1788         }
1789         | function_arglist_common_reparse REPARSE SCM_ARG
1790         {
1791                 $$ = check_scheme_arg (parser, @3,
1792                                        $3, $1, $2);
1793         }
1794         | function_arglist_common_reparse REPARSE bare_number
1795         {
1796                 $$ = check_scheme_arg (parser, @3,
1797                                        $3, $1, $2);
1798         }
1799         | function_arglist_common_reparse REPARSE symbol_list_arg
1800         {
1801                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1802         }
1803         ;
1804
1805 function_arglist_optional:
1806         function_arglist_keep %prec FUNCTION_ARGLIST
1807         | function_arglist_backup BACKUP
1808         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_optional
1809         {
1810                 $$ = scm_cons ($1, $3);
1811         }
1812         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_optional
1813         {
1814                 $$ = scm_cons ($1, $3);
1815         }
1816         ;
1817
1818 function_arglist_closed_optional:
1819         function_arglist_closed_keep %prec FUNCTION_ARGLIST
1820         | function_arglist_backup BACKUP
1821         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_closed_optional
1822         {
1823                 $$ = scm_cons ($1, $3);
1824         }
1825         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_closed_optional
1826         {
1827                 $$ = scm_cons ($1, $3);
1828         }
1829         ;
1830
1831 embedded_scm_closed:
1832         embedded_scm_bare
1833         | scm_function_call_closed
1834         ;
1835
1836 embedded_scm_arg_closed:
1837         embedded_scm_bare_arg
1838         | scm_function_call_closed
1839         | closed_music
1840         ;
1841
1842 scm_function_call_closed:
1843         SCM_FUNCTION function_arglist_closed {
1844                 $$ = MAKE_SYNTAX ("music-function", @$,
1845                                          $1, $2);
1846         } %prec FUNCTION_ARGLIST
1847         ;
1848
1849 function_arglist_bare:
1850         EXPECT_NO_MORE_ARGS {
1851                 $$ = SCM_EOL;
1852         }
1853         | EXPECT_PITCH function_arglist_optional pitch_also_in_chords {
1854                 $$ = scm_cons ($3, $2);
1855         }
1856         | EXPECT_DURATION function_arglist_closed_optional duration_length {
1857                 $$ = scm_cons ($3, $2);
1858         }
1859         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_skip DEFAULT {
1860                 $$ = scm_cons ($1, $3);
1861         }
1862         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_skip DEFAULT {
1863                 $$ = scm_cons ($1, $3);
1864         }
1865         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip DEFAULT {
1866                 $$ = scm_cons ($1, $3);
1867         }
1868         ;
1869
1870 music_function_call:
1871         MUSIC_FUNCTION function_arglist {
1872                 $$ = MAKE_SYNTAX ("music-function", @$,
1873                                          $1, $2);
1874         }
1875         ;
1876
1877
1878 optional_id:
1879         /**/ { $$ = SCM_EOL; }
1880         | '=' simple_string {
1881                 $$ = $2;
1882         }
1883         ;
1884
1885 complex_music:
1886         music_function_call
1887         | repeated_music                { $$ = $1; }
1888         | re_rhythmed_music     { $$ = $1; }
1889         | complex_music_prefix music
1890         {
1891                 $$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
1892         }
1893         ;
1894
1895 complex_music_prefix:
1896         CONTEXT symbol optional_id optional_context_mod {
1897                 Context_mod *ctxmod = unsmob_context_mod ($4);
1898                 SCM mods = SCM_EOL;
1899                 if (ctxmod)
1900                         mods = ctxmod->get_mods ();
1901                 $$ = START_MAKE_SYNTAX ("context-specification", $2, $3, mods, SCM_BOOL_F);
1902         }
1903         | NEWCONTEXT symbol optional_id optional_context_mod {
1904                 Context_mod *ctxmod = unsmob_context_mod ($4);
1905                 SCM mods = SCM_EOL;
1906                 if (ctxmod)
1907                         mods = ctxmod->get_mods ();
1908                 $$ = START_MAKE_SYNTAX ("context-specification", $2, $3, mods, SCM_BOOL_T);
1909         }
1910         ;
1911
1912 mode_changed_music:
1913         mode_changing_head grouped_music_list {
1914                 if ($1 == ly_symbol2scm ("chords"))
1915                 {
1916                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $2);
1917                 }
1918                 else
1919                 {
1920                   $$ = $2;
1921                 }
1922                 parser->lexer_->pop_state ();
1923         }
1924         | mode_changing_head_with_context optional_context_mod grouped_music_list {
1925                 Context_mod *ctxmod = unsmob_context_mod ($2);
1926                 SCM mods = SCM_EOL;
1927                 if (ctxmod)
1928                         mods = ctxmod->get_mods ();
1929                 $$ = MAKE_SYNTAX ("context-specification", @$, $1, SCM_EOL, mods, SCM_BOOL_T, $3);
1930                 if ($1 == ly_symbol2scm ("ChordNames"))
1931                 {
1932                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $$);
1933                 }
1934                 parser->lexer_->pop_state ();
1935         }
1936         ;
1937
1938 mode_changing_head:
1939         NOTEMODE {
1940                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
1941                 parser->lexer_->push_note_state (nn);
1942
1943                 $$ = ly_symbol2scm ("notes");
1944         }
1945         | DRUMMODE
1946                 {
1947                 SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
1948                 parser->lexer_->push_note_state (nn);
1949
1950                 $$ = ly_symbol2scm ("drums");
1951         }
1952         | FIGUREMODE {
1953                 parser->lexer_->push_figuredbass_state ();
1954
1955                 $$ = ly_symbol2scm ("figures");
1956         }
1957         | CHORDMODE {
1958                 SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
1959                 parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
1960                 nn = parser->lexer_->lookup_identifier ("pitchnames");
1961                 parser->lexer_->push_chord_state (nn);
1962                 $$ = ly_symbol2scm ("chords");
1963
1964         }
1965         | LYRICMODE
1966                 { parser->lexer_->push_lyric_state ();
1967                 $$ = ly_symbol2scm ("lyrics");
1968         }
1969         ;
1970
1971 mode_changing_head_with_context:
1972         DRUMS {
1973                 SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
1974                 parser->lexer_->push_note_state (nn);
1975
1976                 $$ = ly_symbol2scm ("DrumStaff");
1977         }
1978         | FIGURES {
1979                 parser->lexer_->push_figuredbass_state ();
1980
1981                 $$ = ly_symbol2scm ("FiguredBass");
1982         }
1983         | CHORDS {
1984                 SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
1985                 parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
1986                 nn = parser->lexer_->lookup_identifier ("pitchnames");
1987                 parser->lexer_->push_chord_state (nn);
1988                 $$ = ly_symbol2scm ("ChordNames");
1989         }
1990         | LYRICS
1991                 { parser->lexer_->push_lyric_state ();
1992                 $$ = ly_symbol2scm ("Lyrics");
1993         }
1994         ;
1995
1996 new_lyrics:
1997         ADDLYRICS { parser->lexer_->push_lyric_state (); }
1998         /*cont */
1999         composite_music {
2000         /* Can also use music at the expensive of two S/Rs similar to
2001            \repeat \alternative */
2002                 parser->lexer_->pop_state ();
2003
2004                 $$ = scm_cons ($3, SCM_EOL);
2005         }
2006         | new_lyrics ADDLYRICS {
2007                 parser->lexer_->push_lyric_state ();
2008         } composite_music {
2009                 parser->lexer_->pop_state ();
2010                 $$ = scm_cons ($4, $1);
2011         }
2012         ;
2013
2014 re_rhythmed_music:
2015         composite_music new_lyrics {
2016                 $$ = MAKE_SYNTAX ("add-lyrics", @$, $1, scm_reverse_x ($2, SCM_EOL));
2017         } %prec COMPOSITE
2018         | LYRICSTO simple_string {
2019                 parser->lexer_->push_lyric_state ();
2020         } music {
2021                 parser->lexer_->pop_state ();
2022                 $$ = MAKE_SYNTAX ("lyric-combine", @$, $2, $4);
2023         }
2024         ;
2025
2026 context_change:
2027         CHANGE STRING '=' STRING  {
2028                 $$ = MAKE_SYNTAX ("context-change", @$, scm_string_to_symbol ($2), $4);
2029         }
2030         ;
2031
2032
2033 property_path:
2034         symbol_list_rev  {
2035                 $$ = scm_reverse_x ($1, SCM_EOL);
2036         }
2037         | symbol_list_rev property_path {
2038                 $$ = scm_reverse_x ($1, $2);
2039         }
2040         ;
2041
2042 property_operation:
2043         symbol '=' scalar {
2044                 $$ = scm_list_3 (ly_symbol2scm ("assign"), $1, $3);
2045         }
2046         | UNSET symbol {
2047                 $$ = scm_list_2 (ly_symbol2scm ("unset"), $2);
2048         }
2049         | OVERRIDE property_path '=' scalar {
2050                 if (scm_ilength ($2) < 2) {
2051                         parser->parser_error (@2, _("bad grob property path"));
2052                         $$ = SCM_UNDEFINED;
2053                 } else {
2054                         $$ = scm_cons (ly_symbol2scm ("push"),
2055                                        scm_cons2 (scm_car ($2),
2056                                                   $4,
2057                                                   scm_cdr ($2)));
2058                 }
2059         }
2060         | REVERT revert_arg {
2061                 $$ = scm_cons (ly_symbol2scm ("pop"), $2);
2062         }
2063         ;
2064
2065 // This is all quite awkward for the sake of substantial backward
2066 // compatibility while at the same time allowing a more "natural" form
2067 // of specification not separating grob specification from grob
2068 // property path.  The purpose of this definition of revert_arg is to
2069 // allow the symbol list which specifies grob and property to revert
2070 // to be optionally be split into two parts after the grob (which in
2071 // this case is just the first element of the list).  symbol_list_part
2072 // is only one path component, but it can be parsed without lookahead,
2073 // so we can follow it with a synthetic BACKUP token when needed.  If
2074 // the first symbol_list_part already contains multiple elements (only
2075 // possible if a Scheme expression provides them), we just parse for
2076 // additional elements introduced by '.', which is what the
2077 // SYMBOL_LIST backup in connection with the immediately following
2078 // rule using symbol_list_arg does.
2079 //
2080 // As long as we don't have our coffers filled with both grob and at
2081 // least one grob property specification, the rest of the required
2082 // symbol list chain may be provided either with or without a leading
2083 // dot.  This is for both allowing the traditional
2084 // \revert Accidental #'color
2085 // as well as well as the "naive" form
2086 // \revert Accidental.color
2087
2088 revert_arg:
2089         revert_arg_backup BACKUP symbol_list_arg
2090         {
2091                 $$ = $3;
2092         }
2093         ;
2094
2095 revert_arg_backup:
2096         revert_arg_part
2097         {
2098                 if (scm_is_null ($1)
2099                     || scm_is_null (scm_cdr ($1)))
2100                         MYBACKUP (SCM_ARG, $1, @1);
2101                 else
2102                         MYBACKUP (SYMBOL_LIST, scm_reverse_x ($1, SCM_EOL), @1);
2103         }
2104         ;
2105
2106 // revert_arg_part delivers results in reverse
2107 revert_arg_part:
2108         symbol_list_part
2109         | revert_arg_backup BACKUP SCM_ARG '.' symbol_list_part
2110         {
2111                 $$ = scm_append_x (scm_list_2 ($5, $3));
2112         }
2113         | revert_arg_backup BACKUP SCM_ARG symbol_list_part
2114         {
2115                 $$ = scm_append_x (scm_list_2 ($4, $3));
2116         }               
2117         ;
2118
2119 context_def_mod:
2120         CONSISTS { $$ = ly_symbol2scm ("consists"); }
2121         | REMOVE { $$ = ly_symbol2scm ("remove"); }
2122
2123         | ACCEPTS { $$ = ly_symbol2scm ("accepts"); }
2124         | DEFAULTCHILD { $$ = ly_symbol2scm ("default-child"); }
2125         | DENIES { $$ = ly_symbol2scm ("denies"); }
2126
2127         | ALIAS { $$ = ly_symbol2scm ("alias"); }
2128         | TYPE { $$ = ly_symbol2scm ("translator-type"); }
2129         | DESCRIPTION { $$ = ly_symbol2scm ("description"); }
2130         | NAME { $$ = ly_symbol2scm ("context-name"); }
2131         ;
2132
2133 context_mod:
2134         property_operation { $$ = $1; }
2135         | context_def_mod STRING {
2136                 $$ = scm_list_2 ($1, $2);
2137         }
2138         | context_def_mod embedded_scm
2139         {
2140                 if (!scm_is_string ($2)
2141                     && ly_symbol2scm ("consists") != $1
2142                     && ly_symbol2scm ("remove") != $1)
2143                 {
2144                         $$ = SCM_EOL;
2145                         parser->parser_error (@1, _ ("only \\consists and \\remove take non-string argument."));
2146                 }
2147                 else
2148                 {
2149                         $$ = scm_list_2 ($1, $2);
2150                 }
2151         }
2152         ;
2153
2154 // If defined, at least two members.
2155 grob_prop_spec:
2156         symbol_list_rev
2157         {
2158                 SCM l = scm_reverse_x ($1, SCM_EOL);
2159                 if (scm_is_pair (l)
2160                     && to_boolean
2161                     (scm_object_property (scm_car (l),
2162                                           ly_symbol2scm ("is-grob?"))))
2163                         l = scm_cons (ly_symbol2scm ("Bottom"), l);
2164                 if (scm_is_null (l) || scm_is_null (scm_cdr (l))) {
2165                         parser->parser_error (@1, _ ("bad grob property path"));
2166                         l = SCM_UNDEFINED;
2167                 }
2168                 $$ = l;
2169         }
2170         ;
2171
2172 // If defined, at least three members
2173 grob_prop_path:
2174         grob_prop_spec
2175         {
2176                 if (!SCM_UNBNDP ($1) && scm_is_null (scm_cddr ($1)))
2177                 {
2178                         parser->parser_error (@1, _ ("bad grob property path"));
2179                         $$ = SCM_UNDEFINED;
2180                 }
2181         }
2182         | grob_prop_spec property_path
2183         {
2184                 if (!SCM_UNBNDP ($1)) {
2185                         $$ = scm_append_x (scm_list_2 ($1, $2));
2186                         if (scm_is_null (scm_cddr ($$))) {
2187                                 parser->parser_error (@$, _ ("bad grob property path"));
2188                                 $$ = SCM_UNDEFINED;
2189                         }
2190                 }
2191
2192         }
2193         ;
2194
2195 // Exactly two elements or undefined
2196 context_prop_spec:
2197         symbol_list_rev
2198         {
2199                 SCM l = scm_reverse_x ($1, SCM_EOL);
2200                 switch (scm_ilength (l)) {
2201                 case 1:
2202                         l = scm_cons (ly_symbol2scm ("Bottom"), l);
2203                 case 2:
2204                         break;
2205                 default:
2206                         parser->parser_error (@1, _ ("bad context property path"));
2207                         l = SCM_UNDEFINED;
2208                 }
2209                 $$ = l;
2210         }
2211         ;
2212
2213 simple_music_property_def:
2214         OVERRIDE grob_prop_path '=' scalar {
2215                 if (SCM_UNBNDP ($2))
2216                         $$ = SCM_UNDEFINED;
2217                 else {
2218                         $$ = scm_list_5 (scm_car ($2),
2219                                          ly_symbol2scm ("OverrideProperty"),
2220                                          scm_cadr ($2),
2221                                          $4,
2222                                          scm_cddr ($2));
2223                 }
2224         }
2225         | REVERT simple_revert_context revert_arg {
2226                 $$ = scm_list_4 ($2,
2227                                  ly_symbol2scm ("RevertProperty"),
2228                                  scm_car ($3),
2229                                  scm_cdr ($3));
2230         }
2231         | SET context_prop_spec '=' scalar {
2232                 if (SCM_UNBNDP ($2))
2233                         $$ = SCM_UNDEFINED;
2234                 else
2235                         $$ = scm_list_4 (scm_car ($2),
2236                                          ly_symbol2scm ("PropertySet"),
2237                                          scm_cadr ($2),
2238                                          $4);
2239         }
2240         | UNSET context_prop_spec {
2241                 if (SCM_UNBNDP ($2))
2242                         $$ = SCM_UNDEFINED;
2243                 else
2244                         $$ = scm_list_3 (scm_car ($2),
2245                                          ly_symbol2scm ("PropertyUnset"),
2246                                          scm_cadr ($2));
2247         }
2248         ;
2249
2250
2251 // This is all quite awkward for the sake of substantial backward
2252 // compatibility while at the same time allowing a more "natural" form
2253 // of specification not separating grob specification from grob
2254 // property path.  The purpose of this definition of
2255 // simple_revert_context is to allow the symbol list which specifies
2256 // grob and property to revert to be optionally be split into two
2257 // parts after the grob (which may be preceded by a context
2258 // specification, a case which we distinguish by checking whether the
2259 // first symbol is a valid grob symbol instead).
2260 //
2261 // See revert_arg above for the main work horse of this arrangement.
2262 // simple_revert_context just caters for the context and delegates the
2263 // rest of the job to revert_arg.
2264
2265 simple_revert_context:
2266         symbol_list_part
2267         {
2268                 $1 = scm_reverse_x ($1, SCM_EOL);
2269                 if (scm_is_null ($1)
2270                     || to_boolean
2271                     (scm_object_property (scm_car ($1),
2272                                           ly_symbol2scm ("is-grob?")))) {
2273                         $$ = ly_symbol2scm ("Bottom");
2274                         parser->lexer_->push_extra_token (SCM_IDENTIFIER, $1);
2275                 } else {
2276                         $$ = scm_car ($1);
2277                         parser->lexer_->push_extra_token (SCM_IDENTIFIER,
2278                                                           scm_cdr ($1));
2279                 }
2280         }
2281         ;
2282
2283 music_property_def:
2284         simple_music_property_def {
2285                 if (SCM_UNBNDP ($1))
2286                         $$ = MAKE_SYNTAX ("void-music", @1);
2287                 else
2288                         $$ = LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("property-operation"), scm_cons2 (parser->self_scm (), make_input (@$), $1));
2289         }
2290         ;
2291
2292 string:
2293         STRING {
2294                 $$ = $1;
2295         }
2296         | full_markup
2297         ;
2298
2299 simple_string: STRING {
2300                 $$ = $1;
2301         }
2302         | embedded_scm_bare
2303         {
2304                 if (scm_is_string ($1)) {
2305                         $$ = $1;
2306                 } else {
2307                         parser->parser_error (@1, (_ ("simple string expected")));
2308                         $$ = scm_string (SCM_EOL);
2309                 }
2310         }
2311         ;
2312
2313 symbol:
2314         STRING {
2315                 $$ = scm_string_to_symbol ($1);
2316         }
2317         | embedded_scm_bare
2318         {
2319                 // This is a bit of overkill but makes the same
2320                 // routine responsible for all symbol interpretations.
2321                 $$ = try_string_variants (ly_lily_module_constant ("symbol?"),
2322                                           $1);
2323                 if (SCM_UNBNDP ($$))
2324                 {
2325                         parser->parser_error (@1, (_ ("symbol expected")));
2326                         // Generate a unique symbol in case it is used
2327                         // for an assignment or similar
2328                         $$ = scm_make_symbol (ly_string2scm ("undefined"));
2329                 }
2330         }
2331         ;
2332
2333 scalar:
2334         embedded_scm_arg
2335         | SCM_IDENTIFIER
2336         | bare_number
2337         // The following is a rather defensive variant of admitting
2338         // negative numbers: the grammar would permit number_factor or
2339         // even number_expression.  However, function arguments allow
2340         // only this simple kind of negative number, so to have things
2341         // like \tweak and \override behave reasonably similar, it
2342         // makes sense to rule out things like -- which are rather an
2343         // accent in function argument contexts.
2344         | '-' bare_number
2345         {
2346                 $$ = scm_difference ($2, SCM_UNDEFINED);
2347         }
2348         | FRACTION
2349         | STRING
2350         | full_markup
2351         ;
2352
2353 scalar_closed:
2354         embedded_scm_arg_closed
2355         | SCM_IDENTIFIER
2356         // for scalar_closed to be an actually closed (no lookahead)
2357         // expression, we'd need to use bare_number_closed here.  It
2358         // turns out that the only use of scalar_closed in TEMPO is
2359         // not of the kind requiring the full closedness criterion.
2360         | bare_number
2361         | '-' bare_number
2362         {
2363                 $$ = scm_difference ($2, SCM_UNDEFINED);
2364         }
2365         | FRACTION
2366         | STRING
2367         | full_markup
2368         ;
2369
2370
2371 event_chord:
2372         simple_element post_events {
2373                 // Let the rhythmic music iterator sort this mess out.
2374                 if (scm_is_pair ($2)) {
2375                         $$ = make_music_from_simple (parser, @1, $1);
2376                         if (unsmob_music ($$))
2377                                 unsmob_music ($$)->set_property ("articulations",
2378                                                                  scm_reverse_x ($2, SCM_EOL));
2379                         else
2380                                 parser->parser_error (@1, _("music expected"));
2381                 }
2382         }
2383         | simple_chord_elements post_events     {
2384                 SCM elts = ly_append2 ($1, scm_reverse_x ($2, SCM_EOL));
2385
2386                 Input i;
2387                 /* why is this giving wrong start location? -ns
2388                  * i = @$; */
2389                 i.set_location (@1, @2);
2390                 $$ = MAKE_SYNTAX ("event-chord", i, elts);
2391         }
2392         | CHORD_REPETITION optional_notemode_duration post_events {
2393                 Input i;
2394                 i.set_location (@1, @3);
2395                 $$ = MAKE_SYNTAX ("repetition-chord", i,
2396                                   $2, scm_reverse_x ($3, SCM_EOL));
2397         }
2398         | MULTI_MEASURE_REST optional_notemode_duration post_events {
2399                 Input i;
2400                 i.set_location (@1, @3);
2401                 $$ = MAKE_SYNTAX ("multi-measure-rest", i, $2,
2402                                   scm_reverse_x ($3, SCM_EOL));
2403         }
2404         | command_element
2405         | note_chord_element
2406         ;
2407
2408
2409 note_chord_element:
2410         chord_body optional_notemode_duration post_events
2411         {
2412                 Music *m = unsmob_music ($1);
2413                 SCM dur = unsmob_duration ($2)->smobbed_copy ();
2414                 SCM es = m->get_property ("elements");
2415                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
2416
2417                 for (SCM s = es; scm_is_pair (s); s = scm_cdr (s))
2418                   unsmob_music (scm_car (s))->set_property ("duration", dur);
2419                 es = ly_append2 (es, postevs);
2420
2421                 m-> set_property ("elements", es);
2422                 m->set_spot (@$);
2423                 $$ = m->self_scm ();
2424         }
2425         ;
2426
2427 chord_body:
2428         ANGLE_OPEN chord_body_elements ANGLE_CLOSE
2429         {
2430                 $$ = MAKE_SYNTAX ("event-chord", @$, scm_reverse_x ($2, SCM_EOL));
2431         }
2432         ;
2433
2434 chord_body_elements:
2435         /* empty */             { $$ = SCM_EOL; }
2436         | chord_body_elements chord_body_element {
2437                 if (!SCM_UNBNDP ($2))
2438                         $$ = scm_cons ($2, $1);
2439         }
2440         ;
2441
2442 chord_body_element:
2443         pitch exclamations questions octave_check post_events
2444         {
2445                 bool q = to_boolean ($3);
2446                 bool ex = to_boolean ($2);
2447                 SCM check = $4;
2448                 SCM post = $5;
2449
2450                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2451                 n->set_property ("pitch", $1);
2452                 if (q)
2453                         n->set_property ("cautionary", SCM_BOOL_T);
2454                 if (ex || q)
2455                         n->set_property ("force-accidental", SCM_BOOL_T);
2456
2457                 if (scm_is_pair (post)) {
2458                         SCM arts = scm_reverse_x (post, SCM_EOL);
2459                         n->set_property ("articulations", arts);
2460                 }
2461                 if (scm_is_number (check))
2462                 {
2463                         int q = scm_to_int (check);
2464                         n->set_property ("absolute-octave", scm_from_int (q-1));
2465                 }
2466
2467                 $$ = n->unprotect ();
2468         }
2469         | DRUM_PITCH post_events {
2470                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2471                 n->set_property ("drum-type", $1);
2472
2473                 if (scm_is_pair ($2)) {
2474                         SCM arts = scm_reverse_x ($2, SCM_EOL);
2475                         n->set_property ("articulations", arts);
2476                 }
2477                 $$ = n->unprotect ();
2478         }
2479         | music_function_chord_body
2480         {
2481                 Music *m = unsmob_music ($1);
2482
2483                 while (m && m->is_mus_type ("music-wrapper-music")) {
2484                         $$ = m->get_property ("element");
2485                         m = unsmob_music ($$);
2486                 }
2487
2488                 if (!(m && m->is_mus_type ("rhythmic-event"))) {
2489                         parser->parser_error (@$, _ ("not a rhythmic event"));
2490                         $$ = SCM_UNDEFINED;
2491                 }
2492         }
2493         ;
2494
2495 music_function_chord_body:
2496         music_function_call
2497         | MUSIC_IDENTIFIER
2498         ;
2499
2500 // Event functions may only take closed arglists, otherwise it would
2501 // not be clear whether a following postevent should be associated
2502 // with the last argument of the event function or with the expression
2503 // for which the function call acts itself as event.
2504
2505 music_function_call_closed:
2506         MUSIC_FUNCTION function_arglist_closed {
2507                 $$ = MAKE_SYNTAX ("music-function", @$,
2508                                          $1, $2);
2509         }
2510         ;
2511
2512 event_function_event:
2513         EVENT_FUNCTION function_arglist_closed {
2514                 $$ = MAKE_SYNTAX ("music-function", @$,
2515                                          $1, $2);
2516         }
2517         ;
2518
2519 command_element:
2520         command_event {
2521                 $$ = $1;
2522         }
2523         ;
2524
2525 command_event:
2526         tempo_event {
2527                 $$ = $1;
2528         }
2529         ;
2530
2531
2532 post_events:
2533         /* empty */ {
2534                 $$ = SCM_EOL;
2535         }
2536         | post_events post_event {
2537                 $$ = $1;
2538                 if (Music *m = unsmob_music ($2))
2539                 {
2540                         if (m->is_mus_type ("post-event-wrapper"))
2541                         {
2542                                 for (SCM p = m->get_property ("elements");
2543                                      scm_is_pair (p);
2544                                      p = scm_cdr (p))
2545                                 {
2546                                         $$ = scm_cons (scm_car (p), $$);
2547                                 }
2548                         } else {
2549                                 m->set_spot (@2);
2550                                 $$ = scm_cons ($2, $$);
2551                         }
2552                 }
2553         }
2554         ;
2555
2556 post_event_nofinger:
2557         direction_less_event {
2558                 $$ = $1;
2559         }
2560         | script_dir music_function_call_closed {
2561                 $$ = $2;
2562                 if (!unsmob_music ($2)->is_mus_type ("post-event")) {
2563                         parser->parser_error (@2, _ ("post-event expected"));
2564                         $$ = SCM_UNSPECIFIED;
2565                 } else if (!SCM_UNBNDP ($1))
2566                 {
2567                         unsmob_music ($$)->set_property ("direction", $1);
2568                 }
2569         }
2570         | HYPHEN {
2571                 if (!parser->lexer_->is_lyric_state ())
2572                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2573                 $$ = MY_MAKE_MUSIC ("HyphenEvent", @$)->unprotect ();
2574         }
2575         | EXTENDER {
2576                 if (!parser->lexer_->is_lyric_state ())
2577                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2578                 $$ = MY_MAKE_MUSIC ("ExtenderEvent", @$)->unprotect ();
2579         }
2580         | script_dir direction_reqd_event {
2581                 if (!SCM_UNBNDP ($1))
2582                 {
2583                         Music *m = unsmob_music ($2);
2584                         m->set_property ("direction", $1);
2585                 }
2586                 $$ = $2;
2587         }
2588         | script_dir direction_less_event {
2589                 if (!SCM_UNBNDP ($1))
2590                 {
2591                         Music *m = unsmob_music ($2);
2592                         m->set_property ("direction", $1);
2593                 }
2594                 $$ = $2;
2595         }
2596         | '^' fingering
2597         {
2598                 $$ = $2;
2599                 unsmob_music ($$)->set_property ("direction", scm_from_int (UP));
2600         }
2601         | '_' fingering
2602         {
2603                 $$ = $2;
2604                 unsmob_music ($$)->set_property ("direction", scm_from_int (DOWN));
2605         }                       
2606         ;
2607
2608 post_event:
2609         post_event_nofinger
2610         | '-' fingering {
2611                 $$ = $2;
2612         }
2613         ;
2614
2615 string_number_event:
2616         E_UNSIGNED {
2617                 Music *s = MY_MAKE_MUSIC ("StringNumberEvent", @$);
2618                 s->set_property ("string-number", $1);
2619                 $$ = s->unprotect ();
2620         }
2621         ;
2622
2623 direction_less_event:
2624         string_number_event
2625         | EVENT_IDENTIFIER      {
2626                 $$ = $1;
2627         }
2628         | tremolo_type  {
2629                Music *a = MY_MAKE_MUSIC ("TremoloEvent", @$);
2630                a->set_property ("tremolo-type", $1);
2631                $$ = a->unprotect ();
2632         }
2633         | event_function_event  
2634         ;
2635
2636 direction_reqd_event:
2637         gen_text_def {
2638                 $$ = $1;
2639         }
2640         | script_abbreviation {
2641                 SCM s = parser->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
2642                 Music *a = MY_MAKE_MUSIC ("ArticulationEvent", @$);
2643                 if (scm_is_string (s))
2644                         a->set_property ("articulation-type", s);
2645                 else parser->parser_error (@1, _ ("expecting string as script definition"));
2646                 $$ = a->unprotect ();
2647         }
2648         ;
2649
2650 octave_check:
2651         /**/ { $$ = SCM_EOL; }
2652         | '=' quotes { $$ = $2; }
2653         ;
2654
2655 quotes:
2656         /* empty */
2657         {
2658                 $$ = SCM_INUM0;
2659         }
2660         | sub_quotes
2661         | sup_quotes
2662         ;
2663
2664 sup_quotes:
2665         '\'' {
2666                 $$ = scm_from_int (1);
2667         }
2668         | sup_quotes '\'' {
2669                 $$ = scm_oneplus ($1);
2670         }
2671         ;
2672
2673 sub_quotes:
2674         ',' {
2675                 $$ = scm_from_int (-1);
2676         }
2677         | sub_quotes ',' {
2678                 $$ = scm_oneminus ($1);
2679         }
2680         ;
2681
2682 steno_pitch:
2683         NOTENAME_PITCH quotes {
2684                 if (!scm_is_eq (SCM_INUM0, $2))
2685                 {
2686                         Pitch p = *unsmob_pitch ($1);
2687                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2688                         $$ = p.smobbed_copy ();
2689                 }
2690         }
2691         ;
2692
2693 /*
2694 ugh. duplication
2695 */
2696
2697 steno_tonic_pitch:
2698         TONICNAME_PITCH quotes {
2699                 if (!scm_is_eq (SCM_INUM0, $2))
2700                 {
2701                         Pitch p = *unsmob_pitch ($1);
2702                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2703                         $$ = p.smobbed_copy ();
2704                 }
2705         }
2706         ;
2707
2708 pitch:
2709         steno_pitch
2710         | PITCH_IDENTIFIER
2711         ;
2712
2713 pitch_also_in_chords:
2714         pitch
2715         | steno_tonic_pitch
2716         ;
2717
2718 gen_text_def:
2719         full_markup {
2720                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2721                 t->set_property ("text", $1);
2722                 $$ = t->unprotect ();
2723         }
2724         | STRING {
2725                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2726                 t->set_property ("text",
2727                         make_simple_markup ($1));
2728                 $$ = t->unprotect ();
2729         }
2730         | embedded_scm_closed
2731         {
2732                 Music *m = unsmob_music ($1);
2733                 if (m && m->is_mus_type ("post-event"))
2734                         $$ = $1;
2735                 else if (Text_interface::is_markup ($1)) {
2736                         Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2737                         t->set_property ("text", $1);
2738                         $$ = t->unprotect ();
2739                 } else
2740                         parser->parser_error (@1, _ ("not an articulation"));
2741         }
2742         ;
2743
2744 fingering:
2745         UNSIGNED {
2746                 Music *t = MY_MAKE_MUSIC ("FingeringEvent", @$);
2747                 t->set_property ("digit", $1);
2748                 $$ = t->unprotect ();
2749         }
2750         ;
2751
2752 script_abbreviation:
2753         '^'             {
2754                 $$ = scm_from_locale_string ("Hat");
2755         }
2756         | '+'           {
2757                 $$ = scm_from_locale_string ("Plus");
2758         }
2759         | '-'           {
2760                 $$ = scm_from_locale_string ("Dash");
2761         }
2762         | '!'           {
2763                 $$ = scm_from_locale_string ("Bang");
2764         }
2765         | ANGLE_CLOSE   {
2766                 $$ = scm_from_locale_string ("Larger");
2767         }
2768         | '.'           {
2769                 $$ = scm_from_locale_string ("Dot");
2770         }
2771         | '_' {
2772                 $$ = scm_from_locale_string ("Underscore");
2773         }
2774         ;
2775
2776 script_dir:
2777         '_'     { $$ = scm_from_int (DOWN); }
2778         | '^'   { $$ = scm_from_int (UP); }
2779         | '-'   { $$ = SCM_UNDEFINED; }
2780         ;
2781
2782 duration_length:
2783         multiplied_duration {
2784                 $$ = $1;
2785         }
2786         ;
2787
2788 maybe_notemode_duration:
2789         {
2790                 $$ = SCM_UNDEFINED;
2791         }
2792         | multiplied_duration   {
2793                 $$ = $1;
2794                 parser->default_duration_ = *unsmob_duration ($$);
2795         }
2796 ;
2797
2798
2799 optional_notemode_duration:
2800         maybe_notemode_duration
2801         {
2802                 if (SCM_UNBNDP ($$))
2803                         $$ = parser->default_duration_.smobbed_copy ();
2804         }
2805         ;
2806
2807 steno_duration:
2808         UNSIGNED dots           {
2809                 int len = 0;
2810                 int n = scm_to_int ($1);
2811                 if (!is_duration (n))
2812                         parser->parser_error (@1, _f ("not a duration: %d", n));
2813                 else
2814                         len = intlog2 (n);
2815
2816                 $$ = Duration (len, scm_to_int ($2)).smobbed_copy ();
2817         }
2818         | DURATION_IDENTIFIER dots      {
2819                 Duration *d = unsmob_duration ($1);
2820                 Duration k (d->duration_log (),
2821                             d->dot_count () + scm_to_int ($2));
2822                 k = k.compressed (d->factor ());
2823                 scm_remember_upto_here_1 ($1);
2824                 $$ = k.smobbed_copy ();
2825         }
2826         ;
2827
2828 multiplied_duration:
2829         steno_duration {
2830                 $$ = $1;
2831         }
2832         | multiplied_duration '*' UNSIGNED {
2833                 $$ = unsmob_duration ($$)->compressed (scm_to_int ($3)).smobbed_copy ();
2834         }
2835         | multiplied_duration '*' FRACTION {
2836                 Rational  m (scm_to_int (scm_car ($3)), scm_to_int (scm_cdr ($3)));
2837
2838                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
2839         }
2840         ;
2841
2842 dots:
2843         /* empty */     {
2844                 $$ = SCM_INUM0;
2845         }
2846         | dots '.' {
2847                 $$ = scm_oneplus ($1);
2848         }
2849         ;
2850
2851 tremolo_type:
2852         ':'     {
2853                 $$ = SCM_INUM0;
2854         }
2855         | ':' UNSIGNED {
2856                 int n = scm_to_int ($2);
2857                 if (!is_duration (n))
2858                         parser->parser_error (@2, _f ("not a duration: %d", n));
2859                 $$ = $2;
2860         }
2861         ;
2862
2863 bass_number:
2864         UNSIGNED { $$ = $1; }
2865         | STRING { $$ = $1; }
2866         | full_markup { $$ = $1; }
2867         | embedded_scm_bare
2868         {
2869                 // as an integer, it needs to be non-negative, and otherwise
2870                 // it needs to be suitable as a markup.
2871                 if (scm_is_integer ($1)
2872                     ? scm_is_true (scm_negative_p ($1))
2873                     : !Text_interface::is_markup ($1))
2874                 {
2875                         parser->parser_error (@1, _ ("bass number expected"));
2876                         $$ = SCM_INUM0;
2877                 }
2878         }
2879         ;
2880
2881 figured_bass_alteration:
2882         '-'     { $$ = ly_rational2scm (FLAT_ALTERATION); }
2883         | '+'   { $$ = ly_rational2scm (SHARP_ALTERATION); }
2884         | '!'   { $$ = scm_from_int (0); }
2885         ;
2886
2887 bass_figure:
2888         FIGURE_SPACE {
2889                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
2890                 $$ = bfr->unprotect ();
2891         }
2892         | bass_number  {
2893                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
2894                 $$ = bfr->self_scm ();
2895
2896                 if (scm_is_number ($1))
2897                         bfr->set_property ("figure", $1);
2898                 else if (Text_interface::is_markup ($1))
2899                         bfr->set_property ("text", $1);
2900
2901                 bfr->unprotect ();
2902         }
2903         | bass_figure ']' {
2904                 $$ = $1;
2905                 unsmob_music ($1)->set_property ("bracket-stop", SCM_BOOL_T);
2906         }
2907         | bass_figure figured_bass_alteration {
2908                 Music *m = unsmob_music ($1);
2909                 if (scm_to_double ($2)) {
2910                         SCM salter = m->get_property ("alteration");
2911                         SCM alter = scm_is_number (salter) ? salter : scm_from_int (0);
2912                         m->set_property ("alteration",
2913                                          scm_sum (alter, $2));
2914                 } else {
2915                         m->set_property ("alteration", scm_from_int (0));
2916                 }
2917         }
2918         | bass_figure figured_bass_modification  {
2919                 Music *m = unsmob_music ($1);
2920                 m->set_property ($2, SCM_BOOL_T);
2921         }
2922         ;
2923
2924
2925 figured_bass_modification:
2926         E_PLUS          {
2927                 $$ = ly_symbol2scm ("augmented");
2928         }
2929         | E_EXCLAMATION {
2930                 $$ = ly_symbol2scm ("no-continuation");
2931         }
2932         | '/'           {
2933                 $$ = ly_symbol2scm ("diminished");
2934         }
2935         | E_BACKSLASH {
2936                 $$ = ly_symbol2scm ("augmented-slash");
2937         }
2938         ;
2939
2940 br_bass_figure:
2941         bass_figure {
2942                 $$ = $1;
2943         }
2944         | '[' bass_figure {
2945                 $$ = $2;
2946                 unsmob_music ($$)->set_property ("bracket-start", SCM_BOOL_T);
2947         }
2948         ;
2949
2950 figure_list:
2951         /**/            {
2952                 $$ = SCM_EOL;
2953         }
2954         | figure_list br_bass_figure {
2955                 $$ = scm_cons ($2, $1);
2956         }
2957         ;
2958
2959 figure_spec:
2960         FIGURE_OPEN figure_list FIGURE_CLOSE {
2961                 $$ = scm_reverse_x ($2, SCM_EOL);
2962         }
2963         ;
2964
2965
2966 optional_rest:
2967         /**/   { $$ = SCM_BOOL_F; }
2968         | REST { $$ = SCM_BOOL_T; }
2969         ;
2970
2971 simple_element:
2972         pitch exclamations questions octave_check maybe_notemode_duration optional_rest {
2973                 if (!parser->lexer_->is_note_state ())
2974                         parser->parser_error (@1, _ ("have to be in Note mode for notes"));
2975                 if (!SCM_UNBNDP ($2)
2976                     || !SCM_UNBNDP ($3)
2977                     || scm_is_number ($4)
2978                     || !SCM_UNBNDP ($5)
2979                     || scm_is_true ($6))
2980                 {
2981                         Music *n = 0;
2982                         if (scm_is_true ($6))
2983                                 n = MY_MAKE_MUSIC ("RestEvent", @$);
2984                         else
2985                                 n = MY_MAKE_MUSIC ("NoteEvent", @$);
2986                         
2987                         n->set_property ("pitch", $1);
2988                         if (SCM_UNBNDP ($5))
2989                                 n->set_property ("duration",
2990                                                  parser->default_duration_.smobbed_copy ());
2991                         else
2992                                 n->set_property ("duration", $5);
2993                         
2994                         if (scm_is_number ($4))
2995                         {
2996                                 int q = scm_to_int ($4);
2997                                 n->set_property ("absolute-octave", scm_from_int (q-1));
2998                         }
2999                         
3000                         if (to_boolean ($3))
3001                                 n->set_property ("cautionary", SCM_BOOL_T);
3002                         if (to_boolean ($2) || to_boolean ($3))
3003                                 n->set_property ("force-accidental", SCM_BOOL_T);
3004                         
3005                         $$ = n->unprotect ();
3006                 }
3007         }
3008         | DRUM_PITCH optional_notemode_duration {
3009                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
3010                 n->set_property ("duration", $2);
3011                 n->set_property ("drum-type", $1);
3012
3013                 $$ = n->unprotect ();
3014         }
3015         | RESTNAME optional_notemode_duration           {
3016                 Music *ev = 0;
3017                 if (ly_scm2string ($1) == "s") {
3018                         /* Space */
3019                         ev = MY_MAKE_MUSIC ("SkipEvent", @$);
3020                   }
3021                 else {
3022                         ev = MY_MAKE_MUSIC ("RestEvent", @$);
3023
3024                     }
3025                 ev->set_property ("duration", $2);
3026                 $$ = ev->unprotect ();
3027         }
3028         ;
3029
3030 simple_chord_elements:
3031         new_chord {
3032                 if (!parser->lexer_->is_chord_state ())
3033                         parser->parser_error (@1, _ ("have to be in Chord mode for chords"));
3034                 $$ = $1;
3035         }
3036         | figure_spec optional_notemode_duration {
3037                 for (SCM s = $1; scm_is_pair (s); s = scm_cdr (s))
3038                 {
3039                         unsmob_music (scm_car (s))->set_property ("duration", $2);
3040                 }
3041                 $$ = $1;
3042         }
3043         ;
3044
3045 lyric_element:
3046         full_markup {
3047                 if (!parser->lexer_->is_lyric_state ())
3048                         parser->parser_error (@1, _ ("markup outside of text script or \\lyricmode"));
3049                 $$ = $1;
3050         }
3051         | STRING {
3052                 if (!parser->lexer_->is_lyric_state ())
3053                         parser->parser_error (@1, _ ("unrecognized string, not in text script or \\lyricmode"));
3054                 $$ = $1;
3055         }
3056         | LYRIC_ELEMENT
3057         ;
3058
3059 lyric_element_music:
3060         lyric_element optional_notemode_duration post_events {
3061                 $$ = MAKE_SYNTAX ("lyric-event", @$, $1, $2);
3062                 if (scm_is_pair ($3))
3063                         unsmob_music ($$)->set_property
3064                                 ("articulations", scm_reverse_x ($3, SCM_EOL));
3065         }
3066         ;
3067
3068 new_chord:
3069         steno_tonic_pitch optional_notemode_duration   {
3070                 $$ = make_chord_elements (@$, $1, $2, SCM_EOL);
3071         }
3072         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
3073                 SCM its = scm_reverse_x ($4, SCM_EOL);
3074                 $$ = make_chord_elements (@$, $1, $2, scm_cons ($3, its));
3075         }
3076         ;
3077
3078 chord_items:
3079         /**/ {
3080                 $$ = SCM_EOL;
3081         }
3082         | chord_items chord_item {
3083                 $$ = scm_cons ($2, $$);
3084         }
3085         ;
3086
3087 chord_separator:
3088         CHORD_COLON {
3089                 $$ = ly_symbol2scm ("chord-colon");
3090         }
3091         | CHORD_CARET {
3092                 $$ = ly_symbol2scm ("chord-caret");
3093         }
3094         | CHORD_SLASH steno_tonic_pitch {
3095                 $$ = scm_list_2 (ly_symbol2scm ("chord-slash"), $2);
3096         }
3097         | CHORD_BASS steno_tonic_pitch {
3098                 $$ = scm_list_2 (ly_symbol2scm ("chord-bass"), $2);
3099         }
3100         ;
3101
3102 chord_item:
3103         chord_separator {
3104                 $$ = $1;
3105         }
3106         | step_numbers {
3107                 $$ = scm_reverse_x ($1, SCM_EOL);
3108         }
3109         | CHORD_MODIFIER  {
3110                 $$ = $1;
3111         }
3112         ;
3113
3114 step_numbers:
3115         step_number { $$ = scm_cons ($1, SCM_EOL); }
3116         | step_numbers '.' step_number {
3117                 $$ = scm_cons ($3, $$);
3118         }
3119         ;
3120
3121 step_number:
3122         UNSIGNED {
3123                 $$ = make_chord_step ($1, 0);
3124         }
3125         | UNSIGNED '+' {
3126                 $$ = make_chord_step ($1, SHARP_ALTERATION);
3127         }
3128         | UNSIGNED CHORD_MINUS {
3129                 $$ = make_chord_step ($1, FLAT_ALTERATION);
3130         }
3131         ;
3132
3133 tempo_range:
3134         UNSIGNED {
3135                 $$ = $1;
3136         }
3137         | UNSIGNED '-' UNSIGNED {
3138                 $$ = scm_cons ($1, $3);
3139         }
3140         ;
3141
3142 /*
3143         UTILITIES
3144
3145 TODO: should deprecate in favor of Scheme?
3146
3147  */
3148 number_expression:
3149         number_expression '+' number_term {
3150                 $$ = scm_sum ($1, $3);
3151         }
3152         | number_expression '-' number_term {
3153                 $$ = scm_difference ($1, $3);
3154         }
3155         | number_term
3156         ;
3157
3158 number_term:
3159         number_factor {
3160                 $$ = $1;
3161         }
3162         | number_factor '*' number_factor {
3163                 $$ = scm_product ($1, $3);
3164         }
3165         | number_factor '/' number_factor {
3166                 $$ = scm_divide ($1, $3);
3167         }
3168         ;
3169
3170 number_factor:
3171         '-'  number_factor { /* %prec UNARY_MINUS */
3172                 $$ = scm_difference ($2, SCM_UNDEFINED);
3173         }
3174         | bare_number
3175         ;
3176
3177
3178 bare_number:
3179         bare_number_closed
3180         | UNSIGNED NUMBER_IDENTIFIER    {
3181                 $$ = scm_product ($1, $2);
3182         }
3183         | REAL NUMBER_IDENTIFIER        {
3184                 $$ = scm_product ($1, $2);
3185         }
3186         ;
3187
3188 bare_number_closed:
3189         UNSIGNED
3190         | REAL
3191         | NUMBER_IDENTIFIER
3192         ;
3193
3194 unsigned_number:
3195         UNSIGNED
3196         | NUMBER_IDENTIFIER
3197         ;
3198
3199 exclamations:
3200                 { $$ = SCM_UNDEFINED; }
3201         | exclamations '!'
3202         {
3203                 if (SCM_UNBNDP ($1))
3204                         $$ = SCM_BOOL_T;
3205                 else
3206                         $$ = scm_not ($1);
3207         }
3208         ;
3209
3210 questions:
3211         { $$ = SCM_UNDEFINED; }
3212         | questions '?'
3213         {
3214                 if (SCM_UNBNDP ($1))
3215                         $$ = SCM_BOOL_T;
3216                 else
3217                         $$ = scm_not ($1);
3218         }
3219         ;
3220
3221 full_markup_list:
3222         MARKUPLIST
3223                 { parser->lexer_->push_markup_state (); }
3224         markup_list {
3225                 $$ = $3;
3226                 parser->lexer_->pop_state ();
3227         }
3228         ;
3229
3230 full_markup:
3231         MARKUP
3232                 { parser->lexer_->push_markup_state (); }
3233         markup_top {
3234                 $$ = $3;
3235                 parser->lexer_->pop_state ();
3236         }
3237         ;
3238
3239 markup_top:
3240         simple_markup_list {
3241                 $$ = scm_list_2 (ly_lily_module_constant ("line-markup"),  $1);
3242         }
3243         | markup_head_1_list simple_markup
3244         {
3245                 $$ = scm_car (MAKE_SYNTAX ("composed-markup-list",
3246                                            @2, $1, scm_list_1 ($2)));
3247         }
3248         | simple_markup {
3249                 $$ = $1;
3250         }
3251         ;
3252
3253 markup_scm:
3254         embedded_scm_bare
3255         {
3256                 if (Text_interface::is_markup ($1))
3257                         MYBACKUP (MARKUP_IDENTIFIER, $1, @1);
3258                 else if (Text_interface::is_markup_list ($1))
3259                         MYBACKUP (MARKUPLIST_IDENTIFIER, $1, @1);
3260                 else {
3261                         parser->parser_error (@1, _ ("not a markup"));
3262                         MYBACKUP (MARKUP_IDENTIFIER, scm_string (SCM_EOL), @1);
3263                 }
3264         } BACKUP
3265         ;
3266                         
3267
3268 simple_markup_list:
3269         markup_composed_list {
3270                 $$ = $1;
3271         }
3272         | markup_uncomposed_list
3273         ;
3274
3275 markup_uncomposed_list:
3276         markup_braced_list {
3277                 $$ = $1;
3278         }
3279         | markup_command_list {
3280                 $$ = scm_list_1 ($1);
3281         }
3282         | markup_scm MARKUPLIST_IDENTIFIER
3283         {
3284                 $$ = $2;
3285         }
3286         ;
3287
3288 markup_list:
3289         simple_markup_list
3290         | markup_score
3291         {
3292                 $$ = scm_list_1 (scm_list_2 (ly_lily_module_constant ("score-lines-markup-list"), $1));
3293         }
3294         ;
3295
3296 markup_score:
3297         SCORE {
3298                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3299                 parser->lexer_->push_note_state (nn);
3300         } '{' score_body '}' {
3301                 $$ = $4;
3302                 parser->lexer_->pop_state ();
3303         }
3304         ;
3305
3306 markup_composed_list:
3307         markup_head_1_list markup_uncomposed_list {
3308                 $$ = MAKE_SYNTAX ("composed-markup-list",
3309                                   @2, $1, $2);
3310         }
3311         ;
3312
3313 markup_braced_list:
3314         '{' markup_braced_list_body '}' {
3315                 $$ = scm_reverse_x ($2, SCM_EOL);
3316         }
3317         ;
3318
3319 markup_braced_list_body:
3320         /* empty */     {  $$ = SCM_EOL; }
3321         | markup_braced_list_body markup {
3322                 $$ = scm_cons ($2, $1);
3323         }
3324         | markup_braced_list_body simple_markup_list {
3325                 $$ = scm_reverse_x ($2, $1);
3326         }
3327         ;
3328
3329 markup_command_list:
3330         MARKUP_LIST_FUNCTION markup_command_list_arguments {
3331           $$ = scm_cons ($1, scm_reverse_x($2, SCM_EOL));
3332         }
3333         ;
3334
3335 markup_command_basic_arguments:
3336         EXPECT_MARKUP_LIST markup_command_list_arguments markup_list {
3337           $$ = scm_cons ($3, $2);
3338         }
3339         | EXPECT_SCM markup_command_list_arguments embedded_scm_closed {
3340           $$ = check_scheme_arg (parser, @3, $3, $2, $1);
3341         }
3342         | EXPECT_NO_MORE_ARGS {
3343           $$ = SCM_EOL;
3344         }
3345         ;
3346
3347 markup_command_list_arguments:
3348         markup_command_basic_arguments { $$ = $1; }
3349         | EXPECT_MARKUP markup_command_list_arguments markup {
3350           $$ = scm_cons ($3, $2);
3351         }
3352         ;
3353
3354 markup_head_1_item:
3355         MARKUP_FUNCTION EXPECT_MARKUP markup_command_list_arguments {
3356           $$ = scm_cons ($1, scm_reverse_x ($3, SCM_EOL));
3357         }
3358         ;
3359
3360 markup_head_1_list:
3361         markup_head_1_item      {
3362                 $$ = scm_list_1 ($1);
3363         }
3364         | markup_head_1_list markup_head_1_item {
3365                 $$ = scm_cons ($2, $1);
3366         }
3367         ;
3368
3369 simple_markup:
3370         STRING {
3371                 $$ = make_simple_markup ($1);
3372         }
3373         | MARKUP_FUNCTION markup_command_basic_arguments {
3374                 $$ = scm_cons ($1, scm_reverse_x ($2, SCM_EOL));
3375         }
3376         | markup_scm MARKUP_IDENTIFIER
3377         {
3378                 $$ = $2;
3379         }
3380         | markup_score
3381         {
3382                 $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), $1);
3383         }
3384         ;
3385
3386 markup:
3387         markup_head_1_list simple_markup
3388         {
3389                 $$ = scm_car (MAKE_SYNTAX ("composed-markup-list",
3390                                            @2, $1, scm_list_1 ($2)));
3391         }
3392         | simple_markup {
3393                 $$ = $1;
3394         }
3395         ;
3396
3397 %%
3398
3399 void
3400 Lily_parser::set_yydebug (bool x)
3401 {
3402         yydebug = x;
3403 }
3404
3405 SCM
3406 Lily_parser::do_yyparse ()
3407 {
3408         SCM retval = SCM_UNDEFINED;
3409         yyparse (this, &retval);
3410         return retval;
3411 }
3412
3413
3414
3415
3416
3417 /*
3418
3419 It is a little strange to have this function in this file, but
3420 otherwise, we have to import music classes into the lexer.
3421
3422 */
3423 int
3424 Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
3425 {
3426         if (unsmob_book (sid)) {
3427                 Book *book =  unsmob_book (sid)->clone ();
3428                 *destination = book->self_scm ();
3429                 book->unprotect ();
3430
3431                 return BOOK_IDENTIFIER;
3432         } else if (scm_is_number (sid)) {
3433                 *destination = sid;
3434                 return NUMBER_IDENTIFIER;
3435         } else if (unsmob_context_def (sid)) {
3436                 Context_def *def= unsmob_context_def (sid)->clone ();
3437
3438                 *destination = def->self_scm ();
3439                 def->unprotect ();
3440
3441                 return CONTEXT_DEF_IDENTIFIER;
3442         } else if (unsmob_context_mod (sid)) {
3443                 *destination = unsmob_context_mod (sid)->smobbed_copy ();
3444
3445                 return CONTEXT_MOD_IDENTIFIER;
3446         } else if (Music *mus = unsmob_music (sid)) {
3447                 mus = mus->clone ();
3448                 *destination = mus->self_scm ();
3449                 bool is_event = mus->is_mus_type ("post-event");
3450                 mus->unprotect ();
3451                 return is_event ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
3452         } else if (unsmob_pitch (sid)) {
3453                 *destination = unsmob_pitch (sid)->smobbed_copy ();
3454                 return PITCH_IDENTIFIER;
3455         } else if (unsmob_duration (sid)) {
3456                 *destination = unsmob_duration (sid)->smobbed_copy ();
3457                 return DURATION_IDENTIFIER;
3458         } else if (unsmob_output_def (sid)) {
3459                 Output_def *p = unsmob_output_def (sid);
3460                 p = p->clone ();
3461
3462                 *destination = p->self_scm ();
3463                 p->unprotect ();
3464                 return OUTPUT_DEF_IDENTIFIER;
3465         }
3466
3467         return -1;
3468 }
3469
3470 SCM
3471 get_next_unique_context_id ()
3472 {
3473         return scm_from_locale_string ("$uniqueContextId");
3474 }
3475
3476
3477 SCM
3478 get_next_unique_lyrics_context_id ()
3479 {
3480         static int new_context_count;
3481         char s[128];
3482         snprintf (s, sizeof (s)-1, "uniqueContext%d", new_context_count++);
3483         return scm_from_locale_string (s);
3484 }
3485
3486 // check_scheme_arg checks one argument with a given predicate for use
3487 // in an argument list and throws a syntax error if it is unusable.
3488 // The argument is prepended to the argument list in any case.  After
3489 // throwing a syntax error, the argument list is terminated with #f as
3490 // its last cdr in order to mark it as uncallable while not losing
3491 // track of its total length.
3492 //
3493 // There are a few special considerations: if optional argument disp
3494 // is given (otherwise it defaults to SCM_UNDEFINED), it will be used
3495 // instead of arg in a prospective error message.  This is useful if
3496 // arg is not the actual argument but rather a transformation of it.
3497 //
3498 // If arg itself is SCM_UNDEFINED, the predicate is considered false
3499 // and an error message using disp is produced unconditionally.
3500
3501 SCM check_scheme_arg (Lily_parser *parser, Input loc,
3502                       SCM arg, SCM args, SCM pred, SCM disp)
3503 {
3504         if (SCM_UNBNDP (arg))
3505                 args = scm_cons (disp, args);
3506         else {
3507                 args = scm_cons (arg, args);
3508                 if (scm_is_true (scm_call_1 (pred, arg)))
3509                         return args;
3510         }
3511         scm_set_cdr_x (scm_last_pair (args), SCM_EOL);
3512         MAKE_SYNTAX ("argument-error", loc, scm_length (args), pred,
3513                      SCM_UNBNDP (disp) ? arg : disp);
3514         scm_set_cdr_x (scm_last_pair (args), SCM_BOOL_F);
3515         return args;
3516 }
3517
3518 SCM loc_on_music (Input loc, SCM arg)
3519 {
3520         if (Music *m = unsmob_music (arg))
3521         {
3522                 m = m->clone ();
3523                 m->set_spot (loc);
3524                 return m->unprotect ();
3525         }
3526         return arg;
3527 }
3528
3529 SCM
3530 try_string_variants (SCM pred, SCM str)
3531 {
3532         // a matching predicate is always ok
3533         if (scm_is_true (scm_call_1 (pred, str)))
3534                 return str;
3535         // a symbol may be interpreted as a list of symbols if it helps
3536         if (scm_is_symbol (str)) {
3537                 str = scm_list_1 (str);
3538                 if (scm_is_true (scm_call_1 (pred, str)))
3539                         return str;
3540                 return SCM_UNDEFINED;
3541         }
3542
3543         // If this cannot be a string representation of a symbol list,
3544         // we are through.
3545
3546         if (!is_regular_identifier (str, true))
3547                 return SCM_UNDEFINED;
3548
3549         str = scm_string_split (str, SCM_MAKE_CHAR ('.'));
3550         for (SCM p = str; scm_is_pair (p); p = scm_cdr (p))
3551                 scm_set_car_x (p, scm_string_to_symbol (scm_car (p)));
3552
3553         // Let's attempt the symbol list interpretation first.
3554
3555         if (scm_is_true (scm_call_1 (pred, str)))
3556                 return str;
3557
3558         // If there is just one symbol in the list, we might interpret
3559         // it as a single symbol
3560
3561         if (scm_is_null (scm_cdr (str)))
3562         {
3563                 str = scm_car (str);
3564                 if (scm_is_true (scm_call_1 (pred, str)))
3565                         return str;
3566         }
3567
3568         return SCM_UNDEFINED;
3569 }
3570
3571 bool
3572 is_regular_identifier (SCM id, bool multiple)
3573 {
3574   if (!scm_is_string (id))
3575           return false;
3576
3577   string str = ly_scm2string (id);
3578
3579   bool middle = false;
3580
3581   for (string::iterator it=str.begin(); it != str.end (); it++)
3582   {
3583           int c = *it & 0xff;
3584           if ((c >= 'a' && c <= 'z')
3585               || (c >= 'A' && c <= 'Z')
3586               || c > 0x7f)
3587                   middle = true;
3588           else if (middle && (c == '-' || c == '_' || (multiple && c == '.')))
3589                   middle = false;
3590           else
3591                   return false;
3592   }
3593   return middle;
3594 }
3595
3596 SCM
3597 make_music_from_simple (Lily_parser *parser, Input loc, SCM simple)
3598 {
3599         if (unsmob_music (simple))
3600                 return simple;
3601         if (parser->lexer_->is_note_state ()) {
3602                 if (scm_is_symbol (simple)) {
3603                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3604                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3605                         n->set_property ("drum-type", simple);
3606                         return n->unprotect ();
3607                 }
3608                 if (unsmob_pitch (simple)) {
3609                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3610                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3611                         n->set_property ("pitch", simple);
3612                         return n->unprotect ();
3613                 }
3614                 return simple;
3615         } else if (parser->lexer_->is_lyric_state ()) {
3616                 if (Text_interface::is_markup (simple))
3617                         return MAKE_SYNTAX ("lyric-event", loc, simple,
3618                                             parser->default_duration_.smobbed_copy ());
3619         } else if (parser->lexer_->is_chord_state ()) {
3620                 if (unsmob_pitch (simple))
3621                         return make_chord_elements (loc, simple,
3622                                                     parser->default_duration_.smobbed_copy (),
3623                                                     SCM_EOL);
3624         }
3625         return simple;
3626 }
3627
3628 Music *
3629 make_music_with_input (SCM name, Input where)
3630 {
3631        Music *m = make_music_by_name (name);
3632        m->set_spot (where);
3633        return m;
3634 }
3635
3636 SCM
3637 make_simple_markup (SCM a)
3638 {
3639         return a;
3640 }
3641
3642 bool
3643 is_duration (int t)
3644 {
3645   return t && t == 1 << intlog2 (t);
3646 }
3647
3648 SCM
3649 make_chord_step (SCM step_scm, Rational alter)
3650 {
3651         int step = scm_to_int (step_scm);
3652
3653         if (step == 7)
3654                 alter += FLAT_ALTERATION;
3655
3656         while (step < 0)
3657                 step += 7;
3658         Pitch m ((step -1) / 7, (step - 1) % 7, alter);
3659         return m.smobbed_copy ();
3660 }
3661
3662
3663 SCM
3664 make_chord_elements (Input loc, SCM pitch, SCM dur, SCM modification_list)
3665 {
3666         SCM chord_ctor = ly_lily_module_constant ("construct-chord-elements");
3667         SCM res = scm_call_3 (chord_ctor, pitch, dur, modification_list);
3668         for (SCM s = res; scm_is_pair (s); s = scm_cdr (s))
3669         {
3670                 unsmob_music (scm_car (s))->set_spot (loc);
3671         }
3672         return res;
3673 }
3674
3675 int
3676 yylex (YYSTYPE *s, YYLTYPE *loc, Lily_parser *parser)
3677 {
3678         Lily_lexer *lex = parser->lexer_;
3679
3680         lex->lexval_ = s;
3681         lex->lexloc_ = loc;
3682         lex->prepare_for_next_token ();
3683         return lex->yylex ();
3684 }