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