]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
Issue 3814: Allow context definitions to be provided by scheme functions
[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         }
1488         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' REAL
1489         {
1490                 $$ = check_scheme_arg (parser, @4,
1491                                        scm_difference ($5, SCM_UNDEFINED),
1492                                        $3, $2);
1493         }
1494         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup '-' NUMBER_IDENTIFIER
1495         {
1496                 $$ = check_scheme_arg (parser, @4,
1497                                        scm_difference ($5, SCM_UNDEFINED),
1498                                        $3, $2);
1499         }
1500         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup embedded_scm_arg
1501         {
1502                 if (scm_is_true (scm_call_1 ($2, $4)))
1503                         $$ = scm_cons ($4, $3);
1504                 else
1505                         $$ = check_scheme_arg (parser, @4,
1506                                                make_music_from_simple
1507                                                (parser, @4, $4),
1508                                                $3, $2);
1509         }
1510         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup bare_number_common
1511         {
1512                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1513         }
1514         | function_arglist_nonbackup_reparse REPARSE pitch_or_music
1515         {
1516                 if (scm_is_true (scm_call_1 ($2, $3)))
1517                         $$ = scm_cons ($3, $1);
1518                 else
1519                         $$ = check_scheme_arg (parser, @3,
1520                                                make_music_from_simple
1521                                                (parser, @3, $3),
1522                                                $1, $2);
1523         }
1524         | function_arglist_nonbackup_reparse REPARSE duration_length
1525         {
1526                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1527         }
1528         | function_arglist_nonbackup_reparse REPARSE bare_number_common
1529         {
1530                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1531         }
1532         | function_arglist_nonbackup_reparse REPARSE SCM_ARG
1533         {
1534                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1535         }
1536         | function_arglist_nonbackup_reparse REPARSE lyric_element_music
1537         {
1538                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1539         }
1540         | function_arglist_nonbackup_reparse REPARSE symbol_list_arg
1541         {
1542                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1543         }
1544         ;
1545
1546 function_arglist_nonbackup_reparse:
1547         EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup SCM_IDENTIFIER
1548         {
1549                 $$ = $3;
1550                 SCM res = try_string_variants ($2, $4);
1551                 if (!SCM_UNBNDP (res))
1552                         if (scm_is_pair (res))
1553                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1554                         else
1555                                 MYREPARSE (@4, $2, SCM_ARG, res);
1556                 else if (scm_is_true
1557                          (scm_call_1
1558                           ($2, make_music_from_simple
1559                            (parser, @4, $4))))
1560                         MYREPARSE (@4, $2, STRING, $4);
1561                 else
1562                         MYREPARSE (@4, $2, SCM_ARG, $4);
1563         }
1564         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup pitch
1565         {
1566                 $$ = $3;
1567                 if (scm_is_true
1568                     (scm_call_1
1569                      ($2, make_music_from_simple
1570                       (parser, @4, $4))))
1571                         MYREPARSE (@4, $2, PITCH_IDENTIFIER, $4);
1572                 else
1573                         MYREPARSE (@4, $2, SCM_ARG, $4);
1574         }
1575         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup steno_tonic_pitch
1576         {
1577                 $$ = $3;
1578                 if (scm_is_true
1579                     (scm_call_1
1580                      ($2, make_music_from_simple
1581                       (parser, @4, $4))))
1582                         MYREPARSE (@4, $2, TONICNAME_PITCH, $4);
1583                 else
1584                         MYREPARSE (@4, $2, SCM_ARG, $4);
1585         }
1586         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup STRING
1587         {
1588                 $$ = $3;
1589                 SCM res = try_string_variants ($2, $4);
1590                 if (!SCM_UNBNDP (res))
1591                         if (scm_is_pair (res))
1592                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1593                         else
1594                                 MYREPARSE (@4, $2, SCM_ARG, res);
1595                 else if (scm_is_true
1596                          (scm_call_1
1597                           ($2, make_music_from_simple
1598                            (parser, @4, $4))))
1599                         MYREPARSE (@4, $2, STRING, $4);
1600                 else
1601                         MYREPARSE (@4, $2, SCM_ARG, $4);
1602         }
1603         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup full_markup
1604         {
1605                 $$ = $3;
1606                 if (scm_is_true (scm_call_1 ($2, $4)))
1607                         MYREPARSE (@4, $2, SCM_ARG, $4);
1608                 else if (scm_is_true
1609                          (scm_call_1
1610                           ($2, make_music_from_simple
1611                            (parser, @4, $4))))
1612                         MYREPARSE (@4, $2, STRING, $4);
1613                 else
1614                         MYREPARSE (@4, $2, SCM_ARG, $4);
1615         }
1616         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup UNSIGNED
1617         {
1618                 $$ = $3;
1619                 if (scm_is_true (scm_call_1 ($2, $4)))
1620                         MYREPARSE (@4, $2, REAL, $4);
1621                 else {
1622                         SCM d = make_duration ($4);
1623                         if (SCM_UNBNDP (d) || scm_is_false (scm_call_1 ($2, d)))
1624                                 MYREPARSE (@4, $2, REAL, $4); // trigger error
1625                         else
1626                                 MYREPARSE (@4, $2, DURATION_IDENTIFIER, d);
1627                 }
1628         }
1629         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_nonbackup DURATION_IDENTIFIER {
1630                 $$ = $3;
1631                 MYREPARSE (@4, $2, DURATION_IDENTIFIER, $4);
1632         }
1633         ;
1634
1635
1636 // function_arglist_backup can't occur at the end of an argument
1637 // list.
1638 function_arglist_backup:
1639         function_arglist_common
1640         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup embedded_scm_arg
1641         {
1642                 if (scm_is_true (scm_call_1 ($2, $4)))
1643                         $$ = scm_cons ($4, $3);
1644                 else {
1645                         $$ = make_music_from_simple (parser, @4, $4);
1646                         if (scm_is_true (scm_call_1 ($2, $$)))
1647                                 $$ = scm_cons ($$, $3);
1648                         else
1649                         {
1650                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1651                                 MYBACKUP (SCM_ARG, $4, @4);
1652                         }
1653                 }
1654         }
1655         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup post_event_nofinger
1656         {
1657                 if (scm_is_true (scm_call_1 ($2, $4)))
1658                 {
1659                         $$ = scm_cons ($4, $3);
1660                 } else {
1661                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1662                         MYBACKUP (EVENT_IDENTIFIER, $4, @4);
1663                 }
1664         }
1665         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup pitch
1666         {
1667                 if (scm_is_true
1668                     (scm_call_1
1669                      ($2, make_music_from_simple
1670                       (parser, @4, $4))))
1671                 {
1672                         $$ = $3;
1673                         MYREPARSE (@4, $2, PITCH_IDENTIFIER, $4);
1674                 } else if (scm_is_true (scm_call_1 ($2, $4)))
1675                         $$ = scm_cons ($4, $3);
1676                 else {
1677                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1678                         MYBACKUP (PITCH_IDENTIFIER, $4, @4);
1679                 }
1680         }
1681         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup steno_tonic_pitch
1682         {
1683                 if (scm_is_true
1684                     (scm_call_1
1685                      ($2, make_music_from_simple
1686                       (parser, @4, $4))))
1687                 {
1688                         $$ = $3;
1689                         MYREPARSE (@4, $2, TONICNAME_PITCH, $4);
1690                 } else if (scm_is_true (scm_call_1 ($2, $4)))
1691                         $$ = scm_cons ($4, $3);
1692                 else {
1693                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1694                         MYBACKUP (TONICNAME_PITCH, $4, @4);
1695                 }
1696         }
1697         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup full_markup
1698         {
1699                 if (scm_is_true (scm_call_1 ($2, $4)))
1700                         $$ = scm_cons ($4, $3);
1701                 else {
1702                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1703                         MYBACKUP (LYRIC_ELEMENT, $4, @4);
1704                 }
1705         }
1706         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup UNSIGNED
1707         {
1708                 if (scm_is_true (scm_call_1 ($2, $4)))
1709                 {
1710                         MYREPARSE (@4, $2, REAL, $4);
1711                         $$ = $3;
1712                 } else {
1713                         SCM d = make_duration ($4);
1714                         if (SCM_UNBNDP (d) || scm_is_false (scm_call_1 ($2, d)))
1715                         {
1716                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1717                                 MYBACKUP (UNSIGNED, $4, @4);
1718                         } else {
1719                                 MYREPARSE (@4, $2, DURATION_IDENTIFIER, d);
1720                                 $$ = $3;
1721                         }
1722                 }
1723         }
1724         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup REAL
1725         {
1726                 if (scm_is_true (scm_call_1 ($2, $4)))
1727                 {
1728                         $$ = $3;
1729                         MYREPARSE (@4, $2, REAL, $4);
1730                 } else {
1731                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1732                         MYBACKUP (REAL, $4, @4);
1733                 }
1734         }
1735         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup NUMBER_IDENTIFIER
1736         {
1737                 if (scm_is_true (scm_call_1 ($2, $4)))
1738                 {
1739                         $$ = scm_cons ($4, $3);
1740                 } else {
1741                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1742                         MYBACKUP (NUMBER_IDENTIFIER, $4, @4);
1743                 }
1744         }
1745         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' UNSIGNED
1746         {
1747                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1748                 if (scm_is_true (scm_call_1 ($2, n))) {
1749                         $$ = $3;
1750                         MYREPARSE (@5, $2, REAL, n);
1751                 } else {
1752                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1753                         t->set_property ("digit", $5);
1754                         $$ = t->unprotect ();
1755                         if (scm_is_true (scm_call_1 ($2, $$)))
1756                                 $$ = scm_cons ($$, $3);
1757                         else {
1758                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1759                                 MYBACKUP (UNSIGNED, $5, @5);
1760                                 parser->lexer_->push_extra_token (@4, '-');
1761                         }
1762                 }
1763                 
1764         }
1765         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' REAL
1766         {
1767                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1768                 if (scm_is_true (scm_call_1 ($2, n))) {
1769                         MYREPARSE (@5, $2, REAL, n);
1770                         $$ = $3;
1771                 } else {
1772                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1773                         MYBACKUP (REAL, n, @5);
1774                 }
1775         }
1776         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup '-' NUMBER_IDENTIFIER
1777         {
1778                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1779                 if (scm_is_true (scm_call_1 ($2, n))) {
1780                         $$ = scm_cons (n, $3);
1781                 } else {
1782                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1783                         MYBACKUP (NUMBER_IDENTIFIER, n, @5);
1784                 }
1785         }
1786         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup DURATION_IDENTIFIER
1787         {
1788                 if (scm_is_true (scm_call_1 ($2, $4)))
1789                 {
1790                         MYREPARSE (@4, $2, DURATION_IDENTIFIER, $4);
1791                         $$ = $3;
1792                 } else {
1793                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1794                         MYBACKUP (DURATION_IDENTIFIER, $4, @4);
1795                 }
1796         }
1797         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup SCM_IDENTIFIER
1798         {
1799                 SCM res = try_string_variants ($2, $4);
1800                 if (!SCM_UNBNDP (res))
1801                         if (scm_is_pair (res)) {
1802                                 $$ = $3;
1803                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1804                         }
1805                         else
1806                                 $$ = scm_cons (res, $3);
1807                 else {
1808                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1809                         MYBACKUP (SCM_IDENTIFIER, $4, @4);
1810                 }
1811         }
1812         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup STRING
1813         {
1814                 SCM res = try_string_variants ($2, $4);
1815                 if (!SCM_UNBNDP (res))
1816                         if (scm_is_pair (res)) {
1817                                 $$ = $3;
1818                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1819                         }
1820                         else
1821                                 $$ = scm_cons (res, $3);
1822                 else {
1823                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1824                         MYBACKUP (STRING, $4, @4);
1825                 }
1826         }
1827         | function_arglist_backup REPARSE pitch_or_music
1828         {
1829                 if (scm_is_true (scm_call_1 ($2, $3)))
1830                         $$ = scm_cons ($3, $1);
1831                 else
1832                         $$ = check_scheme_arg (parser, @3,
1833                                                make_music_from_simple
1834                                                (parser, @3, $3),
1835                                                $1, $2);
1836         }
1837         | function_arglist_backup REPARSE bare_number_common
1838         {
1839                 $$ = check_scheme_arg (parser, @3,
1840                                        $3, $1, $2);
1841         }
1842         | function_arglist_backup REPARSE duration_length
1843         {
1844                 $$ = check_scheme_arg (parser, @3,
1845                                        $3, $1, $2);
1846         }
1847         | function_arglist_backup REPARSE symbol_list_arg
1848         {
1849                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1850         }
1851         ;
1852
1853 function_arglist:
1854         function_arglist_nonbackup
1855         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_nonbackup DEFAULT
1856         {
1857                 $$ = scm_cons (loc_on_music (@4, $1), $3);
1858         }
1859         ;
1860
1861 function_arglist_skip_nonbackup:
1862         function_arglist_nonbackup
1863         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_nonbackup
1864         {
1865                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1866         }
1867         ;
1868
1869 function_arglist_common:
1870         EXPECT_NO_MORE_ARGS {
1871                 $$ = SCM_EOL;
1872         }
1873         | EXPECT_SCM function_arglist_optional embedded_scm_arg
1874         {
1875                 if (scm_is_true (scm_call_1 ($1, $3)))
1876                         $$ = scm_cons ($3, $2);
1877                 else
1878                         $$ = check_scheme_arg (parser, @3,
1879                                                make_music_from_simple
1880                                                (parser, @3, $3),
1881                                                $2, $1);
1882         }
1883         | EXPECT_SCM function_arglist_optional bare_number_common
1884         {
1885                 $$ = check_scheme_arg (parser, @3,
1886                                        $3, $2, $1);
1887         }
1888         | EXPECT_SCM function_arglist_optional post_event_nofinger
1889         {
1890                 $$ = check_scheme_arg (parser, @3,
1891                                        $3, $2, $1);
1892         }
1893         | EXPECT_SCM function_arglist_optional '-' NUMBER_IDENTIFIER
1894         {
1895                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1896                 $$ = check_scheme_arg (parser, @4, n, $2, $1);
1897         }
1898         | function_arglist_common_reparse REPARSE SCM_ARG
1899         {
1900                 $$ = check_scheme_arg (parser, @3,
1901                                        $3, $1, $2);
1902         }
1903         | function_arglist_common_reparse REPARSE lyric_element_music
1904         {
1905                 $$ = check_scheme_arg (parser, @3,
1906                                        $3, $1, $2);
1907         }
1908         | function_arglist_common_reparse REPARSE pitch_or_music
1909         {
1910                 if (scm_is_true (scm_call_1 ($2, $3)))
1911                         $$ = scm_cons ($3, $1);
1912                 else
1913                         $$ = check_scheme_arg (parser, @3,
1914                                                make_music_from_simple
1915                                                (parser, @3, $3),
1916                                                $1, $2);
1917         }
1918         | function_arglist_common_reparse REPARSE bare_number_common
1919         {
1920                 $$ = check_scheme_arg (parser, @3,
1921                                        $3, $1, $2);
1922         }
1923         | function_arglist_common_reparse REPARSE duration_length
1924         {
1925                 $$ = check_scheme_arg (parser, @3,
1926                                        $3, $1, $2);
1927         }
1928         | function_arglist_common_reparse REPARSE symbol_list_arg
1929         {
1930                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1931         }
1932         ;
1933
1934 function_arglist_common_reparse:
1935         EXPECT_SCM function_arglist_optional SCM_IDENTIFIER
1936         {
1937                 $$ = $2;
1938                 SCM res = try_string_variants ($1, $3);
1939                 if (!SCM_UNBNDP (res))
1940                         if (scm_is_pair (res))
1941                                 MYREPARSE (@3, $1, SYMBOL_LIST, res);
1942                         else
1943                                 MYREPARSE (@3, $1, SCM_ARG, res);
1944                 else if (scm_is_true
1945                          (scm_call_1
1946                           ($1, make_music_from_simple (parser, @3, $3))))
1947                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1948                 else
1949                         // This is going to flag a syntax error, we
1950                         // know the predicate to be false.
1951                         MYREPARSE (@3, $1, SCM_ARG, $3);
1952         }
1953         | EXPECT_SCM function_arglist_optional pitch
1954         {
1955                 $$ = $2;
1956                 if (scm_is_true
1957                     (scm_call_1
1958                      ($1, make_music_from_simple
1959                       (parser, @3, $3))))
1960                         MYREPARSE (@3, $1, PITCH_IDENTIFIER, $3);
1961                 else
1962                         MYREPARSE (@3, $1, SCM_ARG, $3);
1963         }
1964         | EXPECT_SCM function_arglist_optional steno_tonic_pitch
1965         {
1966                 $$ = $2;
1967                 if (scm_is_true
1968                     (scm_call_1
1969                      ($1, make_music_from_simple
1970                       (parser, @3, $3))))
1971                         MYREPARSE (@3, $1, TONICNAME_PITCH, $3);
1972                 else
1973                         MYREPARSE (@3, $1, SCM_ARG, $3);
1974         }
1975         | EXPECT_SCM function_arglist_optional STRING
1976         {
1977                 $$ = $2;
1978                 SCM res = try_string_variants ($1, $3);
1979                 if (!SCM_UNBNDP (res))
1980                         if (scm_is_pair (res))
1981                                 MYREPARSE (@3, $1, SYMBOL_LIST, res);
1982                         else
1983                                 MYREPARSE (@3, $1, SCM_ARG, res);
1984                 else if (scm_is_true
1985                          (scm_call_1
1986                           ($1, make_music_from_simple (parser, @3, $3))))
1987                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1988                 else
1989                         // This is going to flag a syntax error, we
1990                         // know the predicate to be false.
1991                         MYREPARSE (@3, $1, SCM_ARG, $3);
1992         }
1993         | EXPECT_SCM function_arglist_optional full_markup
1994         {
1995                 $$ = $2;
1996                 if (scm_is_true (scm_call_1 ($1, $3)))
1997                         MYREPARSE (@3, $1, SCM_ARG, $3);
1998                 else if (scm_is_true
1999                          (scm_call_1
2000                           ($1, make_music_from_simple (parser, @3, $3))))
2001                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
2002                 else
2003                         // This is going to flag a syntax error, we
2004                         // know the predicate to be false.
2005                         MYREPARSE (@3, $1, SCM_ARG, $3);
2006         }
2007         | EXPECT_SCM function_arglist_optional UNSIGNED
2008         {
2009                 $$ = $2;
2010                 if (scm_is_true (scm_call_1 ($1, $3)))
2011                         MYREPARSE (@3, $1, REAL, $3);
2012                 else {
2013                         SCM d = make_duration ($3);
2014                         if (SCM_UNBNDP (d) || scm_is_false (scm_call_1 ($1, d)))
2015                                 MYREPARSE (@3, $1, REAL, $3);
2016                         else
2017                                 MYREPARSE (@3, $1, DURATION_IDENTIFIER, d);
2018                 }
2019         }
2020         | EXPECT_SCM function_arglist_optional DURATION_IDENTIFIER
2021         {
2022                 $$ = $2;
2023                 MYREPARSE (@3, $1, DURATION_IDENTIFIER, $3);
2024         }
2025         | EXPECT_SCM function_arglist_optional '-' UNSIGNED
2026         {
2027                 $$ = $2;
2028                 SCM n = scm_difference ($4, SCM_UNDEFINED);
2029                 if (scm_is_true (scm_call_1 ($1, n)))
2030                         MYREPARSE (@4, $1, REAL, n);
2031                 else {
2032                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @4);
2033                         t->set_property ("digit", $4);
2034                         SCM m = t->unprotect ();
2035                         if (scm_is_true (scm_call_1 ($1, m)))
2036                                 MYREPARSE (@4, $1, SCM_ARG, m);
2037                         else
2038                                 MYREPARSE (@4, $1, SCM_ARG, $4);
2039                 }
2040                 
2041         }
2042         | EXPECT_SCM function_arglist_optional '-' REAL
2043         {
2044                 $$ = $2;
2045                 SCM n = scm_difference ($4, SCM_UNDEFINED);
2046                 MYREPARSE (@4, $1, REAL, n);
2047         }
2048         ;
2049
2050 function_arglist_optional:
2051         function_arglist_backup
2052         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_backup DEFAULT
2053         {
2054                 $$ = scm_cons (loc_on_music (@4, $1), $3);
2055         }
2056         | function_arglist_skip_backup BACKUP
2057         ;
2058
2059 function_arglist_skip_backup:
2060         function_arglist_backup
2061         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip_backup
2062         {
2063                 $$ = scm_cons (loc_on_music (@3, $1), $3);
2064         }
2065         ;
2066
2067 music_function_call:
2068         MUSIC_FUNCTION function_arglist {
2069                 $$ = MAKE_SYNTAX ("music-function", @$,
2070                                          $1, $2);
2071         }
2072         ;
2073
2074
2075 optional_id:
2076         /**/ { $$ = SCM_EOL; }
2077         | '=' simple_string {
2078                 $$ = $2;
2079         }
2080         ;
2081
2082 // We must not have lookahead tokens parsed in lyric mode.  In order
2083 // to save confusion, we take almost the same set as permitted with
2084 // \lyricmode and/or \lyrics.  However, music identifiers are also
2085 // allowed, and they obviously do not require switching into lyrics
2086 // mode for parsing.
2087
2088 lyric_mode_music:
2089         {
2090                 parser->lexer_->push_lyric_state ();
2091         } grouped_music_list
2092         {
2093                 parser->lexer_->pop_state ();
2094                 $$ = $2;
2095         }
2096         | MUSIC_IDENTIFIER
2097         ;
2098
2099 complex_music:
2100         music_function_call
2101         | repeated_music                { $$ = $1; }
2102         | re_rhythmed_music     { $$ = $1; }
2103         | complex_music_prefix music
2104         {
2105                 $$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
2106         }
2107         ;
2108
2109 complex_music_prefix:
2110         CONTEXT symbol optional_id optional_context_mod {
2111                 Context_mod *ctxmod = unsmob_context_mod ($4);
2112                 SCM mods = SCM_EOL;
2113                 if (ctxmod)
2114                         mods = ctxmod->get_mods ();
2115                 $$ = START_MAKE_SYNTAX ("context-specification", $2, $3, mods, SCM_BOOL_F);
2116         }
2117         | NEWCONTEXT symbol optional_id optional_context_mod {
2118                 Context_mod *ctxmod = unsmob_context_mod ($4);
2119                 SCM mods = SCM_EOL;
2120                 if (ctxmod)
2121                         mods = ctxmod->get_mods ();
2122                 $$ = START_MAKE_SYNTAX ("context-specification", $2, $3, mods, SCM_BOOL_T);
2123         }
2124         ;
2125
2126 mode_changed_music:
2127         mode_changing_head grouped_music_list {
2128                 if ($1 == ly_symbol2scm ("chords"))
2129                 {
2130                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $2);
2131                 }
2132                 else
2133                 {
2134                   $$ = $2;
2135                 }
2136                 parser->lexer_->pop_state ();
2137         }
2138         | mode_changing_head_with_context optional_context_mod grouped_music_list {
2139                 Context_mod *ctxmod = unsmob_context_mod ($2);
2140                 SCM mods = SCM_EOL;
2141                 if (ctxmod)
2142                         mods = ctxmod->get_mods ();
2143                 $$ = MAKE_SYNTAX ("context-specification", @$, $1, SCM_EOL, mods, SCM_BOOL_T, $3);
2144                 if ($1 == ly_symbol2scm ("ChordNames"))
2145                 {
2146                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $$);
2147                 }
2148                 parser->lexer_->pop_state ();
2149         }
2150         ;
2151
2152 mode_changing_head:
2153         NOTEMODE {
2154                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
2155                 parser->lexer_->push_note_state (nn);
2156
2157                 $$ = ly_symbol2scm ("notes");
2158         }
2159         | DRUMMODE
2160                 {
2161                 SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
2162                 parser->lexer_->push_note_state (nn);
2163
2164                 $$ = ly_symbol2scm ("drums");
2165         }
2166         | FIGUREMODE {
2167                 parser->lexer_->push_figuredbass_state ();
2168
2169                 $$ = ly_symbol2scm ("figures");
2170         }
2171         | CHORDMODE {
2172                 SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
2173                 parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
2174                 nn = parser->lexer_->lookup_identifier ("pitchnames");
2175                 parser->lexer_->push_chord_state (nn);
2176                 $$ = ly_symbol2scm ("chords");
2177
2178         }
2179         | LYRICMODE
2180                 { parser->lexer_->push_lyric_state ();
2181                 $$ = ly_symbol2scm ("lyrics");
2182         }
2183         ;
2184
2185 mode_changing_head_with_context:
2186         DRUMS {
2187                 SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
2188                 parser->lexer_->push_note_state (nn);
2189
2190                 $$ = ly_symbol2scm ("DrumStaff");
2191         }
2192         | FIGURES {
2193                 parser->lexer_->push_figuredbass_state ();
2194
2195                 $$ = ly_symbol2scm ("FiguredBass");
2196         }
2197         | CHORDS {
2198                 SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
2199                 parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
2200                 nn = parser->lexer_->lookup_identifier ("pitchnames");
2201                 parser->lexer_->push_chord_state (nn);
2202                 $$ = ly_symbol2scm ("ChordNames");
2203         }
2204         | LYRICS
2205                 { parser->lexer_->push_lyric_state ();
2206                 $$ = ly_symbol2scm ("Lyrics");
2207         }
2208         ;
2209
2210 new_lyrics:
2211         ADDLYRICS lyric_mode_music {
2212                 $$ = scm_list_1 ($2);
2213         }
2214         | new_lyrics ADDLYRICS lyric_mode_music {
2215                 $$ = scm_cons ($3, $1);
2216         }
2217         ;
2218
2219 re_rhythmed_music:
2220         composite_music new_lyrics {
2221                 $$ = MAKE_SYNTAX ("add-lyrics", @$, $1, scm_reverse_x ($2, SCM_EOL));
2222         } %prec COMPOSITE
2223         | LYRICSTO simple_string lyric_mode_music {
2224                 $$ = MAKE_SYNTAX ("lyric-combine", @$, $2, $3);
2225         }
2226         ;
2227
2228 context_change:
2229         CHANGE symbol '=' simple_string  {
2230                 $$ = MAKE_SYNTAX ("context-change", @$, $2, $4);
2231         }
2232         ;
2233
2234
2235 property_path:
2236         symbol_list_rev  {
2237                 $$ = scm_reverse_x ($1, SCM_EOL);
2238         }
2239         | symbol_list_rev property_path {
2240                 $$ = scm_reverse_x ($1, $2);
2241         }
2242         ;
2243
2244 property_operation:
2245         symbol '=' scalar {
2246                 $$ = scm_list_3 (ly_symbol2scm ("assign"), $1, $3);
2247         }
2248         | UNSET symbol {
2249                 $$ = scm_list_2 (ly_symbol2scm ("unset"), $2);
2250         }
2251         | OVERRIDE property_path '=' scalar {
2252                 if (scm_ilength ($2) < 2) {
2253                         parser->parser_error (@2, _("bad grob property path"));
2254                         $$ = SCM_UNDEFINED;
2255                 } else {
2256                         $$ = scm_cons (ly_symbol2scm ("push"),
2257                                        scm_cons2 (scm_car ($2),
2258                                                   $4,
2259                                                   scm_cdr ($2)));
2260                 }
2261         }
2262         | REVERT revert_arg {
2263                 $$ = scm_cons (ly_symbol2scm ("pop"), $2);
2264         }
2265         ;
2266
2267 // This is all quite awkward for the sake of substantial backward
2268 // compatibility while at the same time allowing a more "natural" form
2269 // of specification not separating grob specification from grob
2270 // property path.  The purpose of this definition of revert_arg is to
2271 // allow the symbol list which specifies grob and property to revert
2272 // to be optionally be split into two parts after the grob (which in
2273 // this case is just the first element of the list).  symbol_list_part
2274 // is only one path component, but it can be parsed without lookahead,
2275 // so we can follow it with a synthetic BACKUP token when needed.  If
2276 // the first symbol_list_part already contains multiple elements (only
2277 // possible if a Scheme expression provides them), we just parse for
2278 // additional elements introduced by '.', which is what the
2279 // SYMBOL_LIST backup in connection with the immediately following
2280 // rule using symbol_list_arg does.
2281 //
2282 // As long as we don't have our coffers filled with both grob and at
2283 // least one grob property specification, the rest of the required
2284 // symbol list chain may be provided either with or without a leading
2285 // dot.  This is for both allowing the traditional
2286 // \revert Accidental #'color
2287 // as well as well as the "naive" form
2288 // \revert Accidental.color
2289
2290 revert_arg:
2291         revert_arg_backup BACKUP symbol_list_arg
2292         {
2293                 $$ = $3;
2294         }
2295         ;
2296
2297 revert_arg_backup:
2298         revert_arg_part
2299         {
2300                 if (scm_is_null ($1)
2301                     || scm_is_null (scm_cdr ($1)))
2302                         MYBACKUP (SCM_ARG, $1, @1);
2303                 else
2304                         MYBACKUP (SYMBOL_LIST, scm_reverse_x ($1, SCM_EOL), @1);
2305         }
2306         ;
2307
2308 // revert_arg_part delivers results in reverse
2309 revert_arg_part:
2310         symbol_list_part
2311         | revert_arg_backup BACKUP SCM_ARG '.' symbol_list_part
2312         {
2313                 $$ = scm_append_x (scm_list_2 ($5, $3));
2314         }
2315         | revert_arg_backup BACKUP SCM_ARG symbol_list_part
2316         {
2317                 $$ = scm_append_x (scm_list_2 ($4, $3));
2318         }               
2319         ;
2320
2321 context_def_mod:
2322         CONSISTS { $$ = ly_symbol2scm ("consists"); }
2323         | REMOVE { $$ = ly_symbol2scm ("remove"); }
2324
2325         | ACCEPTS { $$ = ly_symbol2scm ("accepts"); }
2326         | DEFAULTCHILD { $$ = ly_symbol2scm ("default-child"); }
2327         | DENIES { $$ = ly_symbol2scm ("denies"); }
2328
2329         | ALIAS { $$ = ly_symbol2scm ("alias"); }
2330         | TYPE { $$ = ly_symbol2scm ("translator-type"); }
2331         | DESCRIPTION { $$ = ly_symbol2scm ("description"); }
2332         | NAME { $$ = ly_symbol2scm ("context-name"); }
2333         ;
2334
2335 context_mod:
2336         property_operation { $$ = $1; }
2337         | context_def_mod STRING {
2338                 $$ = scm_list_2 ($1, $2);
2339         }
2340         | context_def_mod embedded_scm
2341         {
2342                 if (!scm_is_string ($2)
2343                     && ly_symbol2scm ("consists") != $1
2344                     && ly_symbol2scm ("remove") != $1)
2345                 {
2346                         $$ = SCM_EOL;
2347                         parser->parser_error (@1, _ ("only \\consists and \\remove take non-string argument."));
2348                 }
2349                 else
2350                 {
2351                         $$ = scm_list_2 ($1, $2);
2352                 }
2353         }
2354         ;
2355
2356 // If defined, at least two members.
2357 grob_prop_spec:
2358         symbol_list_rev
2359         {
2360                 SCM l = scm_reverse_x ($1, SCM_EOL);
2361                 if (scm_is_pair (l)
2362                     && to_boolean
2363                     (scm_object_property (scm_car (l),
2364                                           ly_symbol2scm ("is-grob?"))))
2365                         l = scm_cons (ly_symbol2scm ("Bottom"), l);
2366                 if (scm_is_null (l) || scm_is_null (scm_cdr (l))) {
2367                         parser->parser_error (@1, _ ("bad grob property path"));
2368                         l = SCM_UNDEFINED;
2369                 }
2370                 $$ = l;
2371         }
2372         ;
2373
2374 // If defined, at least three members
2375 grob_prop_path:
2376         grob_prop_spec
2377         {
2378                 if (!SCM_UNBNDP ($1) && scm_is_null (scm_cddr ($1)))
2379                 {
2380                         parser->parser_error (@1, _ ("bad grob property path"));
2381                         $$ = SCM_UNDEFINED;
2382                 }
2383         }
2384         | grob_prop_spec property_path
2385         {
2386                 if (!SCM_UNBNDP ($1)) {
2387                         $$ = scm_append_x (scm_list_2 ($1, $2));
2388                         if (scm_is_null (scm_cddr ($$))) {
2389                                 parser->parser_error (@$, _ ("bad grob property path"));
2390                                 $$ = SCM_UNDEFINED;
2391                         }
2392                 }
2393
2394         }
2395         ;
2396
2397 // Exactly two elements or undefined
2398 context_prop_spec:
2399         symbol_list_rev
2400         {
2401                 SCM l = scm_reverse_x ($1, SCM_EOL);
2402                 switch (scm_ilength (l)) {
2403                 case 1:
2404                         l = scm_cons (ly_symbol2scm ("Bottom"), l);
2405                 case 2:
2406                         break;
2407                 default:
2408                         parser->parser_error (@1, _ ("bad context property path"));
2409                         l = SCM_UNDEFINED;
2410                 }
2411                 $$ = l;
2412         }
2413         ;
2414
2415 simple_music_property_def:
2416         OVERRIDE grob_prop_path '=' scalar {
2417                 if (SCM_UNBNDP ($2))
2418                         $$ = SCM_UNDEFINED;
2419                 else {
2420                         $$ = scm_list_5 (scm_car ($2),
2421                                          ly_symbol2scm ("OverrideProperty"),
2422                                          scm_cadr ($2),
2423                                          $4,
2424                                          scm_cddr ($2));
2425                 }
2426         }
2427         | REVERT simple_revert_context revert_arg {
2428                 $$ = scm_list_4 ($2,
2429                                  ly_symbol2scm ("RevertProperty"),
2430                                  scm_car ($3),
2431                                  scm_cdr ($3));
2432         }
2433         | SET context_prop_spec '=' scalar {
2434                 if (SCM_UNBNDP ($2))
2435                         $$ = SCM_UNDEFINED;
2436                 else
2437                         $$ = scm_list_4 (scm_car ($2),
2438                                          ly_symbol2scm ("PropertySet"),
2439                                          scm_cadr ($2),
2440                                          $4);
2441         }
2442         | UNSET context_prop_spec {
2443                 if (SCM_UNBNDP ($2))
2444                         $$ = SCM_UNDEFINED;
2445                 else
2446                         $$ = scm_list_3 (scm_car ($2),
2447                                          ly_symbol2scm ("PropertyUnset"),
2448                                          scm_cadr ($2));
2449         }
2450         ;
2451
2452
2453 // This is all quite awkward for the sake of substantial backward
2454 // compatibility while at the same time allowing a more "natural" form
2455 // of specification not separating grob specification from grob
2456 // property path.  The purpose of this definition of
2457 // simple_revert_context is to allow the symbol list which specifies
2458 // grob and property to revert to be optionally be split into two
2459 // parts after the grob (which may be preceded by a context
2460 // specification, a case which we distinguish by checking whether the
2461 // first symbol is a valid grob symbol instead).
2462 //
2463 // See revert_arg above for the main work horse of this arrangement.
2464 // simple_revert_context just caters for the context and delegates the
2465 // rest of the job to revert_arg.
2466
2467 simple_revert_context:
2468         symbol_list_part
2469         {
2470                 $1 = scm_reverse_x ($1, SCM_EOL);
2471                 if (scm_is_null ($1)
2472                     || to_boolean
2473                     (scm_object_property (scm_car ($1),
2474                                           ly_symbol2scm ("is-grob?")))) {
2475                         $$ = ly_symbol2scm ("Bottom");
2476                         parser->lexer_->push_extra_token (@1, SCM_IDENTIFIER, $1);
2477                 } else {
2478                         $$ = scm_car ($1);
2479                         parser->lexer_->push_extra_token (@1, SCM_IDENTIFIER,
2480                                                           scm_cdr ($1));
2481                 }
2482         }
2483         ;
2484
2485 music_property_def:
2486         simple_music_property_def {
2487                 if (SCM_UNBNDP ($1))
2488                         $$ = MAKE_SYNTAX ("void-music", @1);
2489                 else
2490                         $$ = LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("property-operation"), scm_cons2 (parser->self_scm (), make_input (@$), $1));
2491         }
2492         ;
2493
2494 string:
2495         STRING {
2496                 $$ = $1;
2497         }
2498         | full_markup
2499         ;
2500
2501 simple_string: STRING {
2502                 $$ = $1;
2503         }
2504         | embedded_scm_bare
2505         {
2506                 if (scm_is_string ($1)) {
2507                         $$ = $1;
2508                 } else {
2509                         parser->parser_error (@1, (_ ("simple string expected")));
2510                         $$ = scm_string (SCM_EOL);
2511                 }
2512         }
2513         ;
2514
2515 symbol:
2516         STRING {
2517                 $$ = scm_string_to_symbol ($1);
2518         }
2519         | embedded_scm_bare
2520         {
2521                 // This is a bit of overkill but makes the same
2522                 // routine responsible for all symbol interpretations.
2523                 $$ = try_string_variants (ly_lily_module_constant ("symbol?"),
2524                                           $1);
2525                 if (SCM_UNBNDP ($$))
2526                 {
2527                         parser->parser_error (@1, (_ ("symbol expected")));
2528                         // Generate a unique symbol in case it is used
2529                         // for an assignment or similar
2530                         $$ = scm_make_symbol (ly_string2scm ("undefined"));
2531                 }
2532         }
2533         ;
2534
2535 scalar:
2536         embedded_scm_arg
2537         | pitch_or_music
2538         | SCM_IDENTIFIER
2539         | bare_number
2540         // The following is a rather defensive variant of admitting
2541         // negative numbers: the grammar would permit number_factor or
2542         // even number_expression.  However, function arguments allow
2543         // only this simple kind of negative number, so to have things
2544         // like \tweak and \override behave reasonably similar, it
2545         // makes sense to rule out things like -- which are rather an
2546         // accent in function argument contexts.
2547         | '-' bare_number
2548         {
2549                 $$ = scm_difference ($2, SCM_UNDEFINED);
2550         }
2551         | string
2552         ;
2553
2554 event_chord:
2555         simple_element post_events {
2556                 // Let the rhythmic music iterator sort this mess out.
2557                 if (scm_is_pair ($2)) {
2558                         unsmob_music ($$)->set_property ("articulations",
2559                                                          scm_reverse_x ($2, SCM_EOL));
2560                 }
2561         } %prec ':'
2562         | CHORD_REPETITION optional_notemode_duration post_events {
2563                 Input i;
2564                 i.set_location (@1, @3);
2565                 $$ = MAKE_SYNTAX ("repetition-chord", i,
2566                                   $2, scm_reverse_x ($3, SCM_EOL));
2567         } %prec ':'
2568         | MULTI_MEASURE_REST optional_notemode_duration post_events {
2569                 Input i;
2570                 i.set_location (@1, @3);
2571                 $$ = MAKE_SYNTAX ("multi-measure-rest", i, $2,
2572                                   scm_reverse_x ($3, SCM_EOL));
2573         } %prec ':'
2574         | tempo_event
2575         | note_chord_element
2576         ;
2577
2578
2579 note_chord_element:
2580         chord_body optional_notemode_duration post_events
2581         {
2582                 Music *m = unsmob_music ($1);
2583                 SCM dur = unsmob_duration ($2)->smobbed_copy ();
2584                 SCM es = m->get_property ("elements");
2585                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
2586
2587                 for (SCM s = es; scm_is_pair (s); s = scm_cdr (s))
2588                   unsmob_music (scm_car (s))->set_property ("duration", dur);
2589                 es = ly_append2 (es, postevs);
2590
2591                 m-> set_property ("elements", es);
2592                 m->set_spot (@$);
2593                 $$ = m->self_scm ();
2594         } %prec ':'
2595         ;
2596
2597 chord_body:
2598         ANGLE_OPEN chord_body_elements ANGLE_CLOSE
2599         {
2600                 $$ = MAKE_SYNTAX ("event-chord", @$, scm_reverse_x ($2, SCM_EOL));
2601         }
2602         ;
2603
2604 chord_body_elements:
2605         /* empty */             { $$ = SCM_EOL; }
2606         | chord_body_elements chord_body_element {
2607                 if (!SCM_UNBNDP ($2))
2608                         $$ = scm_cons ($2, $1);
2609         }
2610         ;
2611
2612 chord_body_element:
2613         pitch exclamations questions octave_check post_events
2614         {
2615                 bool q = to_boolean ($3);
2616                 bool ex = to_boolean ($2);
2617                 SCM check = $4;
2618                 SCM post = $5;
2619
2620                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2621                 n->set_property ("pitch", $1);
2622                 if (q)
2623                         n->set_property ("cautionary", SCM_BOOL_T);
2624                 if (ex || q)
2625                         n->set_property ("force-accidental", SCM_BOOL_T);
2626
2627                 if (scm_is_pair (post)) {
2628                         SCM arts = scm_reverse_x (post, SCM_EOL);
2629                         n->set_property ("articulations", arts);
2630                 }
2631                 if (scm_is_number (check))
2632                 {
2633                         int q = scm_to_int (check);
2634                         n->set_property ("absolute-octave", scm_from_int (q-1));
2635                 }
2636
2637                 $$ = n->unprotect ();
2638         }
2639         | DRUM_PITCH post_events {
2640                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2641                 n->set_property ("drum-type", $1);
2642
2643                 if (scm_is_pair ($2)) {
2644                         SCM arts = scm_reverse_x ($2, SCM_EOL);
2645                         n->set_property ("articulations", arts);
2646                 }
2647                 $$ = n->unprotect ();
2648         }
2649         | music_function_chord_body
2650         {
2651                 Music *m = unsmob_music ($1);
2652
2653                 while (m && m->is_mus_type ("music-wrapper-music")) {
2654                         $$ = m->get_property ("element");
2655                         m = unsmob_music ($$);
2656                 }
2657
2658                 if (!(m && m->is_mus_type ("rhythmic-event"))) {
2659                         parser->parser_error (@$, _ ("not a rhythmic event"));
2660                         $$ = SCM_UNDEFINED;
2661                 }
2662         }
2663         ;
2664
2665 music_function_chord_body:
2666         music_function_call
2667         | MUSIC_IDENTIFIER
2668         | embedded_scm
2669         ;
2670
2671 event_function_event:
2672         EVENT_FUNCTION function_arglist {
2673                 $$ = MAKE_SYNTAX ("music-function", @$,
2674                                          $1, $2);
2675         }
2676         ;
2677
2678 post_events:
2679         /* empty */ {
2680                 $$ = SCM_EOL;
2681         }
2682         | post_events post_event {
2683                 $$ = $1;
2684                 if (Music *m = unsmob_music ($2))
2685                 {
2686                         if (m->is_mus_type ("post-event-wrapper"))
2687                         {
2688                                 for (SCM p = m->get_property ("elements");
2689                                      scm_is_pair (p);
2690                                      p = scm_cdr (p))
2691                                 {
2692                                         $$ = scm_cons (scm_car (p), $$);
2693                                 }
2694                         } else {
2695                                 m->set_spot (@2);
2696                                 $$ = scm_cons ($2, $$);
2697                         }
2698                 }
2699         }
2700         ;
2701
2702 post_event_nofinger:
2703         direction_less_event {
2704                 $$ = $1;
2705         }
2706         | script_dir music_function_call {
2707                 $$ = $2;
2708                 if (!unsmob_music ($2)->is_mus_type ("post-event")) {
2709                         parser->parser_error (@2, _ ("post-event expected"));
2710                         $$ = SCM_UNSPECIFIED;
2711                 } else if (!SCM_UNBNDP ($1))
2712                 {
2713                         unsmob_music ($$)->set_property ("direction", $1);
2714                 }
2715         }
2716         | HYPHEN {
2717                 if (!parser->lexer_->is_lyric_state ())
2718                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2719                 $$ = MY_MAKE_MUSIC ("HyphenEvent", @$)->unprotect ();
2720         }
2721         | EXTENDER {
2722                 if (!parser->lexer_->is_lyric_state ())
2723                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2724                 $$ = MY_MAKE_MUSIC ("ExtenderEvent", @$)->unprotect ();
2725         }
2726         | script_dir direction_reqd_event {
2727                 if (!SCM_UNBNDP ($1))
2728                 {
2729                         Music *m = unsmob_music ($2);
2730                         m->set_property ("direction", $1);
2731                 }
2732                 $$ = $2;
2733         }
2734         | script_dir direction_less_event {
2735                 if (!SCM_UNBNDP ($1))
2736                 {
2737                         Music *m = unsmob_music ($2);
2738                         m->set_property ("direction", $1);
2739                 }
2740                 $$ = $2;
2741         }
2742         | '^' fingering
2743         {
2744                 $$ = $2;
2745                 unsmob_music ($$)->set_property ("direction", scm_from_int (UP));
2746         }
2747         | '_' fingering
2748         {
2749                 $$ = $2;
2750                 unsmob_music ($$)->set_property ("direction", scm_from_int (DOWN));
2751         }                       
2752         ;
2753
2754 post_event:
2755         post_event_nofinger
2756         | '-' fingering {
2757                 $$ = $2;
2758         }
2759         ;
2760
2761 string_number_event:
2762         E_UNSIGNED {
2763                 Music *s = MY_MAKE_MUSIC ("StringNumberEvent", @$);
2764                 s->set_property ("string-number", $1);
2765                 $$ = s->unprotect ();
2766         }
2767         ;
2768
2769 direction_less_event:
2770         string_number_event
2771         | EVENT_IDENTIFIER      {
2772                 $$ = $1;
2773         }
2774         | tremolo_type  {
2775                Music *a = MY_MAKE_MUSIC ("TremoloEvent", @$);
2776                a->set_property ("tremolo-type", $1);
2777                $$ = a->unprotect ();
2778         }
2779         | event_function_event  
2780         ;
2781
2782 direction_reqd_event:
2783         gen_text_def {
2784                 $$ = $1;
2785         }
2786         | script_abbreviation {
2787                 SCM s = parser->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
2788                 if (scm_is_string (s)) {
2789                         Music *a = MY_MAKE_MUSIC ("ArticulationEvent", @$);
2790                         a->set_property ("articulation-type", s);
2791                         $$ = a->unprotect ();
2792                 } else {
2793                         Music *original = unsmob_music (s);
2794                         if (original && original->is_mus_type ("post-event")) {
2795                                 Music *a = original->clone ();
2796                                 a->set_spot (parser->lexer_->override_input (@$));
2797                                 $$ = a->unprotect ();
2798                         } else {
2799                                 parser->parser_error (@1, _ ("expecting string or post-event as script definition"));
2800                                 $$ = MY_MAKE_MUSIC ("PostEvents", @$)->unprotect ();
2801                         }
2802                 }
2803         }
2804         ;
2805
2806 octave_check:
2807         /**/ { $$ = SCM_EOL; }
2808         | '=' quotes { $$ = $2; }
2809         ;
2810
2811 quotes:
2812         /* empty */
2813         {
2814                 $$ = SCM_INUM0;
2815         }
2816         | sub_quotes
2817         | sup_quotes
2818         ;
2819
2820 sup_quotes:
2821         '\'' {
2822                 $$ = scm_from_int (1);
2823         }
2824         | sup_quotes '\'' {
2825                 $$ = scm_oneplus ($1);
2826         }
2827         ;
2828
2829 sub_quotes:
2830         ',' {
2831                 $$ = scm_from_int (-1);
2832         }
2833         | sub_quotes ',' {
2834                 $$ = scm_oneminus ($1);
2835         }
2836         ;
2837
2838 steno_pitch:
2839         NOTENAME_PITCH quotes {
2840                 if (!scm_is_eq (SCM_INUM0, $2))
2841                 {
2842                         Pitch p = *unsmob_pitch ($1);
2843                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2844                         $$ = p.smobbed_copy ();
2845                 }
2846         }
2847         ;
2848
2849 /*
2850 ugh. duplication
2851 */
2852
2853 steno_tonic_pitch:
2854         TONICNAME_PITCH quotes {
2855                 if (!scm_is_eq (SCM_INUM0, $2))
2856                 {
2857                         Pitch p = *unsmob_pitch ($1);
2858                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2859                         $$ = p.smobbed_copy ();
2860                 }
2861         }
2862         ;
2863
2864 pitch:
2865         steno_pitch
2866         | PITCH_IDENTIFIER quotes {
2867                 if (!scm_is_eq (SCM_INUM0, $2))
2868                 {
2869                         Pitch p = *unsmob_pitch ($1);
2870                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2871                         $$ = p.smobbed_copy ();
2872                 }
2873         }
2874         ;
2875
2876 gen_text_def:
2877         full_markup {
2878                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2879                 t->set_property ("text", $1);
2880                 $$ = t->unprotect ();
2881         }
2882         | STRING {
2883                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2884                 t->set_property ("text",
2885                         make_simple_markup ($1));
2886                 $$ = t->unprotect ();
2887         }
2888         | embedded_scm
2889         {
2890                 Music *m = unsmob_music ($1);
2891                 if (m && m->is_mus_type ("post-event"))
2892                         $$ = $1;
2893                 else if (Text_interface::is_markup ($1)) {
2894                         Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2895                         t->set_property ("text", $1);
2896                         $$ = t->unprotect ();
2897                 } else
2898                         parser->parser_error (@1, _ ("not an articulation"));
2899         }
2900         ;
2901
2902 fingering:
2903         UNSIGNED {
2904                 Music *t = MY_MAKE_MUSIC ("FingeringEvent", @$);
2905                 t->set_property ("digit", $1);
2906                 $$ = t->unprotect ();
2907         }
2908         ;
2909
2910 script_abbreviation:
2911         '^'             {
2912                 $$ = scm_from_locale_string ("Hat");
2913         }
2914         | '+'           {
2915                 $$ = scm_from_locale_string ("Plus");
2916         }
2917         | '-'           {
2918                 $$ = scm_from_locale_string ("Dash");
2919         }
2920         | '!'           {
2921                 $$ = scm_from_locale_string ("Bang");
2922         }
2923         | ANGLE_CLOSE   {
2924                 $$ = scm_from_locale_string ("Larger");
2925         }
2926         | '.'           {
2927                 $$ = scm_from_locale_string ("Dot");
2928         }
2929         | '_' {
2930                 $$ = scm_from_locale_string ("Underscore");
2931         }
2932         ;
2933
2934 script_dir:
2935         '_'     { $$ = scm_from_int (DOWN); }
2936         | '^'   { $$ = scm_from_int (UP); }
2937         | '-'   { $$ = SCM_UNDEFINED; }
2938         ;
2939
2940 duration_length:
2941         multiplied_duration {
2942                 $$ = $1;
2943         }
2944         ;
2945
2946 maybe_notemode_duration:
2947         {
2948                 $$ = SCM_UNDEFINED;
2949         } %prec ':'
2950         | multiplied_duration   {
2951                 $$ = $1;
2952                 parser->default_duration_ = *unsmob_duration ($$);
2953         }
2954 ;
2955
2956
2957 optional_notemode_duration:
2958         maybe_notemode_duration
2959         {
2960                 if (SCM_UNBNDP ($$))
2961                         $$ = parser->default_duration_.smobbed_copy ();
2962         }
2963         ;
2964
2965 steno_duration:
2966         UNSIGNED dots           {
2967                 $$ = make_duration ($1, scm_to_int ($2));
2968                 if (SCM_UNBNDP ($$))
2969                 {
2970                         parser->parser_error (@1, _ ("not a duration"));
2971                         $$ = Duration ().smobbed_copy ();
2972                 }
2973         }
2974         | DURATION_IDENTIFIER dots      {
2975                 Duration *d = unsmob_duration ($1);
2976                 Duration k (d->duration_log (),
2977                             d->dot_count () + scm_to_int ($2));
2978                 k = k.compressed (d->factor ());
2979                 scm_remember_upto_here_1 ($1);
2980                 $$ = k.smobbed_copy ();
2981         }
2982         ;
2983
2984 multiplied_duration:
2985         steno_duration {
2986                 $$ = $1;
2987         }
2988         | multiplied_duration '*' UNSIGNED {
2989                 $$ = unsmob_duration ($$)->compressed (scm_to_int ($3)).smobbed_copy ();
2990         }
2991         | multiplied_duration '*' FRACTION {
2992                 Rational  m (scm_to_int (scm_car ($3)), scm_to_int (scm_cdr ($3)));
2993
2994                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
2995         }
2996         ;
2997
2998 dots:
2999         /* empty */     {
3000                 $$ = SCM_INUM0;
3001         }
3002         | dots '.' {
3003                 $$ = scm_oneplus ($1);
3004         }
3005         ;
3006
3007 tremolo_type:
3008         ':'     {
3009                 $$ = scm_from_int (parser->default_tremolo_type_);
3010         }
3011         | ':' UNSIGNED {
3012                 if (SCM_UNBNDP (make_duration ($2))) {
3013                         parser->parser_error (@2, _ ("not a duration"));
3014                         $$ = scm_from_int (parser->default_tremolo_type_);
3015                 } else {
3016                         $$ = $2;
3017                         parser->default_tremolo_type_ = scm_to_int ($2);
3018                 }
3019         }
3020         ;
3021
3022 bass_number:
3023         UNSIGNED { $$ = $1; }
3024         | STRING { $$ = $1; }
3025         | full_markup { $$ = $1; }
3026         | embedded_scm_bare
3027         {
3028                 // as an integer, it needs to be non-negative, and otherwise
3029                 // it needs to be suitable as a markup.
3030                 if (scm_is_integer ($1)
3031                     ? scm_is_true (scm_negative_p ($1))
3032                     : !Text_interface::is_markup ($1))
3033                 {
3034                         parser->parser_error (@1, _ ("bass number expected"));
3035                         $$ = SCM_INUM0;
3036                 }
3037         }
3038         ;
3039
3040 figured_bass_alteration:
3041         '-'     { $$ = ly_rational2scm (FLAT_ALTERATION); }
3042         | '+'   { $$ = ly_rational2scm (SHARP_ALTERATION); }
3043         | '!'   { $$ = scm_from_int (0); }
3044         ;
3045
3046 bass_figure:
3047         FIGURE_SPACE {
3048                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
3049                 $$ = bfr->unprotect ();
3050         }
3051         | bass_number  {
3052                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
3053                 $$ = bfr->self_scm ();
3054
3055                 if (scm_is_number ($1))
3056                         bfr->set_property ("figure", $1);
3057                 else if (Text_interface::is_markup ($1))
3058                         bfr->set_property ("text", $1);
3059
3060                 bfr->unprotect ();
3061         }
3062         | bass_figure ']' {
3063                 $$ = $1;
3064                 unsmob_music ($1)->set_property ("bracket-stop", SCM_BOOL_T);
3065         }
3066         | bass_figure figured_bass_alteration {
3067                 Music *m = unsmob_music ($1);
3068                 if (scm_to_double ($2)) {
3069                         SCM salter = m->get_property ("alteration");
3070                         SCM alter = scm_is_number (salter) ? salter : scm_from_int (0);
3071                         m->set_property ("alteration",
3072                                          scm_sum (alter, $2));
3073                 } else {
3074                         m->set_property ("alteration", scm_from_int (0));
3075                 }
3076         }
3077         | bass_figure figured_bass_modification  {
3078                 Music *m = unsmob_music ($1);
3079                 m->set_property ($2, SCM_BOOL_T);
3080         }
3081         ;
3082
3083
3084 figured_bass_modification:
3085         E_PLUS          {
3086                 $$ = ly_symbol2scm ("augmented");
3087         }
3088         | E_EXCLAMATION {
3089                 $$ = ly_symbol2scm ("no-continuation");
3090         }
3091         | '/'           {
3092                 $$ = ly_symbol2scm ("diminished");
3093         }
3094         | E_BACKSLASH {
3095                 $$ = ly_symbol2scm ("augmented-slash");
3096         }
3097         ;
3098
3099 br_bass_figure:
3100         bass_figure {
3101                 $$ = $1;
3102         }
3103         | '[' bass_figure {
3104                 $$ = $2;
3105                 unsmob_music ($$)->set_property ("bracket-start", SCM_BOOL_T);
3106         }
3107         ;
3108
3109 figure_list:
3110         /**/            {
3111                 $$ = SCM_EOL;
3112         }
3113         | figure_list br_bass_figure {
3114                 $$ = scm_cons ($2, $1);
3115         }
3116         ;
3117
3118 figure_spec:
3119         FIGURE_OPEN figure_list FIGURE_CLOSE {
3120                 $$ = scm_reverse_x ($2, SCM_EOL);
3121         }
3122         ;
3123
3124
3125 optional_rest:
3126         /**/   { $$ = SCM_BOOL_F; }
3127         | REST { $$ = SCM_BOOL_T; }
3128         ;
3129
3130 pitch_or_music:
3131         pitch exclamations questions octave_check maybe_notemode_duration optional_rest post_events {
3132                 if (!parser->lexer_->is_note_state ())
3133                         parser->parser_error (@1, _ ("have to be in Note mode for notes"));
3134                 if (!SCM_UNBNDP ($2)
3135                     || !SCM_UNBNDP ($3)
3136                     || scm_is_number ($4)
3137                     || !SCM_UNBNDP ($5)
3138                     || scm_is_true ($6)
3139                     || scm_is_pair ($7))
3140                 {
3141                         Music *n = 0;
3142                         if (scm_is_true ($6))
3143                                 n = MY_MAKE_MUSIC ("RestEvent", @$);
3144                         else
3145                                 n = MY_MAKE_MUSIC ("NoteEvent", @$);
3146                         
3147                         n->set_property ("pitch", $1);
3148                         if (SCM_UNBNDP ($5))
3149                                 n->set_property ("duration",
3150                                                  parser->default_duration_.smobbed_copy ());
3151                         else
3152                                 n->set_property ("duration", $5);
3153                         
3154                         if (scm_is_number ($4))
3155                         {
3156                                 int q = scm_to_int ($4);
3157                                 n->set_property ("absolute-octave", scm_from_int (q-1));
3158                         }
3159                         
3160                         if (to_boolean ($3))
3161                                 n->set_property ("cautionary", SCM_BOOL_T);
3162                         if (to_boolean ($2) || to_boolean ($3))
3163                                 n->set_property ("force-accidental", SCM_BOOL_T);
3164                         if (scm_is_pair ($7))
3165                                 n->set_property ("articulations",
3166                                                  scm_reverse_x ($7, SCM_EOL));
3167                         $$ = n->unprotect ();
3168                 }
3169         } %prec ':'
3170         | simple_chord_elements post_events {
3171                 if (scm_is_pair ($2)) {
3172                         if (unsmob_pitch ($1))
3173                                 $1 = make_chord_elements (@1,
3174                                                           $1,
3175                                                           parser->default_duration_.smobbed_copy (),
3176                                                           SCM_EOL);
3177
3178                         SCM elts = ly_append2 ($1, scm_reverse_x ($2, SCM_EOL));
3179
3180                         $$ = MAKE_SYNTAX ("event-chord", @1, elts);
3181                 } else if (!unsmob_pitch ($1))
3182                         $$ = MAKE_SYNTAX ("event-chord", @1, $1);
3183                 // A mere pitch drops through.
3184         } %prec ':'
3185         ;
3186
3187 simple_element:
3188         DRUM_PITCH optional_notemode_duration {
3189                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
3190                 n->set_property ("duration", $2);
3191                 n->set_property ("drum-type", $1);
3192
3193                 $$ = n->unprotect ();
3194         }
3195         | RESTNAME optional_notemode_duration           {
3196                 Music *ev = 0;
3197                 if (ly_scm2string ($1) == "s") {
3198                         /* Space */
3199                         ev = MY_MAKE_MUSIC ("SkipEvent", @$);
3200                   }
3201                 else {
3202                         ev = MY_MAKE_MUSIC ("RestEvent", @$);
3203
3204                     }
3205                 ev->set_property ("duration", $2);
3206                 $$ = ev->unprotect ();
3207         }
3208         ;
3209
3210 // Can return a single pitch rather than a list.
3211 simple_chord_elements:
3212         new_chord {
3213                 if (!parser->lexer_->is_chord_state ())
3214                         parser->parser_error (@1, _ ("have to be in Chord mode for chords"));
3215                 $$ = $1;
3216         }
3217         | figure_spec optional_notemode_duration {
3218                 for (SCM s = $1; scm_is_pair (s); s = scm_cdr (s))
3219                 {
3220                         unsmob_music (scm_car (s))->set_property ("duration", $2);
3221                 }
3222                 $$ = $1;
3223         }
3224         ;
3225
3226 lyric_element:
3227         full_markup {
3228                 if (!parser->lexer_->is_lyric_state ())
3229                         parser->parser_error (@1, _ ("markup outside of text script or \\lyricmode"));
3230                 $$ = $1;
3231         }
3232         | STRING {
3233                 if (!parser->lexer_->is_lyric_state ())
3234                         parser->parser_error (@1, _ ("unrecognized string, not in text script or \\lyricmode"));
3235                 $$ = $1;
3236         }
3237         | LYRIC_ELEMENT
3238         ;
3239
3240 lyric_element_music:
3241         lyric_element optional_notemode_duration post_events {
3242                 $$ = MAKE_SYNTAX ("lyric-event", @$, $1, $2);
3243                 if (scm_is_pair ($3))
3244                         unsmob_music ($$)->set_property
3245                                 ("articulations", scm_reverse_x ($3, SCM_EOL));
3246         } %prec ':'
3247         ;
3248
3249 // Can return a single pitch rather than a list.
3250 new_chord:
3251         steno_tonic_pitch maybe_notemode_duration   {
3252                 if (SCM_UNBNDP ($2))
3253                         $$ = $1;
3254                 else
3255                         $$ = make_chord_elements (@$, $1, $2, SCM_EOL);
3256         }
3257         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
3258                 SCM its = scm_reverse_x ($4, SCM_EOL);
3259                 $$ = make_chord_elements (@$, $1, $2, scm_cons ($3, its));
3260         } %prec ':'
3261         ;
3262
3263 chord_items:
3264         /**/ {
3265                 $$ = SCM_EOL;
3266         }
3267         | chord_items chord_item {
3268                 $$ = scm_cons ($2, $$);
3269         }
3270         ;
3271
3272 chord_separator:
3273         CHORD_COLON {
3274                 $$ = ly_symbol2scm ("chord-colon");
3275         }
3276         | CHORD_CARET {
3277                 $$ = ly_symbol2scm ("chord-caret");
3278         }
3279         | CHORD_SLASH steno_tonic_pitch {
3280                 $$ = scm_list_2 (ly_symbol2scm ("chord-slash"), $2);
3281         }
3282         | CHORD_BASS steno_tonic_pitch {
3283                 $$ = scm_list_2 (ly_symbol2scm ("chord-bass"), $2);
3284         }
3285         ;
3286
3287 chord_item:
3288         chord_separator {
3289                 $$ = $1;
3290         }
3291         | step_numbers {
3292                 $$ = scm_reverse_x ($1, SCM_EOL);
3293         }
3294         | CHORD_MODIFIER  {
3295                 $$ = $1;
3296         }
3297         ;
3298
3299 step_numbers:
3300         step_number { $$ = scm_cons ($1, SCM_EOL); }
3301         | step_numbers '.' step_number {
3302                 $$ = scm_cons ($3, $$);
3303         }
3304         ;
3305
3306 step_number:
3307         UNSIGNED {
3308                 $$ = make_chord_step ($1, 0);
3309         }
3310         | UNSIGNED '+' {
3311                 $$ = make_chord_step ($1, SHARP_ALTERATION);
3312         }
3313         | UNSIGNED CHORD_MINUS {
3314                 $$ = make_chord_step ($1, FLAT_ALTERATION);
3315         }
3316         ;
3317
3318 tempo_range:
3319         unsigned_number {
3320                 $$ = $1;
3321         } %prec ':'
3322         | unsigned_number '-' unsigned_number {
3323                 $$ = scm_cons ($1, $3);
3324         }
3325         ;
3326
3327 /*
3328         UTILITIES
3329
3330 TODO: should deprecate in favor of Scheme?
3331
3332  */
3333 number_expression:
3334         number_expression '+' number_term {
3335                 $$ = scm_sum ($1, $3);
3336         }
3337         | number_expression '-' number_term {
3338                 $$ = scm_difference ($1, $3);
3339         }
3340         | number_term
3341         ;
3342
3343 number_term:
3344         number_factor {
3345                 $$ = $1;
3346         }
3347         | number_factor '*' number_factor {
3348                 $$ = scm_product ($1, $3);
3349         }
3350         | number_factor '/' number_factor {
3351                 $$ = scm_divide ($1, $3);
3352         }
3353         ;
3354
3355 number_factor:
3356         '-'  number_factor { /* %prec UNARY_MINUS */
3357                 $$ = scm_difference ($2, SCM_UNDEFINED);
3358         }
3359         | bare_number
3360         ;
3361
3362 bare_number_common:
3363         REAL
3364         | NUMBER_IDENTIFIER
3365         | REAL NUMBER_IDENTIFIER
3366         {
3367                 $$ = scm_product ($1, $2);
3368         }
3369         ;
3370
3371 bare_number:
3372         bare_number_common
3373         | UNSIGNED
3374         | UNSIGNED NUMBER_IDENTIFIER    {
3375                 $$ = scm_product ($1, $2);
3376         }
3377         ;
3378
3379 unsigned_number:
3380         UNSIGNED
3381         | NUMBER_IDENTIFIER
3382         {
3383                 if (!scm_is_integer ($1)
3384                     || scm_is_true (scm_negative_p ($1)))
3385                 {
3386                         parser->parser_error (@1, _("not an unsigned integer"));
3387                         $$ = SCM_INUM0;
3388                 }
3389         }
3390         | embedded_scm
3391         {
3392                 if (!scm_is_integer ($1)
3393                     || scm_is_true (scm_negative_p ($1)))
3394                 {
3395                         parser->parser_error (@1, _("not an unsigned integer"));
3396                         $$ = SCM_INUM0;
3397                 }
3398         }
3399         ;
3400
3401 exclamations:
3402                 { $$ = SCM_UNDEFINED; }
3403         | exclamations '!'
3404         {
3405                 if (SCM_UNBNDP ($1))
3406                         $$ = SCM_BOOL_T;
3407                 else
3408                         $$ = scm_not ($1);
3409         }
3410         ;
3411
3412 questions:
3413 // This precedence rule is rather weird.  It triggers when '!' is
3414 // encountered after a pitch, and is used for deciding whether to save
3415 // this instead for a figure modification.  This should not actually
3416 // occur in practice as pitches and figures are generated in different
3417 // modes.  Using a greedy (%right) precedence makes sure that we don't
3418 // get stuck in a wrong interpretation.
3419         { $$ = SCM_UNDEFINED; } %prec ':'
3420         | questions '?'
3421         {
3422                 if (SCM_UNBNDP ($1))
3423                         $$ = SCM_BOOL_T;
3424                 else
3425                         $$ = scm_not ($1);
3426         }
3427         ;
3428
3429 full_markup_list:
3430         MARKUPLIST
3431                 { parser->lexer_->push_markup_state (); }
3432         markup_list {
3433                 $$ = $3;
3434                 parser->lexer_->pop_state ();
3435         }
3436         ;
3437
3438 full_markup:
3439         MARKUP
3440                 { parser->lexer_->push_markup_state (); }
3441         markup_top {
3442                 $$ = $3;
3443                 parser->lexer_->pop_state ();
3444         }
3445         ;
3446
3447 markup_top:
3448         markup_list {
3449                 $$ = scm_list_2 (ly_lily_module_constant ("line-markup"),  $1);
3450         }
3451         | markup_head_1_list simple_markup
3452         {
3453                 $$ = scm_car (MAKE_SYNTAX ("composed-markup-list",
3454                                            @2, $1, scm_list_1 ($2)));
3455         }
3456         | simple_markup {
3457                 $$ = $1;
3458         }
3459         ;
3460
3461 markup_scm:
3462         embedded_scm_bare
3463         {
3464                 if (Text_interface::is_markup ($1))
3465                         MYBACKUP (MARKUP_IDENTIFIER, $1, @1);
3466                 else if (Text_interface::is_markup_list ($1))
3467                         MYBACKUP (MARKUPLIST_IDENTIFIER, $1, @1);
3468                 else {
3469                         parser->parser_error (@1, _ ("not a markup"));
3470                         MYBACKUP (MARKUP_IDENTIFIER, scm_string (SCM_EOL), @1);
3471                 }
3472         } BACKUP
3473         ;
3474                         
3475
3476 markup_list:
3477         markup_composed_list {
3478                 $$ = $1;
3479         }
3480         | markup_uncomposed_list
3481         ;
3482
3483 markup_uncomposed_list:
3484         markup_braced_list {
3485                 $$ = $1;
3486         }
3487         | markup_command_list {
3488                 $$ = scm_list_1 ($1);
3489         }
3490         | markup_scm MARKUPLIST_IDENTIFIER
3491         {
3492                 $$ = $2;
3493         }
3494         | SCORELINES {
3495                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3496                 parser->lexer_->push_note_state (nn);
3497         } '{' score_body '}' {
3498                 unsmob_score ($4)->origin ()->set_spot (@$);
3499                 $$ = scm_list_1 (scm_list_2 (ly_lily_module_constant ("score-lines-markup-list"), $4));
3500                 parser->lexer_->pop_state ();
3501         }
3502         ;
3503
3504 markup_composed_list:
3505         markup_head_1_list markup_uncomposed_list {
3506                 $$ = MAKE_SYNTAX ("composed-markup-list",
3507                                   @2, $1, $2);
3508         }
3509         ;
3510
3511 markup_braced_list:
3512         '{' markup_braced_list_body '}' {
3513                 $$ = scm_reverse_x ($2, SCM_EOL);
3514         }
3515         ;
3516
3517 markup_braced_list_body:
3518         /* empty */     {  $$ = SCM_EOL; }
3519         | markup_braced_list_body markup {
3520                 $$ = scm_cons ($2, $1);
3521         }
3522         | markup_braced_list_body markup_list {
3523                 $$ = scm_reverse_x ($2, $1);
3524         }
3525         ;
3526
3527 markup_command_list:
3528         MARKUP_LIST_FUNCTION markup_command_list_arguments {
3529           $$ = scm_cons ($1, scm_reverse_x($2, SCM_EOL));
3530         }
3531         ;
3532
3533 markup_command_basic_arguments:
3534         EXPECT_MARKUP_LIST markup_command_list_arguments markup_list {
3535           $$ = scm_cons ($3, $2);
3536         }
3537         | EXPECT_SCM markup_command_list_arguments embedded_scm {
3538           $$ = check_scheme_arg (parser, @3, $3, $2, $1);
3539         }
3540         | EXPECT_NO_MORE_ARGS {
3541           $$ = SCM_EOL;
3542         }
3543         ;
3544
3545 markup_command_list_arguments:
3546         markup_command_basic_arguments { $$ = $1; }
3547         | EXPECT_MARKUP markup_command_list_arguments markup {
3548           $$ = scm_cons ($3, $2);
3549         }
3550         ;
3551
3552 markup_head_1_item:
3553         MARKUP_FUNCTION EXPECT_MARKUP markup_command_list_arguments {
3554           $$ = scm_cons ($1, scm_reverse_x ($3, SCM_EOL));
3555         }
3556         ;
3557
3558 markup_head_1_list:
3559         markup_head_1_item      {
3560                 $$ = scm_list_1 ($1);
3561         }
3562         | markup_head_1_list markup_head_1_item {
3563                 $$ = scm_cons ($2, $1);
3564         }
3565         ;
3566
3567 simple_markup:
3568         STRING {
3569                 $$ = make_simple_markup ($1);
3570         }
3571         | SCORE {
3572                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3573                 parser->lexer_->push_note_state (nn);
3574         } '{' score_body '}' {
3575                 unsmob_score ($4)->origin ()->set_spot (@$);
3576                 $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), $4);
3577                 parser->lexer_->pop_state ();
3578         }
3579         | MARKUP_FUNCTION markup_command_basic_arguments {
3580                 $$ = scm_cons ($1, scm_reverse_x ($2, SCM_EOL));
3581         }
3582         | markup_scm MARKUP_IDENTIFIER
3583         {
3584                 $$ = $2;
3585         }
3586         ;
3587
3588 markup:
3589         markup_head_1_list simple_markup
3590         {
3591                 $$ = scm_car (MAKE_SYNTAX ("composed-markup-list",
3592                                            @2, $1, scm_list_1 ($2)));
3593         }
3594         | simple_markup {
3595                 $$ = $1;
3596         }
3597         ;
3598
3599 %%
3600
3601 void
3602 Lily_parser::set_yydebug (bool x)
3603 {
3604         yydebug = x;
3605 }
3606
3607 SCM
3608 Lily_parser::do_yyparse ()
3609 {
3610         SCM retval = SCM_UNDEFINED;
3611         yyparse (this, &retval);
3612         return retval;
3613 }
3614
3615
3616
3617
3618
3619 /*
3620
3621 It is a little strange to have this function in this file, but
3622 otherwise, we have to import music classes into the lexer.
3623
3624 */
3625 int
3626 Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
3627 {
3628         if (unsmob_book (sid)) {
3629                 Book *book =  unsmob_book (sid)->clone ();
3630                 *destination = book->self_scm ();
3631                 book->unprotect ();
3632
3633                 return BOOK_IDENTIFIER;
3634         } else if (scm_is_number (sid)) {
3635                 *destination = sid;
3636                 return NUMBER_IDENTIFIER;
3637         } else if (unsmob_context_def (sid))
3638         {
3639                 *destination = unsmob_context_def (sid)->clone ()->unprotect ();
3640                 return SCM_IDENTIFIER;
3641         } else if (unsmob_context_mod (sid)) {
3642                 *destination = unsmob_context_mod (sid)->smobbed_copy ();
3643                 return CONTEXT_MOD_IDENTIFIER;
3644         } else if (Music *mus = unsmob_music (sid)) {
3645                 mus = mus->clone ();
3646                 *destination = mus->self_scm ();
3647                 bool is_event = mus->is_mus_type ("post-event");
3648                 mus->unprotect ();
3649                 return is_event ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
3650         } else if (unsmob_pitch (sid)) {
3651                 *destination = unsmob_pitch (sid)->smobbed_copy ();
3652                 return PITCH_IDENTIFIER;
3653         } else if (unsmob_duration (sid)) {
3654                 *destination = unsmob_duration (sid)->smobbed_copy ();
3655                 return DURATION_IDENTIFIER;
3656         } else if (unsmob_output_def (sid)) {
3657                 *destination = unsmob_output_def (sid)->clone ()->unprotect ();
3658                 return SCM_IDENTIFIER;
3659         } else if (unsmob_score (sid)) {
3660                 *destination = unsmob_score (sid)->clone ()->unprotect ();
3661                 return SCM_IDENTIFIER;
3662         }
3663
3664         return -1;
3665 }
3666
3667 SCM
3668 get_next_unique_context_id ()
3669 {
3670         return scm_from_locale_string ("$uniqueContextId");
3671 }
3672
3673
3674 SCM
3675 get_next_unique_lyrics_context_id ()
3676 {
3677         static int new_context_count;
3678         char s[128];
3679         snprintf (s, sizeof (s)-1, "uniqueContext%d", new_context_count++);
3680         return scm_from_locale_string (s);
3681 }
3682
3683 // check_scheme_arg checks one argument with a given predicate for use
3684 // in an argument list and throws a syntax error if it is unusable.
3685 // The argument is prepended to the argument list in any case.  After
3686 // throwing a syntax error, the argument list is terminated with #f as
3687 // its last cdr in order to mark it as uncallable while not losing
3688 // track of its total length.
3689 //
3690 // There are a few special considerations: if optional argument disp
3691 // is given (otherwise it defaults to SCM_UNDEFINED), it will be used
3692 // instead of arg in a prospective error message.  This is useful if
3693 // arg is not the actual argument but rather a transformation of it.
3694 //
3695 // If arg itself is SCM_UNDEFINED, the predicate is considered false
3696 // and an error message using disp is produced unconditionally.
3697
3698 SCM check_scheme_arg (Lily_parser *parser, Input loc,
3699                       SCM arg, SCM args, SCM pred, SCM disp)
3700 {
3701         if (SCM_UNBNDP (arg))
3702                 args = scm_cons (disp, args);
3703         else {
3704                 args = scm_cons (arg, args);
3705                 if (scm_is_true (scm_call_1 (pred, arg)))
3706                         return args;
3707         }
3708         scm_set_cdr_x (scm_last_pair (args), SCM_EOL);
3709         MAKE_SYNTAX ("argument-error", loc, scm_length (args), pred,
3710                      SCM_UNBNDP (disp) ? arg : disp);
3711         scm_set_cdr_x (scm_last_pair (args), SCM_BOOL_F);
3712         return args;
3713 }
3714
3715 SCM loc_on_music (Input loc, SCM arg)
3716 {
3717         if (Music *m = unsmob_music (arg))
3718         {
3719                 m = m->clone ();
3720                 m->set_spot (loc);
3721                 return m->unprotect ();
3722         }
3723         return arg;
3724 }
3725
3726 SCM
3727 try_string_variants (SCM pred, SCM str)
3728 {
3729         // a matching predicate is always ok
3730         if (scm_is_true (scm_call_1 (pred, str)))
3731                 return str;
3732         // a symbol may be interpreted as a list of symbols if it helps
3733         if (scm_is_symbol (str)) {
3734                 str = scm_list_1 (str);
3735                 if (scm_is_true (scm_call_1 (pred, str)))
3736                         return str;
3737                 return SCM_UNDEFINED;
3738         }
3739
3740         // If this cannot be a string representation of a symbol list,
3741         // we are through.
3742
3743         if (!is_regular_identifier (str, true))
3744                 return SCM_UNDEFINED;
3745
3746         str = scm_string_split (str, SCM_MAKE_CHAR ('.'));
3747         for (SCM p = str; scm_is_pair (p); p = scm_cdr (p))
3748                 scm_set_car_x (p, scm_string_to_symbol (scm_car (p)));
3749
3750         // Let's attempt the symbol list interpretation first.
3751
3752         if (scm_is_true (scm_call_1 (pred, str)))
3753                 return str;
3754
3755         // If there is just one symbol in the list, we might interpret
3756         // it as a single symbol
3757
3758         if (scm_is_null (scm_cdr (str)))
3759         {
3760                 str = scm_car (str);
3761                 if (scm_is_true (scm_call_1 (pred, str)))
3762                         return str;
3763         }
3764
3765         return SCM_UNDEFINED;
3766 }
3767
3768 bool
3769 is_regular_identifier (SCM id, bool multiple)
3770 {
3771   if (!scm_is_string (id))
3772           return false;
3773
3774   string str = ly_scm2string (id);
3775
3776   bool middle = false;
3777
3778   for (string::iterator it=str.begin(); it != str.end (); it++)
3779   {
3780           int c = *it & 0xff;
3781           if ((c >= 'a' && c <= 'z')
3782               || (c >= 'A' && c <= 'Z')
3783               || c > 0x7f)
3784                   middle = true;
3785           else if (middle && (c == '-' || c == '_' || (multiple && c == '.')))
3786                   middle = false;
3787           else
3788                   return false;
3789   }
3790   return middle;
3791 }
3792
3793 SCM
3794 make_music_from_simple (Lily_parser *parser, Input loc, SCM simple)
3795 {
3796         if (unsmob_music (simple))
3797                 return simple;
3798         if (parser->lexer_->is_note_state ()) {
3799                 if (scm_is_symbol (simple)) {
3800                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3801                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3802                         n->set_property ("drum-type", simple);
3803                         return n->unprotect ();
3804                 }
3805                 if (unsmob_pitch (simple)) {
3806                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3807                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3808                         n->set_property ("pitch", simple);
3809                         return n->unprotect ();
3810                 }
3811                 return simple;
3812         } else if (parser->lexer_->is_lyric_state ()) {
3813                 if (Text_interface::is_markup (simple))
3814                         return MAKE_SYNTAX ("lyric-event", loc, simple,
3815                                             parser->default_duration_.smobbed_copy ());
3816         } else if (parser->lexer_->is_chord_state ()) {
3817                 if (unsmob_pitch (simple))
3818                         return MAKE_SYNTAX
3819                                 ("event-chord",
3820                                  loc,
3821                                  make_chord_elements (loc, simple,
3822                                                       parser->default_duration_.smobbed_copy (),
3823                                                       SCM_EOL));
3824         }
3825         return simple;
3826 }
3827
3828 Music *
3829 make_music_with_input (SCM name, Input where)
3830 {
3831        Music *m = make_music_by_name (name);
3832        m->set_spot (where);
3833        return m;
3834 }
3835
3836 SCM
3837 make_simple_markup (SCM a)
3838 {
3839         return a;
3840 }
3841
3842 SCM
3843 make_duration (SCM d, int dots)
3844 {
3845         int t = scm_to_int (d);
3846         if (t > 0 && (t & (t-1)) == 0)
3847                 return Duration (intlog2 (t), dots).smobbed_copy ();
3848         else
3849                 return SCM_UNDEFINED;
3850 }
3851
3852 SCM
3853 make_chord_step (SCM step_scm, Rational alter)
3854 {
3855         int step = scm_to_int (step_scm);
3856
3857         if (step == 7)
3858                 alter += FLAT_ALTERATION;
3859
3860         while (step < 0)
3861                 step += 7;
3862         Pitch m ((step -1) / 7, (step - 1) % 7, alter);
3863         return m.smobbed_copy ();
3864 }
3865
3866
3867 SCM
3868 make_chord_elements (Input loc, SCM pitch, SCM dur, SCM modification_list)
3869 {
3870         SCM chord_ctor = ly_lily_module_constant ("construct-chord-elements");
3871         SCM res = scm_call_3 (chord_ctor, pitch, dur, modification_list);
3872         for (SCM s = res; scm_is_pair (s); s = scm_cdr (s))
3873         {
3874                 unsmob_music (scm_car (s))->set_spot (loc);
3875         }
3876         return res;
3877 }
3878
3879 int
3880 yylex (YYSTYPE *s, YYLTYPE *loc, Lily_parser *parser)
3881 {
3882         Lily_lexer *lex = parser->lexer_;
3883
3884         lex->lexval_ = s;
3885         lex->lexloc_ = loc;
3886         int tok = lex->pop_extra_token ();
3887         if (tok >= 0)
3888                 return tok;
3889         lex->prepare_for_next_token ();
3890         return lex->yylex ();
3891 }