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