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