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