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