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