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