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