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