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