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