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