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