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