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