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