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