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