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