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