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