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