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