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