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