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