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