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