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