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