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