]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
d6a7beac926c46f61f0d037a52c3663bf3ff3071
[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         | embedded_scm
1021         {
1022                 if (unsmob_music ($1)
1023                     || scm_is_eq ($1, SCM_UNSPECIFIED))
1024                         $$ = $1;
1025                 else
1026                 {
1027                         @$.warning (_ ("Ignoring non-music expression"));
1028                         $$ = SCM_UNSPECIFIED;
1029                 }
1030         }
1031         ;
1032
1033 music_arg:
1034         simple_music
1035         {
1036                 $$ = make_music_from_simple (parser, @1, $1);
1037                 if (!unsmob_music ($$))
1038                         parser->parser_error (@1, _ ("music expected"));
1039         }
1040         | composite_music %prec COMPOSITE
1041         ;
1042
1043 music_assign:
1044         simple_music
1045         | composite_music %prec COMPOSITE
1046         ;
1047
1048 repeated_music:
1049         REPEAT simple_string unsigned_number music
1050         {
1051                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, SCM_EOL);
1052         }
1053         | REPEAT simple_string unsigned_number music ALTERNATIVE braced_music_list
1054         {
1055                 $$ = MAKE_SYNTAX ("repeat", @$, $2, $3, $4, $6);
1056         }
1057         ;
1058
1059 sequential_music:
1060         SEQUENTIAL braced_music_list {
1061                 $$ = MAKE_SYNTAX ("sequential-music", @$, $2);
1062         }
1063         | braced_music_list {
1064                 $$ = MAKE_SYNTAX ("sequential-music", @$, $1);
1065         }
1066         ;
1067
1068 simultaneous_music:
1069         SIMULTANEOUS braced_music_list {
1070                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, $2);
1071         }
1072         | DOUBLE_ANGLE_OPEN music_list DOUBLE_ANGLE_CLOSE       {
1073                 $$ = MAKE_SYNTAX ("simultaneous-music", @$, scm_reverse_x ($2, SCM_EOL));
1074         }
1075         ;
1076
1077 simple_music:
1078         event_chord
1079         | music_property_def
1080         | context_change
1081         ;
1082
1083 context_modification:
1084         WITH { parser->lexer_->push_initial_state (); } '{' context_mod_list '}'
1085         {
1086                 parser->lexer_->pop_state ();
1087                 $$ = $4;
1088         }
1089         | WITH CONTEXT_MOD_IDENTIFIER
1090         {
1091                 $$ = $2;
1092         }
1093         | CONTEXT_MOD_IDENTIFIER
1094         {
1095                 $$ = $1;
1096         }
1097         | WITH embedded_scm_closed
1098         {
1099                 if (unsmob_context_mod ($2))
1100                         $$ = $2;
1101                 else {
1102                         parser->parser_error (@2, _ ("not a context mod"));
1103                         $$ = Context_mod ().smobbed_copy ();
1104                 }
1105         }
1106         ;
1107
1108 optional_context_mod:
1109         /**/ {
1110             $$ = SCM_EOL;
1111         }
1112         | context_modification
1113         {
1114               $$ = $1;
1115         }
1116         ;
1117
1118 context_mod_list:
1119         /**/ {
1120             $$ = Context_mod ().smobbed_copy ();
1121         }
1122         | context_mod_list context_mod  {
1123                 if (!SCM_UNBNDP ($2))
1124                         unsmob_context_mod ($1)->add_context_mod ($2);
1125         }
1126         | context_mod_list CONTEXT_MOD_IDENTIFIER {
1127                  Context_mod *md = unsmob_context_mod ($2);
1128                  if (md)
1129                      unsmob_context_mod ($1)->add_context_mods (md->get_mods ());
1130         }
1131         | context_mod_list context_mod_embedded {
1132                 unsmob_context_mod ($1)->add_context_mods
1133                         (unsmob_context_mod ($2)->get_mods ());
1134         }
1135         ;
1136
1137 composite_music:
1138         complex_music
1139         | music_bare
1140         ;
1141
1142 /* Music that can be parsed without lookahead */
1143 closed_music:
1144         music_bare
1145         | complex_music_prefix closed_music
1146         {
1147                 $$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
1148         }
1149         | music_function_call_closed
1150         ;
1151
1152 music_bare:
1153         mode_changed_music
1154         | MUSIC_IDENTIFIER
1155         | grouped_music_list
1156         ;
1157
1158 grouped_music_list:
1159         simultaneous_music              { $$ = $1; }
1160         | sequential_music              { $$ = $1; }
1161         ;
1162
1163 /* An argument list. If a function \foo expects scm scm pitch, then the lexer expands \foo into the token sequence:
1164  MUSIC_FUNCTION EXPECT_PITCH EXPECT_SCM EXPECT_SCM EXPECT_NO_MORE_ARGS
1165 and this rule returns the reversed list of arguments. */
1166
1167 /* Function argument lists come in a number of flavors.  Whenever
1168  * LilyPond has to pick between different flavors, the decision is
1169  * either made because of tokens it has already seen, or it is
1170  * postponed until tokens suitable for making the decision come up.
1171  * For postponing decisions, it may be necessary that the competing
1172  * rules are written in a way making them compatible until a decision
1173  * can be made.  Sometimes this is done by putting common traits into
1174  * a separate "common" rule set.
1175  *
1176  * function_arglist: a full argument list.  Optional arguments at the
1177  * end of the list can only be skipped by writing \default in their
1178  * place.
1179  *
1180  * function_arglist_backup: an argument list ending in an optional
1181  * argument that may be skipped depending on its predicate.
1182  *
1183  * function_arglist_skip: an argument list _not_ ending in an optional
1184  * argument that is actually taken.
1185  *
1186  * function_arglist_nonbackup: an argument list ending in an optional
1187  * argument that may not be skipped because it is in end position and
1188  * has not been shortcircuited with \default.
1189  *
1190  * function_arglist* / function_arglist_closed*: The closed variants
1191  * don't end in simple music expressions that might still accept
1192  * things like a duration or a postevent.
1193  */
1194
1195 function_arglist_skip:
1196         function_arglist_common
1197         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_skip
1198         {
1199                 $$ = scm_cons ($1, $3);
1200         } %prec FUNCTION_ARGLIST
1201         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_skip
1202         {
1203                 $$ = scm_cons ($1, $3);
1204         } %prec FUNCTION_ARGLIST
1205         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip
1206         {
1207                 $$ = scm_cons ($1, $3);
1208         } %prec FUNCTION_ARGLIST
1209         ;
1210
1211
1212 function_arglist_nonbackup_common:
1213         EXPECT_OPTIONAL EXPECT_PITCH function_arglist pitch_also_in_chords {
1214                 $$ = scm_cons ($4, $3);
1215         }
1216         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_closed duration_length {
1217                 $$ = scm_cons ($4, $3);
1218         }
1219         | EXPECT_OPTIONAL EXPECT_SCM function_arglist FRACTION
1220         {
1221                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1222         }
1223         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed post_event_nofinger
1224         {
1225                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1226         }
1227         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed '-' UNSIGNED
1228         {
1229                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1230                 if (scm_is_true (scm_call_1 ($2, n)))
1231                         $$ = scm_cons (n, $3);
1232                 else {
1233                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1234                         t->set_property ("digit", $5);
1235                         $$ = check_scheme_arg (parser, @4, t->unprotect (),
1236                                                $3, $2, n);
1237                 }
1238                 
1239         }
1240         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed '-' REAL
1241         {
1242                 $$ = check_scheme_arg (parser, @4,
1243                                        scm_difference ($5, SCM_UNDEFINED),
1244                                        $3, $2);
1245         }
1246         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed '-' NUMBER_IDENTIFIER
1247         {
1248                 $$ = check_scheme_arg (parser, @4,
1249                                        scm_difference ($5, SCM_UNDEFINED),
1250                                        $3, $2);
1251         }
1252         ;
1253
1254 function_arglist_closed_nonbackup:
1255         function_arglist_nonbackup_common
1256         | EXPECT_OPTIONAL EXPECT_SCM function_arglist embedded_scm_arg_closed
1257         {
1258                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1259         }
1260         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed bare_number_closed
1261         {
1262                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1263         }
1264         | EXPECT_OPTIONAL EXPECT_SCM function_arglist SCM_IDENTIFIER
1265         {
1266                 $$ = check_scheme_arg (parser, @4,
1267                                        try_string_variants ($2, $4),
1268                                        $3, $2, $4);
1269         }
1270         | EXPECT_OPTIONAL EXPECT_SCM function_arglist STRING
1271         {
1272                 $$ = check_scheme_arg (parser, @4,
1273                                        try_string_variants ($2, $4),
1274                                        $3, $2, $4);
1275         }
1276         | EXPECT_OPTIONAL EXPECT_SCM function_arglist full_markup
1277         {
1278                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1279         }
1280         ;
1281
1282 symbol_list_arg:
1283         SYMBOL_LIST
1284         | SYMBOL_LIST '.' symbol_list_rev
1285         {
1286                 $$ = scm_append (scm_list_2 ($1, scm_reverse_x ($3, SCM_EOL)));
1287         }
1288         ;
1289
1290 symbol_list_rev:
1291         symbol_list_part
1292         | symbol_list_rev '.' symbol_list_part
1293         {
1294                 $$ = scm_append_x (scm_list_2 ($3, $1));
1295         }
1296         ;
1297
1298 // symbol_list_part delivers elements in reverse copy.
1299
1300 symbol_list_part:
1301         symbol_list_element
1302         {
1303                 SCM sym_l_p = ly_lily_module_constant ("symbol-list?");
1304                 $$ = try_string_variants (sym_l_p, $1);
1305                 if (SCM_UNBNDP ($$)) {
1306                         parser->parser_error (@1, _("not a symbol"));
1307                         $$ = SCM_EOL;
1308                 } else
1309                         $$ = scm_reverse ($$);
1310         }
1311         ;
1312
1313
1314 symbol_list_element:
1315         STRING
1316         | embedded_scm_bare
1317         ;
1318
1319
1320 function_arglist_nonbackup:
1321         function_arglist_nonbackup_common
1322         | EXPECT_OPTIONAL EXPECT_SCM function_arglist embedded_scm_arg
1323         {
1324                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1325         }
1326         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed bare_number
1327         {
1328                 $$ = check_scheme_arg (parser, @4, $4, $3, $2);
1329         }
1330         | function_arglist_nonbackup_reparse REPARSE SCM_ARG
1331         {
1332                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1333         }
1334         | function_arglist_nonbackup_reparse REPARSE lyric_element_music
1335         {
1336                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1337         }
1338         | function_arglist_nonbackup_reparse REPARSE symbol_list_arg
1339         {
1340                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1341         }
1342         ;
1343
1344 function_arglist_nonbackup_reparse:
1345         EXPECT_OPTIONAL EXPECT_SCM function_arglist SCM_IDENTIFIER
1346         {
1347                 $$ = $3;
1348                 SCM res = try_string_variants ($2, $4);
1349                 if (!SCM_UNBNDP (res))
1350                         if (scm_is_pair (res))
1351                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1352                         else
1353                                 MYREPARSE (@4, $2, SCM_ARG, res);
1354                 else if (scm_is_true
1355                          (scm_call_1
1356                           ($2, make_music_from_simple
1357                            (parser, @4, $4))))
1358                         MYREPARSE (@4, $2, STRING, $4);
1359                 else
1360                         MYREPARSE (@4, $2, SCM_ARG, $4);
1361         }
1362         | EXPECT_OPTIONAL EXPECT_SCM function_arglist STRING
1363         {
1364                 $$ = $3;
1365                 SCM res = try_string_variants ($2, $4);
1366                 if (!SCM_UNBNDP (res))
1367                         if (scm_is_pair (res))
1368                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1369                         else
1370                                 MYREPARSE (@4, $2, SCM_ARG, res);
1371                 else if (scm_is_true
1372                          (scm_call_1
1373                           ($2, make_music_from_simple
1374                            (parser, @4, $4))))
1375                         MYREPARSE (@4, $2, STRING, $4);
1376                 else
1377                         MYREPARSE (@4, $2, SCM_ARG, $4);
1378         }
1379         | EXPECT_OPTIONAL EXPECT_SCM function_arglist full_markup
1380         {
1381                 $$ = $3;
1382                 if (scm_is_true (scm_call_1 ($2, $4)))
1383                         MYREPARSE (@4, $2, SCM_ARG, $4);
1384                 else if (scm_is_true
1385                          (scm_call_1
1386                           ($2, make_music_from_simple
1387                            (parser, @4, $4))))
1388                         MYREPARSE (@4, $2, STRING, $4);
1389                 else
1390                         MYREPARSE (@4, $2, SCM_ARG, $4);
1391         }
1392         ;
1393
1394 function_arglist_keep:
1395         function_arglist_common
1396         | function_arglist_backup
1397         ;
1398
1399 function_arglist_closed_keep:
1400         function_arglist_closed_common
1401         | function_arglist_backup
1402         ;
1403
1404 function_arglist_backup:
1405         EXPECT_OPTIONAL EXPECT_SCM function_arglist_keep embedded_scm_arg_closed
1406         {
1407                 if (scm_is_true (scm_call_1 ($2, $4)))
1408                 {
1409                         $$ = scm_cons ($4, $3);
1410                 } else {
1411                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1412                         MYBACKUP (SCM_ARG, $4, @4);
1413                 }
1414         }
1415         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep post_event_nofinger
1416         {
1417                 if (scm_is_true (scm_call_1 ($2, $4)))
1418                 {
1419                         $$ = scm_cons ($4, $3);
1420                 } else {
1421                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1422                         MYBACKUP (EVENT_IDENTIFIER, $4, @4);
1423                 }
1424         }
1425         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_keep full_markup
1426         {
1427                 if (scm_is_true (scm_call_1 ($2, $4)))
1428                         $$ = scm_cons ($4, $3);
1429                 else {
1430                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1431                         MYBACKUP (LYRIC_ELEMENT, $4, @4);
1432                 }
1433         }
1434         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep UNSIGNED
1435         {
1436                 if (scm_is_true (scm_call_1 ($2, $4)))
1437                 {
1438                         $$ = $3;
1439                         MYREPARSE (@4, $2, UNSIGNED, $4);
1440                 } else {
1441                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1442                         MYBACKUP (UNSIGNED, $4, @4);
1443                 }
1444         }
1445         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep REAL
1446         {
1447                 if (scm_is_true (scm_call_1 ($2, $4)))
1448                 {
1449                         $$ = $3;
1450                         MYREPARSE (@4, $2, REAL, $4);
1451                 } else {
1452                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1453                         MYBACKUP (REAL, $4, @4);
1454                 }
1455         }
1456         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep NUMBER_IDENTIFIER
1457         {
1458                 if (scm_is_true (scm_call_1 ($2, $4)))
1459                 {
1460                         $$ = scm_cons ($4, $3);
1461                 } else {
1462                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1463                         MYBACKUP (NUMBER_IDENTIFIER, $4, @4);
1464                 }
1465         }
1466         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_keep FRACTION
1467         {
1468                 if (scm_is_true (scm_call_1 ($2, $4)))
1469                 {
1470                         $$ = scm_cons ($4, $3);
1471                 } else {
1472                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1473                         MYBACKUP (FRACTION, $4, @4);
1474                 }
1475         }
1476         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep '-' UNSIGNED
1477         {
1478                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1479                 if (scm_is_true (scm_call_1 ($2, n))) {
1480                         $$ = $3;
1481                         MYREPARSE (@5, $2, REAL, n);
1482                 } else {
1483                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @5);
1484                         t->set_property ("digit", $5);
1485                         $$ = t->unprotect ();
1486                         if (scm_is_true (scm_call_1 ($2, $$)))
1487                                 $$ = scm_cons ($$, $3);
1488                         else {
1489                                 $$ = scm_cons (loc_on_music (@3, $1), $3);
1490                                 MYBACKUP (UNSIGNED, $5, @5);
1491                                 parser->lexer_->push_extra_token ('-');
1492                         }
1493                 }
1494                 
1495         }
1496         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep '-' REAL
1497         {
1498                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1499                 if (scm_is_true (scm_call_1 ($2, n))) {
1500                         MYREPARSE (@5, $2, REAL, n);
1501                         $$ = $3;
1502                 } else {
1503                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1504                         MYBACKUP (REAL, n, @5);
1505                 }
1506         }
1507         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_closed_keep '-' NUMBER_IDENTIFIER
1508         {
1509                 SCM n = scm_difference ($5, SCM_UNDEFINED);
1510                 if (scm_is_true (scm_call_1 ($2, n))) {
1511                         $$ = scm_cons (n, $3);
1512                 } else {
1513                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1514                         MYBACKUP (NUMBER_IDENTIFIER, n, @5);
1515                 }
1516         }
1517         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_keep pitch_also_in_chords
1518         {
1519                 $$ = scm_cons ($4, $3);
1520         }
1521         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_closed_keep duration_length
1522         {
1523                 $$ = scm_cons ($4, $3);
1524         }
1525         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_keep SCM_IDENTIFIER
1526         {
1527                 SCM res = try_string_variants ($2, $4);
1528                 if (!SCM_UNBNDP (res))
1529                         if (scm_is_pair (res)) {
1530                                 $$ = $3;
1531                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1532                         }
1533                         else
1534                                 $$ = scm_cons (res, $3);
1535                 else {
1536                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1537                         MYBACKUP (SCM_IDENTIFIER, $4, @4);
1538                 }
1539         }
1540         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_keep STRING
1541         {
1542                 SCM res = try_string_variants ($2, $4);
1543                 if (!SCM_UNBNDP (res))
1544                         if (scm_is_pair (res)) {
1545                                 $$ = $3;
1546                                 MYREPARSE (@4, $2, SYMBOL_LIST, res);
1547                         }
1548                         else
1549                                 $$ = scm_cons (res, $3);
1550                 else {
1551                         $$ = scm_cons (loc_on_music (@3, $1), $3);
1552                         MYBACKUP (STRING, $4, @4);
1553                 }
1554         }
1555         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_backup BACKUP
1556         {
1557                 $$ = scm_cons ($1, $3);
1558                 MYBACKUP(0, SCM_UNDEFINED, @3);
1559         }
1560         | function_arglist_backup REPARSE bare_number
1561         {
1562                 $$ = check_scheme_arg (parser, @3,
1563                                        $3, $1, $2);
1564         }
1565         | function_arglist_backup REPARSE symbol_list_arg
1566         {
1567                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1568         }
1569         ;
1570
1571 function_arglist:
1572         function_arglist_common
1573         | function_arglist_nonbackup
1574         ;
1575
1576 function_arglist_common:
1577         function_arglist_bare
1578         | EXPECT_SCM function_arglist_optional embedded_scm_arg
1579         {
1580                 $$ = check_scheme_arg (parser, @3,
1581                                        $3, $2, $1);
1582         }
1583         | EXPECT_SCM function_arglist_closed_optional bare_number
1584         {
1585                 $$ = check_scheme_arg (parser, @3,
1586                                        $3, $2, $1);
1587         }
1588         | EXPECT_SCM function_arglist_optional FRACTION
1589         {
1590                 $$ = check_scheme_arg (parser, @3,
1591                                        $3, $2, $1);
1592         }
1593         | EXPECT_SCM function_arglist_closed_optional post_event_nofinger
1594         {
1595                 $$ = check_scheme_arg (parser, @3,
1596                                        $3, $2, $1);
1597         }
1598         | EXPECT_SCM function_arglist_closed_optional '-' NUMBER_IDENTIFIER
1599         {
1600                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1601                 $$ = check_scheme_arg (parser, @4, n, $2, $1);
1602         }
1603         | function_arglist_common_reparse REPARSE SCM_ARG
1604         {
1605                 $$ = check_scheme_arg (parser, @3,
1606                                        $3, $1, $2);
1607         }
1608         | function_arglist_common_reparse REPARSE lyric_element_music
1609         {
1610                 $$ = check_scheme_arg (parser, @3,
1611                                        $3, $1, $2);
1612         }
1613         | function_arglist_common_reparse REPARSE bare_number
1614         {
1615                 $$ = check_scheme_arg (parser, @3,
1616                                        $3, $1, $2);
1617         }
1618         | function_arglist_common_reparse REPARSE symbol_list_arg
1619         {
1620                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1621         }
1622         ;
1623
1624 function_arglist_common_reparse:
1625         EXPECT_SCM function_arglist_optional SCM_IDENTIFIER
1626         {
1627                 $$ = $2;
1628                 SCM res = try_string_variants ($1, $3);
1629                 if (!SCM_UNBNDP (res))
1630                         if (scm_is_pair (res))
1631                                 MYREPARSE (@3, $1, SYMBOL_LIST, res);
1632                         else
1633                                 MYREPARSE (@3, $1, SCM_ARG, res);
1634                 else if (scm_is_true
1635                          (scm_call_1
1636                           ($1, make_music_from_simple (parser, @3, $3))))
1637                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1638                 else
1639                         // This is going to flag a syntax error, we
1640                         // know the predicate to be false.
1641                         MYREPARSE (@3, $1, SCM_ARG, $3);
1642         }
1643         | EXPECT_SCM function_arglist_optional STRING
1644         {
1645                 $$ = $2;
1646                 SCM res = try_string_variants ($1, $3);
1647                 if (!SCM_UNBNDP (res))
1648                         if (scm_is_pair (res))
1649                                 MYREPARSE (@3, $1, SYMBOL_LIST, res);
1650                         else
1651                                 MYREPARSE (@3, $1, SCM_ARG, res);
1652                 else if (scm_is_true
1653                          (scm_call_1
1654                           ($1, make_music_from_simple (parser, @3, $3))))
1655                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1656                 else
1657                         // This is going to flag a syntax error, we
1658                         // know the predicate to be false.
1659                         MYREPARSE (@3, $1, SCM_ARG, $3);
1660         }
1661         | EXPECT_SCM function_arglist_optional full_markup
1662         {
1663                 $$ = $2;
1664                 if (scm_is_true (scm_call_1 ($1, $3)))
1665                         MYREPARSE (@3, $1, SCM_ARG, $3);
1666                 else if (scm_is_true
1667                          (scm_call_1
1668                           ($1, make_music_from_simple (parser, @3, $3))))
1669                         MYREPARSE (@3, $1, LYRIC_ELEMENT, $3);
1670                 else
1671                         // This is going to flag a syntax error, we
1672                         // know the predicate to be false.
1673                         MYREPARSE (@3, $1, SCM_ARG, $3);
1674         }
1675         | EXPECT_SCM function_arglist_closed_optional '-' UNSIGNED
1676         {
1677                 $$ = $2;
1678                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1679                 if (scm_is_true (scm_call_1 ($1, n)))
1680                         MYREPARSE (@4, $1, REAL, n);
1681                 else {
1682                         Music *t = MY_MAKE_MUSIC ("FingeringEvent", @4);
1683                         t->set_property ("digit", $4);
1684                         SCM m = t->unprotect ();
1685                         if (scm_is_true (scm_call_1 ($1, m)))
1686                                 MYREPARSE (@4, $1, SCM_ARG, m);
1687                         else
1688                                 MYREPARSE (@4, $1, SCM_ARG, $4);
1689                 }
1690                 
1691         }
1692         | EXPECT_SCM function_arglist_closed_optional '-' REAL
1693         {
1694                 $$ = $2;
1695                 SCM n = scm_difference ($4, SCM_UNDEFINED);
1696                 MYREPARSE (@4, $1, REAL, n);
1697         }
1698         ;
1699
1700 function_arglist_closed:
1701         function_arglist_closed_common
1702         | function_arglist_closed_nonbackup
1703         ;
1704
1705 function_arglist_closed_common:
1706         function_arglist_bare
1707         | EXPECT_SCM function_arglist_optional embedded_scm_arg_closed
1708         {
1709                 $$ = check_scheme_arg (parser, @3,
1710                                        $3, $2, $1);
1711         }
1712         | EXPECT_SCM function_arglist_closed_optional bare_number
1713         {
1714                 $$ = check_scheme_arg (parser, @3,
1715                                        $3, $2, $1);
1716         }
1717         | EXPECT_SCM function_arglist_closed_optional '-' NUMBER_IDENTIFIER
1718         {
1719                 $$ = check_scheme_arg (parser, @3,
1720                                        scm_difference ($4, SCM_UNDEFINED),
1721                                        $2, $1);
1722         }
1723         | EXPECT_SCM function_arglist_closed_optional post_event_nofinger
1724         {
1725                 $$ = check_scheme_arg (parser, @3,
1726                                        $3, $2, $1);
1727         }
1728         | EXPECT_SCM function_arglist_optional FRACTION
1729         {
1730                 $$ = check_scheme_arg (parser, @3,
1731                                        $3, $2, $1);
1732         }
1733         | function_arglist_common_reparse REPARSE SCM_ARG
1734         {
1735                 $$ = check_scheme_arg (parser, @3,
1736                                        $3, $1, $2);
1737         }
1738         | function_arglist_common_reparse REPARSE bare_number
1739         {
1740                 $$ = check_scheme_arg (parser, @3,
1741                                        $3, $1, $2);
1742         }
1743         | function_arglist_common_reparse REPARSE symbol_list_arg
1744         {
1745                 $$ = check_scheme_arg (parser, @3, $3, $1, $2);
1746         }
1747         ;
1748
1749 function_arglist_optional:
1750         function_arglist_keep %prec FUNCTION_ARGLIST
1751         | function_arglist_backup BACKUP
1752         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_optional
1753         {
1754                 $$ = scm_cons ($1, $3);
1755         }
1756         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_optional
1757         {
1758                 $$ = scm_cons ($1, $3);
1759         }
1760         ;
1761
1762 function_arglist_closed_optional:
1763         function_arglist_closed_keep %prec FUNCTION_ARGLIST
1764         | function_arglist_backup BACKUP
1765         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_closed_optional
1766         {
1767                 $$ = scm_cons ($1, $3);
1768         }
1769         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_closed_optional
1770         {
1771                 $$ = scm_cons ($1, $3);
1772         }
1773         ;
1774
1775 embedded_scm_closed:
1776         embedded_scm_bare
1777         | scm_function_call_closed
1778         ;
1779
1780 embedded_scm_arg_closed:
1781         embedded_scm_bare_arg
1782         | scm_function_call_closed
1783         | closed_music
1784         ;
1785
1786 scm_function_call_closed:
1787         SCM_FUNCTION function_arglist_closed {
1788                 $$ = MAKE_SYNTAX ("music-function", @$,
1789                                          $1, $2);
1790         } %prec FUNCTION_ARGLIST
1791         ;
1792
1793 function_arglist_bare:
1794         EXPECT_NO_MORE_ARGS {
1795                 $$ = SCM_EOL;
1796         }
1797         | EXPECT_PITCH function_arglist_optional pitch_also_in_chords {
1798                 $$ = scm_cons ($3, $2);
1799         }
1800         | EXPECT_DURATION function_arglist_closed_optional duration_length {
1801                 $$ = scm_cons ($3, $2);
1802         }
1803         | EXPECT_OPTIONAL EXPECT_PITCH function_arglist_skip DEFAULT {
1804                 $$ = scm_cons ($1, $3);
1805         }
1806         | EXPECT_OPTIONAL EXPECT_DURATION function_arglist_skip DEFAULT {
1807                 $$ = scm_cons ($1, $3);
1808         }
1809         | EXPECT_OPTIONAL EXPECT_SCM function_arglist_skip DEFAULT {
1810                 $$ = scm_cons ($1, $3);
1811         }
1812         ;
1813
1814 music_function_call:
1815         MUSIC_FUNCTION function_arglist {
1816                 $$ = MAKE_SYNTAX ("music-function", @$,
1817                                          $1, $2);
1818         }
1819         ;
1820
1821
1822 optional_id:
1823         /**/ { $$ = SCM_EOL; }
1824         | '=' simple_string {
1825                 $$ = $2;
1826         }
1827         ;
1828
1829 complex_music:
1830         music_function_call
1831         | repeated_music                { $$ = $1; }
1832         | re_rhythmed_music     { $$ = $1; }
1833         | complex_music_prefix music
1834         {
1835                 $$ = FINISH_MAKE_SYNTAX ($1, @$, $2);
1836         }
1837         ;
1838
1839 complex_music_prefix:
1840         CONTEXT simple_string optional_id optional_context_mod {
1841                 Context_mod *ctxmod = unsmob_context_mod ($4);
1842                 SCM mods = SCM_EOL;
1843                 if (ctxmod)
1844                         mods = ctxmod->get_mods ();
1845                 $$ = START_MAKE_SYNTAX ("context-specification", $2, $3, mods, SCM_BOOL_F);
1846         }
1847         | NEWCONTEXT simple_string optional_id optional_context_mod {
1848                 Context_mod *ctxmod = unsmob_context_mod ($4);
1849                 SCM mods = SCM_EOL;
1850                 if (ctxmod)
1851                         mods = ctxmod->get_mods ();
1852                 $$ = START_MAKE_SYNTAX ("context-specification", $2, $3, mods, SCM_BOOL_T);
1853         }
1854         ;
1855
1856 mode_changed_music:
1857         mode_changing_head grouped_music_list {
1858                 if ($1 == ly_symbol2scm ("chords"))
1859                 {
1860                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $2);
1861                 }
1862                 else
1863                 {
1864                   $$ = $2;
1865                 }
1866                 parser->lexer_->pop_state ();
1867         }
1868         | mode_changing_head_with_context optional_context_mod grouped_music_list {
1869                 Context_mod *ctxmod = unsmob_context_mod ($2);
1870                 SCM mods = SCM_EOL;
1871                 if (ctxmod)
1872                         mods = ctxmod->get_mods ();
1873                 $$ = MAKE_SYNTAX ("context-specification", @$, $1, SCM_EOL, mods, SCM_BOOL_T, $3);
1874                 if ($1 == ly_symbol2scm ("ChordNames"))
1875                 {
1876                   $$ = MAKE_SYNTAX ("unrelativable-music", @$, $$);
1877                 }
1878                 parser->lexer_->pop_state ();
1879         }
1880         ;
1881
1882 mode_changing_head:
1883         NOTEMODE {
1884                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
1885                 parser->lexer_->push_note_state (nn);
1886
1887                 $$ = ly_symbol2scm ("notes");
1888         }
1889         | DRUMMODE
1890                 {
1891                 SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
1892                 parser->lexer_->push_note_state (nn);
1893
1894                 $$ = ly_symbol2scm ("drums");
1895         }
1896         | FIGUREMODE {
1897                 parser->lexer_->push_figuredbass_state ();
1898
1899                 $$ = ly_symbol2scm ("figures");
1900         }
1901         | CHORDMODE {
1902                 SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
1903                 parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
1904                 nn = parser->lexer_->lookup_identifier ("pitchnames");
1905                 parser->lexer_->push_chord_state (nn);
1906                 $$ = ly_symbol2scm ("chords");
1907
1908         }
1909         | LYRICMODE
1910                 { parser->lexer_->push_lyric_state ();
1911                 $$ = ly_symbol2scm ("lyrics");
1912         }
1913         ;
1914
1915 mode_changing_head_with_context:
1916         DRUMS {
1917                 SCM nn = parser->lexer_->lookup_identifier ("drumPitchNames");
1918                 parser->lexer_->push_note_state (nn);
1919
1920                 $$ = ly_symbol2scm ("DrumStaff");
1921         }
1922         | FIGURES {
1923                 parser->lexer_->push_figuredbass_state ();
1924
1925                 $$ = ly_symbol2scm ("FiguredBass");
1926         }
1927         | CHORDS {
1928                 SCM nn = parser->lexer_->lookup_identifier ("chordmodifiers");
1929                 parser->lexer_->chordmodifier_tab_ = alist_to_hashq (nn);
1930                 nn = parser->lexer_->lookup_identifier ("pitchnames");
1931                 parser->lexer_->push_chord_state (nn);
1932                 $$ = ly_symbol2scm ("ChordNames");
1933         }
1934         | LYRICS
1935                 { parser->lexer_->push_lyric_state ();
1936                 $$ = ly_symbol2scm ("Lyrics");
1937         }
1938         ;
1939
1940 new_lyrics:
1941         ADDLYRICS { parser->lexer_->push_lyric_state (); }
1942         /*cont */
1943         composite_music {
1944         /* Can also use music at the expensive of two S/Rs similar to
1945            \repeat \alternative */
1946                 parser->lexer_->pop_state ();
1947
1948                 $$ = scm_cons ($3, SCM_EOL);
1949         }
1950         | new_lyrics ADDLYRICS {
1951                 parser->lexer_->push_lyric_state ();
1952         } composite_music {
1953                 parser->lexer_->pop_state ();
1954                 $$ = scm_cons ($4, $1);
1955         }
1956         ;
1957
1958 re_rhythmed_music:
1959         composite_music new_lyrics {
1960                 $$ = MAKE_SYNTAX ("add-lyrics", @$, $1, scm_reverse_x ($2, SCM_EOL));
1961         } %prec COMPOSITE
1962         | LYRICSTO simple_string {
1963                 parser->lexer_->push_lyric_state ();
1964         } music {
1965                 parser->lexer_->pop_state ();
1966                 $$ = MAKE_SYNTAX ("lyric-combine", @$, $2, $4);
1967         }
1968         ;
1969
1970 context_change:
1971         CHANGE STRING '=' STRING  {
1972                 $$ = MAKE_SYNTAX ("context-change", @$, scm_string_to_symbol ($2), $4);
1973         }
1974         ;
1975
1976
1977 property_path:
1978         symbol_list_rev  {
1979                 $$ = scm_reverse_x ($1, SCM_EOL);
1980         }
1981         | symbol_list_rev property_path {
1982                 $$ = scm_reverse_x ($1, $2);
1983         }
1984         ;
1985
1986 property_operation:
1987         STRING '=' scalar {
1988                 $$ = scm_list_3 (ly_symbol2scm ("assign"),
1989                         scm_string_to_symbol ($1), $3);
1990         }
1991         | UNSET simple_string {
1992                 $$ = scm_list_2 (ly_symbol2scm ("unset"),
1993                         scm_string_to_symbol ($2));
1994         }
1995         | OVERRIDE property_path '=' scalar {
1996                 if (scm_ilength ($2) < 2) {
1997                         parser->parser_error (@2, _("bad grob property path"));
1998                         $$ = SCM_UNDEFINED;
1999                 } else {
2000                         $$ = scm_cons (ly_symbol2scm ("push"),
2001                                        scm_cons2 (scm_car ($2),
2002                                                   $4,
2003                                                   scm_cdr ($2)));
2004                 }
2005         }
2006         | REVERT revert_arg {
2007                 $$ = scm_cons (ly_symbol2scm ("pop"), $2);
2008         }
2009         ;
2010
2011 // This is all quite awkward for the sake of substantial backward
2012 // compatibility while at the same time allowing a more "natural" form
2013 // of specification not separating grob specification from grob
2014 // property path.  The purpose of this definition of revert_arg is to
2015 // allow the symbol list which specifies grob and property to revert
2016 // to be optionally be split into two parts after the grob (which in
2017 // this case is just the first element of the list).  symbol_list_part
2018 // is only one path component, but it can be parsed without lookahead,
2019 // so we can follow it with a synthetic BACKUP token when needed.  If
2020 // the first symbol_list_part already contains multiple elements (only
2021 // possible if a Scheme expression provides them), we just parse for
2022 // additional elements introduced by '.', which is what the
2023 // SYMBOL_LIST backup in connection with the immediately following
2024 // rule using symbol_list_arg does.
2025 //
2026 // As long as we don't have our coffers filled with both grob and at
2027 // least one grob property specification, the rest of the required
2028 // symbol list chain may be provided either with or without a leading
2029 // dot.  This is for both allowing the traditional
2030 // \revert Accidental #'color
2031 // as well as well as the "naive" form
2032 // \revert Accidental.color
2033
2034 revert_arg:
2035         revert_arg_part
2036         {
2037                 if (scm_is_null ($1)
2038                     || scm_is_null (scm_cdr ($1)))
2039                         MYBACKUP (SCM_ARG, $1, @1);
2040                 else
2041                         MYBACKUP (SYMBOL_LIST, scm_reverse_x ($1, SCM_EOL), @1);
2042         }
2043         | revert_arg BACKUP symbol_list_arg
2044         {
2045                 $$ = $3;
2046         }
2047         ;
2048
2049 // revert_arg_part delivers results in reverse
2050 revert_arg_part:
2051         symbol_list_part
2052         | revert_arg BACKUP SCM_ARG '.' symbol_list_part
2053         {
2054                 $$ = scm_append_x (scm_list_2 ($5, $3));
2055         }
2056         | revert_arg BACKUP SCM_ARG symbol_list_part
2057         {
2058                 $$ = scm_append_x (scm_list_2 ($4, $3));
2059         }               
2060         ;
2061
2062 context_def_mod:
2063         CONSISTS { $$ = ly_symbol2scm ("consists"); }
2064         | REMOVE { $$ = ly_symbol2scm ("remove"); }
2065
2066         | ACCEPTS { $$ = ly_symbol2scm ("accepts"); }
2067         | DEFAULTCHILD { $$ = ly_symbol2scm ("default-child"); }
2068         | DENIES { $$ = ly_symbol2scm ("denies"); }
2069
2070         | ALIAS { $$ = ly_symbol2scm ("alias"); }
2071         | TYPE { $$ = ly_symbol2scm ("translator-type"); }
2072         | DESCRIPTION { $$ = ly_symbol2scm ("description"); }
2073         | NAME { $$ = ly_symbol2scm ("context-name"); }
2074         ;
2075
2076 context_mod:
2077         property_operation { $$ = $1; }
2078         | context_def_mod STRING {
2079                 $$ = scm_list_2 ($1, $2);
2080         }
2081         | context_def_mod embedded_scm
2082         {
2083                 if (!scm_is_string ($2)
2084                     && ly_symbol2scm ("consists") != $1
2085                     && ly_symbol2scm ("remove") != $1)
2086                 {
2087                         $$ = SCM_EOL;
2088                         parser->parser_error (@1, _ ("only \\consists and \\remove take non-string argument."));
2089                 }
2090                 else
2091                 {
2092                         $$ = scm_list_2 ($1, $2);
2093                 }
2094         }
2095         ;
2096
2097 // If defined, at least two members.
2098 grob_prop_spec:
2099         symbol_list_rev
2100         {
2101                 SCM l = scm_reverse_x ($1, SCM_EOL);
2102                 if (scm_is_pair (l)
2103                     && to_boolean
2104                     (scm_object_property (scm_car (l),
2105                                           ly_symbol2scm ("is-grob?"))))
2106                         l = scm_cons (ly_symbol2scm ("Bottom"), l);
2107                 if (scm_is_null (l) || scm_is_null (scm_cdr (l))) {
2108                         parser->parser_error (@1, _ ("bad grob property path"));
2109                         l = SCM_UNDEFINED;
2110                 }
2111                 $$ = l;
2112         }
2113         ;
2114
2115 // If defined, at least three members
2116 grob_prop_path:
2117         grob_prop_spec
2118         {
2119                 if (!SCM_UNBNDP ($1) && scm_is_null (scm_cddr ($1)))
2120                 {
2121                         parser->parser_error (@1, _ ("bad grob property path"));
2122                         $$ = SCM_UNDEFINED;
2123                 }
2124         }
2125         | grob_prop_spec property_path
2126         {
2127                 if (!SCM_UNBNDP ($1)) {
2128                         $$ = scm_append_x (scm_list_2 ($1, $2));
2129                         if (scm_is_null (scm_cddr ($$))) {
2130                                 parser->parser_error (@$, _ ("bad grob property path"));
2131                                 $$ = SCM_UNDEFINED;
2132                         }
2133                 }
2134
2135         }
2136         ;
2137
2138 // Exactly two elements or undefined
2139 context_prop_spec:
2140         symbol_list_rev
2141         {
2142                 SCM l = scm_reverse_x ($1, SCM_EOL);
2143                 switch (scm_ilength (l)) {
2144                 case 1:
2145                         l = scm_cons (ly_symbol2scm ("Bottom"), l);
2146                 case 2:
2147                         break;
2148                 default:
2149                         parser->parser_error (@1, _ ("bad context property path"));
2150                         l = SCM_UNDEFINED;
2151                 }
2152                 $$ = l;
2153         }
2154         ;
2155
2156 simple_music_property_def:
2157         OVERRIDE grob_prop_path '=' scalar {
2158                 if (SCM_UNBNDP ($2))
2159                         $$ = SCM_UNDEFINED;
2160                 else {
2161                         $$ = scm_list_5 (scm_car ($2),
2162                                          ly_symbol2scm ("OverrideProperty"),
2163                                          scm_cadr ($2),
2164                                          $4,
2165                                          scm_cddr ($2));
2166                 }
2167         }
2168         | REVERT simple_revert_context revert_arg {
2169                 $$ = scm_list_4 ($2,
2170                                  ly_symbol2scm ("RevertProperty"),
2171                                  scm_car ($3),
2172                                  scm_cdr ($3));
2173         }
2174         | SET context_prop_spec '=' scalar {
2175                 if (SCM_UNBNDP ($2))
2176                         $$ = SCM_UNDEFINED;
2177                 else
2178                         $$ = scm_list_4 (scm_car ($2),
2179                                          ly_symbol2scm ("PropertySet"),
2180                                          scm_cadr ($2),
2181                                          $4);
2182         }
2183         | UNSET context_prop_spec {
2184                 if (SCM_UNBNDP ($2))
2185                         $$ = SCM_UNDEFINED;
2186                 else
2187                         $$ = scm_list_3 (scm_car ($2),
2188                                          ly_symbol2scm ("PropertyUnset"),
2189                                          scm_cadr ($2));
2190         }
2191         ;
2192
2193
2194 // This is all quite awkward for the sake of substantial backward
2195 // compatibility while at the same time allowing a more "natural" form
2196 // of specification not separating grob specification from grob
2197 // property path.  The purpose of this definition of
2198 // simple_revert_context is to allow the symbol list which specifies
2199 // grob and property to revert to be optionally be split into two
2200 // parts after the grob (which may be preceded by a context
2201 // specification, a case which we distinguish by checking whether the
2202 // first symbol is a valid grob symbol instead).
2203 //
2204 // See revert_arg above for the main work horse of this arrangement.
2205 // simple_revert_context just caters for the context and delegates the
2206 // rest of the job to revert_arg.
2207
2208 simple_revert_context:
2209         symbol_list_part
2210         {
2211                 $1 = scm_reverse_x ($1, SCM_EOL);
2212                 if (scm_is_null ($1)
2213                     || to_boolean
2214                     (scm_object_property (scm_car ($1),
2215                                           ly_symbol2scm ("is-grob?")))) {
2216                         $$ = ly_symbol2scm ("Bottom");
2217                         parser->lexer_->push_extra_token (SCM_IDENTIFIER, $1);
2218                 } else {
2219                         $$ = scm_car ($1);
2220                         parser->lexer_->push_extra_token (SCM_IDENTIFIER,
2221                                                           scm_cdr ($1));
2222                 }
2223         }
2224         ;
2225
2226 music_property_def:
2227         simple_music_property_def {
2228                 if (SCM_UNBNDP ($1))
2229                         $$ = MAKE_SYNTAX ("void-music", @1);
2230                 else
2231                         $$ = LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("property-operation"), scm_cons2 (parser->self_scm (), make_input (@$), $1));
2232         }
2233         ;
2234
2235 string:
2236         STRING {
2237                 $$ = $1;
2238         }
2239         | full_markup
2240         | string '+' string {
2241                 if (!scm_is_string ($1)) {
2242                         parser->parser_error (@1, (_ ("simple string expected")));
2243                         $1 = scm_string (SCM_EOL);
2244                 }
2245                 if (!scm_is_string ($3)) {
2246                         parser->parser_error (@3, (_ ("simple string expected")));
2247                         $3 = scm_string (SCM_EOL);
2248                 }
2249                 $$ = scm_string_append (scm_list_2 ($1, $3));
2250         }
2251         ;
2252
2253 simple_string: STRING {
2254                 $$ = $1;
2255         }
2256         | embedded_scm_bare
2257         {
2258                 if (scm_is_string ($1)) {
2259                         $$ = $1;
2260                 } else {
2261                         parser->parser_error (@1, (_ ("simple string expected")));
2262                         $$ = scm_string (SCM_EOL);
2263                 }
2264         }
2265         ;
2266
2267 scalar:
2268         embedded_scm_arg
2269         | SCM_IDENTIFIER
2270         | bare_number
2271         | FRACTION
2272         | lyric_element
2273         ;
2274
2275 scalar_closed:
2276         embedded_scm_arg_closed
2277         | SCM_IDENTIFIER
2278         | bare_number
2279         | FRACTION
2280         | lyric_element
2281         ;
2282
2283
2284 event_chord:
2285         simple_element post_events {
2286                 // Let the rhythmic music iterator sort this mess out.
2287                 if (scm_is_pair ($2)) {
2288                         $$ = make_music_from_simple (parser, @1, $1);
2289                         if (unsmob_music ($$))
2290                                 unsmob_music ($$)->set_property ("articulations",
2291                                                                  scm_reverse_x ($2, SCM_EOL));
2292                         else
2293                                 parser->parser_error (@1, _("music expected"));
2294                 }
2295         }
2296         | simple_chord_elements post_events     {
2297                 SCM elts = ly_append2 ($1, scm_reverse_x ($2, SCM_EOL));
2298
2299                 Input i;
2300                 /* why is this giving wrong start location? -ns
2301                  * i = @$; */
2302                 i.set_location (@1, @2);
2303                 $$ = MAKE_SYNTAX ("event-chord", i, elts);
2304         }
2305         | CHORD_REPETITION optional_notemode_duration post_events {
2306                 Input i;
2307                 i.set_location (@1, @3);
2308                 $$ = MAKE_SYNTAX ("repetition-chord", i,
2309                                   $2, scm_reverse_x ($3, SCM_EOL));
2310         }
2311         | MULTI_MEASURE_REST optional_notemode_duration post_events {
2312                 Input i;
2313                 i.set_location (@1, @3);
2314                 $$ = MAKE_SYNTAX ("multi-measure-rest", i, $2,
2315                                   scm_reverse_x ($3, SCM_EOL));
2316         }
2317         | command_element
2318         | note_chord_element
2319         ;
2320
2321
2322 note_chord_element:
2323         chord_body optional_notemode_duration post_events
2324         {
2325                 Music *m = unsmob_music ($1);
2326                 SCM dur = unsmob_duration ($2)->smobbed_copy ();
2327                 SCM es = m->get_property ("elements");
2328                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
2329
2330                 for (SCM s = es; scm_is_pair (s); s = scm_cdr (s))
2331                   unsmob_music (scm_car (s))->set_property ("duration", dur);
2332                 es = ly_append2 (es, postevs);
2333
2334                 m-> set_property ("elements", es);
2335                 m->set_spot (@$);
2336                 $$ = m->self_scm ();
2337         }
2338         ;
2339
2340 chord_body:
2341         ANGLE_OPEN chord_body_elements ANGLE_CLOSE
2342         {
2343                 $$ = MAKE_SYNTAX ("event-chord", @$, scm_reverse_x ($2, SCM_EOL));
2344         }
2345         ;
2346
2347 chord_body_elements:
2348         /* empty */             { $$ = SCM_EOL; }
2349         | chord_body_elements chord_body_element {
2350                 if (!SCM_UNBNDP ($2))
2351                         $$ = scm_cons ($2, $1);
2352         }
2353         ;
2354
2355 chord_body_element:
2356         pitch exclamations questions octave_check post_events
2357         {
2358                 bool q = to_boolean ($3);
2359                 bool ex = to_boolean ($2);
2360                 SCM check = $4;
2361                 SCM post = $5;
2362
2363                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2364                 n->set_property ("pitch", $1);
2365                 if (q)
2366                         n->set_property ("cautionary", SCM_BOOL_T);
2367                 if (ex || q)
2368                         n->set_property ("force-accidental", SCM_BOOL_T);
2369
2370                 if (scm_is_pair (post)) {
2371                         SCM arts = scm_reverse_x (post, SCM_EOL);
2372                         n->set_property ("articulations", arts);
2373                 }
2374                 if (scm_is_number (check))
2375                 {
2376                         int q = scm_to_int (check);
2377                         n->set_property ("absolute-octave", scm_from_int (q-1));
2378                 }
2379
2380                 $$ = n->unprotect ();
2381         }
2382         | DRUM_PITCH post_events {
2383                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2384                 n->set_property ("drum-type", $1);
2385
2386                 if (scm_is_pair ($2)) {
2387                         SCM arts = scm_reverse_x ($2, SCM_EOL);
2388                         n->set_property ("articulations", arts);
2389                 }
2390                 $$ = n->unprotect ();
2391         }
2392         | music_function_chord_body
2393         {
2394                 Music *m = unsmob_music ($1);
2395
2396                 while (m && m->is_mus_type ("music-wrapper-music")) {
2397                         $$ = m->get_property ("element");
2398                         m = unsmob_music ($$);
2399                 }
2400
2401                 if (!(m && m->is_mus_type ("rhythmic-event"))) {
2402                         parser->parser_error (@$, _ ("not a rhythmic event"));
2403                         $$ = SCM_UNDEFINED;
2404                 }
2405         }
2406         ;
2407
2408 music_function_chord_body:
2409         music_function_call
2410         | MUSIC_IDENTIFIER
2411         ;
2412
2413 // Event functions may only take closed arglists, otherwise it would
2414 // not be clear whether a following postevent should be associated
2415 // with the last argument of the event function or with the expression
2416 // for which the function call acts itself as event.
2417
2418 music_function_call_closed:
2419         MUSIC_FUNCTION function_arglist_closed {
2420                 $$ = MAKE_SYNTAX ("music-function", @$,
2421                                          $1, $2);
2422         }
2423         ;
2424
2425 event_function_event:
2426         EVENT_FUNCTION function_arglist_closed {
2427                 $$ = MAKE_SYNTAX ("music-function", @$,
2428                                          $1, $2);
2429         }
2430         ;
2431
2432 command_element:
2433         command_event {
2434                 $$ = $1;
2435         }
2436         | E_BRACKET_OPEN {
2437                 Music *m = MY_MAKE_MUSIC ("LigatureEvent", @$);
2438                 m->set_property ("span-direction", scm_from_int (START));
2439                 $$ = m->unprotect();
2440         }
2441         | E_BRACKET_CLOSE {
2442                 Music *m = MY_MAKE_MUSIC ("LigatureEvent", @$);
2443                 m->set_property ("span-direction", scm_from_int (STOP));
2444                 $$ = m->unprotect ();
2445         }
2446         | E_BACKSLASH {
2447                 $$ = MAKE_SYNTAX ("voice-separator", @$);
2448         }
2449         | '|'      {
2450                 SCM pipe = parser->lexer_->lookup_identifier ("pipeSymbol");
2451
2452                 Music *m = unsmob_music (pipe);
2453                 if (m)
2454                 {
2455                         m = m->clone ();
2456                         m->set_spot (@$);
2457                         $$ = m->unprotect ();
2458                 }
2459                 else
2460                         $$ = MAKE_SYNTAX ("bar-check", @$);
2461
2462         }
2463         ;
2464
2465 command_event:
2466         E_TILDE {
2467                 $$ = MY_MAKE_MUSIC ("PesOrFlexaEvent", @$)->unprotect ();
2468         }
2469         | tempo_event {
2470                 $$ = $1;
2471         }
2472         ;
2473
2474
2475 post_events:
2476         /* empty */ {
2477                 $$ = SCM_EOL;
2478         }
2479         | post_events post_event {
2480                 unsmob_music ($2)->set_spot (@2);
2481                 $$ = scm_cons ($2, $$);
2482         }
2483         ;
2484
2485 post_event_nofinger:
2486         direction_less_event {
2487                 $$ = $1;
2488         }
2489         | script_dir music_function_call_closed {
2490                 $$ = $2;
2491                 if (!SCM_UNBNDP ($1))
2492                 {
2493                         unsmob_music ($$)->set_property ("direction", $1);
2494                 }
2495         }
2496         | HYPHEN {
2497                 if (!parser->lexer_->is_lyric_state ())
2498                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2499                 $$ = MY_MAKE_MUSIC ("HyphenEvent", @$)->unprotect ();
2500         }
2501         | EXTENDER {
2502                 if (!parser->lexer_->is_lyric_state ())
2503                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2504                 $$ = MY_MAKE_MUSIC ("ExtenderEvent", @$)->unprotect ();
2505         }
2506         | script_dir direction_reqd_event {
2507                 if (!SCM_UNBNDP ($1))
2508                 {
2509                         Music *m = unsmob_music ($2);
2510                         m->set_property ("direction", $1);
2511                 }
2512                 $$ = $2;
2513         }
2514         | script_dir direction_less_event {
2515                 if (!SCM_UNBNDP ($1))
2516                 {
2517                         Music *m = unsmob_music ($2);
2518                         m->set_property ("direction", $1);
2519                 }
2520                 $$ = $2;
2521         }
2522         | '^' fingering
2523         {
2524                 $$ = $2;
2525                 unsmob_music ($$)->set_property ("direction", scm_from_int (UP));
2526         }
2527         | '_' fingering
2528         {
2529                 $$ = $2;
2530                 unsmob_music ($$)->set_property ("direction", scm_from_int (DOWN));
2531         }                       
2532         ;
2533
2534 post_event:
2535         post_event_nofinger
2536         | '-' fingering {
2537                 $$ = $2;
2538         }
2539         ;
2540
2541 string_number_event:
2542         E_UNSIGNED {
2543                 Music *s = MY_MAKE_MUSIC ("StringNumberEvent", @$);
2544                 s->set_property ("string-number", $1);
2545                 $$ = s->unprotect ();
2546         }
2547         ;
2548
2549 direction_less_char:
2550         '['  {
2551                 $$ = ly_symbol2scm ("bracketOpenSymbol");
2552         }
2553         | ']'  {
2554                 $$ = ly_symbol2scm ("bracketCloseSymbol");
2555         }
2556         | '~'  {
2557                 $$ = ly_symbol2scm ("tildeSymbol");
2558         }
2559         | '('  {
2560                 $$ = ly_symbol2scm ("parenthesisOpenSymbol");
2561         }
2562         | ')'  {
2563                 $$ = ly_symbol2scm ("parenthesisCloseSymbol");
2564         }
2565         | E_EXCLAMATION  {
2566                 $$ = ly_symbol2scm ("escapedExclamationSymbol");
2567         }
2568         | E_OPEN  {
2569                 $$ = ly_symbol2scm ("escapedParenthesisOpenSymbol");
2570         }
2571         | E_CLOSE  {
2572                 $$ = ly_symbol2scm ("escapedParenthesisCloseSymbol");
2573         }
2574         | E_ANGLE_CLOSE  {
2575                 $$ = ly_symbol2scm ("escapedBiggerSymbol");
2576         }
2577         | E_ANGLE_OPEN  {
2578                 $$ = ly_symbol2scm ("escapedSmallerSymbol");
2579         }
2580         ;
2581
2582 direction_less_event:
2583         direction_less_char {
2584                 SCM predefd = parser->lexer_->lookup_identifier_symbol ($1);
2585                 Music *m = 0;
2586                 if (unsmob_music (predefd))
2587                 {
2588                         m = unsmob_music (predefd)->clone ();
2589                         m->set_spot (@$);
2590                 }
2591                 else
2592                 {
2593                         m = MY_MAKE_MUSIC ("Music", @$);
2594                 }
2595                 $$ = m->unprotect ();
2596         }
2597         | string_number_event
2598         | EVENT_IDENTIFIER      {
2599                 $$ = $1;
2600         }
2601         | tremolo_type  {
2602                Music *a = MY_MAKE_MUSIC ("TremoloEvent", @$);
2603                a->set_property ("tremolo-type", $1);
2604                $$ = a->unprotect ();
2605         }
2606         | event_function_event  
2607         ;
2608
2609 direction_reqd_event:
2610         gen_text_def {
2611                 $$ = $1;
2612         }
2613         | script_abbreviation {
2614                 SCM s = parser->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
2615                 Music *a = MY_MAKE_MUSIC ("ArticulationEvent", @$);
2616                 if (scm_is_string (s))
2617                         a->set_property ("articulation-type", s);
2618                 else parser->parser_error (@1, _ ("expecting string as script definition"));
2619                 $$ = a->unprotect ();
2620         }
2621         ;
2622
2623 octave_check:
2624         /**/ { $$ = SCM_EOL; }
2625         | '=' quotes { $$ = $2; }
2626         ;
2627
2628 quotes:
2629         /* empty */
2630         {
2631                 $$ = SCM_INUM0;
2632         }
2633         | sub_quotes
2634         | sup_quotes
2635         ;
2636
2637 sup_quotes:
2638         '\'' {
2639                 $$ = scm_from_int (1);
2640         }
2641         | sup_quotes '\'' {
2642                 $$ = scm_oneplus ($1);
2643         }
2644         ;
2645
2646 sub_quotes:
2647         ',' {
2648                 $$ = scm_from_int (-1);
2649         }
2650         | sub_quotes ',' {
2651                 $$ = scm_oneminus ($1);
2652         }
2653         ;
2654
2655 steno_pitch:
2656         NOTENAME_PITCH quotes {
2657                 if (!scm_is_eq (SCM_INUM0, $2))
2658                 {
2659                         Pitch p = *unsmob_pitch ($1);
2660                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2661                         $$ = p.smobbed_copy ();
2662                 }
2663         }
2664         ;
2665
2666 /*
2667 ugh. duplication
2668 */
2669
2670 steno_tonic_pitch:
2671         TONICNAME_PITCH quotes {
2672                 if (!scm_is_eq (SCM_INUM0, $2))
2673                 {
2674                         Pitch p = *unsmob_pitch ($1);
2675                         p = p.transposed (Pitch (scm_to_int ($2),0,0));
2676                         $$ = p.smobbed_copy ();
2677                 }
2678         }
2679         ;
2680
2681 pitch:
2682         steno_pitch
2683         | PITCH_IDENTIFIER
2684         ;
2685
2686 pitch_also_in_chords:
2687         pitch
2688         | steno_tonic_pitch
2689         ;
2690
2691 gen_text_def:
2692         full_markup {
2693                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2694                 t->set_property ("text", $1);
2695                 $$ = t->unprotect ();
2696         }
2697         | STRING {
2698                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2699                 t->set_property ("text",
2700                         make_simple_markup ($1));
2701                 $$ = t->unprotect ();
2702         }
2703         | embedded_scm_closed
2704         {
2705                 Music *m = unsmob_music ($1);
2706                 if (m && m->is_mus_type ("post-event"))
2707                         $$ = $1;
2708                 else if (Text_interface::is_markup ($1)) {
2709                         Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2710                         t->set_property ("text", $1);
2711                         $$ = t->unprotect ();
2712                 } else
2713                         parser->parser_error (@1, _ ("not an articulation"));
2714         }
2715         ;
2716
2717 fingering:
2718         UNSIGNED {
2719                 Music *t = MY_MAKE_MUSIC ("FingeringEvent", @$);
2720                 t->set_property ("digit", $1);
2721                 $$ = t->unprotect ();
2722         }
2723         ;
2724
2725 script_abbreviation:
2726         '^'             {
2727                 $$ = scm_from_locale_string ("Hat");
2728         }
2729         | '+'           {
2730                 $$ = scm_from_locale_string ("Plus");
2731         }
2732         | '-'           {
2733                 $$ = scm_from_locale_string ("Dash");
2734         }
2735         | '|'           {
2736                 $$ = scm_from_locale_string ("Bar");
2737         }
2738         | ANGLE_CLOSE   {
2739                 $$ = scm_from_locale_string ("Larger");
2740         }
2741         | '.'           {
2742                 $$ = scm_from_locale_string ("Dot");
2743         }
2744         | '_' {
2745                 $$ = scm_from_locale_string ("Underscore");
2746         }
2747         ;
2748
2749 script_dir:
2750         '_'     { $$ = scm_from_int (DOWN); }
2751         | '^'   { $$ = scm_from_int (UP); }
2752         | '-'   { $$ = SCM_UNDEFINED; }
2753         ;
2754
2755 duration_length:
2756         multiplied_duration {
2757                 $$ = $1;
2758         }
2759         ;
2760
2761 maybe_notemode_duration:
2762         {
2763                 $$ = SCM_UNDEFINED;
2764         }
2765         | multiplied_duration   {
2766                 $$ = $1;
2767                 parser->default_duration_ = *unsmob_duration ($$);
2768         }
2769 ;
2770
2771
2772 optional_notemode_duration:
2773         maybe_notemode_duration
2774         {
2775                 if (SCM_UNBNDP ($$))
2776                         $$ = parser->default_duration_.smobbed_copy ();
2777         }
2778         ;
2779
2780 steno_duration:
2781         UNSIGNED dots           {
2782                 int len = 0;
2783                 int n = scm_to_int ($1);
2784                 if (!is_duration (n))
2785                         parser->parser_error (@1, _f ("not a duration: %d", n));
2786                 else
2787                         len = intlog2 (n);
2788
2789                 $$ = Duration (len, scm_to_int ($2)).smobbed_copy ();
2790         }
2791         | DURATION_IDENTIFIER dots      {
2792                 Duration *d = unsmob_duration ($1);
2793                 Duration k (d->duration_log (),
2794                             d->dot_count () + scm_to_int ($2));
2795                 k = k.compressed (d->factor ());
2796                 scm_remember_upto_here_1 ($1);
2797                 $$ = k.smobbed_copy ();
2798         }
2799         ;
2800
2801 multiplied_duration:
2802         steno_duration {
2803                 $$ = $1;
2804         }
2805         | multiplied_duration '*' UNSIGNED {
2806                 $$ = unsmob_duration ($$)->compressed (scm_to_int ($3)).smobbed_copy ();
2807         }
2808         | multiplied_duration '*' FRACTION {
2809                 Rational  m (scm_to_int (scm_car ($3)), scm_to_int (scm_cdr ($3)));
2810
2811                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
2812         }
2813         ;
2814
2815 dots:
2816         /* empty */     {
2817                 $$ = SCM_INUM0;
2818         }
2819         | dots '.' {
2820                 $$ = scm_oneplus ($1);
2821         }
2822         ;
2823
2824 tremolo_type:
2825         ':'     {
2826                 $$ = SCM_INUM0;
2827         }
2828         | ':' UNSIGNED {
2829                 int n = scm_to_int ($2);
2830                 if (!is_duration (n))
2831                         parser->parser_error (@2, _f ("not a duration: %d", n));
2832                 $$ = $2;
2833         }
2834         ;
2835
2836 bass_number:
2837         UNSIGNED { $$ = $1; }
2838         | STRING { $$ = $1; }
2839         | full_markup { $$ = $1; }
2840         | embedded_scm_bare
2841         {
2842                 // as an integer, it needs to be non-negative, and otherwise
2843                 // it needs to be suitable as a markup.
2844                 if (scm_is_integer ($1)
2845                     ? scm_is_true (scm_negative_p ($1))
2846                     : !Text_interface::is_markup ($1))
2847                 {
2848                         parser->parser_error (@1, _ ("bass number expected"));
2849                         $$ = SCM_INUM0;
2850                 }
2851         }
2852         ;
2853
2854 figured_bass_alteration:
2855         '-'     { $$ = ly_rational2scm (FLAT_ALTERATION); }
2856         | '+'   { $$ = ly_rational2scm (SHARP_ALTERATION); }
2857         | '!'   { $$ = scm_from_int (0); }
2858         ;
2859
2860 bass_figure:
2861         FIGURE_SPACE {
2862                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
2863                 $$ = bfr->unprotect ();
2864         }
2865         | bass_number  {
2866                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
2867                 $$ = bfr->self_scm ();
2868
2869                 if (scm_is_number ($1))
2870                         bfr->set_property ("figure", $1);
2871                 else if (Text_interface::is_markup ($1))
2872                         bfr->set_property ("text", $1);
2873
2874                 bfr->unprotect ();
2875         }
2876         | bass_figure ']' {
2877                 $$ = $1;
2878                 unsmob_music ($1)->set_property ("bracket-stop", SCM_BOOL_T);
2879         }
2880         | bass_figure figured_bass_alteration {
2881                 Music *m = unsmob_music ($1);
2882                 if (scm_to_double ($2)) {
2883                         SCM salter = m->get_property ("alteration");
2884                         SCM alter = scm_is_number (salter) ? salter : scm_from_int (0);
2885                         m->set_property ("alteration",
2886                                          scm_sum (alter, $2));
2887                 } else {
2888                         m->set_property ("alteration", scm_from_int (0));
2889                 }
2890         }
2891         | bass_figure figured_bass_modification  {
2892                 Music *m = unsmob_music ($1);
2893                 if ($2 == ly_symbol2scm ("plus"))
2894                         {
2895                         m->set_property ("augmented", SCM_BOOL_T);
2896                         }
2897                 else if ($2 == ly_symbol2scm ("slash"))
2898                         {
2899                         m->set_property ("diminished", SCM_BOOL_T);
2900                         }
2901                 else if ($2 == ly_symbol2scm ("exclamation"))
2902                         {
2903                         m->set_property ("no-continuation", SCM_BOOL_T);
2904                         }
2905                 else if ($2 == ly_symbol2scm ("backslash"))
2906                         {
2907                         m->set_property ("augmented-slash", SCM_BOOL_T);
2908                         }
2909         }
2910         ;
2911
2912
2913 figured_bass_modification:
2914         E_PLUS          {
2915                 $$ = ly_symbol2scm ("plus");
2916         }
2917         | E_EXCLAMATION {
2918                 $$ = ly_symbol2scm ("exclamation");
2919         }
2920         | '/'           {
2921                 $$ = ly_symbol2scm ("slash");
2922         }
2923         | E_BACKSLASH {
2924                 $$ = ly_symbol2scm ("backslash");
2925         }
2926         ;
2927
2928 br_bass_figure:
2929         bass_figure {
2930                 $$ = $1;
2931         }
2932         | '[' bass_figure {
2933                 $$ = $2;
2934                 unsmob_music ($$)->set_property ("bracket-start", SCM_BOOL_T);
2935         }
2936         ;
2937
2938 figure_list:
2939         /**/            {
2940                 $$ = SCM_EOL;
2941         }
2942         | figure_list br_bass_figure {
2943                 $$ = scm_cons ($2, $1);
2944         }
2945         ;
2946
2947 figure_spec:
2948         FIGURE_OPEN figure_list FIGURE_CLOSE {
2949                 $$ = scm_reverse_x ($2, SCM_EOL);
2950         }
2951         ;
2952
2953
2954 optional_rest:
2955         /**/   { $$ = SCM_BOOL_F; }
2956         | REST { $$ = SCM_BOOL_T; }
2957         ;
2958
2959 simple_element:
2960         pitch exclamations questions octave_check maybe_notemode_duration optional_rest {
2961                 if (!parser->lexer_->is_note_state ())
2962                         parser->parser_error (@1, _ ("have to be in Note mode for notes"));
2963                 if (!SCM_UNBNDP ($2)
2964                     || !SCM_UNBNDP ($3)
2965                     || scm_is_number ($4)
2966                     || !SCM_UNBNDP ($5)
2967                     || scm_is_true ($6))
2968                 {
2969                         Music *n = 0;
2970                         if (scm_is_true ($6))
2971                                 n = MY_MAKE_MUSIC ("RestEvent", @$);
2972                         else
2973                                 n = MY_MAKE_MUSIC ("NoteEvent", @$);
2974                         
2975                         n->set_property ("pitch", $1);
2976                         if (SCM_UNBNDP ($5))
2977                                 n->set_property ("duration",
2978                                                  parser->default_duration_.smobbed_copy ());
2979                         else
2980                                 n->set_property ("duration", $5);
2981                         
2982                         if (scm_is_number ($4))
2983                         {
2984                                 int q = scm_to_int ($4);
2985                                 n->set_property ("absolute-octave", scm_from_int (q-1));
2986                         }
2987                         
2988                         if (to_boolean ($3))
2989                                 n->set_property ("cautionary", SCM_BOOL_T);
2990                         if (to_boolean ($2) || to_boolean ($3))
2991                                 n->set_property ("force-accidental", SCM_BOOL_T);
2992                         
2993                         $$ = n->unprotect ();
2994                 }
2995         }
2996         | DRUM_PITCH optional_notemode_duration {
2997                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2998                 n->set_property ("duration", $2);
2999                 n->set_property ("drum-type", $1);
3000
3001                 $$ = n->unprotect ();
3002         }
3003         | RESTNAME optional_notemode_duration           {
3004                 Music *ev = 0;
3005                 if (ly_scm2string ($1) == "s") {
3006                         /* Space */
3007                         ev = MY_MAKE_MUSIC ("SkipEvent", @$);
3008                   }
3009                 else {
3010                         ev = MY_MAKE_MUSIC ("RestEvent", @$);
3011
3012                     }
3013                 ev->set_property ("duration", $2);
3014                 $$ = ev->unprotect ();
3015         }
3016         ;
3017
3018 simple_chord_elements:
3019         new_chord {
3020                 if (!parser->lexer_->is_chord_state ())
3021                         parser->parser_error (@1, _ ("have to be in Chord mode for chords"));
3022                 $$ = $1;
3023         }
3024         | figure_spec optional_notemode_duration {
3025                 for (SCM s = $1; scm_is_pair (s); s = scm_cdr (s))
3026                 {
3027                         unsmob_music (scm_car (s))->set_property ("duration", $2);
3028                 }
3029                 $$ = $1;
3030         }
3031         ;
3032
3033 lyric_element:
3034         full_markup {
3035                 $$ = $1;
3036         }
3037         | STRING {
3038                 $$ = $1;
3039         }
3040         | LYRIC_ELEMENT
3041         ;
3042
3043 lyric_element_music:
3044         lyric_element optional_notemode_duration post_events {
3045                 $$ = MAKE_SYNTAX ("lyric-event", @$, $1, $2);
3046                 if (scm_is_pair ($3))
3047                         unsmob_music ($$)->set_property
3048                                 ("articulations", scm_reverse_x ($3, SCM_EOL));
3049         }
3050         ;
3051
3052 new_chord:
3053         steno_tonic_pitch optional_notemode_duration   {
3054                 $$ = make_chord_elements (@$, $1, $2, SCM_EOL);
3055         }
3056         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
3057                 SCM its = scm_reverse_x ($4, SCM_EOL);
3058                 $$ = make_chord_elements (@$, $1, $2, scm_cons ($3, its));
3059         }
3060         ;
3061
3062 chord_items:
3063         /**/ {
3064                 $$ = SCM_EOL;
3065         }
3066         | chord_items chord_item {
3067                 $$ = scm_cons ($2, $$);
3068         }
3069         ;
3070
3071 chord_separator:
3072         CHORD_COLON {
3073                 $$ = ly_symbol2scm ("chord-colon");
3074         }
3075         | CHORD_CARET {
3076                 $$ = ly_symbol2scm ("chord-caret");
3077         }
3078         | CHORD_SLASH steno_tonic_pitch {
3079                 $$ = scm_list_2 (ly_symbol2scm ("chord-slash"), $2);
3080         }
3081         | CHORD_BASS steno_tonic_pitch {
3082                 $$ = scm_list_2 (ly_symbol2scm ("chord-bass"), $2);
3083         }
3084         ;
3085
3086 chord_item:
3087         chord_separator {
3088                 $$ = $1;
3089         }
3090         | step_numbers {
3091                 $$ = scm_reverse_x ($1, SCM_EOL);
3092         }
3093         | CHORD_MODIFIER  {
3094                 $$ = $1;
3095         }
3096         ;
3097
3098 step_numbers:
3099         step_number { $$ = scm_cons ($1, SCM_EOL); }
3100         | step_numbers '.' step_number {
3101                 $$ = scm_cons ($3, $$);
3102         }
3103         ;
3104
3105 step_number:
3106         UNSIGNED {
3107                 $$ = make_chord_step ($1, 0);
3108         }
3109         | UNSIGNED '+' {
3110                 $$ = make_chord_step ($1, SHARP_ALTERATION);
3111         }
3112         | UNSIGNED CHORD_MINUS {
3113                 $$ = make_chord_step ($1, FLAT_ALTERATION);
3114         }
3115         ;
3116
3117 tempo_range:
3118         UNSIGNED {
3119                 $$ = $1;
3120         }
3121         | UNSIGNED '~' UNSIGNED {
3122                 $$ = scm_cons ($1, $3);
3123         }
3124         ;
3125
3126 /*
3127         UTILITIES
3128
3129 TODO: should deprecate in favor of Scheme?
3130
3131  */
3132 number_expression:
3133         number_expression '+' number_term {
3134                 $$ = scm_sum ($1, $3);
3135         }
3136         | number_expression '-' number_term {
3137                 $$ = scm_difference ($1, $3);
3138         }
3139         | number_term
3140         ;
3141
3142 number_term:
3143         number_factor {
3144                 $$ = $1;
3145         }
3146         | number_factor '*' number_factor {
3147                 $$ = scm_product ($1, $3);
3148         }
3149         | number_factor '/' number_factor {
3150                 $$ = scm_divide ($1, $3);
3151         }
3152         ;
3153
3154 number_factor:
3155         '-'  number_factor { /* %prec UNARY_MINUS */
3156                 $$ = scm_difference ($2, SCM_UNDEFINED);
3157         }
3158         | bare_number
3159         ;
3160
3161
3162 bare_number:
3163         bare_number_closed
3164         | UNSIGNED NUMBER_IDENTIFIER    {
3165                 $$ = scm_product ($1, $2);
3166         }
3167         | REAL NUMBER_IDENTIFIER        {
3168                 $$ = scm_product ($1, $2);
3169         }
3170         ;
3171
3172 bare_number_closed:
3173         UNSIGNED
3174         | REAL
3175         | NUMBER_IDENTIFIER
3176         ;
3177
3178 unsigned_number:
3179         UNSIGNED
3180         | NUMBER_IDENTIFIER
3181         ;
3182
3183 exclamations:
3184                 { $$ = SCM_UNDEFINED; }
3185         | exclamations '!'
3186         {
3187                 if (SCM_UNBNDP ($1))
3188                         $$ = SCM_BOOL_T;
3189                 else
3190                         $$ = scm_not ($1);
3191         }
3192         ;
3193
3194 questions:
3195         { $$ = SCM_UNDEFINED; }
3196         | questions '?'
3197         {
3198                 if (SCM_UNBNDP ($1))
3199                         $$ = SCM_BOOL_T;
3200                 else
3201                         $$ = scm_not ($1);
3202         }
3203         ;
3204
3205 full_markup_list:
3206         MARKUPLIST
3207                 { parser->lexer_->push_markup_state (); }
3208         markup_list {
3209                 $$ = $3;
3210                 parser->lexer_->pop_state ();
3211         }
3212         ;
3213
3214 full_markup:
3215         MARKUP
3216                 { parser->lexer_->push_markup_state (); }
3217         markup_top {
3218                 $$ = $3;
3219                 parser->lexer_->pop_state ();
3220         }
3221         ;
3222
3223 markup_top:
3224         markup_list {
3225                 $$ = scm_list_2 (ly_lily_module_constant ("line-markup"),  $1);
3226         }
3227         | markup_head_1_list simple_markup      {
3228                 $$ = scm_car (scm_call_2 (ly_lily_module_constant ("map-markup-command-list"), $1, scm_list_1 ($2)));
3229         }
3230         | simple_markup {
3231                 $$ = $1;
3232         }
3233         ;
3234
3235 markup_scm:
3236         embedded_scm_bare
3237         {
3238                 if (Text_interface::is_markup ($1))
3239                         MYBACKUP (MARKUP_IDENTIFIER, $1, @1);
3240                 else if (Text_interface::is_markup_list ($1))
3241                         MYBACKUP (MARKUPLIST_IDENTIFIER, $1, @1);
3242                 else {
3243                         parser->parser_error (@1, _ ("not a markup"));
3244                         MYBACKUP (MARKUP_IDENTIFIER, scm_string (SCM_EOL), @1);
3245                 }
3246         } BACKUP
3247         ;
3248                         
3249
3250 markup_list:
3251         markup_composed_list {
3252                 $$ = $1;
3253         }
3254         | markup_braced_list {
3255                 $$ = $1;
3256         }
3257         | markup_command_list {
3258                 $$ = scm_list_1 ($1);
3259         }
3260         | markup_scm MARKUPLIST_IDENTIFIER
3261         {
3262                 $$ = $2;
3263         }
3264         ;
3265
3266 markup_composed_list:
3267         markup_head_1_list markup_braced_list {
3268                 $$ = scm_call_2 (ly_lily_module_constant ("map-markup-command-list"), $1, $2);
3269
3270         }
3271         ;
3272
3273 markup_braced_list:
3274         '{' markup_braced_list_body '}' {
3275                 $$ = scm_reverse_x ($2, SCM_EOL);
3276         }
3277         ;
3278
3279 markup_braced_list_body:
3280         /* empty */     {  $$ = SCM_EOL; }
3281         | markup_braced_list_body markup {
3282                 $$ = scm_cons ($2, $1);
3283         }
3284         | markup_braced_list_body markup_list {
3285                 $$ = scm_reverse_x ($2, $1);
3286         }
3287         ;
3288
3289 markup_command_list:
3290         MARKUP_LIST_FUNCTION markup_command_list_arguments {
3291           $$ = scm_cons ($1, scm_reverse_x($2, SCM_EOL));
3292         }
3293         ;
3294
3295 markup_command_basic_arguments:
3296         EXPECT_MARKUP_LIST markup_command_list_arguments markup_list {
3297           $$ = scm_cons ($3, $2);
3298         }
3299         | EXPECT_SCM markup_command_list_arguments embedded_scm_closed {
3300           $$ = check_scheme_arg (parser, @3, $3, $2, $1);
3301         }
3302         | EXPECT_NO_MORE_ARGS {
3303           $$ = SCM_EOL;
3304         }
3305         ;
3306
3307 markup_command_list_arguments:
3308         markup_command_basic_arguments { $$ = $1; }
3309         | EXPECT_MARKUP markup_command_list_arguments markup {
3310           $$ = scm_cons ($3, $2);
3311         }
3312         ;
3313
3314 markup_head_1_item:
3315         MARKUP_FUNCTION EXPECT_MARKUP markup_command_list_arguments {
3316           $$ = scm_cons ($1, scm_reverse_x ($3, SCM_EOL));
3317         }
3318         ;
3319
3320 markup_head_1_list:
3321         markup_head_1_item      {
3322                 $$ = scm_list_1 ($1);
3323         }
3324         | markup_head_1_list markup_head_1_item {
3325                 $$ = scm_cons ($2, $1);
3326         }
3327         ;
3328
3329 simple_markup:
3330         STRING {
3331                 $$ = make_simple_markup ($1);
3332         }
3333         | SCORE {
3334                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3335                 parser->lexer_->push_note_state (nn);
3336         } '{' score_body '}' {
3337                 $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), $4);
3338                 parser->lexer_->pop_state ();
3339         }
3340         | MARKUP_FUNCTION markup_command_basic_arguments {
3341                 $$ = scm_cons ($1, scm_reverse_x ($2, SCM_EOL));
3342         }
3343         | markup_scm MARKUP_IDENTIFIER
3344         {
3345                 $$ = $2;
3346         }
3347         ;
3348
3349 markup:
3350         markup_head_1_list simple_markup        {
3351                 SCM mapper = ly_lily_module_constant ("map-markup-command-list");
3352                 $$ = scm_car (scm_call_2 (mapper, $1, scm_list_1 ($2)));
3353         }
3354         | simple_markup {
3355                 $$ = $1;
3356         }
3357         ;
3358
3359 %%
3360
3361 void
3362 Lily_parser::set_yydebug (bool x)
3363 {
3364         yydebug = x;
3365 }
3366
3367 SCM
3368 Lily_parser::do_yyparse ()
3369 {
3370         SCM retval = SCM_UNDEFINED;
3371         yyparse (this, &retval);
3372         return retval;
3373 }
3374
3375
3376
3377
3378
3379 /*
3380
3381 It is a little strange to have this function in this file, but
3382 otherwise, we have to import music classes into the lexer.
3383
3384 */
3385 int
3386 Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
3387 {
3388         if (unsmob_book (sid)) {
3389                 Book *book =  unsmob_book (sid)->clone ();
3390                 *destination = book->self_scm ();
3391                 book->unprotect ();
3392
3393                 return BOOK_IDENTIFIER;
3394         } else if (scm_is_number (sid)) {
3395                 *destination = sid;
3396                 return NUMBER_IDENTIFIER;
3397         } else if (unsmob_context_def (sid)) {
3398                 Context_def *def= unsmob_context_def (sid)->clone ();
3399
3400                 *destination = def->self_scm ();
3401                 def->unprotect ();
3402
3403                 return CONTEXT_DEF_IDENTIFIER;
3404         } else if (unsmob_context_mod (sid)) {
3405                 *destination = unsmob_context_mod (sid)->smobbed_copy ();
3406
3407                 return CONTEXT_MOD_IDENTIFIER;
3408         } else if (Music *mus = unsmob_music (sid)) {
3409                 mus = mus->clone ();
3410                 *destination = mus->self_scm ();
3411                 unsmob_music (*destination)->
3412                         set_property ("origin", make_input (last_input_));
3413
3414                 bool is_event = mus->is_mus_type ("post-event");
3415                 mus->unprotect ();
3416                 return is_event ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
3417         } else if (unsmob_pitch (sid)) {
3418                 *destination = unsmob_pitch (sid)->smobbed_copy ();
3419                 return PITCH_IDENTIFIER;
3420         } else if (unsmob_duration (sid)) {
3421                 *destination = unsmob_duration (sid)->smobbed_copy ();
3422                 return DURATION_IDENTIFIER;
3423         } else if (unsmob_output_def (sid)) {
3424                 Output_def *p = unsmob_output_def (sid);
3425                 p = p->clone ();
3426
3427                 *destination = p->self_scm ();
3428                 p->unprotect ();
3429                 return OUTPUT_DEF_IDENTIFIER;
3430         }
3431
3432         return -1;
3433 }
3434
3435 SCM
3436 get_next_unique_context_id ()
3437 {
3438         return scm_from_locale_string ("$uniqueContextId");
3439 }
3440
3441
3442 SCM
3443 get_next_unique_lyrics_context_id ()
3444 {
3445         static int new_context_count;
3446         char s[128];
3447         snprintf (s, sizeof (s)-1, "uniqueContext%d", new_context_count++);
3448         return scm_from_locale_string (s);
3449 }
3450
3451 // check_scheme_arg checks one argument with a given predicate for use
3452 // in an argument list and throws a syntax error if it is unusable.
3453 // The argument is prepended to the argument list in any case.  After
3454 // throwing a syntax error, the argument list is terminated with #f as
3455 // its last cdr in order to mark it as uncallable while not losing
3456 // track of its total length.
3457 //
3458 // There are a few special considerations: if optional argument disp
3459 // is given (otherwise it defaults to SCM_UNDEFINED), it will be used
3460 // instead of arg in a prospective error message.  This is useful if
3461 // arg is not the actual argument but rather a transformation of it.
3462 //
3463 // If arg itself is SCM_UNDEFINED, the predicate is considered false
3464 // and an error message using disp is produced unconditionally.
3465
3466 SCM check_scheme_arg (Lily_parser *parser, Input loc,
3467                       SCM arg, SCM args, SCM pred, SCM disp)
3468 {
3469         if (SCM_UNBNDP (arg))
3470                 args = scm_cons (disp, args);
3471         else {
3472                 args = scm_cons (arg, args);
3473                 if (scm_is_true (scm_call_1 (pred, arg)))
3474                         return args;
3475         }
3476         scm_set_cdr_x (scm_last_pair (args), SCM_EOL);
3477         MAKE_SYNTAX ("argument-error", loc, scm_length (args), pred,
3478                      SCM_UNBNDP (disp) ? arg : disp);
3479         scm_set_cdr_x (scm_last_pair (args), SCM_BOOL_F);
3480         return args;
3481 }
3482
3483 SCM loc_on_music (Input loc, SCM arg)
3484 {
3485         if (Music *m = unsmob_music (arg))
3486         {
3487                 m = m->clone ();
3488                 m->set_spot (loc);
3489                 return m->unprotect ();
3490         }
3491         return arg;
3492 }
3493
3494 SCM
3495 try_string_variants (SCM pred, SCM str)
3496 {
3497         // a matching predicate is always ok
3498         if (scm_is_true (scm_call_1 (pred, str)))
3499                 return str;
3500         // a symbol may be interpreted as a list of symbols if it helps
3501         if (scm_is_symbol (str)) {
3502                 str = scm_list_1 (str);
3503                 if (scm_is_true (scm_call_1 (pred, str)))
3504                         return str;
3505                 return SCM_UNDEFINED;
3506         }
3507
3508         // If this cannot be a string representation of a symbol list,
3509         // we are through.
3510
3511         if (!is_regular_identifier (str, true))
3512                 return SCM_UNDEFINED;
3513
3514         str = scm_string_split (str, SCM_MAKE_CHAR ('.'));
3515         for (SCM p = str; scm_is_pair (p); p = scm_cdr (p))
3516                 scm_set_car_x (p, scm_string_to_symbol (scm_car (p)));
3517
3518         // Let's attempt the symbol list interpretation first.
3519
3520         if (scm_is_true (scm_call_1 (pred, str)))
3521                 return str;
3522
3523         // If there is just one symbol in the list, we might interpret
3524         // it as a single symbol
3525
3526         if (scm_is_null (scm_cdr (str)))
3527         {
3528                 str = scm_car (str);
3529                 if (scm_is_true (scm_call_1 (pred, str)))
3530                         return str;
3531         }
3532
3533         return SCM_UNDEFINED;
3534 }
3535
3536 bool
3537 is_regular_identifier (SCM id, bool multiple)
3538 {
3539   if (!scm_is_string (id))
3540           return false;
3541
3542   string str = ly_scm2string (id);
3543
3544   bool middle = false;
3545
3546   for (string::iterator it=str.begin(); it != str.end (); it++)
3547   {
3548           int c = *it & 0xff;
3549           if ((c >= 'a' && c <= 'z')
3550               || (c >= 'A' && c <= 'Z')
3551               || c > 0x7f)
3552                   middle = true;
3553           else if (middle && (c == '-' || c == '_' || (multiple && c == '.')))
3554                   middle = false;
3555           else
3556                   return false;
3557   }
3558   return middle;
3559 }
3560
3561 SCM
3562 make_music_from_simple (Lily_parser *parser, Input loc, SCM simple)
3563 {
3564         if (unsmob_music (simple))
3565                 return simple;
3566         if (parser->lexer_->is_note_state ()) {
3567                 if (scm_is_symbol (simple)) {
3568                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3569                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3570                         n->set_property ("drum-type", simple);
3571                         return n->unprotect ();
3572                 }
3573                 if (unsmob_pitch (simple)) {
3574                         Music *n = MY_MAKE_MUSIC ("NoteEvent", loc);
3575                         n->set_property ("duration", parser->default_duration_.smobbed_copy ());
3576                         n->set_property ("pitch", simple);
3577                         return n->unprotect ();
3578                 }
3579                 return simple;
3580         } else if (parser->lexer_->is_lyric_state ()) {
3581                 if (Text_interface::is_markup (simple))
3582                         return MAKE_SYNTAX ("lyric-event", loc, simple,
3583                                             parser->default_duration_.smobbed_copy ());
3584         } else if (parser->lexer_->is_chord_state ()) {
3585                 if (unsmob_pitch (simple))
3586                         return make_chord_elements (loc, simple,
3587                                                     parser->default_duration_.smobbed_copy (),
3588                                                     SCM_EOL);
3589         }
3590         return simple;
3591 }
3592
3593 Music *
3594 make_music_with_input (SCM name, Input where)
3595 {
3596        Music *m = make_music_by_name (name);
3597        m->set_spot (where);
3598        return m;
3599 }
3600
3601 SCM
3602 make_simple_markup (SCM a)
3603 {
3604         return a;
3605 }
3606
3607 bool
3608 is_duration (int t)
3609 {
3610   return t && t == 1 << intlog2 (t);
3611 }
3612
3613 SCM
3614 make_chord_step (SCM step_scm, Rational alter)
3615 {
3616         int step = scm_to_int (step_scm);
3617
3618         if (step == 7)
3619                 alter += FLAT_ALTERATION;
3620
3621         while (step < 0)
3622                 step += 7;
3623         Pitch m ((step -1) / 7, (step - 1) % 7, alter);
3624         return m.smobbed_copy ();
3625 }
3626
3627
3628 SCM
3629 make_chord_elements (Input loc, SCM pitch, SCM dur, SCM modification_list)
3630 {
3631         SCM chord_ctor = ly_lily_module_constant ("construct-chord-elements");
3632         SCM res = scm_call_3 (chord_ctor, pitch, dur, modification_list);
3633         for (SCM s = res; scm_is_pair (s); s = scm_cdr (s))
3634         {
3635                 unsmob_music (scm_car (s))->set_spot (loc);
3636         }
3637         return res;
3638 }
3639
3640 int
3641 yylex (YYSTYPE *s, YYLTYPE *loc, Lily_parser *parser)
3642 {
3643         Lily_lexer *lex = parser->lexer_;
3644
3645         lex->lexval_ = s;
3646         lex->lexloc_ = loc;
3647         lex->prepare_for_next_token ();
3648         return lex->yylex ();
3649 }