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