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