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