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