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