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