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