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