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