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