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