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