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