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