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