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