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