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