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