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