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