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