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