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