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