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