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