]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
Issue 3554: Avoid empty grammar rules using @$ for getting a source location
[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         {
951                 parser->lexer_->pop_state ();
952         }
953         | CONTEXT
954         {
955                 parser->lexer_->pop_state ();
956         } '{' context_def_spec_body '}'
957         {
958                 $$ = $4;
959                 unsmob_context_def ($$)->origin ()->set_spot (@$);
960         }
961         ;
962
963 output_def_body:
964         output_def_head_with_mode_switch '{' {
965                 $$ = $1;
966                 unsmob_output_def ($$)->input_origin_.set_spot (@$);
967         }
968         | output_def_head_with_mode_switch '{' OUTPUT_DEF_IDENTIFIER    {
969                 Output_def *o = unsmob_output_def ($3);
970                 o->input_origin_.set_spot (@$);
971                 $$ = o->self_scm ();
972                 parser->lexer_->remove_scope ();
973                 parser->lexer_->add_scope (o->scope_);
974         }
975         | output_def_body assignment  {
976
977         }
978         | output_def_body embedded_scm  {
979
980         }
981         | output_def_body
982         {
983                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
984                 parser->lexer_->push_note_state (nn);
985         } music_or_context_def
986         {
987                 if (unsmob_context_def ($3))
988                         assign_context_def (unsmob_output_def ($1), $3);
989                 else {
990
991                         SCM proc = parser->lexer_->lookup_identifier
992                                      ("output-def-music-handler");
993                         scm_call_3 (proc, parser->self_scm (),
994                                     $1, $3);
995                 }
996         }
997         | output_def_body error {
998
999         }
1000         ;
1001
1002 tempo_event:
1003         TEMPO steno_duration '=' tempo_range    {
1004                 $$ = MAKE_SYNTAX ("tempo", @$, SCM_EOL, $2, $4);
1005         }
1006         | TEMPO scalar_closed steno_duration '=' tempo_range    {
1007                 $$ = MAKE_SYNTAX ("tempo", @$, $2, $3, $5);
1008         }
1009         | TEMPO scalar {
1010                 $$ = MAKE_SYNTAX ("tempo", @$, $2);
1011         }
1012         ;
1013
1014 /*
1015 The representation of a  list is reversed to have efficient append.  */
1016
1017 music_list:
1018         /* empty */ {
1019                 $$ = SCM_EOL;
1020         }
1021         | music_list music_embedded {
1022                 if (unsmob_music ($2))
1023                         $$ = scm_cons ($2, $1);
1024         }
1025         | music_list error {
1026                 Music *m = MY_MAKE_MUSIC("Music", @$);
1027                 // ugh. code dup
1028                 m->set_property ("error-found", SCM_BOOL_T);
1029                 $$ = scm_cons (m->self_scm (), $1);
1030                 m->unprotect (); /* UGH */
1031         }
1032         ;
1033
1034 braced_music_list:
1035         '{' music_list '}'
1036         {
1037                 $$ = scm_reverse_x ($2, SCM_EOL);
1038         }
1039         ;
1040
1041 music:  music_arg
1042         | lyric_element_music
1043         ;
1044
1045 music_embedded:
1046         music
1047         {
1048                 if (unsmob_music ($1)->is_mus_type ("post-event")) {
1049                         parser->parser_error (@1, _ ("unexpected post-event"));
1050                         $$ = SCM_UNSPECIFIED;
1051                 }
1052         }
1053         | music_embedded_backup
1054         {
1055                 $$ = $1;
1056         }
1057         | music_embedded_backup BACKUP lyric_element_music
1058         {
1059                 $$ = $3;
1060         }
1061         ;
1062
1063 music_embedded_backup:
1064         embedded_scm
1065         {
1066                 if (scm_is_eq ($1, SCM_UNSPECIFIED))
1067                         $$ = $1;
1068                 else if (Music *m = unsmob_music ($1)) {
1069                         if (m->is_mus_type ("post-event")) {
1070                                 parser->parser_error
1071                                         (@1, _ ("unexpected post-event"));
1072                                 $$ = SCM_UNSPECIFIED;
1073                         } else
1074                                 $$ = $1;
1075                 } else if (parser->lexer_->is_lyric_state ()
1076                            && Text_interface::is_markup ($1))
1077                         MYBACKUP (LYRIC_ELEMENT, $1, @1);
1078                 else {
1079                         @$.warning (_ ("Ignoring non-music expression"));
1080                         $$ = $1;
1081                 }
1082         }
1083         ;
1084
1085 music_arg:
1086         simple_music
1087         {
1088                 $$ = make_music_from_simple (parser, @1, $1);
1089                 if (!unsmob_music ($$))
1090                         parser->parser_error (@1, _ ("music expected"));
1091         }
1092         | composite_music %prec COMPOSITE
1093         ;
1094
1095 music_assign:
1096         simple_music
1097         | composite_music %prec COMPOSITE
1098         ;
1099
1100 repeated_music:
1101         REPEAT simple_string unsigned_number music
1102         {
1103                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, SCM_EOL);
1104         }
1105         | REPEAT simple_string unsigned_number music ALTERNATIVE braced_music_list
1106         {
1107                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, $6);
1108         }
1109         ;
1110
1111 sequential_music:
1112         SEQUENTIAL braced_music_list {
1113                 $$ = MAKE_SYNTAX ("sequential-music", @$, $2);
1114         }
1115         | braced_music_list {
1116                 $$ = MAKE_SYNTAX ("sequential-music", @$, $1);
1117         }
1118         ;
1119
1120 simultaneous_music:
1121         SIMULTANEOUS braced_music_list {
1122                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, $2);
1123         }
1124         | DOUBLE_ANGLE_OPEN music_list DOUBLE_ANGLE_CLOSE       {
1125                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, scm_reverse_x ($2, SCM_EOL));
1126         }
1127         ;
1128
1129 simple_music:
1130         event_chord
1131         | music_property_def
1132         | context_change
1133         ;
1134
1135 context_modification:
1136         WITH { parser->lexer_->push_initial_state (); } '{' context_mod_list '}'
1137         {
1138                 parser->lexer_->pop_state ();
1139                 $$ = $4;
1140         }
1141         | WITH CONTEXT_MOD_IDENTIFIER
1142         {
1143                 $$ = $2;
1144         }
1145         | CONTEXT_MOD_IDENTIFIER
1146         {
1147                 $$ = $1;
1148         }
1149         | WITH embedded_scm_closed
1150         {
1151                 if (unsmob_context_mod ($2))
1152                         $$ = $2;
1153                 else {
1154                         parser->parser_error (@2, _ ("not a context mod"));
1155                         $$ = Context_mod ().smobbed_copy ();
1156                 }
1157         }
1158         ;
1159
1160 optional_context_mod:
1161         /**/ {
1162             $$ = SCM_EOL;
1163         }
1164         | context_modification
1165         {
1166               $$ = $1;
1167         }
1168         ;
1169
1170 context_mod_list:
1171         /**/ {
1172             $$ = Context_mod ().smobbed_copy ();
1173         }
1174         | context_mod_list context_mod  {
1175                 if (!SCM_UNBNDP ($2))
1176                         unsmob_context_mod ($1)->add_context_mod ($2);
1177         }
1178         | context_mod_list CONTEXT_MOD_IDENTIFIER {
1179                  Context_mod *md = unsmob_context_mod ($2);
1180                  if (md)
1181                      unsmob_context_mod ($1)->add_context_mods (md->get_mods ());
1182         }
1183         | context_mod_list context_mod_embedded {
1184                 unsmob_context_mod ($1)->add_context_mods
1185                         (unsmob_context_mod ($2)->get_mods ());
1186         }
1187         ;
1188
1189 composite_music:
1190         complex_music
1191         | music_bare
1192         ;
1193
1194 /* Music that can be parsed without lookahead */
1195 closed_music:
1196         music_bare
1197         | complex_music_prefix closed_music
1198         {
1199                 $$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
1200         }
1201         | music_function_call_closed
1202         ;
1203
1204 music_bare:
1205         mode_changed_music
1206         | MUSIC_IDENTIFIER
1207         | grouped_music_list
1208         ;
1209
1210 grouped_music_list:
1211         simultaneous_music              { $$ = $1; }
1212         | sequential_music              { $$ = $1; }
1213         ;
1214
1215 /* An argument list. If a function \foo expects scm scm pitch, then the lexer expands \foo into the token sequence:
1216  MUSIC_FUNCTION EXPECT_PITCH EXPECT_SCM EXPECT_SCM EXPECT_NO_MORE_ARGS
1217 and this rule returns the reversed list of arguments. */
1218
1219 /* Function argument lists come in a number of flavors.  Whenever
1220  * LilyPond has to pick between different flavors, the decision is
1221  * either made because of tokens it has already seen, or it is
1222  * postponed until tokens suitable for making the decision come up.
1223  * For postponing decisions, it may be necessary that the competing
1224  * rules are written in a way making them compatible until a decision
1225  * can be made.  Sometimes this is done by putting common traits into
1226  * a separate "common" rule set.
1227  *
1228  * function_arglist: a full argument list.  Optional arguments at the
1229  * end of the list can only be skipped by writing \default in their
1230  * place.
1231  *
1232  * function_arglist_backup: an argument list ending in an optional
1233  * argument that may be skipped depending on its predicate.
1234  *
1235  * function_arglist_skip: an argument list _not_ ending in an optional
1236  * argument that is actually taken.
1237  *
1238  * function_arglist_nonbackup: an argument list ending in an optional
1239  * argument that may not be skipped because it is in end position and
1240  * has not been shortcircuited with \default.
1241  *
1242  * function_arglist* / function_arglist_closed*: The closed variants
1243  * don't end in simple music expressions that might still accept
1244  * things like a duration or a postevent.
1245  */
1246
1247 function_arglist_skip:
1248         function_arglist_common
1249         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_skip
1250         {
1251                 $$ = scm_cons ($1, $3);
1252         } %prec FUNCTION_ARGLIST
1253         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_skip
1254         {
1255                 $$ = scm_cons ($1, $3);
1256         } %prec FUNCTION_ARGLIST
1257         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip
1258         {
1259                 $$ = scm_cons ($1, $3);
1260         } %prec FUNCTION_ARGLIST
1261         ;
1262
1263
1264 function_arglist_nonbackup_common:
1265         EXPECT_OPTIONAL EXPECT_PITCH function_arglist pitch_also_in_chords {
1266                 $$ = scm_cons ($4, $3);
1267         }
1268         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_closed duration_length {
1269                 $$ = scm_cons ($4, $3);
1270         }
1271         | EXPECT_OPTIONAL EXPECT_SCM function_arglist FRACTION
1272         {
1273                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1274         }
1275         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed post_event_nofinger
1276         {
1277                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1278         }
1279         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed '-' UNSIGNED
1280         {
1281                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1282                 if (scm_is_true (scm_call_1 ($2, n)))
1283                         $$ = scm_cons (n, $3);
1284                 else {
1285                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1286                         t->set_property ("digit", $5);
1287                         $$ = check_scheme_arg (parser, @4, t->unprotect (),
1288                                                $3, $2, n);
1289                 }
1290                 
1291         }
1292         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed '-' REAL
1293         {
1294                 $$ = check_scheme_arg (parser, @4,
1295                                        scm_difference ($5, SCM_UNDEFINED),
1296                                        $3, $2);
1297         }
1298         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed '-' NUMBER_IDENTIFIER
1299         {
1300                 $$ = check_scheme_arg (parser, @4,
1301                                        scm_difference ($5, SCM_UNDEFINED),
1302                                        $3, $2);
1303         }
1304         ;
1305
1306 function_arglist_closed_nonbackup:
1307         function_arglist_nonbackup_common
1308         | EXPECT_OPTIONAL EXPECT_SCM function_arglist embedded_scm_arg_closed
1309         {
1310                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1311         }
1312         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed bare_number_closed
1313         {
1314                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1315         }
1316         | EXPECT_OPTIONAL EXPECT_SCM function_arglist SCM_IDENTIFIER
1317         {
1318                 $$ = check_scheme_arg (parser, @4,
1319                                        try_string_variants ($2, $4),
1320                                        $3, $2, $4);
1321         }
1322         | EXPECT_OPTIONAL EXPECT_SCM function_arglist STRING
1323         {
1324                 $$ = check_scheme_arg (parser, @4,
1325                                        try_string_variants ($2, $4),
1326                                        $3, $2, $4);
1327         }
1328         | EXPECT_OPTIONAL EXPECT_SCM function_arglist full_markup
1329         {
1330                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1331         }
1332         ;
1333
1334 symbol_list_arg:
1335         SYMBOL_LIST
1336         | SYMBOL_LIST '.' symbol_list_rev
1337         {
1338                 $$ = scm_append (scm_list_2 ($1, scm_reverse_x ($3, SCM_EOL)));
1339         }
1340         ;
1341
1342 symbol_list_rev:
1343         symbol_list_part
1344         | symbol_list_rev '.' symbol_list_part
1345         {
1346                 $$ = scm_append_x (scm_list_2 ($3, $1));
1347         }
1348         ;
1349
1350 // symbol_list_part delivers elements in reverse copy.
1351
1352 symbol_list_part:
1353         symbol_list_element
1354         {
1355                 SCM sym_l_p = ly_lily_module_constant ("symbol-list?");
1356                 $$ = try_string_variants (sym_l_p, $1);
1357                 if (SCM_UNBNDP ($$)) {
1358                         parser->parser_error (@1, _("not a symbol"));
1359                         $$ = SCM_EOL;
1360                 } else
1361                         $$ = scm_reverse ($$);
1362         }
1363         ;
1364
1365
1366 symbol_list_element:
1367         STRING
1368         | embedded_scm_bare
1369         ;
1370
1371
1372 function_arglist_nonbackup:
1373         function_arglist_nonbackup_common
1374         | EXPECT_OPTIONAL EXPECT_SCM function_arglist embedded_scm_arg
1375         {
1376                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1377         }
1378         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed bare_number
1379         {
1380                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1381         }
1382         | function_arglist_nonbackup_reparse REPARSE SCM_ARG
1383         {
1384                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1385         }
1386         | function_arglist_nonbackup_reparse REPARSE lyric_element_music
1387         {
1388                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1389         }
1390         | function_arglist_nonbackup_reparse REPARSE symbol_list_arg
1391         {
1392                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1393         }
1394         ;
1395
1396 function_arglist_nonbackup_reparse:
1397         EXPECT_OPTIONAL EXPECT_SCM function_arglist SCM_IDENTIFIER
1398         {
1399                 $$ = $3;
1400                 SCM res = try_string_variants ($2, $4);
1401                 if (!SCM_UNBNDP (res))
1402                         if (scm_is_pair (res))
1403                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1404                         else
1405                                 MYREPARSE (@4, $2, SCM_ARG, res);
1406                 else if (scm_is_true
1407                          (scm_call_1
1408                           ($2, make_music_from_simple
1409                            (parser, @4, $4))))
1410                         MYREPARSE (@4, $2, STRING, $4);
1411                 else
1412                         MYREPARSE (@4, $2, SCM_ARG, $4);
1413         }
1414         | EXPECT_OPTIONAL EXPECT_SCM function_arglist STRING
1415         {
1416                 $$ = $3;
1417                 SCM res = try_string_variants ($2, $4);
1418                 if (!SCM_UNBNDP (res))
1419                         if (scm_is_pair (res))
1420                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1421                         else
1422                                 MYREPARSE (@4, $2, SCM_ARG, res);
1423                 else if (scm_is_true
1424                          (scm_call_1
1425                           ($2, make_music_from_simple
1426                            (parser, @4, $4))))
1427                         MYREPARSE (@4, $2, STRING, $4);
1428                 else
1429                         MYREPARSE (@4, $2, SCM_ARG, $4);
1430         }
1431         | EXPECT_OPTIONAL EXPECT_SCM function_arglist full_markup
1432         {
1433                 $$ = $3;
1434                 if (scm_is_true (scm_call_1 ($2, $4)))
1435                         MYREPARSE (@4, $2, SCM_ARG, $4);
1436                 else if (scm_is_true
1437                          (scm_call_1
1438                           ($2, make_music_from_simple
1439                            (parser, @4, $4))))
1440                         MYREPARSE (@4, $2, STRING, $4);
1441                 else
1442                         MYREPARSE (@4, $2, SCM_ARG, $4);
1443         }
1444         ;
1445
1446 function_arglist_keep:
1447         function_arglist_common
1448         | function_arglist_backup
1449         ;
1450
1451 function_arglist_closed_keep:
1452         function_arglist_closed_common
1453         | function_arglist_backup
1454         ;
1455
1456 function_arglist_backup:
1457         EXPECT_OPTIONAL EXPECT_SCM function_arglist_keep embedded_scm_arg_closed
1458         {
1459                 if (scm_is_true (scm_call_1 ($2, $4)))
1460                 {
1461                         $$ = scm_cons ($4, $3);
1462                 } else {
1463                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1464                         MYBACKUP (SCM_ARG, $4, @4);
1465                 }
1466         }
1467         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep post_event_nofinger
1468         {
1469                 if (scm_is_true (scm_call_1 ($2, $4)))
1470                 {
1471                         $$ = scm_cons ($4, $3);
1472                 } else {
1473                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1474                         MYBACKUP (EVENT_IDENTIFIER, $4, @4);
1475                 }
1476         }
1477         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_keep full_markup
1478         {
1479                 if (scm_is_true (scm_call_1 ($2, $4)))
1480                         $$ = scm_cons ($4, $3);
1481                 else {
1482                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1483                         MYBACKUP (LYRIC_ELEMENT, $4, @4);
1484                 }
1485         }
1486         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep UNSIGNED
1487         {
1488                 if (scm_is_true (scm_call_1 ($2, $4)))
1489                 {
1490                         $$ = $3;
1491                         MYREPARSE (@4, $2, UNSIGNED, $4);
1492                 } else {
1493                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1494                         MYBACKUP (UNSIGNED, $4, @4);
1495                 }
1496         }
1497         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep REAL
1498         {
1499                 if (scm_is_true (scm_call_1 ($2, $4)))
1500                 {
1501                         $$ = $3;
1502                         MYREPARSE (@4, $2, REAL, $4);
1503                 } else {
1504                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1505                         MYBACKUP (REAL, $4, @4);
1506                 }
1507         }
1508         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep NUMBER_IDENTIFIER
1509         {
1510                 if (scm_is_true (scm_call_1 ($2, $4)))
1511                 {
1512                         $$ = scm_cons ($4, $3);
1513                 } else {
1514                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1515                         MYBACKUP (NUMBER_IDENTIFIER, $4, @4);
1516                 }
1517         }
1518         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_keep FRACTION
1519         {
1520                 if (scm_is_true (scm_call_1 ($2, $4)))
1521                 {
1522                         $$ = scm_cons ($4, $3);
1523                 } else {
1524                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1525                         MYBACKUP (FRACTION, $4, @4);
1526                 }
1527         }
1528         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep '-' UNSIGNED
1529         {
1530                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1531                 if (scm_is_true (scm_call_1 ($2, n))) {
1532                         $$ = $3;
1533                         MYREPARSE (@5, $2, REAL, n);
1534                 } else {
1535                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1536                         t->set_property ("digit", $5);
1537                         $$ = t->unprotect ();
1538                         if (scm_is_true (scm_call_1 ($2, $$)))
1539                                 $$ = scm_cons ($$, $3);
1540                         else {
1541                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1542                                 MYBACKUP (UNSIGNED, $5, @5);
1543                                 parser->lexer_->push_extra_token ('-');
1544                         }
1545                 }
1546                 
1547         }
1548         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep '-' REAL
1549         {
1550                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1551                 if (scm_is_true (scm_call_1 ($2, n))) {
1552                         MYREPARSE (@5, $2, REAL, n);
1553                         $$ = $3;
1554                 } else {
1555                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1556                         MYBACKUP (REAL, n, @5);
1557                 }
1558         }
1559         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep '-' NUMBER_IDENTIFIER
1560         {
1561                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1562                 if (scm_is_true (scm_call_1 ($2, n))) {
1563                         $$ = scm_cons (n, $3);
1564                 } else {
1565                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1566                         MYBACKUP (NUMBER_IDENTIFIER, n, @5);
1567                 }
1568         }
1569         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_keep pitch_also_in_chords
1570         {
1571                 $$ = scm_cons ($4, $3);
1572         }
1573         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_closed_keep duration_length
1574         {
1575                 $$ = scm_cons ($4, $3);
1576         }
1577         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_keep SCM_IDENTIFIER
1578         {
1579                 SCM res = try_string_variants ($2, $4);
1580                 if (!SCM_UNBNDP (res))
1581                         if (scm_is_pair (res)) {
1582                                 $$ = $3;
1583                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1584                         }
1585                         else
1586                                 $$ = scm_cons (res, $3);
1587                 else {
1588                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1589                         MYBACKUP (SCM_IDENTIFIER, $4, @4);
1590                 }
1591         }
1592         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_keep STRING
1593         {
1594                 SCM res = try_string_variants ($2, $4);
1595                 if (!SCM_UNBNDP (res))
1596                         if (scm_is_pair (res)) {
1597                                 $$ = $3;
1598                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1599                         }
1600                         else
1601                                 $$ = scm_cons (res, $3);
1602                 else {
1603                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1604                         MYBACKUP (STRING, $4, @4);
1605                 }
1606         }
1607         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup BACKUP
1608         {
1609                 $$ = scm_cons ($1, $3);
1610                 MYBACKUP(0, SCM_UNDEFINED, @3);
1611         }
1612         | function_arglist_backup REPARSE bare_number
1613         {
1614                 $$ = check_scheme_arg (parser, @3,
1615                                        $3, $1, $2);
1616         }
1617         | function_arglist_backup REPARSE symbol_list_arg
1618         {
1619                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1620         }
1621         ;
1622
1623 function_arglist:
1624         function_arglist_common
1625         | function_arglist_nonbackup
1626         ;
1627
1628 function_arglist_common:
1629         function_arglist_bare
1630         | EXPECT_SCM function_arglist_optional embedded_scm_arg
1631         {
1632                 $$ = check_scheme_arg (parser, @3,
1633                                        $3, $2, $1);
1634         }
1635         | EXPECT_SCM function_arglist_closed_optional bare_number
1636         {
1637                 $$ = check_scheme_arg (parser, @3,
1638                                        $3, $2, $1);
1639         }
1640         | EXPECT_SCM function_arglist_optional FRACTION
1641         {
1642                 $$ = check_scheme_arg (parser, @3,
1643                                        $3, $2, $1);
1644         }
1645         | EXPECT_SCM function_arglist_closed_optional post_event_nofinger
1646         {
1647                 $$ = check_scheme_arg (parser, @3,
1648                                        $3, $2, $1);
1649         }
1650         | EXPECT_SCM function_arglist_closed_optional '-' NUMBER_IDENTIFIER
1651         {
1652                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1653                 $$ = check_scheme_arg (parser, @4, n, $2, $1);
1654         }
1655         | function_arglist_common_reparse REPARSE SCM_ARG
1656         {
1657                 $$ = check_scheme_arg (parser, @3,
1658                                        $3, $1, $2);
1659         }
1660         | function_arglist_common_reparse REPARSE lyric_element_music
1661         {
1662                 $$ = check_scheme_arg (parser, @3,
1663                                        $3, $1, $2);
1664         }
1665         | function_arglist_common_reparse REPARSE bare_number
1666         {
1667                 $$ = check_scheme_arg (parser, @3,
1668                                        $3, $1, $2);
1669         }
1670         | function_arglist_common_reparse REPARSE symbol_list_arg
1671         {
1672                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1673         }
1674         ;
1675
1676 function_arglist_common_reparse:
1677         EXPECT_SCM function_arglist_optional SCM_IDENTIFIER
1678         {
1679                 $$ = $2;
1680                 SCM res = try_string_variants ($1, $3);
1681                 if (!SCM_UNBNDP (res))
1682                         if (scm_is_pair (res))
1683                                 MYREPARSE (@3, $1, SYMBOL_LIST, res);
1684                         else
1685                                 MYREPARSE (@3, $1, SCM_ARG, res);
1686                 else if (scm_is_true
1687                          (scm_call_1
1688                           ($1, make_music_from_simple (parser, @3, $3))))
1689                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1690                 else
1691                         // This is going to flag a syntax error, we
1692                         // know the predicate to be false.
1693                         MYREPARSE (@3, $1, SCM_ARG, $3);
1694         }
1695         | EXPECT_SCM function_arglist_optional STRING
1696         {
1697                 $$ = $2;
1698                 SCM res = try_string_variants ($1, $3);
1699                 if (!SCM_UNBNDP (res))
1700                         if (scm_is_pair (res))
1701                                 MYREPARSE (@3, $1, SYMBOL_LIST, res);
1702                         else
1703                                 MYREPARSE (@3, $1, SCM_ARG, res);
1704                 else if (scm_is_true
1705                          (scm_call_1
1706                           ($1, make_music_from_simple (parser, @3, $3))))
1707                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1708                 else
1709                         // This is going to flag a syntax error, we
1710                         // know the predicate to be false.
1711                         MYREPARSE (@3, $1, SCM_ARG, $3);
1712         }
1713         | EXPECT_SCM function_arglist_optional full_markup
1714         {
1715                 $$ = $2;
1716                 if (scm_is_true (scm_call_1 ($1, $3)))
1717                         MYREPARSE (@3, $1, SCM_ARG, $3);
1718                 else if (scm_is_true
1719                          (scm_call_1
1720                           ($1, make_music_from_simple (parser, @3, $3))))
1721                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1722                 else
1723                         // This is going to flag a syntax error, we
1724                         // know the predicate to be false.
1725                         MYREPARSE (@3, $1, SCM_ARG, $3);
1726         }
1727         | EXPECT_SCM function_arglist_closed_optional '-' UNSIGNED
1728         {
1729                 $$ = $2;
1730                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1731                 if (scm_is_true (scm_call_1 ($1, n)))
1732                         MYREPARSE (@4, $1, REAL, n);
1733                 else {
1734                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @4);
1735                         t->set_property ("digit", $4);
1736                         SCM m = t->unprotect ();
1737                         if (scm_is_true (scm_call_1 ($1, m)))
1738                                 MYREPARSE (@4, $1, SCM_ARG, m);
1739                         else
1740                                 MYREPARSE (@4, $1, SCM_ARG, $4);
1741                 }
1742                 
1743         }
1744         | EXPECT_SCM function_arglist_closed_optional '-' REAL
1745         {
1746                 $$ = $2;
1747                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1748                 MYREPARSE (@4, $1, REAL, n);
1749         }
1750         ;
1751
1752 function_arglist_closed:
1753         function_arglist_closed_common
1754         | function_arglist_closed_nonbackup
1755         ;
1756
1757 function_arglist_closed_common:
1758         function_arglist_bare
1759         | EXPECT_SCM function_arglist_optional embedded_scm_arg_closed
1760         {
1761                 $$ = check_scheme_arg (parser, @3,
1762                                        $3, $2, $1);
1763         }
1764         | EXPECT_SCM function_arglist_closed_optional bare_number
1765         {
1766                 $$ = check_scheme_arg (parser, @3,
1767                                        $3, $2, $1);
1768         }
1769         | EXPECT_SCM function_arglist_closed_optional '-' NUMBER_IDENTIFIER
1770         {
1771                 $$ = check_scheme_arg (parser, @3,
1772                                        scm_difference ($4, SCM_UNDEFINED),
1773                                        $2, $1);
1774         }
1775         | EXPECT_SCM function_arglist_closed_optional post_event_nofinger
1776         {
1777                 $$ = check_scheme_arg (parser, @3,
1778                                        $3, $2, $1);
1779         }
1780         | EXPECT_SCM function_arglist_optional FRACTION
1781         {
1782                 $$ = check_scheme_arg (parser, @3,
1783                                        $3, $2, $1);
1784         }
1785         | function_arglist_common_reparse REPARSE SCM_ARG
1786         {
1787                 $$ = check_scheme_arg (parser, @3,
1788                                        $3, $1, $2);
1789         }
1790         | function_arglist_common_reparse REPARSE bare_number
1791         {
1792                 $$ = check_scheme_arg (parser, @3,
1793                                        $3, $1, $2);
1794         }
1795         | function_arglist_common_reparse REPARSE symbol_list_arg
1796         {
1797                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1798         }
1799         ;
1800
1801 function_arglist_optional:
1802         function_arglist_keep %prec FUNCTION_ARGLIST
1803         | function_arglist_backup BACKUP
1804         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_optional
1805         {
1806                 $$ = scm_cons ($1, $3);
1807         }
1808         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_optional
1809         {
1810                 $$ = scm_cons ($1, $3);
1811         }
1812         ;
1813
1814 function_arglist_closed_optional:
1815         function_arglist_closed_keep %prec FUNCTION_ARGLIST
1816         | function_arglist_backup BACKUP
1817         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_closed_optional
1818         {
1819                 $$ = scm_cons ($1, $3);
1820         }
1821         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_closed_optional
1822         {
1823                 $$ = scm_cons ($1, $3);
1824         }
1825         ;
1826
1827 embedded_scm_closed:
1828         embedded_scm_bare
1829         | scm_function_call_closed
1830         ;
1831
1832 embedded_scm_arg_closed:
1833         embedded_scm_bare_arg
1834         | scm_function_call_closed
1835         | closed_music
1836         ;
1837
1838 scm_function_call_closed:
1839         SCM_FUNCTION function_arglist_closed {
1840                 $$ = MAKE_SYNTAX ("music-function", @$,
1841                                          $1, $2);
1842         } %prec FUNCTION_ARGLIST
1843         ;
1844
1845 function_arglist_bare:
1846         EXPECT_NO_MORE_ARGS {
1847                 $$ = SCM_EOL;
1848         }
1849         | EXPECT_PITCH function_arglist_optional pitch_also_in_chords {
1850                 $$ = scm_cons ($3, $2);
1851         }
1852         | EXPECT_DURATION function_arglist_closed_optional duration_length {
1853                 $$ = scm_cons ($3, $2);
1854         }
1855         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_skip DEFAULT {
1856                 $$ = scm_cons ($1, $3);
1857         }
1858         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_skip DEFAULT {
1859                 $$ = scm_cons ($1, $3);
1860         }
1861         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip DEFAULT {
1862                 $$ = scm_cons ($1, $3);
1863         }
1864         ;
1865
1866 music_function_call:
1867         MUSIC_FUNCTION function_arglist {
1868                 $$ = MAKE_SYNTAX ("music-function", @$,
1869                                          $1, $2);
1870         }
1871         ;
1872
1873
1874 optional_id:
1875         /**/ { $$ = SCM_EOL; }
1876         | '=' simple_string {
1877                 $$ = $2;
1878         }
1879         ;
1880
1881 complex_music:
1882         music_function_call
1883         | repeated_music                { $$ = $1; }
1884         | re_rhythmed_music     { $$ = $1; }
1885         | complex_music_prefix music
1886         {
1887                 $$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
1888         }
1889         ;
1890
1891 complex_music_prefix:
1892         CONTEXT symbol optional_id optional_context_mod {
1893                 Context_mod *ctxmod = unsmob_context_mod ($4);
1894                 SCM mods = SCM_EOL;
1895                 if (ctxmod)
1896                         mods = ctxmod->get_mods ();
1897                 $$ = START_MAKE_SYNTAX ("context-specification", $2, $3, mods, SCM_BOOL_F);
1898         }
1899         | NEWCONTEXT symbol optional_id optional_context_mod {
1900                 Context_mod *ctxmod = unsmob_context_mod ($4);
1901                 SCM mods = SCM_EOL;
1902                 if (ctxmod)
1903                         mods = ctxmod->get_mods ();
1904                 $$ = START_MAKE_SYNTAX ("context-specification", $2, $3, mods, SCM_BOOL_T);
1905         }
1906         ;
1907
1908 mode_changed_music:
1909         mode_changing_head grouped_music_list {
1910                 if ($1 == ly_symbol2scm ("chords"))
1911                 {
1912                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $2);
1913                 }
1914                 else
1915                 {
1916                   $$ = $2;
1917                 }
1918                 parser->lexer_->pop_state ();
1919         }
1920         | mode_changing_head_with_context optional_context_mod grouped_music_list {
1921                 Context_mod *ctxmod = unsmob_context_mod ($2);
1922                 SCM mods = SCM_EOL;
1923                 if (ctxmod)
1924                         mods = ctxmod->get_mods ();
1925                 $$ = MAKE_SYNTAX ("context-specification", @$, $1, SCM_EOL, mods, SCM_BOOL_T, $3);
1926                 if ($1 == ly_symbol2scm ("ChordNames"))
1927                 {
1928                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $$);
1929                 }
1930                 parser->lexer_->pop_state ();
1931         }
1932         ;
1933
1934 mode_changing_head:
1935         NOTEMODE {
1936                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
1937                 parser->lexer_->push_note_state (nn);
1938
1939                 $$ = ly_symbol2scm ("notes");
1940         }
1941         | DRUMMODE
1942                 {
1943                 SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
1944                 parser->lexer_->push_note_state (nn);
1945
1946                 $$ = ly_symbol2scm ("drums");
1947         }
1948         | FIGUREMODE {
1949                 parser->lexer_->push_figuredbass_state ();
1950
1951                 $$ = ly_symbol2scm ("figures");
1952         }
1953         | CHORDMODE {
1954                 SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
1955                 parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
1956                 nn = parser->lexer_->lookup_identifier ("pitchnames");
1957                 parser->lexer_->push_chord_state (nn);
1958                 $$ = ly_symbol2scm ("chords");
1959
1960         }
1961         | LYRICMODE
1962                 { parser->lexer_->push_lyric_state ();
1963                 $$ = ly_symbol2scm ("lyrics");
1964         }
1965         ;
1966
1967 mode_changing_head_with_context:
1968         DRUMS {
1969                 SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
1970                 parser->lexer_->push_note_state (nn);
1971
1972                 $$ = ly_symbol2scm ("DrumStaff");
1973         }
1974         | FIGURES {
1975                 parser->lexer_->push_figuredbass_state ();
1976
1977                 $$ = ly_symbol2scm ("FiguredBass");
1978         }
1979         | CHORDS {
1980                 SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
1981                 parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
1982                 nn = parser->lexer_->lookup_identifier ("pitchnames");
1983                 parser->lexer_->push_chord_state (nn);
1984                 $$ = ly_symbol2scm ("ChordNames");
1985         }
1986         | LYRICS
1987                 { parser->lexer_->push_lyric_state ();
1988                 $$ = ly_symbol2scm ("Lyrics");
1989         }
1990         ;
1991
1992 new_lyrics:
1993         ADDLYRICS { parser->lexer_->push_lyric_state (); }
1994         /*cont */
1995         composite_music {
1996         /* Can also use music at the expensive of two S/Rs similar to
1997            \repeat \alternative */
1998                 parser->lexer_->pop_state ();
1999
2000                 $$ = scm_cons ($3, SCM_EOL);
2001         }
2002         | new_lyrics ADDLYRICS {
2003                 parser->lexer_->push_lyric_state ();
2004         } composite_music {
2005                 parser->lexer_->pop_state ();
2006                 $$ = scm_cons ($4, $1);
2007         }
2008         ;
2009
2010 re_rhythmed_music:
2011         composite_music new_lyrics {
2012                 $$ = MAKE_SYNTAX ("add-lyrics", @$, $1, scm_reverse_x ($2, SCM_EOL));
2013         } %prec COMPOSITE
2014         | LYRICSTO simple_string {
2015                 parser->lexer_->push_lyric_state ();
2016         } music {
2017                 parser->lexer_->pop_state ();
2018                 $$ = MAKE_SYNTAX ("lyric-combine", @$, $2, $4);
2019         }
2020         ;
2021
2022 context_change:
2023         CHANGE STRING '=' STRING  {
2024                 $$ = MAKE_SYNTAX ("context-change", @$, scm_string_to_symbol ($2), $4);
2025         }
2026         ;
2027
2028
2029 property_path:
2030         symbol_list_rev  {
2031                 $$ = scm_reverse_x ($1, SCM_EOL);
2032         }
2033         | symbol_list_rev property_path {
2034                 $$ = scm_reverse_x ($1, $2);
2035         }
2036         ;
2037
2038 property_operation:
2039         symbol '=' scalar {
2040                 $$ = scm_list_3 (ly_symbol2scm ("assign"), $1, $3);
2041         }
2042         | UNSET symbol {
2043                 $$ = scm_list_2 (ly_symbol2scm ("unset"), $2);
2044         }
2045         | OVERRIDE property_path '=' scalar {
2046                 if (scm_ilength ($2) < 2) {
2047                         parser->parser_error (@2, _("bad grob property path"));
2048                         $$ = SCM_UNDEFINED;
2049                 } else {
2050                         $$ = scm_cons (ly_symbol2scm ("push"),
2051                                        scm_cons2 (scm_car ($2),
2052                                                   $4,
2053                                                   scm_cdr ($2)));
2054                 }
2055         }
2056         | REVERT revert_arg {
2057                 $$ = scm_cons (ly_symbol2scm ("pop"), $2);
2058         }
2059         ;
2060
2061 // This is all quite awkward for the sake of substantial backward
2062 // compatibility while at the same time allowing a more "natural" form
2063 // of specification not separating grob specification from grob
2064 // property path.  The purpose of this definition of revert_arg is to
2065 // allow the symbol list which specifies grob and property to revert
2066 // to be optionally be split into two parts after the grob (which in
2067 // this case is just the first element of the list).  symbol_list_part
2068 // is only one path component, but it can be parsed without lookahead,
2069 // so we can follow it with a synthetic BACKUP token when needed.  If
2070 // the first symbol_list_part already contains multiple elements (only
2071 // possible if a Scheme expression provides them), we just parse for
2072 // additional elements introduced by '.', which is what the
2073 // SYMBOL_LIST backup in connection with the immediately following
2074 // rule using symbol_list_arg does.
2075 //
2076 // As long as we don't have our coffers filled with both grob and at
2077 // least one grob property specification, the rest of the required
2078 // symbol list chain may be provided either with or without a leading
2079 // dot.  This is for both allowing the traditional
2080 // \revert Accidental #'color
2081 // as well as well as the "naive" form
2082 // \revert Accidental.color
2083
2084 revert_arg:
2085         revert_arg_backup BACKUP symbol_list_arg
2086         {
2087                 $$ = $3;
2088         }
2089         ;
2090
2091 revert_arg_backup:
2092         revert_arg_part
2093         {
2094                 if (scm_is_null ($1)
2095                     || scm_is_null (scm_cdr ($1)))
2096                         MYBACKUP (SCM_ARG, $1, @1);
2097                 else
2098                         MYBACKUP (SYMBOL_LIST, scm_reverse_x ($1, SCM_EOL), @1);
2099         }
2100         ;
2101
2102 // revert_arg_part delivers results in reverse
2103 revert_arg_part:
2104         symbol_list_part
2105         | revert_arg_backup BACKUP SCM_ARG '.' symbol_list_part
2106         {
2107                 $$ = scm_append_x (scm_list_2 ($5, $3));
2108         }
2109         | revert_arg_backup BACKUP SCM_ARG symbol_list_part
2110         {
2111                 $$ = scm_append_x (scm_list_2 ($4, $3));
2112         }               
2113         ;
2114
2115 context_def_mod:
2116         CONSISTS { $$ = ly_symbol2scm ("consists"); }
2117         | REMOVE { $$ = ly_symbol2scm ("remove"); }
2118
2119         | ACCEPTS { $$ = ly_symbol2scm ("accepts"); }
2120         | DEFAULTCHILD { $$ = ly_symbol2scm ("default-child"); }
2121         | DENIES { $$ = ly_symbol2scm ("denies"); }
2122
2123         | ALIAS { $$ = ly_symbol2scm ("alias"); }
2124         | TYPE { $$ = ly_symbol2scm ("translator-type"); }
2125         | DESCRIPTION { $$ = ly_symbol2scm ("description"); }
2126         | NAME { $$ = ly_symbol2scm ("context-name"); }
2127         ;
2128
2129 context_mod:
2130         property_operation { $$ = $1; }
2131         | context_def_mod STRING {
2132                 $$ = scm_list_2 ($1, $2);
2133         }
2134         | context_def_mod embedded_scm
2135         {
2136                 if (!scm_is_string ($2)
2137                     && ly_symbol2scm ("consists") != $1
2138                     && ly_symbol2scm ("remove") != $1)
2139                 {
2140                         $$ = SCM_EOL;
2141                         parser->parser_error (@1, _ ("only \\consists and \\remove take non-string argument."));
2142                 }
2143                 else
2144                 {
2145                         $$ = scm_list_2 ($1, $2);
2146                 }
2147         }
2148         ;
2149
2150 // If defined, at least two members.
2151 grob_prop_spec:
2152         symbol_list_rev
2153         {
2154                 SCM l = scm_reverse_x ($1, SCM_EOL);
2155                 if (scm_is_pair (l)
2156                     && to_boolean
2157                     (scm_object_property (scm_car (l),
2158                                           ly_symbol2scm ("is-grob?"))))
2159                         l = scm_cons (ly_symbol2scm ("Bottom"), l);
2160                 if (scm_is_null (l) || scm_is_null (scm_cdr (l))) {
2161                         parser->parser_error (@1, _ ("bad grob property path"));
2162                         l = SCM_UNDEFINED;
2163                 }
2164                 $$ = l;
2165         }
2166         ;
2167
2168 // If defined, at least three members
2169 grob_prop_path:
2170         grob_prop_spec
2171         {
2172                 if (!SCM_UNBNDP ($1) && scm_is_null (scm_cddr ($1)))
2173                 {
2174                         parser->parser_error (@1, _ ("bad grob property path"));
2175                         $$ = SCM_UNDEFINED;
2176                 }
2177         }
2178         | grob_prop_spec property_path
2179         {
2180                 if (!SCM_UNBNDP ($1)) {
2181                         $$ = scm_append_x (scm_list_2 ($1, $2));
2182                         if (scm_is_null (scm_cddr ($$))) {
2183                                 parser->parser_error (@$, _ ("bad grob property path"));
2184                                 $$ = SCM_UNDEFINED;
2185                         }
2186                 }
2187
2188         }
2189         ;
2190
2191 // Exactly two elements or undefined
2192 context_prop_spec:
2193         symbol_list_rev
2194         {
2195                 SCM l = scm_reverse_x ($1, SCM_EOL);
2196                 switch (scm_ilength (l)) {
2197                 case 1:
2198                         l = scm_cons (ly_symbol2scm ("Bottom"), l);
2199                 case 2:
2200                         break;
2201                 default:
2202                         parser->parser_error (@1, _ ("bad context property path"));
2203                         l = SCM_UNDEFINED;
2204                 }
2205                 $$ = l;
2206         }
2207         ;
2208
2209 simple_music_property_def:
2210         OVERRIDE grob_prop_path '=' scalar {
2211                 if (SCM_UNBNDP ($2))
2212                         $$ = SCM_UNDEFINED;
2213                 else {
2214                         $$ = scm_list_5 (scm_car ($2),
2215                                          ly_symbol2scm ("OverrideProperty"),
2216                                          scm_cadr ($2),
2217                                          $4,
2218                                          scm_cddr ($2));
2219                 }
2220         }
2221         | REVERT simple_revert_context revert_arg {
2222                 $$ = scm_list_4 ($2,
2223                                  ly_symbol2scm ("RevertProperty"),
2224                                  scm_car ($3),
2225                                  scm_cdr ($3));
2226         }
2227         | SET context_prop_spec '=' scalar {
2228                 if (SCM_UNBNDP ($2))
2229                         $$ = SCM_UNDEFINED;
2230                 else
2231                         $$ = scm_list_4 (scm_car ($2),
2232                                          ly_symbol2scm ("PropertySet"),
2233                                          scm_cadr ($2),
2234                                          $4);
2235         }
2236         | UNSET context_prop_spec {
2237                 if (SCM_UNBNDP ($2))
2238                         $$ = SCM_UNDEFINED;
2239                 else
2240                         $$ = scm_list_3 (scm_car ($2),
2241                                          ly_symbol2scm ("PropertyUnset"),
2242                                          scm_cadr ($2));
2243         }
2244         ;
2245
2246
2247 // This is all quite awkward for the sake of substantial backward
2248 // compatibility while at the same time allowing a more "natural" form
2249 // of specification not separating grob specification from grob
2250 // property path.  The purpose of this definition of
2251 // simple_revert_context is to allow the symbol list which specifies
2252 // grob and property to revert to be optionally be split into two
2253 // parts after the grob (which may be preceded by a context
2254 // specification, a case which we distinguish by checking whether the
2255 // first symbol is a valid grob symbol instead).
2256 //
2257 // See revert_arg above for the main work horse of this arrangement.
2258 // simple_revert_context just caters for the context and delegates the
2259 // rest of the job to revert_arg.
2260
2261 simple_revert_context:
2262         symbol_list_part
2263         {
2264                 $1 = scm_reverse_x ($1, SCM_EOL);
2265                 if (scm_is_null ($1)
2266                     || to_boolean
2267                     (scm_object_property (scm_car ($1),
2268                                           ly_symbol2scm ("is-grob?")))) {
2269                         $$ = ly_symbol2scm ("Bottom");
2270                         parser->lexer_->push_extra_token (SCM_IDENTIFIER, $1);
2271                 } else {
2272                         $$ = scm_car ($1);
2273                         parser->lexer_->push_extra_token (SCM_IDENTIFIER,
2274                                                           scm_cdr ($1));
2275                 }
2276         }
2277         ;
2278
2279 music_property_def:
2280         simple_music_property_def {
2281                 if (SCM_UNBNDP ($1))
2282                         $$ = MAKE_SYNTAX ("void-music", @1);
2283                 else
2284                         $$ = LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("property-operation"), scm_cons2 (parser->self_scm (), make_input (@$), $1));
2285         }
2286         ;
2287
2288 string:
2289         STRING {
2290                 $$ = $1;
2291         }
2292         | full_markup
2293         ;
2294
2295 simple_string: STRING {
2296                 $$ = $1;
2297         }
2298         | embedded_scm_bare
2299         {
2300                 if (scm_is_string ($1)) {
2301                         $$ = $1;
2302                 } else {
2303                         parser->parser_error (@1, (_ ("simple string expected")));
2304                         $$ = scm_string (SCM_EOL);
2305                 }
2306         }
2307         ;
2308
2309 symbol:
2310         STRING {
2311                 $$ = scm_string_to_symbol ($1);
2312         }
2313         | embedded_scm_bare
2314         {
2315                 // This is a bit of overkill but makes the same
2316                 // routine responsible for all symbol interpretations.
2317                 $$ = try_string_variants (ly_lily_module_constant ("symbol?"),
2318                                           $1);
2319                 if (SCM_UNBNDP ($$))
2320                 {
2321                         parser->parser_error (@1, (_ ("symbol expected")));
2322                         // Generate a unique symbol in case it is used
2323                         // for an assignment or similar
2324                         $$ = scm_make_symbol (ly_string2scm ("undefined"));
2325                 }
2326         }
2327         ;
2328
2329 scalar:
2330         embedded_scm_arg
2331         | SCM_IDENTIFIER
2332         | bare_number
2333         // The following is a rather defensive variant of admitting
2334         // negative numbers: the grammar would permit number_factor or
2335         // even number_expression.  However, function arguments allow
2336         // only this simple kind of negative number, so to have things
2337         // like \tweak and \override behave reasonably similar, it
2338         // makes sense to rule out things like -- which are rather an
2339         // accent in function argument contexts.
2340         | '-' bare_number
2341         {
2342                 $$ = scm_difference ($2, SCM_UNDEFINED);
2343         }
2344         | FRACTION
2345         | STRING
2346         | full_markup
2347         ;
2348
2349 scalar_closed:
2350         embedded_scm_arg_closed
2351         | SCM_IDENTIFIER
2352         // for scalar_closed to be an actually closed (no lookahead)
2353         // expression, we'd need to use bare_number_closed here.  It
2354         // turns out that the only use of scalar_closed in TEMPO is
2355         // not of the kind requiring the full closedness criterion.
2356         | bare_number
2357         | '-' bare_number
2358         {
2359                 $$ = scm_difference ($2, SCM_UNDEFINED);
2360         }
2361         | FRACTION
2362         | STRING
2363         | full_markup
2364         ;
2365
2366
2367 event_chord:
2368         simple_element post_events {
2369                 // Let the rhythmic music iterator sort this mess out.
2370                 if (scm_is_pair ($2)) {
2371                         $$ = make_music_from_simple (parser, @1, $1);
2372                         if (unsmob_music ($$))
2373                                 unsmob_music ($$)->set_property ("articulations",
2374                                                                  scm_reverse_x ($2, SCM_EOL));
2375                         else
2376                                 parser->parser_error (@1, _("music expected"));
2377                 }
2378         }
2379         | simple_chord_elements post_events     {
2380                 SCM elts = ly_append2 ($1, scm_reverse_x ($2, SCM_EOL));
2381
2382                 Input i;
2383                 /* why is this giving wrong start location? -ns
2384                  * i = @$; */
2385                 i.set_location (@1, @2);
2386                 $$ = MAKE_SYNTAX ("event-chord", i, elts);
2387         }
2388         | CHORD_REPETITION optional_notemode_duration post_events {
2389                 Input i;
2390                 i.set_location (@1, @3);
2391                 $$ = MAKE_SYNTAX ("repetition-chord", i,
2392                                   $2, scm_reverse_x ($3, SCM_EOL));
2393         }
2394         | MULTI_MEASURE_REST optional_notemode_duration post_events {
2395                 Input i;
2396                 i.set_location (@1, @3);
2397                 $$ = MAKE_SYNTAX ("multi-measure-rest", i, $2,
2398                                   scm_reverse_x ($3, SCM_EOL));
2399         }
2400         | command_element
2401         | note_chord_element
2402         ;
2403
2404
2405 note_chord_element:
2406         chord_body optional_notemode_duration post_events
2407         {
2408                 Music *m = unsmob_music ($1);
2409                 SCM dur = unsmob_duration ($2)->smobbed_copy ();
2410                 SCM es = m->get_property ("elements");
2411                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
2412
2413                 for (SCM s = es; scm_is_pair (s); s = scm_cdr (s))
2414                   unsmob_music (scm_car (s))->set_property ("duration", dur);
2415                 es = ly_append2 (es, postevs);
2416
2417                 m-> set_property ("elements", es);
2418                 m->set_spot (@$);
2419                 $$ = m->self_scm ();
2420         }
2421         ;
2422
2423 chord_body:
2424         ANGLE_OPEN chord_body_elements ANGLE_CLOSE
2425         {
2426                 $$ = MAKE_SYNTAX ("event-chord", @$, scm_reverse_x ($2, SCM_EOL));
2427         }
2428         ;
2429
2430 chord_body_elements:
2431         /* empty */             { $$ = SCM_EOL; }
2432         | chord_body_elements chord_body_element {
2433                 if (!SCM_UNBNDP ($2))
2434                         $$ = scm_cons ($2, $1);
2435         }
2436         ;
2437
2438 chord_body_element:
2439         pitch exclamations questions octave_check post_events
2440         {
2441                 bool q = to_boolean ($3);
2442                 bool ex = to_boolean ($2);
2443                 SCM check = $4;
2444                 SCM post = $5;
2445
2446                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2447                 n->set_property ("pitch", $1);
2448                 if (q)
2449                         n->set_property ("cautionary", SCM_BOOL_T);
2450                 if (ex || q)
2451                         n->set_property ("force-accidental", SCM_BOOL_T);
2452
2453                 if (scm_is_pair (post)) {
2454                         SCM arts = scm_reverse_x (post, SCM_EOL);
2455                         n->set_property ("articulations", arts);
2456                 }
2457                 if (scm_is_number (check))
2458                 {
2459                         int q = scm_to_int (check);
2460                         n->set_property ("absolute-octave", scm_from_int (q-1));
2461                 }
2462
2463                 $$ = n->unprotect ();
2464         }
2465         | DRUM_PITCH post_events {
2466                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2467                 n->set_property ("drum-type", $1);
2468
2469                 if (scm_is_pair ($2)) {
2470                         SCM arts = scm_reverse_x ($2, SCM_EOL);
2471                         n->set_property ("articulations", arts);
2472                 }
2473                 $$ = n->unprotect ();
2474         }
2475         | music_function_chord_body
2476         {
2477                 Music *m = unsmob_music ($1);
2478
2479                 while (m && m->is_mus_type ("music-wrapper-music")) {
2480                         $$ = m->get_property ("element");
2481                         m = unsmob_music ($$);
2482                 }
2483
2484                 if (!(m && m->is_mus_type ("rhythmic-event"))) {
2485                         parser->parser_error (@$, _ ("not a rhythmic event"));
2486                         $$ = SCM_UNDEFINED;
2487                 }
2488         }
2489         ;
2490
2491 music_function_chord_body:
2492         music_function_call
2493         | MUSIC_IDENTIFIER
2494         ;
2495
2496 // Event functions may only take closed arglists, otherwise it would
2497 // not be clear whether a following postevent should be associated
2498 // with the last argument of the event function or with the expression
2499 // for which the function call acts itself as event.
2500
2501 music_function_call_closed:
2502         MUSIC_FUNCTION function_arglist_closed {
2503                 $$ = MAKE_SYNTAX ("music-function", @$,
2504                                          $1, $2);
2505         }
2506         ;
2507
2508 event_function_event:
2509         EVENT_FUNCTION function_arglist_closed {
2510                 $$ = MAKE_SYNTAX ("music-function", @$,
2511                                          $1, $2);
2512         }
2513         ;
2514
2515 command_element:
2516         command_event {
2517                 $$ = $1;
2518         }
2519         ;
2520
2521 command_event:
2522         tempo_event {
2523                 $$ = $1;
2524         }
2525         ;
2526
2527
2528 post_events:
2529         /* empty */ {
2530                 $$ = SCM_EOL;
2531         }
2532         | post_events post_event {
2533                 $$ = $1;
2534                 if (Music *m = unsmob_music ($2))
2535                 {
2536                         if (m->is_mus_type ("post-event-wrapper"))
2537                         {
2538                                 for (SCM p = m->get_property ("elements");
2539                                      scm_is_pair (p);
2540                                      p = scm_cdr (p))
2541                                 {
2542                                         $$ = scm_cons (scm_car (p), $$);
2543                                 }
2544                         } else {
2545                                 m->set_spot (@2);
2546                                 $$ = scm_cons ($2, $$);
2547                         }
2548                 }
2549         }
2550         ;
2551
2552 post_event_nofinger:
2553         direction_less_event {
2554                 $$ = $1;
2555         }
2556         | script_dir music_function_call_closed {
2557                 $$ = $2;
2558                 if (!unsmob_music ($2)->is_mus_type ("post-event")) {
2559                         parser->parser_error (@2, _ ("post-event expected"));
2560                         $$ = SCM_UNSPECIFIED;
2561                 } else if (!SCM_UNBNDP ($1))
2562                 {
2563                         unsmob_music ($$)->set_property ("direction", $1);
2564                 }
2565         }
2566         | HYPHEN {
2567                 if (!parser->lexer_->is_lyric_state ())
2568                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2569                 $$ = MY_MAKE_MUSIC ("HyphenEvent", @$)->unprotect ();
2570         }
2571         | EXTENDER {
2572                 if (!parser->lexer_->is_lyric_state ())
2573                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2574                 $$ = MY_MAKE_MUSIC ("ExtenderEvent", @$)->unprotect ();
2575         }
2576         | script_dir direction_reqd_event {
2577                 if (!SCM_UNBNDP ($1))
2578                 {
2579                         Music *m = unsmob_music ($2);
2580                         m->set_property ("direction", $1);
2581                 }
2582                 $$ = $2;
2583         }
2584         | script_dir direction_less_event {
2585                 if (!SCM_UNBNDP ($1))
2586                 {
2587                         Music *m = unsmob_music ($2);
2588                         m->set_property ("direction", $1);
2589                 }
2590                 $$ = $2;
2591         }
2592         | '^' fingering
2593         {
2594                 $$ = $2;
2595                 unsmob_music ($$)->set_property ("direction", scm_from_int (UP));
2596         }
2597         | '_' fingering
2598         {
2599                 $$ = $2;
2600                 unsmob_music ($$)->set_property ("direction", scm_from_int (DOWN));
2601         }                       
2602         ;
2603
2604 post_event:
2605         post_event_nofinger
2606         | '-' fingering {
2607                 $$ = $2;
2608         }
2609         ;
2610
2611 string_number_event:
2612         E_UNSIGNED {
2613                 Music *s = MY_MAKE_MUSIC ("StringNumberEvent", @$);
2614                 s->set_property ("string-number", $1);
2615                 $$ = s->unprotect ();
2616         }
2617         ;
2618
2619 direction_less_event:
2620         string_number_event
2621         | EVENT_IDENTIFIER      {
2622                 $$ = $1;
2623         }
2624         | tremolo_type  {
2625                Music *a = MY_MAKE_MUSIC ("TremoloEvent", @$);
2626                a->set_property ("tremolo-type", $1);
2627                $$ = a->unprotect ();
2628         }
2629         | event_function_event  
2630         ;
2631
2632 direction_reqd_event:
2633         gen_text_def {
2634                 $$ = $1;
2635         }
2636         | script_abbreviation {
2637                 SCM s = parser->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
2638                 Music *a = MY_MAKE_MUSIC ("ArticulationEvent", @$);
2639                 if (scm_is_string (s))
2640                         a->set_property ("articulation-type", s);
2641                 else parser->parser_error (@1, _ ("expecting string as script definition"));
2642                 $$ = a->unprotect ();
2643         }
2644         ;
2645
2646 octave_check:
2647         /**/ { $$ = SCM_EOL; }
2648         | '=' quotes { $$ = $2; }
2649         ;
2650
2651 quotes:
2652         /* empty */
2653         {
2654                 $$ = SCM_INUM0;
2655         }
2656         | sub_quotes
2657         | sup_quotes
2658         ;
2659
2660 sup_quotes:
2661         '\'' {
2662                 $$ = scm_from_int (1);
2663         }
2664         | sup_quotes '\'' {
2665                 $$ = scm_oneplus ($1);
2666         }
2667         ;
2668
2669 sub_quotes:
2670         ',' {
2671                 $$ = scm_from_int (-1);
2672         }
2673         | sub_quotes ',' {
2674                 $$ = scm_oneminus ($1);
2675         }
2676         ;
2677
2678 steno_pitch:
2679         NOTENAME_PITCH quotes {
2680                 if (!scm_is_eq (SCM_INUM0, $2))
2681                 {
2682                         Pitch p = *unsmob_pitch ($1);
2683                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2684                         $$ = p.smobbed_copy ();
2685                 }
2686         }
2687         ;
2688
2689 /*
2690 ugh. duplication
2691 */
2692
2693 steno_tonic_pitch:
2694         TONICNAME_PITCH quotes {
2695                 if (!scm_is_eq (SCM_INUM0, $2))
2696                 {
2697                         Pitch p = *unsmob_pitch ($1);
2698                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2699                         $$ = p.smobbed_copy ();
2700                 }
2701         }
2702         ;
2703
2704 pitch:
2705         steno_pitch
2706         | PITCH_IDENTIFIER
2707         ;
2708
2709 pitch_also_in_chords:
2710         pitch
2711         | steno_tonic_pitch
2712         ;
2713
2714 gen_text_def:
2715         full_markup {
2716                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2717                 t->set_property ("text", $1);
2718                 $$ = t->unprotect ();
2719         }
2720         | STRING {
2721                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2722                 t->set_property ("text",
2723                         make_simple_markup ($1));
2724                 $$ = t->unprotect ();
2725         }
2726         | embedded_scm_closed
2727         {
2728                 Music *m = unsmob_music ($1);
2729                 if (m && m->is_mus_type ("post-event"))
2730                         $$ = $1;
2731                 else if (Text_interface::is_markup ($1)) {
2732                         Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2733                         t->set_property ("text", $1);
2734                         $$ = t->unprotect ();
2735                 } else
2736                         parser->parser_error (@1, _ ("not an articulation"));
2737         }
2738         ;
2739
2740 fingering:
2741         UNSIGNED {
2742                 Music *t = MY_MAKE_MUSIC ("FingeringEvent", @$);
2743                 t->set_property ("digit", $1);
2744                 $$ = t->unprotect ();
2745         }
2746         ;
2747
2748 script_abbreviation:
2749         '^'             {
2750                 $$ = scm_from_locale_string ("Hat");
2751         }
2752         | '+'           {
2753                 $$ = scm_from_locale_string ("Plus");
2754         }
2755         | '-'           {
2756                 $$ = scm_from_locale_string ("Dash");
2757         }
2758         | '!'           {
2759                 $$ = scm_from_locale_string ("Bang");
2760         }
2761         | ANGLE_CLOSE   {
2762                 $$ = scm_from_locale_string ("Larger");
2763         }
2764         | '.'           {
2765                 $$ = scm_from_locale_string ("Dot");
2766         }
2767         | '_' {
2768                 $$ = scm_from_locale_string ("Underscore");
2769         }
2770         ;
2771
2772 script_dir:
2773         '_'     { $$ = scm_from_int (DOWN); }
2774         | '^'   { $$ = scm_from_int (UP); }
2775         | '-'   { $$ = SCM_UNDEFINED; }
2776         ;
2777
2778 duration_length:
2779         multiplied_duration {
2780                 $$ = $1;
2781         }
2782         ;
2783
2784 maybe_notemode_duration:
2785         {
2786                 $$ = SCM_UNDEFINED;
2787         }
2788         | multiplied_duration   {
2789                 $$ = $1;
2790                 parser->default_duration_ = *unsmob_duration ($$);
2791         }
2792 ;
2793
2794
2795 optional_notemode_duration:
2796         maybe_notemode_duration
2797         {
2798                 if (SCM_UNBNDP ($$))
2799                         $$ = parser->default_duration_.smobbed_copy ();
2800         }
2801         ;
2802
2803 steno_duration:
2804         UNSIGNED dots           {
2805                 int len = 0;
2806                 int n = scm_to_int ($1);
2807                 if (!is_duration (n))
2808                         parser->parser_error (@1, _f ("not a duration: %d", n));
2809                 else
2810                         len = intlog2 (n);
2811
2812                 $$ = Duration (len, scm_to_int ($2)).smobbed_copy ();
2813         }
2814         | DURATION_IDENTIFIER dots      {
2815                 Duration *d = unsmob_duration ($1);
2816                 Duration k (d->duration_log (),
2817                             d->dot_count () + scm_to_int ($2));
2818                 k = k.compressed (d->factor ());
2819                 scm_remember_upto_here_1 ($1);
2820                 $$ = k.smobbed_copy ();
2821         }
2822         ;
2823
2824 multiplied_duration:
2825         steno_duration {
2826                 $$ = $1;
2827         }
2828         | multiplied_duration '*' UNSIGNED {
2829                 $$ = unsmob_duration ($$)->compressed (scm_to_int ($3)).smobbed_copy ();
2830         }
2831         | multiplied_duration '*' FRACTION {
2832                 Rational  m (scm_to_int (scm_car ($3)), scm_to_int (scm_cdr ($3)));
2833
2834                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
2835         }
2836         ;
2837
2838 dots:
2839         /* empty */     {
2840                 $$ = SCM_INUM0;
2841         }
2842         | dots '.' {
2843                 $$ = scm_oneplus ($1);
2844         }
2845         ;
2846
2847 tremolo_type:
2848         ':'     {
2849                 $$ = SCM_INUM0;
2850         }
2851         | ':' UNSIGNED {
2852                 int n = scm_to_int ($2);
2853                 if (!is_duration (n))
2854                         parser->parser_error (@2, _f ("not a duration: %d", n));
2855                 $$ = $2;
2856         }
2857         ;
2858
2859 bass_number:
2860         UNSIGNED { $$ = $1; }
2861         | STRING { $$ = $1; }
2862         | full_markup { $$ = $1; }
2863         | embedded_scm_bare
2864         {
2865                 // as an integer, it needs to be non-negative, and otherwise
2866                 // it needs to be suitable as a markup.
2867                 if (scm_is_integer ($1)
2868                     ? scm_is_true (scm_negative_p ($1))
2869                     : !Text_interface::is_markup ($1))
2870                 {
2871                         parser->parser_error (@1, _ ("bass number expected"));
2872                         $$ = SCM_INUM0;
2873                 }
2874         }
2875         ;
2876
2877 figured_bass_alteration:
2878         '-'     { $$ = ly_rational2scm (FLAT_ALTERATION); }
2879         | '+'   { $$ = ly_rational2scm (SHARP_ALTERATION); }
2880         | '!'   { $$ = scm_from_int (0); }
2881         ;
2882
2883 bass_figure:
2884         FIGURE_SPACE {
2885                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
2886                 $$ = bfr->unprotect ();
2887         }
2888         | bass_number  {
2889                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
2890                 $$ = bfr->self_scm ();
2891
2892                 if (scm_is_number ($1))
2893                         bfr->set_property ("figure", $1);
2894                 else if (Text_interface::is_markup ($1))
2895                         bfr->set_property ("text", $1);
2896
2897                 bfr->unprotect ();
2898         }
2899         | bass_figure ']' {
2900                 $$ = $1;
2901                 unsmob_music ($1)->set_property ("bracket-stop", SCM_BOOL_T);
2902         }
2903         | bass_figure figured_bass_alteration {
2904                 Music *m = unsmob_music ($1);
2905                 if (scm_to_double ($2)) {
2906                         SCM salter = m->get_property ("alteration");
2907                         SCM alter = scm_is_number (salter) ? salter : scm_from_int (0);
2908                         m->set_property ("alteration",
2909                                          scm_sum (alter, $2));
2910                 } else {
2911                         m->set_property ("alteration", scm_from_int (0));
2912                 }
2913         }
2914         | bass_figure figured_bass_modification  {
2915                 Music *m = unsmob_music ($1);
2916                 m->set_property ($2, SCM_BOOL_T);
2917         }
2918         ;
2919
2920
2921 figured_bass_modification:
2922         E_PLUS          {
2923                 $$ = ly_symbol2scm ("augmented");
2924         }
2925         | E_EXCLAMATION {
2926                 $$ = ly_symbol2scm ("no-continuation");
2927         }
2928         | '/'           {
2929                 $$ = ly_symbol2scm ("diminished");
2930         }
2931         | E_BACKSLASH {
2932                 $$ = ly_symbol2scm ("augmented-slash");
2933         }
2934         ;
2935
2936 br_bass_figure:
2937         bass_figure {
2938                 $$ = $1;
2939         }
2940         | '[' bass_figure {
2941                 $$ = $2;
2942                 unsmob_music ($$)->set_property ("bracket-start", SCM_BOOL_T);
2943         }
2944         ;
2945
2946 figure_list:
2947         /**/            {
2948                 $$ = SCM_EOL;
2949         }
2950         | figure_list br_bass_figure {
2951                 $$ = scm_cons ($2, $1);
2952         }
2953         ;
2954
2955 figure_spec:
2956         FIGURE_OPEN figure_list FIGURE_CLOSE {
2957                 $$ = scm_reverse_x ($2, SCM_EOL);
2958         }
2959         ;
2960
2961
2962 optional_rest:
2963         /**/   { $$ = SCM_BOOL_F; }
2964         | REST { $$ = SCM_BOOL_T; }
2965         ;
2966
2967 simple_element:
2968         pitch exclamations questions octave_check maybe_notemode_duration optional_rest {
2969                 if (!parser->lexer_->is_note_state ())
2970                         parser->parser_error (@1, _ ("have to be in Note mode for notes"));
2971                 if (!SCM_UNBNDP ($2)
2972                     || !SCM_UNBNDP ($3)
2973                     || scm_is_number ($4)
2974                     || !SCM_UNBNDP ($5)
2975                     || scm_is_true ($6))
2976                 {
2977                         Music *n = 0;
2978                         if (scm_is_true ($6))
2979                                 n = MY_MAKE_MUSIC ("RestEvent", @$);
2980                         else
2981                                 n = MY_MAKE_MUSIC ("NoteEvent", @$);
2982                         
2983                         n->set_property ("pitch", $1);
2984                         if (SCM_UNBNDP ($5))
2985                                 n->set_property ("duration",
2986                                                  parser->default_duration_.smobbed_copy ());
2987                         else
2988                                 n->set_property ("duration", $5);
2989                         
2990                         if (scm_is_number ($4))
2991                         {
2992                                 int q = scm_to_int ($4);
2993                                 n->set_property ("absolute-octave", scm_from_int (q-1));
2994                         }
2995                         
2996                         if (to_boolean ($3))
2997                                 n->set_property ("cautionary", SCM_BOOL_T);
2998                         if (to_boolean ($2) || to_boolean ($3))
2999                                 n->set_property ("force-accidental", SCM_BOOL_T);
3000                         
3001                         $$ = n->unprotect ();
3002                 }
3003         }
3004         | DRUM_PITCH optional_notemode_duration {
3005                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
3006                 n->set_property ("duration", $2);
3007                 n->set_property ("drum-type", $1);
3008
3009                 $$ = n->unprotect ();
3010         }
3011         | RESTNAME optional_notemode_duration           {
3012                 Music *ev = 0;
3013                 if (ly_scm2string ($1) == "s") {
3014                         /* Space */
3015                         ev = MY_MAKE_MUSIC ("SkipEvent", @$);
3016                   }
3017                 else {
3018                         ev = MY_MAKE_MUSIC ("RestEvent", @$);
3019
3020                     }
3021                 ev->set_property ("duration", $2);
3022                 $$ = ev->unprotect ();
3023         }
3024         ;
3025
3026 simple_chord_elements:
3027         new_chord {
3028                 if (!parser->lexer_->is_chord_state ())
3029                         parser->parser_error (@1, _ ("have to be in Chord mode for chords"));
3030                 $$ = $1;
3031         }
3032         | figure_spec optional_notemode_duration {
3033                 for (SCM s = $1; scm_is_pair (s); s = scm_cdr (s))
3034                 {
3035                         unsmob_music (scm_car (s))->set_property ("duration", $2);
3036                 }
3037                 $$ = $1;
3038         }
3039         ;
3040
3041 lyric_element:
3042         full_markup {
3043                 if (!parser->lexer_->is_lyric_state ())
3044                         parser->parser_error (@1, _ ("markup outside of text script or \\lyricmode"));
3045                 $$ = $1;
3046         }
3047         | STRING {
3048                 if (!parser->lexer_->is_lyric_state ())
3049                         parser->parser_error (@1, _ ("unrecognized string, not in text script or \\lyricmode"));
3050                 $$ = $1;
3051         }
3052         | LYRIC_ELEMENT
3053         ;
3054
3055 lyric_element_music:
3056         lyric_element optional_notemode_duration post_events {
3057                 $$ = MAKE_SYNTAX ("lyric-event", @$, $1, $2);
3058                 if (scm_is_pair ($3))
3059                         unsmob_music ($$)->set_property
3060                                 ("articulations", scm_reverse_x ($3, SCM_EOL));
3061         }
3062         ;
3063
3064 new_chord:
3065         steno_tonic_pitch optional_notemode_duration   {
3066                 $$ = make_chord_elements (@$, $1, $2, SCM_EOL);
3067         }
3068         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
3069                 SCM its = scm_reverse_x ($4, SCM_EOL);
3070                 $$ = make_chord_elements (@$, $1, $2, scm_cons ($3, its));
3071         }
3072         ;
3073
3074 chord_items:
3075         /**/ {
3076                 $$ = SCM_EOL;
3077         }
3078         | chord_items chord_item {
3079                 $$ = scm_cons ($2, $$);
3080         }
3081         ;
3082
3083 chord_separator:
3084         CHORD_COLON {
3085                 $$ = ly_symbol2scm ("chord-colon");
3086         }
3087         | CHORD_CARET {
3088                 $$ = ly_symbol2scm ("chord-caret");
3089         }
3090         | CHORD_SLASH steno_tonic_pitch {
3091                 $$ = scm_list_2 (ly_symbol2scm ("chord-slash"), $2);
3092         }
3093         | CHORD_BASS steno_tonic_pitch {
3094                 $$ = scm_list_2 (ly_symbol2scm ("chord-bass"), $2);
3095         }
3096         ;
3097
3098 chord_item:
3099         chord_separator {
3100                 $$ = $1;
3101         }
3102         | step_numbers {
3103                 $$ = scm_reverse_x ($1, SCM_EOL);
3104         }
3105         | CHORD_MODIFIER  {
3106                 $$ = $1;
3107         }
3108         ;
3109
3110 step_numbers:
3111         step_number { $$ = scm_cons ($1, SCM_EOL); }
3112         | step_numbers '.' step_number {
3113                 $$ = scm_cons ($3, $$);
3114         }
3115         ;
3116
3117 step_number:
3118         UNSIGNED {
3119                 $$ = make_chord_step ($1, 0);
3120         }
3121         | UNSIGNED '+' {
3122                 $$ = make_chord_step ($1, SHARP_ALTERATION);
3123         }
3124         | UNSIGNED CHORD_MINUS {
3125                 $$ = make_chord_step ($1, FLAT_ALTERATION);
3126         }
3127         ;
3128
3129 tempo_range:
3130         UNSIGNED {
3131                 $$ = $1;
3132         }
3133         | UNSIGNED '-' UNSIGNED {
3134                 $$ = scm_cons ($1, $3);
3135         }
3136         ;
3137
3138 /*
3139         UTILITIES
3140
3141 TODO: should deprecate in favor of Scheme?
3142
3143  */
3144 number_expression:
3145         number_expression '+' number_term {
3146                 $$ = scm_sum ($1, $3);
3147         }
3148         | number_expression '-' number_term {
3149                 $$ = scm_difference ($1, $3);
3150         }
3151         | number_term
3152         ;
3153
3154 number_term:
3155         number_factor {
3156                 $$ = $1;
3157         }
3158         | number_factor '*' number_factor {
3159                 $$ = scm_product ($1, $3);
3160         }
3161         | number_factor '/' number_factor {
3162                 $$ = scm_divide ($1, $3);
3163         }
3164         ;
3165
3166 number_factor:
3167         '-'  number_factor { /* %prec UNARY_MINUS */
3168                 $$ = scm_difference ($2, SCM_UNDEFINED);
3169         }
3170         | bare_number
3171         ;
3172
3173
3174 bare_number:
3175         bare_number_closed
3176         | UNSIGNED NUMBER_IDENTIFIER    {
3177                 $$ = scm_product ($1, $2);
3178         }
3179         | REAL NUMBER_IDENTIFIER        {
3180                 $$ = scm_product ($1, $2);
3181         }
3182         ;
3183
3184 bare_number_closed:
3185         UNSIGNED
3186         | REAL
3187         | NUMBER_IDENTIFIER
3188         ;
3189
3190 unsigned_number:
3191         UNSIGNED
3192         | NUMBER_IDENTIFIER
3193         ;
3194
3195 exclamations:
3196                 { $$ = SCM_UNDEFINED; }
3197         | exclamations '!'
3198         {
3199                 if (SCM_UNBNDP ($1))
3200                         $$ = SCM_BOOL_T;
3201                 else
3202                         $$ = scm_not ($1);
3203         }
3204         ;
3205
3206 questions:
3207         { $$ = SCM_UNDEFINED; }
3208         | questions '?'
3209         {
3210                 if (SCM_UNBNDP ($1))
3211                         $$ = SCM_BOOL_T;
3212                 else
3213                         $$ = scm_not ($1);
3214         }
3215         ;
3216
3217 full_markup_list:
3218         MARKUPLIST
3219                 { parser->lexer_->push_markup_state (); }
3220         markup_list {
3221                 $$ = $3;
3222                 parser->lexer_->pop_state ();
3223         }
3224         ;
3225
3226 full_markup:
3227         MARKUP
3228                 { parser->lexer_->push_markup_state (); }
3229         markup_top {
3230                 $$ = $3;
3231                 parser->lexer_->pop_state ();
3232         }
3233         ;
3234
3235 markup_top:
3236         simple_markup_list {
3237                 $$ = scm_list_2 (ly_lily_module_constant ("line-markup"),  $1);
3238         }
3239         | markup_head_1_list simple_markup
3240         {
3241                 $$ = scm_car (MAKE_SYNTAX ("composed-markup-list",
3242                                            @2, $1, scm_list_1 ($2)));
3243         }
3244         | simple_markup {
3245                 $$ = $1;
3246         }
3247         ;
3248
3249 markup_scm:
3250         embedded_scm_bare
3251         {
3252                 if (Text_interface::is_markup ($1))
3253                         MYBACKUP (MARKUP_IDENTIFIER, $1, @1);
3254                 else if (Text_interface::is_markup_list ($1))
3255                         MYBACKUP (MARKUPLIST_IDENTIFIER, $1, @1);
3256                 else {
3257                         parser->parser_error (@1, _ ("not a markup"));
3258                         MYBACKUP (MARKUP_IDENTIFIER, scm_string (SCM_EOL), @1);
3259                 }
3260         } BACKUP
3261         ;
3262                         
3263
3264 simple_markup_list:
3265         markup_composed_list {
3266                 $$ = $1;
3267         }
3268         | markup_uncomposed_list
3269         ;
3270
3271 markup_uncomposed_list:
3272         markup_braced_list {
3273                 $$ = $1;
3274         }
3275         | markup_command_list {
3276                 $$ = scm_list_1 ($1);
3277         }
3278         | markup_scm MARKUPLIST_IDENTIFIER
3279         {
3280                 $$ = $2;
3281         }
3282         ;
3283
3284 markup_list:
3285         simple_markup_list
3286         | markup_score
3287         {
3288                 $$ = scm_list_1 (scm_list_2 (ly_lily_module_constant ("score-lines-markup-list"), $1));
3289         }
3290         ;
3291
3292 markup_score:
3293         SCORE {
3294                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3295                 parser->lexer_->push_note_state (nn);
3296         } '{' score_body '}' {
3297                 $$ = $4;
3298                 parser->lexer_->pop_state ();
3299         }
3300         ;
3301
3302 markup_composed_list:
3303         markup_head_1_list markup_uncomposed_list {
3304                 $$ = MAKE_SYNTAX ("composed-markup-list",
3305                                   @2, $1, $2);
3306         }
3307         ;
3308
3309 markup_braced_list:
3310         '{' markup_braced_list_body '}' {
3311                 $$ = scm_reverse_x ($2, SCM_EOL);
3312         }
3313         ;
3314
3315 markup_braced_list_body:
3316         /* empty */     {  $$ = SCM_EOL; }
3317         | markup_braced_list_body markup {
3318                 $$ = scm_cons ($2, $1);
3319         }
3320         | markup_braced_list_body simple_markup_list {
3321                 $$ = scm_reverse_x ($2, $1);
3322         }
3323         ;
3324
3325 markup_command_list:
3326         MARKUP_LIST_FUNCTION markup_command_list_arguments {
3327           $$ = scm_cons ($1, scm_reverse_x($2, SCM_EOL));
3328         }
3329         ;
3330
3331 markup_command_basic_arguments:
3332         EXPECT_MARKUP_LIST markup_command_list_arguments markup_list {
3333           $$ = scm_cons ($3, $2);
3334         }
3335         | EXPECT_SCM markup_command_list_arguments embedded_scm_closed {
3336           $$ = check_scheme_arg (parser, @3, $3, $2, $1);
3337         }
3338         | EXPECT_NO_MORE_ARGS {
3339           $$ = SCM_EOL;
3340         }
3341         ;
3342
3343 markup_command_list_arguments:
3344         markup_command_basic_arguments { $$ = $1; }
3345         | EXPECT_MARKUP markup_command_list_arguments markup {
3346           $$ = scm_cons ($3, $2);
3347         }
3348         ;
3349
3350 markup_head_1_item:
3351         MARKUP_FUNCTION EXPECT_MARKUP markup_command_list_arguments {
3352           $$ = scm_cons ($1, scm_reverse_x ($3, SCM_EOL));
3353         }
3354         ;
3355
3356 markup_head_1_list:
3357         markup_head_1_item      {
3358                 $$ = scm_list_1 ($1);
3359         }
3360         | markup_head_1_list markup_head_1_item {
3361                 $$ = scm_cons ($2, $1);
3362         }
3363         ;
3364
3365 simple_markup:
3366         STRING {
3367                 $$ = make_simple_markup ($1);
3368         }
3369         | MARKUP_FUNCTION markup_command_basic_arguments {
3370                 $$ = scm_cons ($1, scm_reverse_x ($2, SCM_EOL));
3371         }
3372         | markup_scm MARKUP_IDENTIFIER
3373         {
3374                 $$ = $2;
3375         }
3376         | markup_score
3377         {
3378                 $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), $1);
3379         }
3380         ;
3381
3382 markup:
3383         markup_head_1_list simple_markup
3384         {
3385                 $$ = scm_car (MAKE_SYNTAX ("composed-markup-list",
3386                                            @2, $1, scm_list_1 ($2)));
3387         }
3388         | simple_markup {
3389                 $$ = $1;
3390         }
3391         ;
3392
3393 %%
3394
3395 void
3396 Lily_parser::set_yydebug (bool x)
3397 {
3398         yydebug = x;
3399 }
3400
3401 SCM
3402 Lily_parser::do_yyparse ()
3403 {
3404         SCM retval = SCM_UNDEFINED;
3405         yyparse (this, &retval);
3406         return retval;
3407 }
3408
3409
3410
3411
3412
3413 /*
3414
3415 It is a little strange to have this function in this file, but
3416 otherwise, we have to import music classes into the lexer.
3417
3418 */
3419 int
3420 Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
3421 {
3422         if (unsmob_book (sid)) {
3423                 Book *book =  unsmob_book (sid)->clone ();
3424                 *destination = book->self_scm ();
3425                 book->unprotect ();
3426
3427                 return BOOK_IDENTIFIER;
3428         } else if (scm_is_number (sid)) {
3429                 *destination = sid;
3430                 return NUMBER_IDENTIFIER;
3431         } else if (unsmob_context_def (sid)) {
3432                 Context_def *def= unsmob_context_def (sid)->clone ();
3433
3434                 *destination = def->self_scm ();
3435                 def->unprotect ();
3436
3437                 return CONTEXT_DEF_IDENTIFIER;
3438         } else if (unsmob_context_mod (sid)) {
3439                 *destination = unsmob_context_mod (sid)->smobbed_copy ();
3440
3441                 return CONTEXT_MOD_IDENTIFIER;
3442         } else if (Music *mus = unsmob_music (sid)) {
3443                 mus = mus->clone ();
3444                 *destination = mus->self_scm ();
3445                 bool is_event = mus->is_mus_type ("post-event");
3446                 mus->unprotect ();
3447                 return is_event ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
3448         } else if (unsmob_pitch (sid)) {
3449                 *destination = unsmob_pitch (sid)->smobbed_copy ();
3450                 return PITCH_IDENTIFIER;
3451         } else if (unsmob_duration (sid)) {
3452                 *destination = unsmob_duration (sid)->smobbed_copy ();
3453                 return DURATION_IDENTIFIER;
3454         } else if (unsmob_output_def (sid)) {
3455                 Output_def *p = unsmob_output_def (sid);
3456                 p = p->clone ();
3457
3458                 *destination = p->self_scm ();
3459                 p->unprotect ();
3460                 return OUTPUT_DEF_IDENTIFIER;
3461         }
3462
3463         return -1;
3464 }
3465
3466 SCM
3467 get_next_unique_context_id ()
3468 {
3469         return scm_from_locale_string ("$uniqueContextId");
3470 }
3471
3472
3473 SCM
3474 get_next_unique_lyrics_context_id ()
3475 {
3476         static int new_context_count;
3477         char s[128];
3478         snprintf (s, sizeof (s)-1, "uniqueContext%d", new_context_count++);
3479         return scm_from_locale_string (s);
3480 }
3481
3482 // check_scheme_arg checks one argument with a given predicate for use
3483 // in an argument list and throws a syntax error if it is unusable.
3484 // The argument is prepended to the argument list in any case.  After
3485 // throwing a syntax error, the argument list is terminated with #f as
3486 // its last cdr in order to mark it as uncallable while not losing
3487 // track of its total length.
3488 //
3489 // There are a few special considerations: if optional argument disp
3490 // is given (otherwise it defaults to SCM_UNDEFINED), it will be used
3491 // instead of arg in a prospective error message.  This is useful if
3492 // arg is not the actual argument but rather a transformation of it.
3493 //
3494 // If arg itself is SCM_UNDEFINED, the predicate is considered false
3495 // and an error message using disp is produced unconditionally.
3496
3497 SCM check_scheme_arg (Lily_parser *parser, Input loc,
3498                       SCM arg, SCM args, SCM pred, SCM disp)
3499 {
3500         if (SCM_UNBNDP (arg))
3501                 args = scm_cons (disp, args);
3502         else {
3503                 args = scm_cons (arg, args);
3504                 if (scm_is_true (scm_call_1 (pred, arg)))
3505                         return args;
3506         }
3507         scm_set_cdr_x (scm_last_pair (args), SCM_EOL);
3508         MAKE_SYNTAX ("argument-error", loc, scm_length (args), pred,
3509                      SCM_UNBNDP (disp) ? arg : disp);
3510         scm_set_cdr_x (scm_last_pair (args), SCM_BOOL_F);
3511         return args;
3512 }
3513
3514 SCM loc_on_music (Input loc, SCM arg)
3515 {
3516         if (Music *m = unsmob_music (arg))
3517         {
3518                 m = m->clone ();
3519                 m->set_spot (loc);
3520                 return m->unprotect ();
3521         }
3522         return arg;
3523 }
3524
3525 SCM
3526 try_string_variants (SCM pred, SCM str)
3527 {
3528         // a matching predicate is always ok
3529         if (scm_is_true (scm_call_1 (pred, str)))
3530                 return str;
3531         // a symbol may be interpreted as a list of symbols if it helps
3532         if (scm_is_symbol (str)) {
3533                 str = scm_list_1 (str);
3534                 if (scm_is_true (scm_call_1 (pred, str)))
3535                         return str;
3536                 return SCM_UNDEFINED;
3537         }
3538
3539         // If this cannot be a string representation of a symbol list,
3540         // we are through.
3541
3542         if (!is_regular_identifier (str, true))
3543                 return SCM_UNDEFINED;
3544
3545         str = scm_string_split (str, SCM_MAKE_CHAR ('.'));
3546         for (SCM p = str; scm_is_pair (p); p = scm_cdr (p))
3547                 scm_set_car_x (p, scm_string_to_symbol (scm_car (p)));
3548
3549         // Let's attempt the symbol list interpretation first.
3550
3551         if (scm_is_true (scm_call_1 (pred, str)))
3552                 return str;
3553
3554         // If there is just one symbol in the list, we might interpret
3555         // it as a single symbol
3556
3557         if (scm_is_null (scm_cdr (str)))
3558         {
3559                 str = scm_car (str);
3560                 if (scm_is_true (scm_call_1 (pred, str)))
3561                         return str;
3562         }
3563
3564         return SCM_UNDEFINED;
3565 }
3566
3567 bool
3568 is_regular_identifier (SCM id, bool multiple)
3569 {
3570   if (!scm_is_string (id))
3571           return false;
3572
3573   string str = ly_scm2string (id);
3574
3575   bool middle = false;
3576
3577   for (string::iterator it=str.begin(); it != str.end (); it++)
3578   {
3579           int c = *it & 0xff;
3580           if ((c >= 'a' && c <= 'z')
3581               || (c >= 'A' && c <= 'Z')
3582               || c > 0x7f)
3583                   middle = true;
3584           else if (middle && (c == '-' || c == '_' || (multiple && c == '.')))
3585                   middle = false;
3586           else
3587                   return false;
3588   }
3589   return middle;
3590 }
3591
3592 SCM
3593 make_music_from_simple (Lily_parser *parser, Input loc, SCM simple)
3594 {
3595         if (unsmob_music (simple))
3596                 return simple;
3597         if (parser->lexer_->is_note_state ()) {
3598                 if (scm_is_symbol (simple)) {
3599                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3600                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3601                         n->set_property ("drum-type", simple);
3602                         return n->unprotect ();
3603                 }
3604                 if (unsmob_pitch (simple)) {
3605                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3606                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3607                         n->set_property ("pitch", simple);
3608                         return n->unprotect ();
3609                 }
3610                 return simple;
3611         } else if (parser->lexer_->is_lyric_state ()) {
3612                 if (Text_interface::is_markup (simple))
3613                         return MAKE_SYNTAX ("lyric-event", loc, simple,
3614                                             parser->default_duration_.smobbed_copy ());
3615         } else if (parser->lexer_->is_chord_state ()) {
3616                 if (unsmob_pitch (simple))
3617                         return make_chord_elements (loc, simple,
3618                                                     parser->default_duration_.smobbed_copy (),
3619                                                     SCM_EOL);
3620         }
3621         return simple;
3622 }
3623
3624 Music *
3625 make_music_with_input (SCM name, Input where)
3626 {
3627        Music *m = make_music_by_name (name);
3628        m->set_spot (where);
3629        return m;
3630 }
3631
3632 SCM
3633 make_simple_markup (SCM a)
3634 {
3635         return a;
3636 }
3637
3638 bool
3639 is_duration (int t)
3640 {
3641   return t && t == 1 << intlog2 (t);
3642 }
3643
3644 SCM
3645 make_chord_step (SCM step_scm, Rational alter)
3646 {
3647         int step = scm_to_int (step_scm);
3648
3649         if (step == 7)
3650                 alter += FLAT_ALTERATION;
3651
3652         while (step < 0)
3653                 step += 7;
3654         Pitch m ((step -1) / 7, (step - 1) % 7, alter);
3655         return m.smobbed_copy ();
3656 }
3657
3658
3659 SCM
3660 make_chord_elements (Input loc, SCM pitch, SCM dur, SCM modification_list)
3661 {
3662         SCM chord_ctor = ly_lily_module_constant ("construct-chord-elements");
3663         SCM res = scm_call_3 (chord_ctor, pitch, dur, modification_list);
3664         for (SCM s = res; scm_is_pair (s); s = scm_cdr (s))
3665         {
3666                 unsmob_music (scm_car (s))->set_spot (loc);
3667         }
3668         return res;
3669 }
3670
3671 int
3672 yylex (YYSTYPE *s, YYLTYPE *loc, Lily_parser *parser)
3673 {
3674         Lily_lexer *lex = parser->lexer_;
3675
3676         lex->lexval_ = s;
3677         lex->lexloc_ = loc;
3678         lex->prepare_for_next_token ();
3679         return lex->yylex ();
3680 }