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