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