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