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