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