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