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