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