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