]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
Issue 3648/1: Isolated durations in music sequences now stand for unpitched notes
[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 DURATION_IDENTIFIER
337 %token EVENT_IDENTIFIER
338 %token EVENT_FUNCTION
339 %token FRACTION
340 %token LYRIC_ELEMENT
341 %token MARKUP_FUNCTION
342 %token MARKUP_LIST_FUNCTION
343 %token MARKUP_IDENTIFIER
344 %token MARKUPLIST_IDENTIFIER
345 %token MUSIC_FUNCTION
346 %token MUSIC_IDENTIFIER
347 %token NOTENAME_PITCH
348 %token NUMBER_IDENTIFIER
349 %token OUTPUT_DEF_IDENTIFIER
350 %token REAL
351 %token REPEAT_IDENTIFIER
352 %token RESTNAME
353 %token SCM_ARG
354 %token SCM_FUNCTION
355 %token SCM_IDENTIFIER
356 %token SCM_TOKEN
357 %token STRING
358 %token SYMBOL_LIST
359 %token TONICNAME_PITCH
360
361 %left '-' '+'
362
363 /* We don't assign precedence to / and *, because we might need varied
364 prec levels in different prods */
365
366 %left UNARY_MINUS
367
368 %%
369
370 start_symbol:
371         lilypond
372         | EMBEDDED_LILY {
373                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
374                 parser->lexer_->push_note_state (nn);
375         } embedded_lilypond {
376                 parser->lexer_->pop_state ();
377                 *retval = $3;
378         }
379         ;
380
381 lilypond:       /* empty */ { $$ = SCM_UNSPECIFIED; }
382         | lilypond toplevel_expression {
383         }
384         | lilypond assignment {
385         }
386         | lilypond error {
387                 parser->error_level_ = 1;
388         }
389         | lilypond INVALID      {
390                 parser->error_level_ = 1;
391         }
392         ;
393
394
395 toplevel_expression:
396         {
397                 parser->lexer_->add_scope (get_header (parser));
398         } lilypond_header {
399                 parser->lexer_->set_identifier (ly_symbol2scm ("$defaultheader"), $2);
400         }
401         | book_block {
402                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-book-handler");
403                 scm_call_2 (proc, parser->self_scm (), $1);
404         }
405         | bookpart_block {
406                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-bookpart-handler");
407                 scm_call_2 (proc, parser->self_scm (), $1);
408         }
409         | score_block {
410                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-score-handler");
411                 scm_call_2 (proc, parser->self_scm (), $1);
412         }
413         | composite_music {
414                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-music-handler");
415                 scm_call_2 (proc, parser->self_scm (), $1);
416         }
417         | full_markup {
418                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-text-handler");
419                 scm_call_2 (proc, parser->self_scm (), scm_list_1 ($1));
420         }
421         | full_markup_list {
422                 SCM proc = parser->lexer_->lookup_identifier ("toplevel-text-handler");
423                 scm_call_2 (proc, parser->self_scm (), $1);
424         }
425         | SCM_TOKEN {
426                 // Evaluate and ignore #xxx, as opposed to \xxx
427                 parser->lexer_->eval_scm_token ($1);
428         }
429         | embedded_scm_active
430         {
431                 SCM out = SCM_UNDEFINED;
432                 if (Text_interface::is_markup ($1))
433                         out = scm_list_1 ($1);
434                 else if (Text_interface::is_markup_list ($1))
435                         out = $1;
436                 if (scm_is_pair (out))
437                 {
438                         SCM proc = parser->lexer_->lookup_identifier ("toplevel-text-handler");
439                         scm_call_2 (proc, parser->self_scm (), out);
440                 } else if (!scm_is_eq ($1, SCM_UNSPECIFIED))
441                         parser->parser_error (@1, _("bad expression type"));
442         }
443         | output_def {
444                 SCM id = SCM_EOL;
445                 Output_def * od = unsmob_output_def ($1);
446
447                 if (od->c_variable ("is-paper") == SCM_BOOL_T)
448                         id = ly_symbol2scm ("$defaultpaper");
449                 else if (od->c_variable ("is-midi") == SCM_BOOL_T)
450                         id = ly_symbol2scm ("$defaultmidi");
451                 else if (od->c_variable ("is-layout") == SCM_BOOL_T)
452                         id = ly_symbol2scm ("$defaultlayout");
453
454                 parser->lexer_->set_identifier (id, $1);
455         }
456         ;
457
458 embedded_scm_bare:
459         SCM_TOKEN
460         {
461                 $$ = parser->lexer_->eval_scm_token ($1);
462         }
463         | SCM_IDENTIFIER
464         ;
465
466 embedded_scm_active:
467         SCM_IDENTIFIER
468         | scm_function_call
469         ;
470
471 embedded_scm_bare_arg:
472         SCM_ARG
473         | SCM_TOKEN
474         {
475                 $$ = parser->lexer_->eval_scm_token ($1);
476         }
477         | full_markup_list
478         | context_modification
479         | score_block
480         | context_def_spec_block
481         | book_block
482         | bookpart_block
483         | output_def
484         ;
485
486 /* The generic version may end in music, or not */
487
488 embedded_scm:
489         embedded_scm_bare
490         | scm_function_call
491         ;
492
493 /* embedded_scm_arg is _not_ casting pitches to music by default, this
494  * has to be done by the function itself.  Note that this may cause
495  * the results of scm_function_call or embedded_scm_bare_arg to be
496  * turned into music from pitches as well.  Note that this creates a
497  * distinctly awkward situation for calculated drum pitches.  Those
498  * are at the current point of time rejected as music constituents as
499  * they can't be distinguished from "proper" symbols.
500  */
501
502 embedded_scm_arg:
503         embedded_scm_bare_arg
504         | scm_function_call
505         | music_assign
506         ;
507
508 scm_function_call:
509         SCM_FUNCTION function_arglist {
510                 $$ = MAKE_SYNTAX ("music-function", @$,
511                                          $1, $2);
512         }
513         ;
514
515 embedded_lilypond_number:
516         '-' embedded_lilypond_number
517         {
518                 $$ = scm_difference ($2, SCM_UNDEFINED);
519         }
520         | bare_number_common
521         | UNSIGNED NUMBER_IDENTIFIER
522         {
523                 $$ = scm_product ($1, $2);
524         }
525         ;
526
527 embedded_lilypond:
528         /* empty */
529         {
530                 // FIXME: @$ does not contain a useful source location
531                 // for empty rules, and the only token in the whole
532                 // production, EMBEDDED_LILY, is synthetic and also
533                 // contains no source location.
534                 $$ = MAKE_SYNTAX ("void-music", @$);
535         }
536         | identifier_init_nonumber
537         | embedded_lilypond_number
538         | post_event post_events
539         {
540                 $$ = scm_reverse_x ($2, SCM_EOL);
541                 if (Music *m = unsmob_music ($1))
542                 {
543                         if (m->is_mus_type ("post-event-wrapper"))
544                                 $$ = scm_append
545                                         (scm_list_2 (m->get_property ("elements"),
546                                                      $$));
547                         else
548                                 $$ = scm_cons ($1, $$);
549                 }
550                 if (scm_is_pair ($$)
551                     && scm_is_null (scm_cdr ($$)))
552                         $$ = scm_car ($$);
553                 else
554                 {
555                         Music * m = MY_MAKE_MUSIC ("PostEvents", @$);
556                         m->set_property ("elements", $$);
557                         $$ = m->unprotect ();
558                 }
559         }
560         | multiplied_duration
561         | music_embedded music_embedded music_list {
562                 $3 = scm_reverse_x ($3, SCM_EOL);
563                 if (unsmob_music ($2))
564                         $3 = scm_cons ($2, $3);
565                 if (unsmob_music ($1))
566                         $3 = scm_cons ($1, $3);
567                 $$ = MAKE_SYNTAX ("sequential-music", @$, $3);
568         }
569         | error {
570                 parser->error_level_ = 1;
571                 $$ = SCM_UNSPECIFIED;
572         }
573         | INVALID embedded_lilypond {
574                 parser->error_level_ = 1;
575                 $$ = $2;
576         }
577         ;
578
579
580 lilypond_header_body:
581         /* empty */ { $$ = SCM_UNSPECIFIED; }
582         | lilypond_header_body assignment  {
583
584         }
585         | lilypond_header_body embedded_scm  {
586
587         }
588         ;
589
590 lilypond_header:
591         HEADER '{' lilypond_header_body '}'     {
592                 $$ = parser->lexer_->remove_scope ();
593         }
594         ;
595
596 /*
597         DECLARATIONS
598 */
599 assignment_id:
600         STRING          { $$ = $1; }
601         ;
602
603 assignment:
604         assignment_id '=' identifier_init  {
605                 parser->lexer_->set_identifier ($1, $3);
606                 $$ = SCM_UNSPECIFIED;
607         }
608         | assignment_id property_path '=' identifier_init {
609                 SCM path = scm_cons (scm_string_to_symbol ($1), $2);
610                 parser->lexer_->set_identifier (path, $4);
611                 $$ = SCM_UNSPECIFIED;
612         }
613         ;
614
615
616 identifier_init:
617         identifier_init_nonumber
618         | number_expression
619         | post_event_nofinger post_events
620         {
621                 $$ = scm_reverse_x ($2, SCM_EOL);
622                 if (Music *m = unsmob_music ($1))
623                 {
624                         if (m->is_mus_type ("post-event-wrapper"))
625                                 $$ = scm_append
626                                         (scm_list_2 (m->get_property ("elements"),
627                                                      $$));
628                         else
629                                 $$ = scm_cons ($1, $$);
630                 }
631                 if (scm_is_pair ($$)
632                     && scm_is_null (scm_cdr ($$)))
633                         $$ = scm_car ($$);
634                 else
635                 {
636                         Music * m = MY_MAKE_MUSIC ("PostEvents", @$);
637                         m->set_property ("elements", $$);
638                         $$ = m->unprotect ();
639                 }
640         }
641         ;
642
643 identifier_init_nonumber:
644         score_block
645         | book_block
646         | bookpart_block
647         | output_def
648         | context_def_spec_block
649         | music_assign
650         | FRACTION
651         | string
652         | embedded_scm
653         | full_markup_list
654         | context_modification
655         ;
656
657 context_def_spec_block:
658         CONTEXT '{' context_def_spec_body '}'
659                 {
660                 $$ = $3;
661                 unsmob_context_def ($$)->origin ()->set_spot (@$);
662         }
663         ;
664
665 context_mod_arg:
666         embedded_scm
667         |
668         {
669                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
670                 parser->lexer_->push_note_state (nn);
671         }
672         composite_music
673         {
674                 parser->lexer_->pop_state ();
675                 $$ = $2;
676         }
677         ;
678
679 context_mod_embedded:
680         context_mod_arg
681         {
682                 if (unsmob_music ($1)) {
683                         SCM proc = parser->lexer_->lookup_identifier ("context-mod-music-handler");
684                         $1 = scm_call_2 (proc, parser->self_scm (), $1);
685                 }
686                 if (unsmob_context_mod ($1))
687                         $$ = $1;
688                 else {
689                         parser->parser_error (@1, _ ("not a context mod"));
690                         $$ = Context_mod ().smobbed_copy ();
691                 }
692         }
693         ;
694
695
696 context_def_spec_body:
697         /**/ {
698                 $$ = Context_def::make_scm ();
699         }
700         | CONTEXT_DEF_IDENTIFIER {
701                 $$ = $1;
702         }
703         | context_def_spec_body context_mod {
704                 if (!SCM_UNBNDP ($2))
705                         unsmob_context_def ($$)->add_context_mod ($2);
706         }
707         | context_def_spec_body context_modification {
708                 Context_def *td = unsmob_context_def ($$);
709                 SCM new_mods = unsmob_context_mod ($2)->get_mods ();
710                 for (SCM m = new_mods; scm_is_pair (m); m = scm_cdr (m)) {
711                     td->add_context_mod (scm_car (m));
712                 }
713         }
714         | context_def_spec_body context_mod_embedded {
715                 Context_def *td = unsmob_context_def ($$);
716                 SCM new_mods = unsmob_context_mod ($2)->get_mods ();
717                 for (SCM m = new_mods; scm_is_pair (m); m = scm_cdr (m)) {
718                     td->add_context_mod (scm_car (m));
719                 }
720         }
721         ;
722
723
724
725 book_block:
726         BOOK '{' book_body '}'  {
727                 $$ = $3;
728                 unsmob_book ($$)->origin ()->set_spot (@$);
729                 pop_paper (parser);
730                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-book"), SCM_BOOL_F);
731         }
732         ;
733
734 /* FIXME:
735    * Use 'handlers' like for toplevel-* stuff?
736    * grok \layout and \midi?  */
737 book_body:
738         {
739                 Book *book = new Book;
740                 init_papers (parser);
741                 book->paper_ = dynamic_cast<Output_def*> (unsmob_output_def (parser->lexer_->lookup_identifier ("$defaultpaper"))->clone ());
742                 book->paper_->unprotect ();
743                 push_paper (parser, book->paper_);
744                 book->header_ = get_header (parser);
745                 $$ = book->unprotect ();
746                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-book"), $$);
747         }
748         | BOOK_IDENTIFIER {
749                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-book"), $1);
750         }
751         | book_body paper_block {
752                 unsmob_book ($1)->paper_ = unsmob_output_def ($2);
753                 set_paper (parser, unsmob_output_def ($2));
754         }
755         | book_body bookpart_block {
756                 SCM proc = parser->lexer_->lookup_identifier ("book-bookpart-handler");
757                 scm_call_2 (proc, $1, $2);
758         }
759         | book_body score_block {
760                 SCM proc = parser->lexer_->lookup_identifier ("book-score-handler");
761                 scm_call_2 (proc, $1, $2);
762         }
763         | book_body composite_music {
764                 SCM proc = parser->lexer_->lookup_identifier ("book-music-handler");
765                 scm_call_3 (proc, parser->self_scm (), $1, $2);
766         }
767         | book_body full_markup {
768                 SCM proc = parser->lexer_->lookup_identifier ("book-text-handler");
769                 scm_call_2 (proc, $1, scm_list_1 ($2));
770         }
771         | book_body full_markup_list {
772                 SCM proc = parser->lexer_->lookup_identifier ("book-text-handler");
773                 scm_call_2 (proc, $1, $2);
774         }
775         | book_body SCM_TOKEN {
776                 // Evaluate and ignore #xxx, as opposed to \xxx
777                 parser->lexer_->eval_scm_token ($2);
778         }
779         | book_body embedded_scm_active
780         {
781                 SCM out = SCM_UNDEFINED;
782                 if (Text_interface::is_markup ($2))
783                         out = scm_list_1 ($2);
784                 else if (Text_interface::is_markup_list ($2))
785                         out = $2;
786                 if (scm_is_pair (out))
787                 {
788                         SCM proc = parser->lexer_->lookup_identifier ("book-text-handler");
789                         scm_call_2 (proc, $1, out);
790                 } else if (!scm_is_eq ($2, SCM_UNSPECIFIED))
791                         parser->parser_error (@2, _("bad expression type"));
792         }
793         | book_body
794         {
795                 parser->lexer_->add_scope (unsmob_book ($1)->header_);
796         } lilypond_header
797         | book_body error {
798                 Book *book = unsmob_book ($1);
799                 book->paper_ = 0;
800                 book->scores_ = SCM_EOL;
801                 book->bookparts_ = SCM_EOL;
802         }
803         ;
804
805 bookpart_block:
806         BOOKPART '{' bookpart_body '}' {
807                 $$ = $3;
808                 unsmob_book ($$)->origin ()->set_spot (@$);
809                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), SCM_BOOL_F);
810         }
811         ;
812
813 bookpart_body:
814         {
815                 Book *book = new Book;
816                 $$ = book->unprotect ();
817                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), $$);
818         }
819         | BOOK_IDENTIFIER {
820                 parser->lexer_->set_identifier (ly_symbol2scm ("$current-bookpart"), $1);
821         }
822         | bookpart_body paper_block {
823                 unsmob_book ($$)->paper_ = unsmob_output_def ($2);
824         }
825         | bookpart_body score_block {
826                 SCM proc = parser->lexer_->lookup_identifier ("bookpart-score-handler");
827                 scm_call_2 (proc, $1, $2);
828         }
829         | bookpart_body composite_music {
830                 SCM proc = parser->lexer_->lookup_identifier ("bookpart-music-handler");
831                 scm_call_3 (proc, parser->self_scm (), $1, $2);
832         }
833         | bookpart_body full_markup {
834                 SCM proc = parser->lexer_->lookup_identifier ("bookpart-text-handler");
835                 scm_call_2 (proc, $1, scm_list_1 ($2));
836         }
837         | bookpart_body full_markup_list {
838                 SCM proc = parser->lexer_->lookup_identifier ("bookpart-text-handler");
839                 scm_call_2 (proc, $1, $2);
840         }
841         | bookpart_body SCM_TOKEN {
842                 // Evaluate and ignore #xxx, as opposed to \xxx
843                 parser->lexer_->eval_scm_token ($2);
844         }
845         | bookpart_body embedded_scm_active
846         {
847                 SCM out = SCM_UNDEFINED;
848                 if (Text_interface::is_markup ($2))
849                         out = scm_list_1 ($2);
850                 else if (Text_interface::is_markup_list ($2))
851                         out = $2;
852                 if (scm_is_pair (out))
853                 {
854                         SCM proc = parser->lexer_->lookup_identifier ("bookpart-text-handler");
855                         scm_call_2 (proc, $1, out);
856                 } else if (!scm_is_eq ($2, SCM_UNSPECIFIED))
857                         parser->parser_error (@2, _("bad expression type"));
858         }
859         | bookpart_body
860         {
861                 Book *book = unsmob_book ($1);
862                 if (!ly_is_module (book->header_))
863                         book->header_ = ly_make_module (false);
864                 parser->lexer_->add_scope (book->header_);
865         } lilypond_header
866         | bookpart_body error {
867                 Book *book = unsmob_book ($1);
868                 book->paper_ = 0;
869                 book->scores_ = SCM_EOL;
870         }
871         ;
872
873 score_block:
874         SCORE '{' score_body '}'        {
875                 $$ = $3;
876         }
877         ;
878
879 score_body:
880         music {
881                 SCM scorify = ly_lily_module_constant ("scorify-music");
882                 $$ = scm_call_2 (scorify, $1, parser->self_scm ());
883
884                 unsmob_score ($$)->origin ()->set_spot (@$);
885         }
886         | embedded_scm_active {
887                 Score *score;
888                 if (unsmob_score ($1))
889                         score = new Score (*unsmob_score ($1));
890                 else {
891                         score = new Score;
892                         parser->parser_error (@1, _("score expected"));
893                 }
894                 unsmob_score ($$)->origin ()->set_spot (@$);
895                 $$ = score->unprotect ();
896         }
897         | score_body
898         {
899                 Score *score = unsmob_score ($1);
900                 if (!ly_is_module (score->get_header ()))
901                         score->set_header (ly_make_module (false));
902                 parser->lexer_->add_scope (score->get_header ());
903         } lilypond_header
904         | score_body output_def {
905                 Output_def *od = unsmob_output_def ($2);
906                 if (od->lookup_variable (ly_symbol2scm ("is-paper")) == SCM_BOOL_T)
907                 {
908                         parser->parser_error (@2, _("\\paper cannot be used in \\score, use \\layout instead"));
909
910                 }
911                 else
912                 {
913                         unsmob_score ($1)->add_output_def (od);
914                 }
915         }
916         | score_body error {
917                 unsmob_score ($$)->error_found_ = true;
918         }
919         ;
920
921
922 /*
923         OUTPUT DEF
924 */
925
926 paper_block:
927         output_def {
928                 Output_def *od = unsmob_output_def ($1);
929
930                 if (od->lookup_variable (ly_symbol2scm ("is-paper")) != SCM_BOOL_T)
931                 {
932                         parser->parser_error (@1, _ ("need \\paper for paper block"));
933                         $$ = get_paper (parser)->unprotect ();
934                 }
935         }
936         ;
937
938
939 output_def:
940         output_def_body '}' {
941                 $$ = $1;
942
943                 parser->lexer_->remove_scope ();
944                 parser->lexer_->pop_state ();
945         }
946         ;
947
948 output_def_head:
949         PAPER {
950                 Output_def *p = get_paper (parser);
951                 p->input_origin_ = @$;
952                 parser->lexer_->add_scope (p->scope_);
953                 $$ = p->unprotect ();
954         }
955         | MIDI    {
956                 Output_def *p = get_midi (parser);
957                 $$ = p->unprotect ();
958                 parser->lexer_->add_scope (p->scope_);
959         }
960         | LAYOUT        {
961                 Output_def *p = get_layout (parser);
962
963                 parser->lexer_->add_scope (p->scope_);
964                 $$ = p->unprotect ();
965         }
966         ;
967
968 output_def_head_with_mode_switch:
969         output_def_head {
970                 parser->lexer_->push_initial_state ();
971                 $$ = $1;
972         }
973         ;
974
975 // We need this weird nonterminal because both music as well as a
976 // context definition can start with \context and the difference is
977 // only apparent after looking at the next token.  If it is '{', there
978 // is still time to escape from notes mode.
979
980 music_or_context_def:
981         music_arg
982         | context_def_spec_block
983         ;
984
985 output_def_body:
986         output_def_head_with_mode_switch '{' {
987                 $$ = $1;
988                 unsmob_output_def ($$)->input_origin_.set_spot (@$);
989         }
990         | output_def_head_with_mode_switch '{' OUTPUT_DEF_IDENTIFIER    {
991                 Output_def *o = unsmob_output_def ($3);
992                 o->input_origin_.set_spot (@$);
993                 $$ = o->self_scm ();
994                 parser->lexer_->remove_scope ();
995                 parser->lexer_->add_scope (o->scope_);
996         }
997         | output_def_body assignment  {
998
999         }
1000         | output_def_body embedded_scm  {
1001
1002         }
1003         | output_def_body
1004         {
1005                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
1006                 parser->lexer_->push_note_state (nn);
1007         } music_or_context_def
1008         {
1009                 parser->lexer_->pop_state ();
1010                 if (unsmob_context_def ($3))
1011                         assign_context_def (unsmob_output_def ($1), $3);
1012                 else {
1013
1014                         SCM proc = parser->lexer_->lookup_identifier
1015                                      ("output-def-music-handler");
1016                         scm_call_3 (proc, parser->self_scm (),
1017                                     $1, $3);
1018                 }
1019         }
1020         | output_def_body error {
1021
1022         }
1023         ;
1024
1025 tempo_event:
1026         TEMPO steno_duration '=' tempo_range    {
1027                 $$ = MAKE_SYNTAX ("tempo", @$, SCM_EOL, $2, $4);
1028         }
1029         | TEMPO scalar steno_duration '=' tempo_range   {
1030                 $$ = MAKE_SYNTAX ("tempo", @$, $2, $3, $5);
1031         }
1032         | TEMPO scalar {
1033                 $$ = MAKE_SYNTAX ("tempo", @$, $2);
1034         } %prec ':'
1035         ;
1036
1037 /*
1038 The representation of a  list is reversed to have efficient append.  */
1039
1040 music_list:
1041         /* empty */ {
1042                 $$ = SCM_EOL;
1043         }
1044         | music_list music_embedded {
1045                 if (unsmob_music ($2))
1046                         $$ = scm_cons ($2, $1);
1047         }
1048         | music_list error {
1049                 Music *m = MY_MAKE_MUSIC("Music", @$);
1050                 // ugh. code dup
1051                 m->set_property ("error-found", SCM_BOOL_T);
1052                 $$ = scm_cons (m->self_scm (), $1);
1053                 m->unprotect (); /* UGH */
1054         }
1055         ;
1056
1057 braced_music_list:
1058         '{' music_list '}'
1059         {
1060                 $$ = scm_reverse_x ($2, SCM_EOL);
1061         }
1062         ;
1063
1064 music:  music_arg
1065         | lyric_element_music
1066         ;
1067
1068 music_embedded:
1069         music
1070         {
1071                 if (unsmob_music ($1)->is_mus_type ("post-event")) {
1072                         parser->parser_error (@1, _ ("unexpected post-event"));
1073                         $$ = SCM_UNSPECIFIED;
1074                 }
1075         }
1076         | music_embedded_backup
1077         {
1078                 $$ = $1;
1079         }
1080         | music_embedded_backup BACKUP lyric_element_music
1081         {
1082                 $$ = $3;
1083         }
1084         | multiplied_duration post_events
1085         {
1086                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
1087
1088                 parser->default_duration_ = *unsmob_duration ($1);
1089                 n->set_property ("duration", $1);
1090
1091                 if (scm_is_pair ($2))
1092                         n->set_property ("articulations",
1093                                          scm_reverse_x ($2, SCM_EOL));
1094                 $$ = n->unprotect ();
1095         }
1096         ;
1097
1098 music_embedded_backup:
1099         embedded_scm
1100         {
1101                 if (scm_is_eq ($1, SCM_UNSPECIFIED))
1102                         $$ = $1;
1103                 else if (Music *m = unsmob_music ($1)) {
1104                         if (m->is_mus_type ("post-event")) {
1105                                 parser->parser_error
1106                                         (@1, _ ("unexpected post-event"));
1107                                 $$ = SCM_UNSPECIFIED;
1108                         } else
1109                                 $$ = $1;
1110                 } else if (parser->lexer_->is_lyric_state ()
1111                            && Text_interface::is_markup ($1))
1112                         MYBACKUP (LYRIC_ELEMENT, $1, @1);
1113                 else {
1114                         @$.warning (_ ("Ignoring non-music expression"));
1115                         $$ = $1;
1116                 }
1117         }
1118         ;
1119
1120 music_arg:
1121         simple_music
1122         {
1123                 $$ = make_music_from_simple (parser, @1, $1);
1124                 if (!unsmob_music ($$))
1125                 {
1126                         parser->parser_error (@1, _ ("music expected"));
1127                         $$ = MAKE_SYNTAX ("void-music", @$);
1128                 }
1129         }
1130         | composite_music %prec COMPOSITE
1131         ;
1132
1133 music_assign:
1134         simple_music
1135         | composite_music %prec COMPOSITE
1136         ;
1137
1138 repeated_music:
1139         REPEAT simple_string unsigned_number music
1140         {
1141                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, SCM_EOL);
1142         }
1143         | REPEAT_IDENTIFIER music
1144         {
1145                 $$ = MAKE_SYNTAX ("repeat", @$, scm_car ($1), scm_cdr ($1),
1146                                   $2, SCM_EOL);
1147         }
1148         | REPEAT simple_string unsigned_number music ALTERNATIVE braced_music_list
1149         {
1150                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, $6);
1151         }
1152         | REPEAT_IDENTIFIER music ALTERNATIVE braced_music_list
1153         {
1154                 $$ = MAKE_SYNTAX ("repeat", @$, scm_car ($1), scm_cdr ($1),
1155                                   $2, $4);
1156         }
1157         ;
1158
1159 sequential_music:
1160         SEQUENTIAL braced_music_list {
1161                 $$ = MAKE_SYNTAX ("sequential-music", @$, $2);
1162         }
1163         | braced_music_list {
1164                 $$ = MAKE_SYNTAX ("sequential-music", @$, $1);
1165         }
1166         ;
1167
1168 simultaneous_music:
1169         SIMULTANEOUS braced_music_list {
1170                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, $2);
1171         }
1172         | DOUBLE_ANGLE_OPEN music_list DOUBLE_ANGLE_CLOSE       {
1173                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, scm_reverse_x ($2, SCM_EOL));
1174         }
1175         ;
1176
1177 simple_music:
1178         event_chord
1179         | music_property_def
1180         | context_change
1181         ;
1182
1183 context_modification:
1184         WITH
1185         {
1186                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
1187                 parser->lexer_->push_note_state (nn);
1188         } '{' context_mod_list '}'
1189         {
1190                 parser->lexer_->pop_state ();
1191                 $$ = $4;
1192         }
1193         | WITH CONTEXT_MOD_IDENTIFIER
1194         {
1195                 $$ = $2;
1196         }
1197         | CONTEXT_MOD_IDENTIFIER
1198         {
1199                 $$ = $1;
1200         }
1201         | WITH context_modification_arg
1202         {
1203                 if (unsmob_music ($2)) {
1204                         SCM proc = parser->lexer_->lookup_identifier ("context-mod-music-handler");
1205                         $2 = scm_call_2 (proc, parser->self_scm (), $2);
1206                 }
1207                 if (unsmob_context_mod ($2))
1208                         $$ = $2;
1209                 else {
1210                         parser->parser_error (@2, _ ("not a context mod"));
1211                         $$ = Context_mod ().smobbed_copy ();
1212                 }
1213         }
1214         ;
1215
1216 context_modification_arg:
1217         embedded_scm_closed
1218         | MUSIC_IDENTIFIER
1219         ;
1220
1221 optional_context_mod:
1222         /**/ {
1223             $$ = SCM_EOL;
1224         }
1225         | context_modification
1226         {
1227               $$ = $1;
1228         }
1229         ;
1230
1231 context_mod_list:
1232         /**/ {
1233             $$ = Context_mod ().smobbed_copy ();
1234         }
1235         | context_mod_list context_mod  {
1236                 if (!SCM_UNBNDP ($2))
1237                         unsmob_context_mod ($1)->add_context_mod ($2);
1238         }
1239         | context_mod_list CONTEXT_MOD_IDENTIFIER {
1240                  Context_mod *md = unsmob_context_mod ($2);
1241                  if (md)
1242                      unsmob_context_mod ($1)->add_context_mods (md->get_mods ());
1243         }
1244         | context_mod_list context_mod_embedded {
1245                 unsmob_context_mod ($1)->add_context_mods
1246                         (unsmob_context_mod ($2)->get_mods ());
1247         }
1248         ;
1249
1250 composite_music:
1251         complex_music
1252         | music_bare
1253         ;
1254
1255 /* Music that can be parsed without lookahead */
1256 closed_music:
1257         music_bare
1258         | complex_music_prefix closed_music
1259         {
1260                 $$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
1261         }
1262         | music_function_call_closed
1263         ;
1264
1265 music_bare:
1266         mode_changed_music
1267         | MUSIC_IDENTIFIER
1268         | grouped_music_list
1269         ;
1270
1271 grouped_music_list:
1272         simultaneous_music              { $$ = $1; }
1273         | sequential_music              { $$ = $1; }
1274         ;
1275
1276 /* Function argument lists are arguably the most complex part in the
1277  * parser.  They are pretty tricky to understand because of the way
1278  * they are processed, and because of optional arguments that can be
1279  * omitted.  When there are several optional arguments in a row,
1280  * omitting one automatically omits all following arguments.  Optional
1281  * arguments can only be omitted when either
1282  *
1283  * a) the omission is explicitly started with \default
1284  * b) the omission is implicitly started by an argument not matching
1285  *    its predicate, and there is a mandatory argument later that can
1286  *    "catch" the argument that does not fit.
1287  *
1288  * When argument parsing starts, the lexer pushes EXPECT_SCM tokens
1289  * (corresponding to mandatory arguments and having a predicate
1290  * function as semantic value) or EXPECT_OPTIONAL EXPECT_SCM (where
1291  * the semantic value of the EXPECT_OPTIONAL token is the default to
1292  * use when the optional argument is omitted, and EXPECT_SCM again has
1293  * the argument predicate as semantic value) in reverse order to the
1294  * parser, followed by EXPECT_NO_MORE_ARGS.  The argument list is then
1295  * processed inside-out while actual tokens are consumed.
1296  *
1297  * This means that the argument list tokens determine the actions
1298  * taken as they arrive.  The structure of the argument list is known
1299  * to the parser and stored in its parse stack when the first argument
1300  * is being parsed.  What the parser does not know is which predicates
1301  * will match and whether or not \default will be appearing in the
1302  * argument list, and where.
1303  *
1304  * Many of the basic nonterminals used for argument list scanning come
1305  * in a "normal" and a "closed" flavor.  A closed expression is one
1306  * that can be parsed without a lookahead token.  That makes it
1307  * feasible for an optional argument that may need to be skipped:
1308  * skipping can only be accomplished by pushing back the token into
1309  * the lexer, and that only works when there is no lookahead token.
1310  *
1311  * Sequences of 0 or more optional arguments are scanned using either
1312  * function_arglist_backup or function_arglist_nonbackup.  The first
1313  * is used when optional arguments are followed by at least one
1314  * mandatory argument: in that case optional arguments may be skipped
1315  * by either a false predicate (in which case the expression will be
1316  * pushed back as one or more tokens, preceded by a BACKUP token) or
1317  * by using \default.
1318  *
1319  * If optional arguments are at the end of the argument list, they are
1320  * instead scanned using function_arglist_nonbackup: here the only
1321  * manner to enter into skipping of optional arguments is the use of
1322  * \default.
1323  *
1324  * The argument list of a normal function call is parsed using
1325  * function_arglist.  The part of an argument list before a mandatory
1326  * argument is parsed using function_arglist_optional.
1327  *
1328  * The difference is that leading optional arguments are scanned using
1329  * function_arglist_nonbackup and function_arglist_backup,
1330  * respectively.
1331  *
1332  * Most other details are obvious in the rules themselves.
1333  *
1334  */
1335
1336 function_arglist_nonbackup_common:
1337         EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup FRACTION
1338         {
1339                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1340         }
1341         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup post_event_nofinger
1342         {
1343                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1344         }
1345         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' UNSIGNED
1346         {
1347                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1348                 if (scm_is_true (scm_call_1 ($2, n)))
1349                         $$ = scm_cons (n, $3);
1350                 else {
1351                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1352                         t->set_property ("digit", $5);
1353                         $$ = check_scheme_arg (parser, @4, t->unprotect (),
1354                                                $3, $2, n);
1355                 }
1356                 
1357         }
1358         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' REAL
1359         {
1360                 $$ = check_scheme_arg (parser, @4,
1361                                        scm_difference ($5, SCM_UNDEFINED),
1362                                        $3, $2);
1363         }
1364         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' NUMBER_IDENTIFIER
1365         {
1366                 $$ = check_scheme_arg (parser, @4,
1367                                        scm_difference ($5, SCM_UNDEFINED),
1368                                        $3, $2);
1369         }
1370         ;
1371
1372 function_arglist_closed_nonbackup:
1373         function_arglist_nonbackup_common
1374         | function_arglist_closed_common
1375         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup embedded_scm_arg_closed
1376         {
1377                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1378         }
1379         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup bare_number_closed
1380         {
1381                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1382         }
1383         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup SCM_IDENTIFIER
1384         {
1385                 $$ = check_scheme_arg (parser, @4,
1386                                        try_string_variants ($2, $4),
1387                                        $3, $2, $4);
1388         }
1389         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup STRING
1390         {
1391                 $$ = check_scheme_arg (parser, @4,
1392                                        try_string_variants ($2, $4),
1393                                        $3, $2, $4);
1394         }
1395         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup full_markup
1396         {
1397                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1398         }
1399         ;
1400
1401 symbol_list_arg:
1402         SYMBOL_LIST
1403         | SYMBOL_LIST '.' symbol_list_rev
1404         {
1405                 $$ = scm_append (scm_list_2 ($1, scm_reverse_x ($3, SCM_EOL)));
1406         }
1407         ;
1408
1409 symbol_list_rev:
1410         symbol_list_part
1411         | symbol_list_rev '.' symbol_list_part
1412         {
1413                 $$ = scm_append_x (scm_list_2 ($3, $1));
1414         }
1415         ;
1416
1417 // symbol_list_part delivers elements in reverse copy.
1418
1419 symbol_list_part:
1420         symbol_list_element
1421         {
1422                 SCM sym_l_p = ly_lily_module_constant ("symbol-list?");
1423                 $$ = try_string_variants (sym_l_p, $1);
1424                 if (SCM_UNBNDP ($$)) {
1425                         parser->parser_error (@1, _("not a symbol"));
1426                         $$ = SCM_EOL;
1427                 } else
1428                         $$ = scm_reverse ($$);
1429         }
1430         ;
1431
1432
1433 symbol_list_element:
1434         STRING
1435         | embedded_scm_bare
1436         ;
1437
1438
1439 function_arglist_nonbackup:
1440         function_arglist_nonbackup_common
1441         | function_arglist_common
1442         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup embedded_scm_arg
1443         {
1444                 if (scm_is_true (scm_call_1 ($2, $4)))
1445                         $$ = scm_cons ($4, $3);
1446                 else
1447                         $$ = check_scheme_arg (parser, @4,
1448                                                make_music_from_simple
1449                                                (parser, @4, $4),
1450                                                $3, $2);
1451         }
1452         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup bare_number_common
1453         {
1454                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1455         }
1456         | function_arglist_nonbackup_reparse REPARSE duration_length
1457         {
1458                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1459         }
1460         | function_arglist_nonbackup_reparse REPARSE bare_number_common
1461         {
1462                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1463         }
1464         | function_arglist_nonbackup_reparse REPARSE SCM_ARG
1465         {
1466                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1467         }
1468         | function_arglist_nonbackup_reparse REPARSE lyric_element_music
1469         {
1470                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1471         }
1472         | function_arglist_nonbackup_reparse REPARSE symbol_list_arg
1473         {
1474                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1475         }
1476         ;
1477
1478 function_arglist_nonbackup_reparse:
1479         EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup SCM_IDENTIFIER
1480         {
1481                 $$ = $3;
1482                 SCM res = try_string_variants ($2, $4);
1483                 if (!SCM_UNBNDP (res))
1484                         if (scm_is_pair (res))
1485                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1486                         else
1487                                 MYREPARSE (@4, $2, SCM_ARG, res);
1488                 else if (scm_is_true
1489                          (scm_call_1
1490                           ($2, make_music_from_simple
1491                            (parser, @4, $4))))
1492                         MYREPARSE (@4, $2, STRING, $4);
1493                 else
1494                         MYREPARSE (@4, $2, SCM_ARG, $4);
1495         }
1496         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup STRING
1497         {
1498                 $$ = $3;
1499                 SCM res = try_string_variants ($2, $4);
1500                 if (!SCM_UNBNDP (res))
1501                         if (scm_is_pair (res))
1502                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1503                         else
1504                                 MYREPARSE (@4, $2, SCM_ARG, res);
1505                 else if (scm_is_true
1506                          (scm_call_1
1507                           ($2, make_music_from_simple
1508                            (parser, @4, $4))))
1509                         MYREPARSE (@4, $2, STRING, $4);
1510                 else
1511                         MYREPARSE (@4, $2, SCM_ARG, $4);
1512         }
1513         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup full_markup
1514         {
1515                 $$ = $3;
1516                 if (scm_is_true (scm_call_1 ($2, $4)))
1517                         MYREPARSE (@4, $2, SCM_ARG, $4);
1518                 else if (scm_is_true
1519                          (scm_call_1
1520                           ($2, make_music_from_simple
1521                            (parser, @4, $4))))
1522                         MYREPARSE (@4, $2, STRING, $4);
1523                 else
1524                         MYREPARSE (@4, $2, SCM_ARG, $4);
1525         }
1526         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup UNSIGNED
1527         {
1528                 $$ = $3;
1529                 if (scm_is_true (scm_call_1 ($2, $4)))
1530                         MYREPARSE (@4, $2, REAL, $4);
1531                 else {
1532                         SCM d = make_duration ($4);
1533                         if (SCM_UNBNDP (d) || scm_is_false (scm_call_1 ($2, d)))
1534                                 MYREPARSE (@4, $2, REAL, $4); // trigger error
1535                         else
1536                                 MYREPARSE (@4, $2, DURATION_IDENTIFIER, d);
1537                 }
1538         }
1539         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup DURATION_IDENTIFIER {
1540                 $$ = $3;
1541                 MYREPARSE (@4, $2, DURATION_IDENTIFIER, $4);
1542         }
1543         ;
1544
1545
1546 function_arglist_backup:
1547         function_arglist_backup_common
1548         | function_arglist_common
1549         ;
1550
1551 function_arglist_backup_common:
1552         EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup embedded_scm_arg_closed
1553         {
1554                 if (scm_is_true (scm_call_1 ($2, $4)))
1555                 {
1556                         $$ = scm_cons ($4, $3);
1557                 } else {
1558                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1559                         MYBACKUP (SCM_ARG, $4, @4);
1560                 }
1561         }
1562         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup REPEAT simple_string unsigned_number
1563         {
1564                 $4 = MAKE_SYNTAX ("repeat", @4, $5, $6,
1565                                   MY_MAKE_MUSIC ("Music", @4)->unprotect (),
1566                                   SCM_EOL);
1567                 if (scm_is_true (scm_call_1 ($2, $4)))
1568                 {
1569                         $$ = $3;
1570                         MYREPARSE (@4, $2, REPEAT_IDENTIFIER, scm_cons ($5, $6));
1571                 } else {
1572                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1573                         MYBACKUP (REPEAT_IDENTIFIER, scm_cons ($5, $6), @4);
1574                 }
1575         }
1576         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup chord_body
1577         {
1578                 if (scm_is_true (scm_call_1 ($2, $4)))
1579                 {
1580                         $$ = $3;
1581                         MYREPARSE (@4, $2, CHORD_BODY_IDENTIFIER, $4);
1582                 } else {
1583                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1584                         MYBACKUP (CHORD_BODY_IDENTIFIER, $4, @4);
1585                 }
1586         }
1587         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup post_event_nofinger
1588         {
1589                 if (scm_is_true (scm_call_1 ($2, $4)))
1590                 {
1591                         $$ = scm_cons ($4, $3);
1592                 } else {
1593                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1594                         MYBACKUP (EVENT_IDENTIFIER, $4, @4);
1595                 }
1596         }
1597         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup full_markup
1598         {
1599                 if (scm_is_true (scm_call_1 ($2, $4)))
1600                         $$ = scm_cons ($4, $3);
1601                 else {
1602                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1603                         MYBACKUP (LYRIC_ELEMENT, $4, @4);
1604                 }
1605         }
1606         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup UNSIGNED
1607         {
1608                 if (scm_is_true (scm_call_1 ($2, $4)))
1609                 {
1610                         MYREPARSE (@4, $2, REAL, $4);
1611                         $$ = $3;
1612                 } else {
1613                         SCM d = make_duration ($4);
1614                         if (SCM_UNBNDP (d) || scm_is_false (scm_call_1 ($2, d)))
1615                         {
1616                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1617                                 MYBACKUP (UNSIGNED, $4, @4);
1618                         } else {
1619                                 MYREPARSE (@4, $2, DURATION_IDENTIFIER, d);
1620                                 $$ = $3;
1621                         }
1622                 }
1623         }
1624         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup REAL
1625         {
1626                 if (scm_is_true (scm_call_1 ($2, $4)))
1627                 {
1628                         $$ = $3;
1629                         MYREPARSE (@4, $2, REAL, $4);
1630                 } else {
1631                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1632                         MYBACKUP (REAL, $4, @4);
1633                 }
1634         }
1635         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup NUMBER_IDENTIFIER
1636         {
1637                 if (scm_is_true (scm_call_1 ($2, $4)))
1638                 {
1639                         $$ = scm_cons ($4, $3);
1640                 } else {
1641                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1642                         MYBACKUP (NUMBER_IDENTIFIER, $4, @4);
1643                 }
1644         }
1645         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup FRACTION
1646         {
1647                 if (scm_is_true (scm_call_1 ($2, $4)))
1648                 {
1649                         $$ = scm_cons ($4, $3);
1650                 } else {
1651                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1652                         MYBACKUP (FRACTION, $4, @4);
1653                 }
1654         }
1655         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' UNSIGNED
1656         {
1657                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1658                 if (scm_is_true (scm_call_1 ($2, n))) {
1659                         $$ = $3;
1660                         MYREPARSE (@5, $2, REAL, n);
1661                 } else {
1662                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1663                         t->set_property ("digit", $5);
1664                         $$ = t->unprotect ();
1665                         if (scm_is_true (scm_call_1 ($2, $$)))
1666                                 $$ = scm_cons ($$, $3);
1667                         else {
1668                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1669                                 MYBACKUP (UNSIGNED, $5, @5);
1670                                 parser->lexer_->push_extra_token ('-');
1671                         }
1672                 }
1673                 
1674         }
1675         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' REAL
1676         {
1677                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1678                 if (scm_is_true (scm_call_1 ($2, n))) {
1679                         MYREPARSE (@5, $2, REAL, n);
1680                         $$ = $3;
1681                 } else {
1682                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1683                         MYBACKUP (REAL, n, @5);
1684                 }
1685         }
1686         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' NUMBER_IDENTIFIER
1687         {
1688                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1689                 if (scm_is_true (scm_call_1 ($2, n))) {
1690                         $$ = scm_cons (n, $3);
1691                 } else {
1692                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1693                         MYBACKUP (NUMBER_IDENTIFIER, n, @5);
1694                 }
1695         }
1696         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup PITCH_IDENTIFIER
1697         {
1698                 if (scm_is_true (scm_call_1 ($2, $4)))
1699                 {
1700                         $$ = scm_cons ($4, $3);
1701                 } else {
1702                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1703                         MYBACKUP (PITCH_IDENTIFIER, $4, @4);
1704                 }
1705         }
1706         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup NOTENAME_PITCH
1707         {
1708                 if (scm_is_true (scm_call_1 ($2, $4)))
1709                 {
1710                         MYREPARSE (@4, $2, NOTENAME_PITCH, $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                 if (scm_is_true (scm_call_1 ($2, $4)))
1720                 {
1721                         MYREPARSE (@4, $2, TONICNAME_PITCH, $4);
1722                         $$ = $3;
1723                 } else {
1724                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1725                         MYBACKUP (TONICNAME_PITCH, $4, @4);
1726                 }
1727         }
1728         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup DURATION_IDENTIFIER
1729         {
1730                 if (scm_is_true (scm_call_1 ($2, $4)))
1731                 {
1732                         MYREPARSE (@4, $2, DURATION_IDENTIFIER, $4);
1733                         $$ = $3;
1734                 } else {
1735                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1736                         MYBACKUP (DURATION_IDENTIFIER, $4, @4);
1737                 }
1738         }
1739         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup SCM_IDENTIFIER
1740         {
1741                 SCM res = try_string_variants ($2, $4);
1742                 if (!SCM_UNBNDP (res))
1743                         if (scm_is_pair (res)) {
1744                                 $$ = $3;
1745                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1746                         }
1747                         else
1748                                 $$ = scm_cons (res, $3);
1749                 else {
1750                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1751                         MYBACKUP (SCM_IDENTIFIER, $4, @4);
1752                 }
1753         }
1754         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup STRING
1755         {
1756                 SCM res = try_string_variants ($2, $4);
1757                 if (!SCM_UNBNDP (res))
1758                         if (scm_is_pair (res)) {
1759                                 $$ = $3;
1760                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1761                         }
1762                         else
1763                                 $$ = scm_cons (res, $3);
1764                 else {
1765                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1766                         MYBACKUP (STRING, $4, @4);
1767                 }
1768         }
1769         | function_arglist_backup REPARSE music_assign
1770         {
1771                 if (scm_is_true (scm_call_1 ($2, $3)))
1772                         $$ = scm_cons ($3, $1);
1773                 else
1774                         $$ = check_scheme_arg (parser, @3,
1775                                                make_music_from_simple
1776                                                (parser, @3, $3),
1777                                                $1, $2);
1778         }
1779         | function_arglist_backup REPARSE bare_number_common
1780         {
1781                 $$ = check_scheme_arg (parser, @3,
1782                                        $3, $1, $2);
1783         }
1784         | function_arglist_backup REPARSE duration_length
1785         {
1786                 $$ = check_scheme_arg (parser, @3,
1787                                        $3, $1, $2);
1788         }
1789         | function_arglist_backup REPARSE symbol_list_arg
1790         {
1791                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1792         }
1793         ;
1794
1795 function_arglist:
1796         function_arglist_nonbackup
1797         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_nonbackup DEFAULT
1798         {
1799                 $$ = scm_cons (loc_on_music (@4, $1), $3);
1800         }
1801         ;
1802
1803 function_arglist_skip_nonbackup:
1804         function_arglist_nonbackup
1805         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_nonbackup
1806         {
1807                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1808         }
1809         ;
1810
1811 function_arglist_common:
1812         EXPECT_NO_MORE_ARGS {
1813                 $$ = SCM_EOL;
1814         }
1815         | EXPECT_SCM function_arglist_optional embedded_scm_arg
1816         {
1817                 if (scm_is_true (scm_call_1 ($1, $3)))
1818                         $$ = scm_cons ($3, $2);
1819                 else
1820                         $$ = check_scheme_arg (parser, @3,
1821                                                make_music_from_simple
1822                                                (parser, @3, $3),
1823                                                $2, $1);
1824         }
1825         | EXPECT_SCM function_arglist_optional bare_number_common
1826         {
1827                 $$ = check_scheme_arg (parser, @3,
1828                                        $3, $2, $1);
1829         }
1830         | EXPECT_SCM function_arglist_optional FRACTION
1831         {
1832                 $$ = check_scheme_arg (parser, @3,
1833                                        $3, $2, $1);
1834         }
1835         | EXPECT_SCM function_arglist_optional post_event_nofinger
1836         {
1837                 $$ = check_scheme_arg (parser, @3,
1838                                        $3, $2, $1);
1839         }
1840         | EXPECT_SCM function_arglist_optional '-' NUMBER_IDENTIFIER
1841         {
1842                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1843                 $$ = check_scheme_arg (parser, @4, n, $2, $1);
1844         }
1845         | function_arglist_common_reparse REPARSE SCM_ARG
1846         {
1847                 $$ = check_scheme_arg (parser, @3,
1848                                        $3, $1, $2);
1849         }
1850         | function_arglist_common_reparse REPARSE lyric_element_music
1851         {
1852                 $$ = check_scheme_arg (parser, @3,
1853                                        $3, $1, $2);
1854         }
1855         | function_arglist_common_reparse REPARSE bare_number_common
1856         {
1857                 $$ = check_scheme_arg (parser, @3,
1858                                        $3, $1, $2);
1859         }
1860         | function_arglist_common_reparse REPARSE duration_length
1861         {
1862                 $$ = check_scheme_arg (parser, @3,
1863                                        $3, $1, $2);
1864         }
1865         | function_arglist_common_reparse REPARSE symbol_list_arg
1866         {
1867                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1868         }
1869         ;
1870
1871 function_arglist_common_reparse:
1872         EXPECT_SCM function_arglist_optional SCM_IDENTIFIER
1873         {
1874                 $$ = $2;
1875                 SCM res = try_string_variants ($1, $3);
1876                 if (!SCM_UNBNDP (res))
1877                         if (scm_is_pair (res))
1878                                 MYREPARSE (@3, $1, SYMBOL_LIST, res);
1879                         else
1880                                 MYREPARSE (@3, $1, SCM_ARG, res);
1881                 else if (scm_is_true
1882                          (scm_call_1
1883                           ($1, make_music_from_simple (parser, @3, $3))))
1884                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1885                 else
1886                         // This is going to flag a syntax error, we
1887                         // know the predicate to be false.
1888                         MYREPARSE (@3, $1, SCM_ARG, $3);
1889         }
1890         | EXPECT_SCM function_arglist_optional STRING
1891         {
1892                 $$ = $2;
1893                 SCM res = try_string_variants ($1, $3);
1894                 if (!SCM_UNBNDP (res))
1895                         if (scm_is_pair (res))
1896                                 MYREPARSE (@3, $1, SYMBOL_LIST, res);
1897                         else
1898                                 MYREPARSE (@3, $1, SCM_ARG, res);
1899                 else if (scm_is_true
1900                          (scm_call_1
1901                           ($1, make_music_from_simple (parser, @3, $3))))
1902                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1903                 else
1904                         // This is going to flag a syntax error, we
1905                         // know the predicate to be false.
1906                         MYREPARSE (@3, $1, SCM_ARG, $3);
1907         }
1908         | EXPECT_SCM function_arglist_optional full_markup
1909         {
1910                 $$ = $2;
1911                 if (scm_is_true (scm_call_1 ($1, $3)))
1912                         MYREPARSE (@3, $1, SCM_ARG, $3);
1913                 else if (scm_is_true
1914                          (scm_call_1
1915                           ($1, make_music_from_simple (parser, @3, $3))))
1916                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1917                 else
1918                         // This is going to flag a syntax error, we
1919                         // know the predicate to be false.
1920                         MYREPARSE (@3, $1, SCM_ARG, $3);
1921         }
1922         | EXPECT_SCM function_arglist_optional UNSIGNED
1923         {
1924                 $$ = $2;
1925                 if (scm_is_true (scm_call_1 ($1, $3)))
1926                         MYREPARSE (@3, $1, REAL, $3);
1927                 else {
1928                         SCM d = make_duration ($3);
1929                         if (SCM_UNBNDP (d) || scm_is_false (scm_call_1 ($1, d)))
1930                                 MYREPARSE (@3, $1, REAL, $3);
1931                         else
1932                                 MYREPARSE (@3, $1, DURATION_IDENTIFIER, d);
1933                 }
1934         }
1935         | EXPECT_SCM function_arglist_optional DURATION_IDENTIFIER
1936         {
1937                 $$ = $2;
1938                 MYREPARSE (@3, $1, DURATION_IDENTIFIER, $3);
1939         }
1940         | EXPECT_SCM function_arglist_optional '-' UNSIGNED
1941         {
1942                 $$ = $2;
1943                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1944                 if (scm_is_true (scm_call_1 ($1, n)))
1945                         MYREPARSE (@4, $1, REAL, n);
1946                 else {
1947                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @4);
1948                         t->set_property ("digit", $4);
1949                         SCM m = t->unprotect ();
1950                         if (scm_is_true (scm_call_1 ($1, m)))
1951                                 MYREPARSE (@4, $1, SCM_ARG, m);
1952                         else
1953                                 MYREPARSE (@4, $1, SCM_ARG, $4);
1954                 }
1955                 
1956         }
1957         | EXPECT_SCM function_arglist_optional '-' REAL
1958         {
1959                 $$ = $2;
1960                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1961                 MYREPARSE (@4, $1, REAL, n);
1962         }
1963         ;
1964
1965 function_arglist_closed:
1966         function_arglist_closed_nonbackup
1967         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_nonbackup DEFAULT
1968         {
1969                 $$ = scm_cons (loc_on_music (@4, $1), $3);
1970         }
1971         ;
1972
1973 function_arglist_closed_common:
1974         EXPECT_NO_MORE_ARGS {
1975                 $$ = SCM_EOL;
1976         }
1977         | EXPECT_SCM function_arglist_optional embedded_scm_arg_closed
1978         {
1979                 $$ = check_scheme_arg (parser, @3,
1980                                        $3, $2, $1);
1981         }
1982         | EXPECT_SCM function_arglist_optional bare_number_common_closed
1983         {
1984                 $$ = check_scheme_arg (parser, @3,
1985                                        $3, $2, $1);
1986         }
1987         | EXPECT_SCM function_arglist_optional '-' NUMBER_IDENTIFIER
1988         {
1989                 $$ = check_scheme_arg (parser, @3,
1990                                        scm_difference ($4, SCM_UNDEFINED),
1991                                        $2, $1);
1992         }
1993         | EXPECT_SCM function_arglist_optional post_event_nofinger
1994         {
1995                 $$ = check_scheme_arg (parser, @3,
1996                                        $3, $2, $1);
1997         }
1998         | EXPECT_SCM function_arglist_optional FRACTION
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 STRING '=' STRING  {
2212                 $$ = MAKE_SYNTAX ("context-change", @$, scm_string_to_symbol ($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         | FRACTION
2533         | STRING
2534         | full_markup
2535         ;
2536
2537 event_chord:
2538         simple_element post_events {
2539                 // Let the rhythmic music iterator sort this mess out.
2540                 if (scm_is_pair ($2)) {
2541                         $$ = make_music_from_simple (parser, @1, $1);
2542                         if (unsmob_music ($$))
2543                                 unsmob_music ($$)->set_property ("articulations",
2544                                                                  scm_reverse_x ($2, SCM_EOL));
2545                         else
2546                         {
2547                                 parser->parser_error (@1, _("music expected"));
2548                                 $$ = MAKE_SYNTAX ("void-music", @1);
2549                         }
2550                 }
2551         } %prec ':'
2552         | simple_chord_elements post_events     {
2553                 SCM elts = ly_append2 ($1, scm_reverse_x ($2, SCM_EOL));
2554
2555                 Input i;
2556                 /* why is this giving wrong start location? -ns
2557                  * i = @$; */
2558                 i.set_location (@1, @2);
2559                 $$ = MAKE_SYNTAX ("event-chord", i, elts);
2560         } %prec ':'
2561         | CHORD_REPETITION optional_notemode_duration post_events {
2562                 Input i;
2563                 i.set_location (@1, @3);
2564                 $$ = MAKE_SYNTAX ("repetition-chord", i,
2565                                   $2, scm_reverse_x ($3, SCM_EOL));
2566         } %prec ':'
2567         | MULTI_MEASURE_REST optional_notemode_duration post_events {
2568                 Input i;
2569                 i.set_location (@1, @3);
2570                 $$ = MAKE_SYNTAX ("multi-measure-rest", i, $2,
2571                                   scm_reverse_x ($3, SCM_EOL));
2572         } %prec ':'
2573         | command_element
2574         | note_chord_element
2575         ;
2576
2577
2578 note_chord_element:
2579         chord_body optional_notemode_duration post_events
2580         {
2581                 Music *m = unsmob_music ($1);
2582                 SCM dur = unsmob_duration ($2)->smobbed_copy ();
2583                 SCM es = m->get_property ("elements");
2584                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
2585
2586                 for (SCM s = es; scm_is_pair (s); s = scm_cdr (s))
2587                   unsmob_music (scm_car (s))->set_property ("duration", dur);
2588                 es = ly_append2 (es, postevs);
2589
2590                 m-> set_property ("elements", es);
2591                 m->set_spot (@$);
2592                 $$ = m->self_scm ();
2593         } %prec ':'
2594         ;
2595
2596 chord_body:
2597         ANGLE_OPEN chord_body_elements ANGLE_CLOSE
2598         {
2599                 $$ = MAKE_SYNTAX ("event-chord", @$, scm_reverse_x ($2, SCM_EOL));
2600         }
2601         | CHORD_BODY_IDENTIFIER
2602         ;
2603
2604 chord_body_elements:
2605         /* empty */             { $$ = SCM_EOL; }
2606         | chord_body_elements chord_body_element {
2607                 if (!SCM_UNBNDP ($2))
2608                         $$ = scm_cons ($2, $1);
2609         }
2610         ;
2611
2612 chord_body_element:
2613         pitch exclamations questions octave_check post_events
2614         {
2615                 bool q = to_boolean ($3);
2616                 bool ex = to_boolean ($2);
2617                 SCM check = $4;
2618                 SCM post = $5;
2619
2620                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2621                 n->set_property ("pitch", $1);
2622                 if (q)
2623                         n->set_property ("cautionary", SCM_BOOL_T);
2624                 if (ex || q)
2625                         n->set_property ("force-accidental", SCM_BOOL_T);
2626
2627                 if (scm_is_pair (post)) {
2628                         SCM arts = scm_reverse_x (post, SCM_EOL);
2629                         n->set_property ("articulations", arts);
2630                 }
2631                 if (scm_is_number (check))
2632                 {
2633                         int q = scm_to_int (check);
2634                         n->set_property ("absolute-octave", scm_from_int (q-1));
2635                 }
2636
2637                 $$ = n->unprotect ();
2638         }
2639         | DRUM_PITCH post_events {
2640                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2641                 n->set_property ("drum-type", $1);
2642
2643                 if (scm_is_pair ($2)) {
2644                         SCM arts = scm_reverse_x ($2, SCM_EOL);
2645                         n->set_property ("articulations", arts);
2646                 }
2647                 $$ = n->unprotect ();
2648         }
2649         | music_function_chord_body
2650         {
2651                 Music *m = unsmob_music ($1);
2652
2653                 while (m && m->is_mus_type ("music-wrapper-music")) {
2654                         $$ = m->get_property ("element");
2655                         m = unsmob_music ($$);
2656                 }
2657
2658                 if (!(m && m->is_mus_type ("rhythmic-event"))) {
2659                         parser->parser_error (@$, _ ("not a rhythmic event"));
2660                         $$ = SCM_UNDEFINED;
2661                 }
2662         }
2663         ;
2664
2665 music_function_chord_body:
2666         music_function_call
2667         | MUSIC_IDENTIFIER
2668         ;
2669
2670 // Event functions may only take closed arglists, otherwise it would
2671 // not be clear whether a following postevent should be associated
2672 // with the last argument of the event function or with the expression
2673 // for which the function call acts itself as event.
2674
2675 music_function_call_closed:
2676         MUSIC_FUNCTION function_arglist_closed {
2677                 $$ = MAKE_SYNTAX ("music-function", @$,
2678                                          $1, $2);
2679         }
2680         ;
2681
2682 event_function_event:
2683         EVENT_FUNCTION function_arglist_closed {
2684                 $$ = MAKE_SYNTAX ("music-function", @$,
2685                                          $1, $2);
2686         }
2687         ;
2688
2689 command_element:
2690         command_event {
2691                 $$ = $1;
2692         }
2693         ;
2694
2695 command_event:
2696         tempo_event {
2697                 $$ = $1;
2698         }
2699         ;
2700
2701
2702 post_events:
2703         /* empty */ {
2704                 $$ = SCM_EOL;
2705         }
2706         | post_events post_event {
2707                 $$ = $1;
2708                 if (Music *m = unsmob_music ($2))
2709                 {
2710                         if (m->is_mus_type ("post-event-wrapper"))
2711                         {
2712                                 for (SCM p = m->get_property ("elements");
2713                                      scm_is_pair (p);
2714                                      p = scm_cdr (p))
2715                                 {
2716                                         $$ = scm_cons (scm_car (p), $$);
2717                                 }
2718                         } else {
2719                                 m->set_spot (@2);
2720                                 $$ = scm_cons ($2, $$);
2721                         }
2722                 }
2723         }
2724         ;
2725
2726 post_event_nofinger:
2727         direction_less_event {
2728                 $$ = $1;
2729         }
2730         | script_dir music_function_call_closed {
2731                 $$ = $2;
2732                 if (!unsmob_music ($2)->is_mus_type ("post-event")) {
2733                         parser->parser_error (@2, _ ("post-event expected"));
2734                         $$ = SCM_UNSPECIFIED;
2735                 } else if (!SCM_UNBNDP ($1))
2736                 {
2737                         unsmob_music ($$)->set_property ("direction", $1);
2738                 }
2739         }
2740         | HYPHEN {
2741                 if (!parser->lexer_->is_lyric_state ())
2742                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2743                 $$ = MY_MAKE_MUSIC ("HyphenEvent", @$)->unprotect ();
2744         }
2745         | EXTENDER {
2746                 if (!parser->lexer_->is_lyric_state ())
2747                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2748                 $$ = MY_MAKE_MUSIC ("ExtenderEvent", @$)->unprotect ();
2749         }
2750         | script_dir direction_reqd_event {
2751                 if (!SCM_UNBNDP ($1))
2752                 {
2753                         Music *m = unsmob_music ($2);
2754                         m->set_property ("direction", $1);
2755                 }
2756                 $$ = $2;
2757         }
2758         | script_dir direction_less_event {
2759                 if (!SCM_UNBNDP ($1))
2760                 {
2761                         Music *m = unsmob_music ($2);
2762                         m->set_property ("direction", $1);
2763                 }
2764                 $$ = $2;
2765         }
2766         | '^' fingering
2767         {
2768                 $$ = $2;
2769                 unsmob_music ($$)->set_property ("direction", scm_from_int (UP));
2770         }
2771         | '_' fingering
2772         {
2773                 $$ = $2;
2774                 unsmob_music ($$)->set_property ("direction", scm_from_int (DOWN));
2775         }                       
2776         ;
2777
2778 post_event:
2779         post_event_nofinger
2780         | '-' fingering {
2781                 $$ = $2;
2782         }
2783         ;
2784
2785 string_number_event:
2786         E_UNSIGNED {
2787                 Music *s = MY_MAKE_MUSIC ("StringNumberEvent", @$);
2788                 s->set_property ("string-number", $1);
2789                 $$ = s->unprotect ();
2790         }
2791         ;
2792
2793 direction_less_event:
2794         string_number_event
2795         | EVENT_IDENTIFIER      {
2796                 $$ = $1;
2797         }
2798         | tremolo_type  {
2799                Music *a = MY_MAKE_MUSIC ("TremoloEvent", @$);
2800                a->set_property ("tremolo-type", $1);
2801                $$ = a->unprotect ();
2802         }
2803         | event_function_event  
2804         ;
2805
2806 direction_reqd_event:
2807         gen_text_def {
2808                 $$ = $1;
2809         }
2810         | script_abbreviation {
2811                 SCM s = parser->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
2812                 Music *a = MY_MAKE_MUSIC ("ArticulationEvent", @$);
2813                 if (scm_is_string (s))
2814                         a->set_property ("articulation-type", s);
2815                 else parser->parser_error (@1, _ ("expecting string as script definition"));
2816                 $$ = a->unprotect ();
2817         }
2818         ;
2819
2820 octave_check:
2821         /**/ { $$ = SCM_EOL; }
2822         | '=' quotes { $$ = $2; }
2823         ;
2824
2825 quotes:
2826         /* empty */
2827         {
2828                 $$ = SCM_INUM0;
2829         }
2830         | sub_quotes
2831         | sup_quotes
2832         ;
2833
2834 sup_quotes:
2835         '\'' {
2836                 $$ = scm_from_int (1);
2837         }
2838         | sup_quotes '\'' {
2839                 $$ = scm_oneplus ($1);
2840         }
2841         ;
2842
2843 sub_quotes:
2844         ',' {
2845                 $$ = scm_from_int (-1);
2846         }
2847         | sub_quotes ',' {
2848                 $$ = scm_oneminus ($1);
2849         }
2850         ;
2851
2852 steno_pitch:
2853         NOTENAME_PITCH quotes {
2854                 if (!scm_is_eq (SCM_INUM0, $2))
2855                 {
2856                         Pitch p = *unsmob_pitch ($1);
2857                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2858                         $$ = p.smobbed_copy ();
2859                 }
2860         }
2861         ;
2862
2863 /*
2864 ugh. duplication
2865 */
2866
2867 steno_tonic_pitch:
2868         TONICNAME_PITCH quotes {
2869                 if (!scm_is_eq (SCM_INUM0, $2))
2870                 {
2871                         Pitch p = *unsmob_pitch ($1);
2872                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2873                         $$ = p.smobbed_copy ();
2874                 }
2875         }
2876         ;
2877
2878 pitch:
2879         steno_pitch
2880         | PITCH_IDENTIFIER
2881         ;
2882
2883 gen_text_def:
2884         full_markup {
2885                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2886                 t->set_property ("text", $1);
2887                 $$ = t->unprotect ();
2888         }
2889         | STRING {
2890                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2891                 t->set_property ("text",
2892                         make_simple_markup ($1));
2893                 $$ = t->unprotect ();
2894         }
2895         | embedded_scm_closed
2896         {
2897                 Music *m = unsmob_music ($1);
2898                 if (m && m->is_mus_type ("post-event"))
2899                         $$ = $1;
2900                 else if (Text_interface::is_markup ($1)) {
2901                         Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2902                         t->set_property ("text", $1);
2903                         $$ = t->unprotect ();
2904                 } else
2905                         parser->parser_error (@1, _ ("not an articulation"));
2906         }
2907         ;
2908
2909 fingering:
2910         UNSIGNED {
2911                 Music *t = MY_MAKE_MUSIC ("FingeringEvent", @$);
2912                 t->set_property ("digit", $1);
2913                 $$ = t->unprotect ();
2914         }
2915         ;
2916
2917 script_abbreviation:
2918         '^'             {
2919                 $$ = scm_from_locale_string ("Hat");
2920         }
2921         | '+'           {
2922                 $$ = scm_from_locale_string ("Plus");
2923         }
2924         | '-'           {
2925                 $$ = scm_from_locale_string ("Dash");
2926         }
2927         | '!'           {
2928                 $$ = scm_from_locale_string ("Bang");
2929         }
2930         | ANGLE_CLOSE   {
2931                 $$ = scm_from_locale_string ("Larger");
2932         }
2933         | '.'           {
2934                 $$ = scm_from_locale_string ("Dot");
2935         }
2936         | '_' {
2937                 $$ = scm_from_locale_string ("Underscore");
2938         }
2939         ;
2940
2941 script_dir:
2942         '_'     { $$ = scm_from_int (DOWN); }
2943         | '^'   { $$ = scm_from_int (UP); }
2944         | '-'   { $$ = SCM_UNDEFINED; }
2945         ;
2946
2947 duration_length:
2948         multiplied_duration {
2949                 $$ = $1;
2950         }
2951         ;
2952
2953 maybe_notemode_duration:
2954         {
2955                 $$ = SCM_UNDEFINED;
2956         } %prec ':'
2957         | multiplied_duration   {
2958                 $$ = $1;
2959                 parser->default_duration_ = *unsmob_duration ($$);
2960         }
2961 ;
2962
2963
2964 optional_notemode_duration:
2965         maybe_notemode_duration
2966         {
2967                 if (SCM_UNBNDP ($$))
2968                         $$ = parser->default_duration_.smobbed_copy ();
2969         }
2970         ;
2971
2972 steno_duration:
2973         UNSIGNED dots           {
2974                 $$ = make_duration ($1, scm_to_int ($2));
2975                 if (SCM_UNBNDP ($$))
2976                 {
2977                         parser->parser_error (@1, _ ("not a duration"));
2978                         $$ = Duration ().smobbed_copy ();
2979                 }
2980         }
2981         | DURATION_IDENTIFIER dots      {
2982                 Duration *d = unsmob_duration ($1);
2983                 Duration k (d->duration_log (),
2984                             d->dot_count () + scm_to_int ($2));
2985                 k = k.compressed (d->factor ());
2986                 scm_remember_upto_here_1 ($1);
2987                 $$ = k.smobbed_copy ();
2988         }
2989         ;
2990
2991 multiplied_duration:
2992         steno_duration {
2993                 $$ = $1;
2994         }
2995         | multiplied_duration '*' UNSIGNED {
2996                 $$ = unsmob_duration ($$)->compressed (scm_to_int ($3)).smobbed_copy ();
2997         }
2998         | multiplied_duration '*' FRACTION {
2999                 Rational  m (scm_to_int (scm_car ($3)), scm_to_int (scm_cdr ($3)));
3000
3001                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
3002         }
3003         ;
3004
3005 dots:
3006         /* empty */     {
3007                 $$ = SCM_INUM0;
3008         }
3009         | dots '.' {
3010                 $$ = scm_oneplus ($1);
3011         }
3012         ;
3013
3014 tremolo_type:
3015         ':'     {
3016                 $$ = SCM_INUM0;
3017         }
3018         | ':' UNSIGNED {
3019                 if (SCM_UNBNDP (make_duration ($2)))
3020                         parser->parser_error (@2, _ ("not a duration"));
3021                 $$ = $2;
3022         }
3023         ;
3024
3025 bass_number:
3026         UNSIGNED { $$ = $1; }
3027         | STRING { $$ = $1; }
3028         | full_markup { $$ = $1; }
3029         | embedded_scm_bare
3030         {
3031                 // as an integer, it needs to be non-negative, and otherwise
3032                 // it needs to be suitable as a markup.
3033                 if (scm_is_integer ($1)
3034                     ? scm_is_true (scm_negative_p ($1))
3035                     : !Text_interface::is_markup ($1))
3036                 {
3037                         parser->parser_error (@1, _ ("bass number expected"));
3038                         $$ = SCM_INUM0;
3039                 }
3040         }
3041         ;
3042
3043 figured_bass_alteration:
3044         '-'     { $$ = ly_rational2scm (FLAT_ALTERATION); }
3045         | '+'   { $$ = ly_rational2scm (SHARP_ALTERATION); }
3046         | '!'   { $$ = scm_from_int (0); }
3047         ;
3048
3049 bass_figure:
3050         FIGURE_SPACE {
3051                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
3052                 $$ = bfr->unprotect ();
3053         }
3054         | bass_number  {
3055                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
3056                 $$ = bfr->self_scm ();
3057
3058                 if (scm_is_number ($1))
3059                         bfr->set_property ("figure", $1);
3060                 else if (Text_interface::is_markup ($1))
3061                         bfr->set_property ("text", $1);
3062
3063                 bfr->unprotect ();
3064         }
3065         | bass_figure ']' {
3066                 $$ = $1;
3067                 unsmob_music ($1)->set_property ("bracket-stop", SCM_BOOL_T);
3068         }
3069         | bass_figure figured_bass_alteration {
3070                 Music *m = unsmob_music ($1);
3071                 if (scm_to_double ($2)) {
3072                         SCM salter = m->get_property ("alteration");
3073                         SCM alter = scm_is_number (salter) ? salter : scm_from_int (0);
3074                         m->set_property ("alteration",
3075                                          scm_sum (alter, $2));
3076                 } else {
3077                         m->set_property ("alteration", scm_from_int (0));
3078                 }
3079         }
3080         | bass_figure figured_bass_modification  {
3081                 Music *m = unsmob_music ($1);
3082                 m->set_property ($2, SCM_BOOL_T);
3083         }
3084         ;
3085
3086
3087 figured_bass_modification:
3088         E_PLUS          {
3089                 $$ = ly_symbol2scm ("augmented");
3090         }
3091         | E_EXCLAMATION {
3092                 $$ = ly_symbol2scm ("no-continuation");
3093         }
3094         | '/'           {
3095                 $$ = ly_symbol2scm ("diminished");
3096         }
3097         | E_BACKSLASH {
3098                 $$ = ly_symbol2scm ("augmented-slash");
3099         }
3100         ;
3101
3102 br_bass_figure:
3103         bass_figure {
3104                 $$ = $1;
3105         }
3106         | '[' bass_figure {
3107                 $$ = $2;
3108                 unsmob_music ($$)->set_property ("bracket-start", SCM_BOOL_T);
3109         }
3110         ;
3111
3112 figure_list:
3113         /**/            {
3114                 $$ = SCM_EOL;
3115         }
3116         | figure_list br_bass_figure {
3117                 $$ = scm_cons ($2, $1);
3118         }
3119         ;
3120
3121 figure_spec:
3122         FIGURE_OPEN figure_list FIGURE_CLOSE {
3123                 $$ = scm_reverse_x ($2, SCM_EOL);
3124         }
3125         ;
3126
3127
3128 optional_rest:
3129         /**/   { $$ = SCM_BOOL_F; }
3130         | REST { $$ = SCM_BOOL_T; }
3131         ;
3132
3133 simple_element:
3134         pitch exclamations questions octave_check maybe_notemode_duration optional_rest {
3135                 if (!parser->lexer_->is_note_state ())
3136                         parser->parser_error (@1, _ ("have to be in Note mode for notes"));
3137                 if (!SCM_UNBNDP ($2)
3138                     || !SCM_UNBNDP ($3)
3139                     || scm_is_number ($4)
3140                     || !SCM_UNBNDP ($5)
3141                     || scm_is_true ($6))
3142                 {
3143                         Music *n = 0;
3144                         if (scm_is_true ($6))
3145                                 n = MY_MAKE_MUSIC ("RestEvent", @$);
3146                         else
3147                                 n = MY_MAKE_MUSIC ("NoteEvent", @$);
3148                         
3149                         n->set_property ("pitch", $1);
3150                         if (SCM_UNBNDP ($5))
3151                                 n->set_property ("duration",
3152                                                  parser->default_duration_.smobbed_copy ());
3153                         else
3154                                 n->set_property ("duration", $5);
3155                         
3156                         if (scm_is_number ($4))
3157                         {
3158                                 int q = scm_to_int ($4);
3159                                 n->set_property ("absolute-octave", scm_from_int (q-1));
3160                         }
3161                         
3162                         if (to_boolean ($3))
3163                                 n->set_property ("cautionary", SCM_BOOL_T);
3164                         if (to_boolean ($2) || to_boolean ($3))
3165                                 n->set_property ("force-accidental", SCM_BOOL_T);
3166                         
3167                         $$ = n->unprotect ();
3168                 }
3169         }
3170         | DRUM_PITCH optional_notemode_duration {
3171                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
3172                 n->set_property ("duration", $2);
3173                 n->set_property ("drum-type", $1);
3174
3175                 $$ = n->unprotect ();
3176         }
3177         | RESTNAME optional_notemode_duration           {
3178                 Music *ev = 0;
3179                 if (ly_scm2string ($1) == "s") {
3180                         /* Space */
3181                         ev = MY_MAKE_MUSIC ("SkipEvent", @$);
3182                   }
3183                 else {
3184                         ev = MY_MAKE_MUSIC ("RestEvent", @$);
3185
3186                     }
3187                 ev->set_property ("duration", $2);
3188                 $$ = ev->unprotect ();
3189         }
3190         ;
3191
3192 simple_chord_elements:
3193         new_chord {
3194                 if (!parser->lexer_->is_chord_state ())
3195                         parser->parser_error (@1, _ ("have to be in Chord mode for chords"));
3196                 $$ = $1;
3197         }
3198         | figure_spec optional_notemode_duration {
3199                 for (SCM s = $1; scm_is_pair (s); s = scm_cdr (s))
3200                 {
3201                         unsmob_music (scm_car (s))->set_property ("duration", $2);
3202                 }
3203                 $$ = $1;
3204         }
3205         ;
3206
3207 lyric_element:
3208         full_markup {
3209                 if (!parser->lexer_->is_lyric_state ())
3210                         parser->parser_error (@1, _ ("markup outside of text script or \\lyricmode"));
3211                 $$ = $1;
3212         }
3213         | STRING {
3214                 if (!parser->lexer_->is_lyric_state ())
3215                         parser->parser_error (@1, _ ("unrecognized string, not in text script or \\lyricmode"));
3216                 $$ = $1;
3217         }
3218         | LYRIC_ELEMENT
3219         ;
3220
3221 lyric_element_music:
3222         lyric_element optional_notemode_duration post_events {
3223                 $$ = MAKE_SYNTAX ("lyric-event", @$, $1, $2);
3224                 if (scm_is_pair ($3))
3225                         unsmob_music ($$)->set_property
3226                                 ("articulations", scm_reverse_x ($3, SCM_EOL));
3227         } %prec ':'
3228         ;
3229
3230 new_chord:
3231         steno_tonic_pitch optional_notemode_duration   {
3232                 $$ = make_chord_elements (@$, $1, $2, SCM_EOL);
3233         }
3234         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
3235                 SCM its = scm_reverse_x ($4, SCM_EOL);
3236                 $$ = make_chord_elements (@$, $1, $2, scm_cons ($3, its));
3237         } %prec ':'
3238         ;
3239
3240 chord_items:
3241         /**/ {
3242                 $$ = SCM_EOL;
3243         }
3244         | chord_items chord_item {
3245                 $$ = scm_cons ($2, $$);
3246         }
3247         ;
3248
3249 chord_separator:
3250         CHORD_COLON {
3251                 $$ = ly_symbol2scm ("chord-colon");
3252         }
3253         | CHORD_CARET {
3254                 $$ = ly_symbol2scm ("chord-caret");
3255         }
3256         | CHORD_SLASH steno_tonic_pitch {
3257                 $$ = scm_list_2 (ly_symbol2scm ("chord-slash"), $2);
3258         }
3259         | CHORD_BASS steno_tonic_pitch {
3260                 $$ = scm_list_2 (ly_symbol2scm ("chord-bass"), $2);
3261         }
3262         ;
3263
3264 chord_item:
3265         chord_separator {
3266                 $$ = $1;
3267         }
3268         | step_numbers {
3269                 $$ = scm_reverse_x ($1, SCM_EOL);
3270         }
3271         | CHORD_MODIFIER  {
3272                 $$ = $1;
3273         }
3274         ;
3275
3276 step_numbers:
3277         step_number { $$ = scm_cons ($1, SCM_EOL); }
3278         | step_numbers '.' step_number {
3279                 $$ = scm_cons ($3, $$);
3280         }
3281         ;
3282
3283 step_number:
3284         UNSIGNED {
3285                 $$ = make_chord_step ($1, 0);
3286         }
3287         | UNSIGNED '+' {
3288                 $$ = make_chord_step ($1, SHARP_ALTERATION);
3289         }
3290         | UNSIGNED CHORD_MINUS {
3291                 $$ = make_chord_step ($1, FLAT_ALTERATION);
3292         }
3293         ;
3294
3295 tempo_range:
3296         UNSIGNED {
3297                 $$ = $1;
3298         }
3299         | UNSIGNED '-' UNSIGNED {
3300                 $$ = scm_cons ($1, $3);
3301         }
3302         ;
3303
3304 /*
3305         UTILITIES
3306
3307 TODO: should deprecate in favor of Scheme?
3308
3309  */
3310 number_expression:
3311         number_expression '+' number_term {
3312                 $$ = scm_sum ($1, $3);
3313         }
3314         | number_expression '-' number_term {
3315                 $$ = scm_difference ($1, $3);
3316         }
3317         | number_term
3318         ;
3319
3320 number_term:
3321         number_factor {
3322                 $$ = $1;
3323         }
3324         | number_factor '*' number_factor {
3325                 $$ = scm_product ($1, $3);
3326         }
3327         | number_factor '/' number_factor {
3328                 $$ = scm_divide ($1, $3);
3329         }
3330         ;
3331
3332 number_factor:
3333         '-'  number_factor { /* %prec UNARY_MINUS */
3334                 $$ = scm_difference ($2, SCM_UNDEFINED);
3335         }
3336         | bare_number
3337         ;
3338
3339 bare_number_common:
3340         bare_number_common_closed
3341         | REAL NUMBER_IDENTIFIER
3342         {
3343                 $$ = scm_product ($1, $2);
3344         }
3345         ;
3346
3347 bare_number_common_closed:
3348         REAL
3349         | NUMBER_IDENTIFIER
3350         ;
3351
3352 bare_number:
3353         bare_number_common
3354         | UNSIGNED
3355         | UNSIGNED NUMBER_IDENTIFIER    {
3356                 $$ = scm_product ($1, $2);
3357         }
3358         ;
3359
3360 bare_number_closed:
3361         UNSIGNED
3362         | bare_number_common_closed
3363         ;
3364
3365 unsigned_number:
3366         UNSIGNED
3367         | NUMBER_IDENTIFIER
3368         ;
3369
3370 exclamations:
3371                 { $$ = SCM_UNDEFINED; }
3372         | exclamations '!'
3373         {
3374                 if (SCM_UNBNDP ($1))
3375                         $$ = SCM_BOOL_T;
3376                 else
3377                         $$ = scm_not ($1);
3378         }
3379         ;
3380
3381 questions:
3382         { $$ = SCM_UNDEFINED; }
3383         | questions '?'
3384         {
3385                 if (SCM_UNBNDP ($1))
3386                         $$ = SCM_BOOL_T;
3387                 else
3388                         $$ = scm_not ($1);
3389         }
3390         ;
3391
3392 full_markup_list:
3393         MARKUPLIST
3394                 { parser->lexer_->push_markup_state (); }
3395         markup_list {
3396                 $$ = $3;
3397                 parser->lexer_->pop_state ();
3398         }
3399         ;
3400
3401 full_markup:
3402         MARKUP
3403                 { parser->lexer_->push_markup_state (); }
3404         markup_top {
3405                 $$ = $3;
3406                 parser->lexer_->pop_state ();
3407         }
3408         ;
3409
3410 markup_top:
3411         markup_list {
3412                 $$ = scm_list_2 (ly_lily_module_constant ("line-markup"),  $1);
3413         }
3414         | markup_head_1_list simple_markup
3415         {
3416                 $$ = scm_car (MAKE_SYNTAX ("composed-markup-list",
3417                                            @2, $1, scm_list_1 ($2)));
3418         }
3419         | simple_markup {
3420                 $$ = $1;
3421         }
3422         ;
3423
3424 markup_scm:
3425         embedded_scm_bare
3426         {
3427                 if (Text_interface::is_markup ($1))
3428                         MYBACKUP (MARKUP_IDENTIFIER, $1, @1);
3429                 else if (Text_interface::is_markup_list ($1))
3430                         MYBACKUP (MARKUPLIST_IDENTIFIER, $1, @1);
3431                 else {
3432                         parser->parser_error (@1, _ ("not a markup"));
3433                         MYBACKUP (MARKUP_IDENTIFIER, scm_string (SCM_EOL), @1);
3434                 }
3435         } BACKUP
3436         ;
3437                         
3438
3439 markup_list:
3440         markup_composed_list {
3441                 $$ = $1;
3442         }
3443         | markup_uncomposed_list
3444         ;
3445
3446 markup_uncomposed_list:
3447         markup_braced_list {
3448                 $$ = $1;
3449         }
3450         | markup_command_list {
3451                 $$ = scm_list_1 ($1);
3452         }
3453         | markup_scm MARKUPLIST_IDENTIFIER
3454         {
3455                 $$ = $2;
3456         }
3457         | SCORELINES {
3458                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3459                 parser->lexer_->push_note_state (nn);
3460         } '{' score_body '}' {
3461                 $$ = scm_list_1 (scm_list_2 (ly_lily_module_constant ("score-lines-markup-list"), $4));
3462                 parser->lexer_->pop_state ();
3463         }
3464         ;
3465
3466 markup_composed_list:
3467         markup_head_1_list markup_uncomposed_list {
3468                 $$ = MAKE_SYNTAX ("composed-markup-list",
3469                                   @2, $1, $2);
3470         }
3471         ;
3472
3473 markup_braced_list:
3474         '{' markup_braced_list_body '}' {
3475                 $$ = scm_reverse_x ($2, SCM_EOL);
3476         }
3477         ;
3478
3479 markup_braced_list_body:
3480         /* empty */     {  $$ = SCM_EOL; }
3481         | markup_braced_list_body markup {
3482                 $$ = scm_cons ($2, $1);
3483         }
3484         | markup_braced_list_body markup_list {
3485                 $$ = scm_reverse_x ($2, $1);
3486         }
3487         ;
3488
3489 markup_command_list:
3490         MARKUP_LIST_FUNCTION markup_command_list_arguments {
3491           $$ = scm_cons ($1, scm_reverse_x($2, SCM_EOL));
3492         }
3493         ;
3494
3495 markup_command_basic_arguments:
3496         EXPECT_MARKUP_LIST markup_command_list_arguments markup_list {
3497           $$ = scm_cons ($3, $2);
3498         }
3499         | EXPECT_SCM markup_command_list_arguments embedded_scm_closed {
3500           $$ = check_scheme_arg (parser, @3, $3, $2, $1);
3501         }
3502         | EXPECT_NO_MORE_ARGS {
3503           $$ = SCM_EOL;
3504         }
3505         ;
3506
3507 markup_command_list_arguments:
3508         markup_command_basic_arguments { $$ = $1; }
3509         | EXPECT_MARKUP markup_command_list_arguments markup {
3510           $$ = scm_cons ($3, $2);
3511         }
3512         ;
3513
3514 markup_head_1_item:
3515         MARKUP_FUNCTION EXPECT_MARKUP markup_command_list_arguments {
3516           $$ = scm_cons ($1, scm_reverse_x ($3, SCM_EOL));
3517         }
3518         ;
3519
3520 markup_head_1_list:
3521         markup_head_1_item      {
3522                 $$ = scm_list_1 ($1);
3523         }
3524         | markup_head_1_list markup_head_1_item {
3525                 $$ = scm_cons ($2, $1);
3526         }
3527         ;
3528
3529 simple_markup:
3530         STRING {
3531                 $$ = make_simple_markup ($1);
3532         }
3533         | SCORE {
3534                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3535                 parser->lexer_->push_note_state (nn);
3536         } '{' score_body '}' {
3537                 $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), $4);
3538                 parser->lexer_->pop_state ();
3539         }
3540         | MARKUP_FUNCTION markup_command_basic_arguments {
3541                 $$ = scm_cons ($1, scm_reverse_x ($2, SCM_EOL));
3542         }
3543         | markup_scm MARKUP_IDENTIFIER
3544         {
3545                 $$ = $2;
3546         }
3547         ;
3548
3549 markup:
3550         markup_head_1_list simple_markup
3551         {
3552                 $$ = scm_car (MAKE_SYNTAX ("composed-markup-list",
3553                                            @2, $1, scm_list_1 ($2)));
3554         }
3555         | simple_markup {
3556                 $$ = $1;
3557         }
3558         ;
3559
3560 %%
3561
3562 void
3563 Lily_parser::set_yydebug (bool x)
3564 {
3565         yydebug = x;
3566 }
3567
3568 SCM
3569 Lily_parser::do_yyparse ()
3570 {
3571         SCM retval = SCM_UNDEFINED;
3572         yyparse (this, &retval);
3573         return retval;
3574 }
3575
3576
3577
3578
3579
3580 /*
3581
3582 It is a little strange to have this function in this file, but
3583 otherwise, we have to import music classes into the lexer.
3584
3585 */
3586 int
3587 Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
3588 {
3589         if (unsmob_book (sid)) {
3590                 Book *book =  unsmob_book (sid)->clone ();
3591                 *destination = book->self_scm ();
3592                 book->unprotect ();
3593
3594                 return BOOK_IDENTIFIER;
3595         } else if (scm_is_number (sid)) {
3596                 *destination = sid;
3597                 return NUMBER_IDENTIFIER;
3598         } else if (unsmob_context_def (sid)) {
3599                 Context_def *def= unsmob_context_def (sid)->clone ();
3600
3601                 *destination = def->self_scm ();
3602                 def->unprotect ();
3603
3604                 return CONTEXT_DEF_IDENTIFIER;
3605         } else if (unsmob_context_mod (sid)) {
3606                 *destination = unsmob_context_mod (sid)->smobbed_copy ();
3607
3608                 return CONTEXT_MOD_IDENTIFIER;
3609         } else if (Music *mus = unsmob_music (sid)) {
3610                 mus = mus->clone ();
3611                 *destination = mus->self_scm ();
3612                 bool is_event = mus->is_mus_type ("post-event");
3613                 mus->unprotect ();
3614                 return is_event ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
3615         } else if (unsmob_pitch (sid)) {
3616                 *destination = unsmob_pitch (sid)->smobbed_copy ();
3617                 return PITCH_IDENTIFIER;
3618         } else if (unsmob_duration (sid)) {
3619                 *destination = unsmob_duration (sid)->smobbed_copy ();
3620                 return DURATION_IDENTIFIER;
3621         } else if (unsmob_output_def (sid)) {
3622                 Output_def *p = unsmob_output_def (sid);
3623                 p = p->clone ();
3624
3625                 *destination = p->self_scm ();
3626                 p->unprotect ();
3627                 return OUTPUT_DEF_IDENTIFIER;
3628         }
3629
3630         return -1;
3631 }
3632
3633 SCM
3634 get_next_unique_context_id ()
3635 {
3636         return scm_from_locale_string ("$uniqueContextId");
3637 }
3638
3639
3640 SCM
3641 get_next_unique_lyrics_context_id ()
3642 {
3643         static int new_context_count;
3644         char s[128];
3645         snprintf (s, sizeof (s)-1, "uniqueContext%d", new_context_count++);
3646         return scm_from_locale_string (s);
3647 }
3648
3649 // check_scheme_arg checks one argument with a given predicate for use
3650 // in an argument list and throws a syntax error if it is unusable.
3651 // The argument is prepended to the argument list in any case.  After
3652 // throwing a syntax error, the argument list is terminated with #f as
3653 // its last cdr in order to mark it as uncallable while not losing
3654 // track of its total length.
3655 //
3656 // There are a few special considerations: if optional argument disp
3657 // is given (otherwise it defaults to SCM_UNDEFINED), it will be used
3658 // instead of arg in a prospective error message.  This is useful if
3659 // arg is not the actual argument but rather a transformation of it.
3660 //
3661 // If arg itself is SCM_UNDEFINED, the predicate is considered false
3662 // and an error message using disp is produced unconditionally.
3663
3664 SCM check_scheme_arg (Lily_parser *parser, Input loc,
3665                       SCM arg, SCM args, SCM pred, SCM disp)
3666 {
3667         if (SCM_UNBNDP (arg))
3668                 args = scm_cons (disp, args);
3669         else {
3670                 args = scm_cons (arg, args);
3671                 if (scm_is_true (scm_call_1 (pred, arg)))
3672                         return args;
3673         }
3674         scm_set_cdr_x (scm_last_pair (args), SCM_EOL);
3675         MAKE_SYNTAX ("argument-error", loc, scm_length (args), pred,
3676                      SCM_UNBNDP (disp) ? arg : disp);
3677         scm_set_cdr_x (scm_last_pair (args), SCM_BOOL_F);
3678         return args;
3679 }
3680
3681 SCM loc_on_music (Input loc, SCM arg)
3682 {
3683         if (Music *m = unsmob_music (arg))
3684         {
3685                 m = m->clone ();
3686                 m->set_spot (loc);
3687                 return m->unprotect ();
3688         }
3689         return arg;
3690 }
3691
3692 SCM
3693 try_string_variants (SCM pred, SCM str)
3694 {
3695         // a matching predicate is always ok
3696         if (scm_is_true (scm_call_1 (pred, str)))
3697                 return str;
3698         // a symbol may be interpreted as a list of symbols if it helps
3699         if (scm_is_symbol (str)) {
3700                 str = scm_list_1 (str);
3701                 if (scm_is_true (scm_call_1 (pred, str)))
3702                         return str;
3703                 return SCM_UNDEFINED;
3704         }
3705
3706         // If this cannot be a string representation of a symbol list,
3707         // we are through.
3708
3709         if (!is_regular_identifier (str, true))
3710                 return SCM_UNDEFINED;
3711
3712         str = scm_string_split (str, SCM_MAKE_CHAR ('.'));
3713         for (SCM p = str; scm_is_pair (p); p = scm_cdr (p))
3714                 scm_set_car_x (p, scm_string_to_symbol (scm_car (p)));
3715
3716         // Let's attempt the symbol list interpretation first.
3717
3718         if (scm_is_true (scm_call_1 (pred, str)))
3719                 return str;
3720
3721         // If there is just one symbol in the list, we might interpret
3722         // it as a single symbol
3723
3724         if (scm_is_null (scm_cdr (str)))
3725         {
3726                 str = scm_car (str);
3727                 if (scm_is_true (scm_call_1 (pred, str)))
3728                         return str;
3729         }
3730
3731         return SCM_UNDEFINED;
3732 }
3733
3734 bool
3735 is_regular_identifier (SCM id, bool multiple)
3736 {
3737   if (!scm_is_string (id))
3738           return false;
3739
3740   string str = ly_scm2string (id);
3741
3742   bool middle = false;
3743
3744   for (string::iterator it=str.begin(); it != str.end (); it++)
3745   {
3746           int c = *it & 0xff;
3747           if ((c >= 'a' && c <= 'z')
3748               || (c >= 'A' && c <= 'Z')
3749               || c > 0x7f)
3750                   middle = true;
3751           else if (middle && (c == '-' || c == '_' || (multiple && c == '.')))
3752                   middle = false;
3753           else
3754                   return false;
3755   }
3756   return middle;
3757 }
3758
3759 SCM
3760 make_music_from_simple (Lily_parser *parser, Input loc, SCM simple)
3761 {
3762         if (unsmob_music (simple))
3763                 return simple;
3764         if (parser->lexer_->is_note_state ()) {
3765                 if (scm_is_symbol (simple)) {
3766                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3767                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3768                         n->set_property ("drum-type", simple);
3769                         return n->unprotect ();
3770                 }
3771                 if (unsmob_pitch (simple)) {
3772                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3773                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3774                         n->set_property ("pitch", simple);
3775                         return n->unprotect ();
3776                 }
3777                 return simple;
3778         } else if (parser->lexer_->is_lyric_state ()) {
3779                 if (Text_interface::is_markup (simple))
3780                         return MAKE_SYNTAX ("lyric-event", loc, simple,
3781                                             parser->default_duration_.smobbed_copy ());
3782         } else if (parser->lexer_->is_chord_state ()) {
3783                 if (unsmob_pitch (simple))
3784                         return make_chord_elements (loc, simple,
3785                                                     parser->default_duration_.smobbed_copy (),
3786                                                     SCM_EOL);
3787         }
3788         return simple;
3789 }
3790
3791 Music *
3792 make_music_with_input (SCM name, Input where)
3793 {
3794        Music *m = make_music_by_name (name);
3795        m->set_spot (where);
3796        return m;
3797 }
3798
3799 SCM
3800 make_simple_markup (SCM a)
3801 {
3802         return a;
3803 }
3804
3805 SCM
3806 make_duration (SCM d, int dots)
3807 {
3808         int t = scm_to_int (d);
3809         if (t > 0 && (t & (t-1)) == 0)
3810                 return Duration (intlog2 (t), dots).smobbed_copy ();
3811         else
3812                 return SCM_UNDEFINED;
3813 }
3814
3815 SCM
3816 make_chord_step (SCM step_scm, Rational alter)
3817 {
3818         int step = scm_to_int (step_scm);
3819
3820         if (step == 7)
3821                 alter += FLAT_ALTERATION;
3822
3823         while (step < 0)
3824                 step += 7;
3825         Pitch m ((step -1) / 7, (step - 1) % 7, alter);
3826         return m.smobbed_copy ();
3827 }
3828
3829
3830 SCM
3831 make_chord_elements (Input loc, SCM pitch, SCM dur, SCM modification_list)
3832 {
3833         SCM chord_ctor = ly_lily_module_constant ("construct-chord-elements");
3834         SCM res = scm_call_3 (chord_ctor, pitch, dur, modification_list);
3835         for (SCM s = res; scm_is_pair (s); s = scm_cdr (s))
3836         {
3837                 unsmob_music (scm_car (s))->set_spot (loc);
3838         }
3839         return res;
3840 }
3841
3842 int
3843 yylex (YYSTYPE *s, YYLTYPE *loc, Lily_parser *parser)
3844 {
3845         Lily_lexer *lex = parser->lexer_;
3846
3847         lex->lexval_ = s;
3848         lex->lexloc_ = loc;
3849         lex->prepare_for_next_token ();
3850         return lex->yylex ();
3851 }