]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
Don't wrap EventChord around rhythmic events outside of music lists.
[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 doesnt 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                 unsmob_music ($1)->set_property ("articulations",
2041                                                  scm_reverse_x ($2, SCM_EOL));
2042         }
2043         | simple_chord_elements post_events     {
2044                 SCM elts = ly_append2 ($1, scm_reverse_x ($2, SCM_EOL));
2045
2046                 Input i;
2047                 /* why is this giving wrong start location? -ns
2048                  * i = @$; */
2049                 i.set_location (@1, @2);
2050                 $$ = MAKE_SYNTAX ("event-chord", i, elts);
2051         }
2052         | CHORD_REPETITION optional_notemode_duration post_events {
2053                 Input i;
2054                 i.set_location (@1, @3);
2055                 $$ = MAKE_SYNTAX ("repetition-chord", i,
2056                                   parser->lexer_->chord_repetition_.last_chord_,
2057                                   parser->lexer_->chord_repetition_.repetition_function_,
2058                                   $2, scm_reverse_x ($3, SCM_EOL));
2059         }
2060         | MULTI_MEASURE_REST optional_notemode_duration post_events {
2061                 Input i;
2062                 i.set_location (@1, @3);
2063                 $$ = MAKE_SYNTAX ("multi-measure-rest", i, $2,
2064                                   scm_reverse_x ($3, SCM_EOL));
2065         }
2066         | command_element
2067         /* note chord elements are memorized into
2068            parser->lexer_->chord_repetition_ so that the chord repetition
2069            mechanism copy them when a chord repetition symbol is found
2070         */
2071         | note_chord_element    {
2072                 parser->lexer_->chord_repetition_.last_chord_ = $$;
2073         }
2074         ;
2075
2076
2077 note_chord_element:
2078         chord_body optional_notemode_duration post_events
2079         {
2080                 Music *m = unsmob_music ($1);
2081                 SCM dur = unsmob_duration ($2)->smobbed_copy ();
2082                 SCM es = m->get_property ("elements");
2083                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
2084
2085                 for (SCM s = es; scm_is_pair (s); s = scm_cdr (s))
2086                   unsmob_music (scm_car (s))->set_property ("duration", dur);
2087                 es = ly_append2 (es, postevs);
2088
2089                 m-> set_property ("elements", es);
2090                 m->set_spot (@$);
2091                 $$ = m->self_scm ();
2092         }
2093         ;
2094
2095 chord_body:
2096         ANGLE_OPEN chord_body_elements ANGLE_CLOSE
2097         {
2098                 $$ = MAKE_SYNTAX ("event-chord", @$, scm_reverse_x ($2, SCM_EOL));
2099         }
2100         ;
2101
2102 chord_body_elements:
2103         /* empty */             { $$ = SCM_EOL; }
2104         | chord_body_elements chord_body_element {
2105                 if (!SCM_UNBNDP ($2))
2106                         $$ = scm_cons ($2, $1);
2107         }
2108         ;
2109
2110 chord_body_element:
2111         pitch exclamations questions octave_check post_events
2112         {
2113                 int q = $3;
2114                 int ex = $2;
2115                 SCM check = $4;
2116                 SCM post = $5;
2117
2118                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2119                 n->set_property ("pitch", $1);
2120                 if (q % 2)
2121                         n->set_property ("cautionary", SCM_BOOL_T);
2122                 if (ex % 2 || q % 2)
2123                         n->set_property ("force-accidental", SCM_BOOL_T);
2124
2125                 if (scm_is_pair (post)) {
2126                         SCM arts = scm_reverse_x (post, SCM_EOL);
2127                         n->set_property ("articulations", arts);
2128                 }
2129                 if (scm_is_number (check))
2130                 {
2131                         int q = scm_to_int (check);
2132                         n->set_property ("absolute-octave", scm_from_int (q-1));
2133                 }
2134
2135                 $$ = n->unprotect ();
2136         }
2137         | DRUM_PITCH post_events {
2138                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2139                 n->set_property ("drum-type", $1);
2140
2141                 if (scm_is_pair ($2)) {
2142                         SCM arts = scm_reverse_x ($2, SCM_EOL);
2143                         n->set_property ("articulations", arts);
2144                 }
2145                 $$ = n->unprotect ();
2146         }
2147         | music_function_chord_body
2148         {
2149                 if (!unsmob_music ($$)->is_mus_type ("rhythmic-event")) {
2150                         parser->parser_error (@$, _ ("not a rhythmic event"));
2151                         $$ = SCM_UNDEFINED;
2152                 }
2153         }
2154         ;
2155
2156 music_function_chord_body:
2157         music_function_call
2158         | MUSIC_IDENTIFIER
2159         ;
2160
2161 // Event functions may only take closed arglists, otherwise it would
2162 // not be clear whether a following postevent should be associated
2163 // with the last argument of the event function or with the expression
2164 // for which the function call acts itself as event.
2165
2166 music_function_event:
2167         MUSIC_FUNCTION function_arglist_closed {
2168                 $$ = MAKE_SYNTAX ("music-function", @$,
2169                                          $1, $2);
2170         }
2171         ;
2172
2173 event_function_event:
2174         EVENT_FUNCTION function_arglist_closed {
2175                 $$ = MAKE_SYNTAX ("music-function", @$,
2176                                          $1, $2);
2177         }
2178         ;
2179
2180 command_element:
2181         command_event {
2182                 $$ = $1;
2183         }
2184         | E_BRACKET_OPEN {
2185                 Music *m = MY_MAKE_MUSIC ("LigatureEvent", @$);
2186                 m->set_property ("span-direction", scm_from_int (START));
2187                 $$ = m->unprotect();
2188         }
2189         | E_BRACKET_CLOSE {
2190                 Music *m = MY_MAKE_MUSIC ("LigatureEvent", @$);
2191                 m->set_property ("span-direction", scm_from_int (STOP));
2192                 $$ = m->unprotect ();
2193         }
2194         | E_BACKSLASH {
2195                 $$ = MAKE_SYNTAX ("voice-separator", @$);
2196         }
2197         | '|'      {
2198                 SCM pipe = parser->lexer_->lookup_identifier ("pipeSymbol");
2199
2200                 Music *m = unsmob_music (pipe);
2201                 if (m)
2202                 {
2203                         m = m->clone ();
2204                         m->set_spot (@$);
2205                         $$ = m->unprotect ();
2206                 }
2207                 else
2208                         $$ = MAKE_SYNTAX ("bar-check", @$);
2209
2210         }
2211         ;
2212
2213 command_event:
2214         E_TILDE {
2215                 $$ = MY_MAKE_MUSIC ("PesOrFlexaEvent", @$)->unprotect ();
2216         }
2217         | tempo_event {
2218                 $$ = $1;
2219         }
2220         ;
2221
2222
2223 post_events:
2224         /* empty */ {
2225                 $$ = SCM_EOL;
2226         }
2227         | post_events post_event {
2228                 unsmob_music ($2)->set_spot (@2);
2229                 $$ = scm_cons ($2, $$);
2230         }
2231         ;
2232
2233 post_event_nofinger:
2234         direction_less_event {
2235                 $$ = $1;
2236         }
2237         | script_dir music_function_event {
2238                 $$ = $2;
2239                 if ($1)
2240                 {
2241                         unsmob_music ($$)->set_property ("direction", scm_from_int ($1));
2242                 }
2243         }
2244         | HYPHEN {
2245                 if (!parser->lexer_->is_lyric_state ())
2246                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2247                 $$ = MY_MAKE_MUSIC ("HyphenEvent", @$)->unprotect ();
2248         }
2249         | EXTENDER {
2250                 if (!parser->lexer_->is_lyric_state ())
2251                         parser->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2252                 $$ = MY_MAKE_MUSIC ("ExtenderEvent", @$)->unprotect ();
2253         }
2254         | script_dir direction_reqd_event {
2255                 if ($1)
2256                 {
2257                         Music *m = unsmob_music ($2);
2258                         m->set_property ("direction", scm_from_int ($1));
2259                 }
2260                 $$ = $2;
2261         }
2262         | script_dir direction_less_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         | string_number_event
2271         | '^' fingering
2272         {
2273                 $$ = $2;
2274                 unsmob_music ($$)->set_property ("direction", scm_from_int (UP));
2275         }
2276         | '_' fingering
2277         {
2278                 $$ = $2;
2279                 unsmob_music ($$)->set_property ("direction", scm_from_int (DOWN));
2280         }                       
2281         ;
2282
2283 post_event:
2284         post_event_nofinger
2285         | '-' fingering {
2286                 $$ = $2;
2287         }
2288         ;
2289
2290 string_number_event:
2291         E_UNSIGNED {
2292                 Music *s = MY_MAKE_MUSIC ("StringNumberEvent", @$);
2293                 s->set_property ("string-number", scm_from_int ($1));
2294                 $$ = s->unprotect ();
2295         }
2296         ;
2297
2298 direction_less_char:
2299         '['  {
2300                 $$ = ly_symbol2scm ("bracketOpenSymbol");
2301         }
2302         | ']'  {
2303                 $$ = ly_symbol2scm ("bracketCloseSymbol");
2304         }
2305         | '~'  {
2306                 $$ = ly_symbol2scm ("tildeSymbol");
2307         }
2308         | '('  {
2309                 $$ = ly_symbol2scm ("parenthesisOpenSymbol");
2310         }
2311         | ')'  {
2312                 $$ = ly_symbol2scm ("parenthesisCloseSymbol");
2313         }
2314         | E_EXCLAMATION  {
2315                 $$ = ly_symbol2scm ("escapedExclamationSymbol");
2316         }
2317         | E_OPEN  {
2318                 $$ = ly_symbol2scm ("escapedParenthesisOpenSymbol");
2319         }
2320         | E_CLOSE  {
2321                 $$ = ly_symbol2scm ("escapedParenthesisCloseSymbol");
2322         }
2323         | E_ANGLE_CLOSE  {
2324                 $$ = ly_symbol2scm ("escapedBiggerSymbol");
2325         }
2326         | E_ANGLE_OPEN  {
2327                 $$ = ly_symbol2scm ("escapedSmallerSymbol");
2328         }
2329         ;
2330
2331 direction_less_event:
2332         direction_less_char {
2333                 SCM predefd = parser->lexer_->lookup_identifier_symbol ($1);
2334                 Music *m = 0;
2335                 if (unsmob_music (predefd))
2336                 {
2337                         m = unsmob_music (predefd)->clone ();
2338                         m->set_spot (@$);
2339                 }
2340                 else
2341                 {
2342                         m = MY_MAKE_MUSIC ("Music", @$);
2343                 }
2344                 $$ = m->unprotect ();
2345         }
2346         | EVENT_IDENTIFIER      {
2347                 $$ = $1;
2348         }
2349         | tremolo_type  {
2350                Music *a = MY_MAKE_MUSIC ("TremoloEvent", @$);
2351                a->set_property ("tremolo-type", scm_from_int ($1));
2352                $$ = a->unprotect ();
2353         }
2354         | event_function_event  
2355         ;
2356
2357 direction_reqd_event:
2358         gen_text_def {
2359                 $$ = $1;
2360         }
2361         | script_abbreviation {
2362                 SCM s = parser->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
2363                 Music *a = MY_MAKE_MUSIC ("ArticulationEvent", @$);
2364                 if (scm_is_string (s))
2365                         a->set_property ("articulation-type", s);
2366                 else parser->parser_error (@1, _ ("expecting string as script definition"));
2367                 $$ = a->unprotect ();
2368         }
2369         ;
2370
2371 octave_check:
2372         /**/ { $$ = SCM_EOL; }
2373         | '='  { $$ = scm_from_int (0); }
2374         | '=' sub_quotes { $$ = scm_from_int (-$2); }
2375         | '=' sup_quotes { $$ = scm_from_int ($2); }
2376         ;
2377
2378 sup_quotes:
2379         '\'' {
2380                 $$ = 1;
2381         }
2382         | sup_quotes '\'' {
2383                 $$ ++;
2384         }
2385         ;
2386
2387 sub_quotes:
2388         ',' {
2389                 $$ = 1;
2390         }
2391         | sub_quotes ',' {
2392                 $$++;
2393         }
2394         ;
2395
2396 steno_pitch:
2397         NOTENAME_PITCH  {
2398                 $$ = $1;
2399         }
2400         | NOTENAME_PITCH sup_quotes     {
2401                 Pitch p = *unsmob_pitch ($1);
2402                 p = p.transposed (Pitch ($2,0,0));
2403                 $$ = p.smobbed_copy ();
2404         }
2405         | NOTENAME_PITCH sub_quotes      {
2406                 Pitch p =* unsmob_pitch ($1);
2407                 p = p.transposed (Pitch (-$2,0,0));
2408                 $$ = p.smobbed_copy ();
2409         }
2410         ;
2411
2412 /*
2413 ugh. duplication
2414 */
2415
2416 steno_tonic_pitch:
2417         TONICNAME_PITCH {
2418                 $$ = $1;
2419         }
2420         | TONICNAME_PITCH sup_quotes    {
2421                 Pitch p = *unsmob_pitch ($1);
2422                 p = p.transposed (Pitch ($2,0,0));
2423                 $$ = p.smobbed_copy ();
2424         }
2425         | TONICNAME_PITCH sub_quotes     {
2426                 Pitch p = *unsmob_pitch ($1);
2427
2428                 p = p.transposed (Pitch (-$2,0,0));
2429                 $$ = p.smobbed_copy ();
2430         }
2431         ;
2432
2433 pitch:
2434         steno_pitch {
2435                 $$ = $1;
2436         }
2437         | PITCH_IDENTIFIER
2438         ;
2439
2440 pitch_also_in_chords:
2441         pitch
2442         | steno_tonic_pitch
2443         ;
2444
2445 gen_text_def:
2446         full_markup {
2447                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2448                 t->set_property ("text", $1);
2449                 $$ = t->unprotect ();
2450         }
2451         | simple_string {
2452                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
2453                 t->set_property ("text",
2454                         make_simple_markup ($1));
2455                 $$ = t->unprotect ();
2456         }
2457         ;
2458
2459 fingering:
2460         UNSIGNED {
2461                 Music *t = MY_MAKE_MUSIC ("FingeringEvent", @$);
2462                 t->set_property ("digit", $1);
2463                 $$ = t->unprotect ();
2464         }
2465         ;
2466
2467 script_abbreviation:
2468         '^'             {
2469                 $$ = scm_from_locale_string ("Hat");
2470         }
2471         | '+'           {
2472                 $$ = scm_from_locale_string ("Plus");
2473         }
2474         | '-'           {
2475                 $$ = scm_from_locale_string ("Dash");
2476         }
2477         | '|'           {
2478                 $$ = scm_from_locale_string ("Bar");
2479         }
2480         | ANGLE_CLOSE   {
2481                 $$ = scm_from_locale_string ("Larger");
2482         }
2483         | '.'           {
2484                 $$ = scm_from_locale_string ("Dot");
2485         }
2486         | '_' {
2487                 $$ = scm_from_locale_string ("Underscore");
2488         }
2489         ;
2490
2491 script_dir:
2492         '_'     { $$ = DOWN; }
2493         | '^'   { $$ = UP; }
2494         | '-'   { $$ = CENTER; }
2495         ;
2496
2497 duration_length:
2498         multiplied_duration {
2499                 $$ = $1;
2500         }
2501         ;
2502
2503 optional_notemode_duration:
2504         {
2505                 Duration dd = parser->default_duration_;
2506                 $$ = dd.smobbed_copy ();
2507         }
2508         | multiplied_duration   {
2509                 $$ = $1;
2510                 parser->default_duration_ = *unsmob_duration ($$);
2511         }
2512         ;
2513
2514 steno_duration:
2515         bare_unsigned dots              {
2516                 int len = 0;
2517                 if (!is_duration ($1))
2518                         parser->parser_error (@1, _f ("not a duration: %d", $1));
2519                 else
2520                         len = intlog2 ($1);
2521
2522                 $$ = Duration (len, $2).smobbed_copy ();
2523         }
2524         | DURATION_IDENTIFIER dots      {
2525                 Duration *d = unsmob_duration ($1);
2526                 Duration k (d->duration_log (), d->dot_count () + $2);
2527                 k = k.compressed (d->factor ());
2528                 *d = k;
2529                 $$ = $1;
2530         }
2531         ;
2532
2533 multiplied_duration:
2534         steno_duration {
2535                 $$ = $1;
2536         }
2537         | multiplied_duration '*' bare_unsigned {
2538                 $$ = unsmob_duration ($$)->compressed ( $3) .smobbed_copy ();
2539         }
2540         | multiplied_duration '*' FRACTION {
2541                 Rational  m (scm_to_int (scm_car ($3)), scm_to_int (scm_cdr ($3)));
2542
2543                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
2544         }
2545         ;
2546
2547 fraction:
2548         FRACTION { $$ = $1; }
2549         | UNSIGNED '/' UNSIGNED {
2550                 $$ = scm_cons ($1, $3);
2551         }
2552         ;
2553
2554 dots:
2555         /* empty */     {
2556                 $$ = 0;
2557         }
2558         | dots '.' {
2559                 $$ ++;
2560         }
2561         ;
2562
2563 tremolo_type:
2564         ':'     {
2565                 $$ = 0;
2566         }
2567         | ':' bare_unsigned {
2568                 if (!is_duration ($2))
2569                         parser->parser_error (@2, _f ("not a duration: %d", $2));
2570                 $$ = $2;
2571         }
2572         ;
2573
2574 bass_number:
2575         UNSIGNED { $$ = $1; }
2576         | STRING { $$ = $1; }
2577         | full_markup { $$ = $1; }
2578         ;
2579
2580 figured_bass_alteration:
2581         '-'     { $$ = ly_rational2scm (FLAT_ALTERATION); }
2582         | '+'   { $$ = ly_rational2scm (SHARP_ALTERATION); }
2583         | '!'   { $$ = scm_from_int (0); }
2584         ;
2585
2586 bass_figure:
2587         FIGURE_SPACE {
2588                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
2589                 $$ = bfr->unprotect ();
2590         }
2591         | bass_number  {
2592                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
2593                 $$ = bfr->self_scm ();
2594
2595                 if (scm_is_number ($1))
2596                         bfr->set_property ("figure", $1);
2597                 else if (Text_interface::is_markup ($1))
2598                         bfr->set_property ("text", $1);
2599
2600                 bfr->unprotect ();
2601         }
2602         | bass_figure ']' {
2603                 $$ = $1;
2604                 unsmob_music ($1)->set_property ("bracket-stop", SCM_BOOL_T);
2605         }
2606         | bass_figure figured_bass_alteration {
2607                 Music *m = unsmob_music ($1);
2608                 if (scm_to_double ($2)) {
2609                         SCM salter = m->get_property ("alteration");
2610                         SCM alter = scm_is_number (salter) ? salter : scm_from_int (0);
2611                         m->set_property ("alteration",
2612                                          scm_sum (alter, $2));
2613                 } else {
2614                         m->set_property ("alteration", scm_from_int (0));
2615                 }
2616         }
2617         | bass_figure figured_bass_modification  {
2618                 Music *m = unsmob_music ($1);
2619                 if ($2 == ly_symbol2scm ("plus"))
2620                         {
2621                         m->set_property ("augmented", SCM_BOOL_T);
2622                         }
2623                 else if ($2 == ly_symbol2scm ("slash"))
2624                         {
2625                         m->set_property ("diminished", SCM_BOOL_T);
2626                         }
2627                 else if ($2 == ly_symbol2scm ("exclamation"))
2628                         {
2629                         m->set_property ("no-continuation", SCM_BOOL_T);
2630                         }
2631                 else if ($2 == ly_symbol2scm ("backslash"))
2632                         {
2633                         m->set_property ("augmented-slash", SCM_BOOL_T);
2634                         }
2635         }
2636         ;
2637
2638
2639 figured_bass_modification:
2640         E_PLUS          {
2641                 $$ = ly_symbol2scm ("plus");
2642         }
2643         | E_EXCLAMATION {
2644                 $$ = ly_symbol2scm ("exclamation");
2645         }
2646         | '/'           {
2647                 $$ = ly_symbol2scm ("slash");
2648         }
2649         | E_BACKSLASH {
2650                 $$ = ly_symbol2scm ("backslash");
2651         }
2652         ;
2653
2654 br_bass_figure:
2655         bass_figure {
2656                 $$ = $1;
2657         }
2658         | '[' bass_figure {
2659                 $$ = $2;
2660                 unsmob_music ($$)->set_property ("bracket-start", SCM_BOOL_T);
2661         }
2662         ;
2663
2664 figure_list:
2665         /**/            {
2666                 $$ = SCM_EOL;
2667         }
2668         | figure_list br_bass_figure {
2669                 $$ = scm_cons ($2, $1);
2670         }
2671         ;
2672
2673 figure_spec:
2674         FIGURE_OPEN figure_list FIGURE_CLOSE {
2675                 $$ = scm_reverse_x ($2, SCM_EOL);
2676         }
2677         ;
2678
2679
2680 optional_rest:
2681         /**/   { $$ = 0; }
2682         | REST { $$ = 1; }
2683         ;
2684
2685 simple_element:
2686         pitch exclamations questions octave_check optional_notemode_duration optional_rest {
2687                 if (!parser->lexer_->is_note_state ())
2688                         parser->parser_error (@1, _ ("have to be in Note mode for notes"));
2689
2690                 Music *n = 0;
2691                 if ($6)
2692                         n = MY_MAKE_MUSIC ("RestEvent", @$);
2693                 else
2694                         n = MY_MAKE_MUSIC ("NoteEvent", @$);
2695
2696                 n->set_property ("pitch", $1);
2697                 n->set_property ("duration", $5);
2698
2699                 if (scm_is_number ($4))
2700                 {
2701                         int q = scm_to_int ($4);
2702                         n->set_property ("absolute-octave", scm_from_int (q-1));
2703                 }
2704
2705                 if ($3 % 2)
2706                         n->set_property ("cautionary", SCM_BOOL_T);
2707                 if ($2 % 2 || $3 % 2)
2708                         n->set_property ("force-accidental", SCM_BOOL_T);
2709
2710                 $$ = n->unprotect ();
2711         }
2712         | DRUM_PITCH optional_notemode_duration {
2713                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2714                 n->set_property ("duration", $2);
2715                 n->set_property ("drum-type", $1);
2716
2717                 $$ = n->unprotect ();
2718         }
2719         | RESTNAME optional_notemode_duration           {
2720                 Music *ev = 0;
2721                 if (ly_scm2string ($1) == "s") {
2722                         /* Space */
2723                         ev = MY_MAKE_MUSIC ("SkipEvent", @$);
2724                   }
2725                 else {
2726                         ev = MY_MAKE_MUSIC ("RestEvent", @$);
2727
2728                     }
2729                 ev->set_property ("duration", $2);
2730                 $$ = ev->unprotect ();
2731         }
2732         ;
2733
2734 simple_chord_elements:
2735         new_chord {
2736                 if (!parser->lexer_->is_chord_state ())
2737                         parser->parser_error (@1, _ ("have to be in Chord mode for chords"));
2738                 $$ = $1;
2739         }
2740         | figure_spec optional_notemode_duration {
2741                 for (SCM s = $1; scm_is_pair (s); s = scm_cdr (s))
2742                 {
2743                         unsmob_music (scm_car (s))->set_property ("duration", $2);
2744                 }
2745                 $$ = $1;
2746         }
2747         ;
2748
2749 lyric_element:
2750         lyric_markup {
2751                 $$ = $1;
2752         }
2753         | LYRICS_STRING {
2754                 $$ = $1;
2755         }
2756         ;
2757
2758 lyric_element_arg:
2759         lyric_element
2760         | lyric_element multiplied_duration post_events {
2761                 $$ = MAKE_SYNTAX ("lyric-event", @$, $1, $2);
2762                 unsmob_music ($$)->set_property
2763                         ("articulations", scm_reverse_x ($3, SCM_EOL));
2764         }
2765         | lyric_element post_event post_events {
2766                 $$ = MAKE_SYNTAX ("lyric-event", @$, $1,
2767                                   parser->default_duration_.smobbed_copy ());
2768                 unsmob_music ($$)->set_property
2769                         ("articulations", scm_cons ($2, scm_reverse_x ($3, SCM_EOL)));
2770         }
2771         | LYRIC_ELEMENT optional_notemode_duration post_events {
2772                 $$ = MAKE_SYNTAX ("lyric-event", @$, $1, $2);
2773                 unsmob_music ($$)->set_property
2774                         ("articulations", scm_reverse_x ($3, SCM_EOL));
2775         }
2776         ;
2777
2778
2779 lyric_element_music:
2780         lyric_element optional_notemode_duration post_events {
2781                 $$ = MAKE_SYNTAX ("lyric-event", @$, $1, $2);
2782                 unsmob_music ($$)->set_property
2783                         ("articulations", scm_reverse_x ($3, SCM_EOL));
2784         }
2785         ;
2786
2787 new_chord:
2788         steno_tonic_pitch optional_notemode_duration   {
2789                 $$ = make_chord_elements ($1, $2, SCM_EOL);
2790         }
2791         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
2792                 SCM its = scm_reverse_x ($4, SCM_EOL);
2793                 $$ = make_chord_elements ($1, $2, scm_cons ($3, its));
2794         }
2795         ;
2796
2797 chord_items:
2798         /**/ {
2799                 $$ = SCM_EOL;
2800         }
2801         | chord_items chord_item {
2802                 $$ = scm_cons ($2, $$);
2803         }
2804         ;
2805
2806 chord_separator:
2807         CHORD_COLON {
2808                 $$ = ly_symbol2scm ("chord-colon");
2809         }
2810         | CHORD_CARET {
2811                 $$ = ly_symbol2scm ("chord-caret");
2812         }
2813         | CHORD_SLASH steno_tonic_pitch {
2814                 $$ = scm_list_2 (ly_symbol2scm ("chord-slash"), $2);
2815         }
2816         | CHORD_BASS steno_tonic_pitch {
2817                 $$ = scm_list_2 (ly_symbol2scm ("chord-bass"), $2);
2818         }
2819         ;
2820
2821 chord_item:
2822         chord_separator {
2823                 $$ = $1;
2824         }
2825         | step_numbers {
2826                 $$ = scm_reverse_x ($1, SCM_EOL);
2827         }
2828         | CHORD_MODIFIER  {
2829                 $$ = $1;
2830         }
2831         ;
2832
2833 step_numbers:
2834         step_number { $$ = scm_cons ($1, SCM_EOL); }
2835         | step_numbers '.' step_number {
2836                 $$ = scm_cons ($3, $$);
2837         }
2838         ;
2839
2840 step_number:
2841         bare_unsigned {
2842                 $$ = make_chord_step ($1, 0);
2843         }
2844         | bare_unsigned '+' {
2845                 $$ = make_chord_step ($1, SHARP_ALTERATION);
2846         }
2847         | bare_unsigned CHORD_MINUS {
2848                 $$ = make_chord_step ($1, FLAT_ALTERATION);
2849         }
2850         ;
2851
2852 tempo_range:
2853         bare_unsigned {
2854                 $$ = scm_from_int ($1);
2855         }
2856         | bare_unsigned '~' bare_unsigned {
2857                 $$ = scm_cons (scm_from_int ($1), scm_from_int ($3));
2858         }
2859         ;
2860
2861 /*
2862         UTILITIES
2863
2864 TODO: should deprecate in favor of Scheme?
2865
2866  */
2867 number_expression:
2868         number_expression '+' number_term {
2869                 $$ = scm_sum ($1, $3);
2870         }
2871         | number_expression '-' number_term {
2872                 $$ = scm_difference ($1, $3);
2873         }
2874         | number_term
2875         ;
2876
2877 number_term:
2878         number_factor {
2879                 $$ = $1;
2880         }
2881         | number_factor '*' number_factor {
2882                 $$ = scm_product ($1, $3);
2883         }
2884         | number_factor '/' number_factor {
2885                 $$ = scm_divide ($1, $3);
2886         }
2887         ;
2888
2889 number_factor:
2890         '-'  number_factor { /* %prec UNARY_MINUS */
2891                 $$ = scm_difference ($2, SCM_UNDEFINED);
2892         }
2893         | bare_number
2894         ;
2895
2896
2897 bare_number:
2898         bare_number_closed
2899         | UNSIGNED NUMBER_IDENTIFIER    {
2900                 $$ = scm_product ($1, $2);
2901         }
2902         | REAL NUMBER_IDENTIFIER        {
2903                 $$ = scm_product ($1, $2);
2904         }
2905         ;
2906
2907 bare_number_closed:
2908         UNSIGNED
2909         | REAL
2910         | NUMBER_IDENTIFIER
2911         ;
2912
2913 bare_unsigned:
2914         UNSIGNED {
2915                 $$ = scm_to_int ($1);
2916         }
2917         ;
2918
2919 unsigned_number:
2920         UNSIGNED
2921         | NUMBER_IDENTIFIER
2922         ;
2923
2924 exclamations:
2925                 { $$ = 0; }
2926         | exclamations '!'      { $$ ++; }
2927         ;
2928
2929 questions:
2930                 { $$ = 0; }
2931         | questions '?' { $$ ++; }
2932         ;
2933
2934 /*
2935 This should be done more dynamically if possible.
2936 */
2937
2938 lyric_markup:
2939         LYRIC_MARKUP_IDENTIFIER {
2940                 $$ = $1;
2941         }
2942         | LYRIC_MARKUP
2943                 { parser->lexer_->push_markup_state (); }
2944         markup_top {
2945                 $$ = $3;
2946                 parser->lexer_->pop_state ();
2947         }
2948         ;
2949
2950 full_markup_list:
2951         MARKUPLIST_IDENTIFIER {
2952                 $$ = $1;
2953         }
2954         | MARKUPLIST
2955                 { parser->lexer_->push_markup_state (); }
2956         markup_list {
2957                 $$ = $3;
2958                 parser->lexer_->pop_state ();
2959         }
2960         ;
2961
2962 full_markup:
2963         MARKUP_IDENTIFIER {
2964                 $$ = $1;
2965         }
2966         | MARKUP
2967                 { parser->lexer_->push_markup_state (); }
2968         markup_top {
2969                 $$ = $3;
2970                 parser->lexer_->pop_state ();
2971         }
2972         ;
2973
2974 markup_top:
2975         markup_list {
2976                 $$ = scm_list_2 (ly_lily_module_constant ("line-markup"),  $1);
2977         }
2978         | markup_head_1_list simple_markup      {
2979                 $$ = scm_car (scm_call_2 (ly_lily_module_constant ("map-markup-command-list"), $1, scm_list_1 ($2)));
2980         }
2981         | simple_markup {
2982                 $$ = $1;
2983         }
2984         ;
2985
2986 markup_scm:
2987         embedded_scm_bare
2988         {
2989                 if (Text_interface::is_markup ($1))
2990                         MYBACKUP (MARKUP_IDENTIFIER, $1, @1);
2991                 else if (Text_interface::is_markup_list ($1))
2992                         MYBACKUP (MARKUPLIST_IDENTIFIER, $1, @1);
2993                 else {
2994                         parser->parser_error (@1, _ ("not a markup"));
2995                         MYBACKUP (MARKUP_IDENTIFIER, scm_string (SCM_EOL), @1);
2996                 }
2997         } BACKUP
2998         ;
2999                         
3000
3001 markup_list:
3002         MARKUPLIST_IDENTIFIER {
3003                 $$ = $1;
3004         }
3005         | markup_composed_list {
3006                 $$ = $1;
3007         }
3008         | markup_braced_list {
3009                 $$ = $1;
3010         }
3011         | markup_command_list {
3012                 $$ = scm_list_1 ($1);
3013         }
3014         | markup_scm MARKUPLIST_IDENTIFIER
3015         {
3016                 $$ = $2;
3017         }
3018         ;
3019
3020 markup_composed_list:
3021         markup_head_1_list markup_braced_list {
3022                 $$ = scm_call_2 (ly_lily_module_constant ("map-markup-command-list"), $1, $2);
3023
3024         }
3025         ;
3026
3027 markup_braced_list:
3028         '{' markup_braced_list_body '}' {
3029                 $$ = scm_reverse_x ($2, SCM_EOL);
3030         }
3031         ;
3032
3033 markup_braced_list_body:
3034         /* empty */     {  $$ = SCM_EOL; }
3035         | markup_braced_list_body markup {
3036                 $$ = scm_cons ($2, $1);
3037         }
3038         | markup_braced_list_body markup_list {
3039                 $$ = scm_reverse_x ($2, $1);
3040         }
3041         ;
3042
3043 markup_command_list:
3044         MARKUP_LIST_FUNCTION markup_command_list_arguments {
3045           $$ = scm_cons ($1, scm_reverse_x($2, SCM_EOL));
3046         }
3047         ;
3048
3049 markup_command_basic_arguments:
3050         EXPECT_MARKUP_LIST markup_command_list_arguments markup_list {
3051           $$ = scm_cons ($3, $2);
3052         }
3053         | EXPECT_SCM markup_command_list_arguments embedded_scm_closed {
3054           $$ = check_scheme_arg (parser, @3, $3, $2, $1);
3055         }
3056         | EXPECT_NO_MORE_ARGS {
3057           $$ = SCM_EOL;
3058         }
3059         ;
3060
3061 markup_command_list_arguments:
3062         markup_command_basic_arguments { $$ = $1; }
3063         | EXPECT_MARKUP markup_command_list_arguments markup {
3064           $$ = scm_cons ($3, $2);
3065         }
3066         ;
3067
3068 markup_head_1_item:
3069         MARKUP_FUNCTION EXPECT_MARKUP markup_command_list_arguments {
3070           $$ = scm_cons ($1, scm_reverse_x ($3, SCM_EOL));
3071         }
3072         ;
3073
3074 markup_head_1_list:
3075         markup_head_1_item      {
3076                 $$ = scm_list_1 ($1);
3077         }
3078         | markup_head_1_list markup_head_1_item {
3079                 $$ = scm_cons ($2, $1);
3080         }
3081         ;
3082
3083 simple_markup:
3084         STRING {
3085                 $$ = make_simple_markup ($1);
3086         }
3087         | MARKUP_IDENTIFIER {
3088                 $$ = $1;
3089         }
3090         | LYRIC_MARKUP_IDENTIFIER {
3091                 $$ = $1;
3092         }
3093         | STRING_IDENTIFIER {
3094                 $$ = $1;
3095         }
3096         | SCORE {
3097                 SCM nn = parser->lexer_->lookup_identifier ("pitchnames");
3098                 parser->lexer_->push_note_state (alist_to_hashq (nn));
3099         } '{' score_body '}' {
3100                 Score * sc = $4;
3101                 $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), sc->self_scm ());
3102                 sc->unprotect ();
3103                 parser->lexer_->pop_state ();
3104         }
3105         | MARKUP_FUNCTION markup_command_basic_arguments {
3106                 $$ = scm_cons ($1, scm_reverse_x ($2, SCM_EOL));
3107         }
3108         | markup_scm MARKUP_IDENTIFIER
3109         {
3110                 $$ = $2;
3111         }
3112         ;
3113
3114 markup:
3115         markup_head_1_list simple_markup        {
3116                 SCM mapper = ly_lily_module_constant ("map-markup-command-list");
3117                 $$ = scm_car (scm_call_2 (mapper, $1, scm_list_1 ($2)));
3118         }
3119         | simple_markup {
3120                 $$ = $1;
3121         }
3122         ;
3123
3124 %%
3125
3126 void
3127 Lily_parser::set_yydebug (bool x)
3128 {
3129         yydebug = x;
3130 }
3131
3132 void
3133 Lily_parser::do_yyparse ()
3134 {
3135         yyparse (this);
3136 }
3137
3138
3139
3140
3141
3142 /*
3143
3144 It is a little strange to have this function in this file, but
3145 otherwise, we have to import music classes into the lexer.
3146
3147 */
3148 int
3149 Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
3150 {
3151         if (scm_is_string (sid)) {
3152                 *destination = sid;
3153                 return STRING_IDENTIFIER;
3154         } else if (unsmob_book (sid)) {
3155                 Book *book =  unsmob_book (sid)->clone ();
3156                 *destination = book->self_scm ();
3157                 book->unprotect ();
3158
3159                 return BOOK_IDENTIFIER;
3160         } else if (scm_is_number (sid)) {
3161                 *destination = sid;
3162                 return NUMBER_IDENTIFIER;
3163         } else if (unsmob_context_def (sid)) {
3164                 Context_def *def= unsmob_context_def (sid)->clone ();
3165
3166                 *destination = def->self_scm ();
3167                 def->unprotect ();
3168
3169                 return CONTEXT_DEF_IDENTIFIER;
3170         } else if (unsmob_context_mod (sid)) {
3171                 *destination = unsmob_context_mod (sid)->smobbed_copy ();
3172
3173                 return CONTEXT_MOD_IDENTIFIER;
3174         } else if (unsmob_score (sid)) {
3175                 Score *score = new Score (*unsmob_score (sid));
3176                 *destination = score->self_scm ();
3177
3178                 score->unprotect ();
3179                 return SCORE_IDENTIFIER;
3180         } else if (Music *mus = unsmob_music (sid)) {
3181                 mus = mus->clone ();
3182                 *destination = mus->self_scm ();
3183                 unsmob_music (*destination)->
3184                         set_property ("origin", make_input (last_input_));
3185
3186                 bool is_event = mus->is_mus_type ("post-event");
3187                 mus->unprotect ();
3188                 return is_event ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
3189         } else if (unsmob_pitch (sid)) {
3190                 *destination = unsmob_pitch (sid)->smobbed_copy ();
3191                 return PITCH_IDENTIFIER;
3192         } else if (unsmob_duration (sid)) {
3193                 *destination = unsmob_duration (sid)->smobbed_copy ();
3194                 return DURATION_IDENTIFIER;
3195         } else if (unsmob_output_def (sid)) {
3196                 Output_def *p = unsmob_output_def (sid);
3197                 p = p->clone ();
3198
3199                 *destination = p->self_scm ();
3200                 p->unprotect ();
3201                 return OUTPUT_DEF_IDENTIFIER;
3202         } else if (Text_interface::is_markup (sid)) {
3203                 *destination = sid;
3204                 if (is_lyric_state ())
3205                         return LYRIC_MARKUP_IDENTIFIER;
3206                 return MARKUP_IDENTIFIER;
3207         } else if (Text_interface::is_markup_list (sid)) {
3208                 *destination = sid;
3209                 return MARKUPLIST_IDENTIFIER;
3210         }
3211
3212         return -1;
3213 }
3214
3215 SCM
3216 get_next_unique_context_id ()
3217 {
3218         return scm_from_locale_string ("$uniqueContextId");
3219 }
3220
3221
3222 SCM
3223 get_next_unique_lyrics_context_id ()
3224 {
3225         static int new_context_count;
3226         char s[128];
3227         snprintf (s, sizeof (s)-1, "uniqueContext%d", new_context_count++);
3228         return scm_from_locale_string (s);
3229 }
3230
3231 SCM check_scheme_arg (Lily_parser *parser, Input loc,
3232                       SCM arg, SCM args, SCM pred)
3233 {
3234         args = scm_cons (arg, args);
3235         if (scm_is_true (scm_call_1 (pred, arg)))
3236                 return args;
3237         scm_set_cdr_x (scm_last_pair (args), SCM_EOL);
3238         MAKE_SYNTAX ("argument-error", loc, scm_length (args), pred, arg);
3239         scm_set_cdr_x (scm_last_pair (args), SCM_BOOL_F);
3240         return args;
3241 }
3242
3243 SCM loc_on_music (Input loc, SCM arg)
3244 {
3245         if (Music *m = unsmob_music (arg))
3246         {
3247                 m = m->clone ();
3248                 m->set_spot (loc);
3249                 return m->unprotect ();
3250         }
3251         return arg;
3252 }
3253
3254 bool
3255 is_regular_identifier (SCM id)
3256 {
3257   string str = ly_scm2string (id);
3258   char const *s = str.c_str ();
3259
3260   bool v = true;
3261 #if 0
3262   isalpha (*s);
3263   s++;
3264 #endif
3265   while (*s && v)
3266    {
3267         v = v && isalnum (*s);
3268         s++;
3269    }
3270   return v;
3271 }
3272
3273 Music *
3274 make_music_with_input (SCM name, Input where)
3275 {
3276        Music *m = make_music_by_name (name);
3277        m->set_spot (where);
3278        return m;
3279 }
3280
3281 SCM
3282 make_simple_markup (SCM a)
3283 {
3284         return a;
3285 }
3286
3287 bool
3288 is_duration (int t)
3289 {
3290   return t && t == 1 << intlog2 (t);
3291 }
3292
3293 void
3294 set_music_properties (Music *p, SCM a)
3295 {
3296   for (SCM k = a; scm_is_pair (k); k = scm_cdr (k))
3297         p->set_property (scm_caar (k), scm_cdar (k));
3298 }
3299
3300
3301 SCM
3302 make_chord_step (int step, Rational alter)
3303 {
3304         if (step == 7)
3305                 alter += FLAT_ALTERATION;
3306
3307         while (step < 0)
3308                 step += 7;
3309         Pitch m ((step -1) / 7, (step - 1) % 7, alter);
3310         return m.smobbed_copy ();
3311 }
3312
3313
3314 SCM
3315 make_chord_elements (SCM pitch, SCM dur, SCM modification_list)
3316 {
3317         SCM chord_ctor = ly_lily_module_constant ("construct-chord-elements");
3318         return scm_call_3 (chord_ctor, pitch, dur, modification_list);
3319 }
3320
3321 int
3322 yylex (YYSTYPE *s, YYLTYPE *loc, Lily_parser *parser)
3323 {
3324         Lily_lexer *lex = parser->lexer_;
3325
3326         lex->lexval_ = s;
3327         lex->lexloc_ = loc;
3328         lex->prepare_for_next_token ();
3329         return lex->yylex ();
3330 }