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