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