]> git.donarmstrong.com Git - lilypond.git/blob - lily/parser.yy
Fix post_events list order for chord repetitions.
[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 EXPECT_NO_MORE_ARGS
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, scm_reverse_x ($3, SCM_EOL));
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,
1489                                   scm_reverse_x ($3, SCM_EOL));
1490         }
1491         | command_element
1492         /* note chord elements are memorized into
1493            PARSER->lexer_->chord_repetition_ so that the chord repetition
1494            mechanism copy them when a chord repetition symbol is found
1495         */
1496         | note_chord_element    {
1497                 PARSER->lexer_->chord_repetition_.last_chord_ = $$;
1498         }
1499         ;
1500
1501
1502 note_chord_element:
1503         chord_body optional_notemode_duration post_events
1504         {
1505                 Music *m = unsmob_music ($1);
1506                 SCM dur = unsmob_duration ($2)->smobbed_copy ();
1507                 SCM es = m->get_property ("elements");
1508                 SCM postevs = scm_reverse_x ($3, SCM_EOL);
1509
1510                 for (SCM s = es; scm_is_pair (s); s = scm_cdr (s))
1511                   unsmob_music (scm_car (s))->set_property ("duration", dur);
1512                 es = ly_append2 (es, postevs);
1513
1514                 m-> set_property ("elements", es);
1515                 m->set_spot (@$);
1516                 $$ = m->self_scm ();
1517         }
1518         ;
1519
1520 chord_body:
1521         ANGLE_OPEN chord_body_elements ANGLE_CLOSE
1522         {
1523                 $$ = MAKE_SYNTAX ("event-chord", @$, scm_reverse_x ($2, SCM_EOL));
1524         }
1525         ;
1526
1527 chord_body_elements:
1528         /* empty */             { $$ = SCM_EOL; }
1529         | chord_body_elements chord_body_element {
1530                 $$ = scm_cons ($2, $1);
1531         }
1532         ;
1533
1534 chord_body_element:
1535         pitch exclamations questions octave_check post_events
1536         {
1537                 int q = $3;
1538                 int ex = $2;
1539                 SCM check = $4;
1540                 SCM post = $5;
1541
1542                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
1543                 n->set_property ("pitch", $1);
1544                 if (q % 2)
1545                         n->set_property ("cautionary", SCM_BOOL_T);
1546                 if (ex % 2 || q % 2)
1547                         n->set_property ("force-accidental", SCM_BOOL_T);
1548
1549                 if (scm_is_pair (post)) {
1550                         SCM arts = scm_reverse_x (post, SCM_EOL);
1551                         n->set_property ("articulations", arts);
1552                 }
1553                 if (scm_is_number (check))
1554                 {
1555                         int q = scm_to_int (check);
1556                         n->set_property ("absolute-octave", scm_from_int (q-1));
1557                 }
1558
1559                 $$ = n->unprotect ();
1560         }
1561         | DRUM_PITCH post_events {
1562                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
1563                 n->set_property ("duration", $2);
1564                 n->set_property ("drum-type", $1);
1565
1566                 if (scm_is_pair ($2)) {
1567                         SCM arts = scm_reverse_x ($2, SCM_EOL);
1568                         n->set_property ("articulations", arts);
1569                 }
1570                 $$ = n->unprotect ();
1571         }
1572         | music_function_chord_body {
1573                 $$ = run_music_function (PARSER, $1);
1574         }
1575         ;
1576
1577 music_function_identifier_musicless_prefix: MUSIC_FUNCTION {
1578                 SCM sig = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
1579                 if (scm_is_pair (sig) && to_boolean (scm_memq (ly_music_p_proc, scm_cdr (scm_reverse (sig)))))
1580                 {
1581                         PARSER->parser_error (@$, "Music function applied to event may not have a Music argument, except as the last argument.");
1582                 }
1583         }
1584         ;
1585
1586 music_function_chord_body:
1587         /* We could allow chord functions to have multiple music arguments,
1588            but it's more consistent with music_function_event if we
1589            prohibit it here too */
1590         music_function_identifier_musicless_prefix EXPECT_MUSIC function_arglist_nonmusic chord_body_element {
1591                 $$ = ly_append2 (scm_list_2 ($1, make_input (@$)), scm_reverse_x ($3, scm_list_1 ($4)));
1592         }
1593         | music_function_identifier_musicless_prefix function_arglist_nonmusic {
1594                 $$ = ly_append2 (scm_list_2 ($1, make_input (@$)), scm_reverse_x ($2, SCM_EOL));
1595         }
1596         ;
1597
1598 music_function_event:
1599         /* Post-events can only have the last argument as music, without this
1600            restriction we get a shift/reduce conflict from e.g.
1601            c8-\partcombine c8 -. */
1602         music_function_identifier_musicless_prefix EXPECT_MUSIC function_arglist_nonmusic post_event {
1603                 $$ = ly_append2 (scm_list_2 ($1, make_input (@$)), scm_reverse_x ($3, scm_list_1 ($4)));
1604         }
1605         | music_function_identifier_musicless_prefix function_arglist_nonmusic {
1606                 $$ = ly_append2 (scm_list_2 ($1, make_input (@$)), scm_reverse_x ($2, SCM_EOL));
1607         }
1608         ;
1609
1610 command_element:
1611         command_event {
1612                 $$ = $1;
1613         }
1614         | SKIP duration_length {
1615                 $$ = MAKE_SYNTAX ("skip-music", @$, $2);
1616         }
1617         | E_BRACKET_OPEN {
1618                 Music *m = MY_MAKE_MUSIC ("LigatureEvent", @$);
1619                 m->set_property ("span-direction", scm_from_int (START));
1620                 $$ = m->unprotect();
1621         }
1622         | E_BRACKET_CLOSE {
1623                 Music *m = MY_MAKE_MUSIC ("LigatureEvent", @$);
1624                 m->set_property ("span-direction", scm_from_int (STOP));
1625                 $$ = m->unprotect ();
1626         }
1627         | E_BACKSLASH {
1628                 $$ = MAKE_SYNTAX ("voice-separator", @$, SCM_UNDEFINED);
1629         }
1630         | '|'      {
1631                 SCM pipe = PARSER->lexer_->lookup_identifier ("pipeSymbol");
1632
1633                 Music *m = unsmob_music (pipe);
1634                 if (m)
1635                 {
1636                         m = m->clone ();
1637                         m->set_spot (@$);
1638                         $$ = m->unprotect ();
1639                 }
1640                 else
1641                         $$ = MAKE_SYNTAX ("bar-check", @$, SCM_UNDEFINED);
1642
1643         }
1644         | PARTIAL duration_length       {
1645                 Moment m = - unsmob_duration ($2)->get_length ();
1646                 $$ = MAKE_SYNTAX ("property-operation", @$, SCM_BOOL_F, ly_symbol2scm ("Timing"), ly_symbol2scm ("PropertySet"), ly_symbol2scm ("measurePosition"), m.smobbed_copy ());
1647                 $$ = MAKE_SYNTAX ("context-specification", @$, ly_symbol2scm ("Score"), SCM_BOOL_F, $$, SCM_EOL, SCM_BOOL_F);
1648         }
1649
1650         | TIME_T fraction  {
1651                 SCM proc = ly_lily_module_constant ("make-time-signature-set");
1652
1653                 $$ = scm_apply_2   (proc, scm_car ($2), scm_cdr ($2), SCM_EOL);
1654         }
1655         | MARK scalar {
1656                 SCM proc = ly_lily_module_constant ("make-mark-set");
1657
1658                 $$ = scm_call_1 (proc, $2);
1659         }
1660         ;
1661
1662 command_event:
1663         E_TILDE {
1664                 $$ = MY_MAKE_MUSIC ("PesOrFlexaEvent", @$)->unprotect ();
1665         }
1666         | MARK DEFAULT  {
1667                 Music *m = MY_MAKE_MUSIC ("MarkEvent", @$);
1668                 $$ = m->unprotect ();
1669         }
1670         | tempo_event {
1671                 $$ = $1;
1672         }
1673         | KEY DEFAULT {
1674                 Music *key = MY_MAKE_MUSIC ("KeyChangeEvent", @$);
1675                 $$ = key->unprotect ();
1676         }
1677         | KEY NOTENAME_PITCH SCM_IDENTIFIER     {
1678
1679                 Music *key = MY_MAKE_MUSIC ("KeyChangeEvent", @$);
1680                 if (scm_ilength ($3) > 0)
1681                 {
1682                         key->set_property ("pitch-alist", $3);
1683                         key->set_property ("tonic", Pitch (0, 0, 0).smobbed_copy ());
1684                         key->transpose (* unsmob_pitch ($2));
1685                 } else {
1686                         PARSER->parser_error (@3, _ ("second argument must be pitch list"));
1687                 }
1688
1689                 $$ = key->unprotect ();
1690         }
1691         ;
1692
1693
1694 post_events:
1695         /* empty */ {
1696                 $$ = SCM_EOL;
1697         }
1698         | post_events post_event {
1699                 unsmob_music ($2)->set_spot (@2);
1700                 $$ = scm_cons ($2, $$);
1701         }
1702         ;
1703
1704 post_event:
1705         direction_less_event {
1706                 $$ = $1;
1707         }
1708         | '-' music_function_event {
1709                 $$ = run_music_function (PARSER, $2);
1710         }
1711         | HYPHEN {
1712                 if (!PARSER->lexer_->is_lyric_state ())
1713                         PARSER->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
1714                 $$ = MY_MAKE_MUSIC ("HyphenEvent", @$)->unprotect ();
1715         }
1716         | EXTENDER {
1717                 if (!PARSER->lexer_->is_lyric_state ())
1718                         PARSER->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
1719                 $$ = MY_MAKE_MUSIC ("ExtenderEvent", @$)->unprotect ();
1720         }
1721         | script_dir direction_reqd_event {
1722                 if ($1)
1723                 {
1724                         Music *m = unsmob_music ($2);
1725                         m->set_property ("direction", scm_from_int ($1));
1726                 }
1727                 $$ = $2;
1728         }
1729         | script_dir direction_less_event {
1730                 if ($1)
1731                 {
1732                         Music *m = unsmob_music ($2);
1733                         m->set_property ("direction", scm_from_int ($1));
1734                 }
1735                 $$ = $2;
1736         }
1737         | string_number_event
1738         ;
1739
1740 string_number_event:
1741         E_UNSIGNED {
1742                 Music *s = MY_MAKE_MUSIC ("StringNumberEvent", @$);
1743                 s->set_property ("string-number", scm_from_int ($1));
1744                 $$ = s->unprotect ();
1745         }
1746         ;
1747
1748 direction_less_char:
1749         '['  {
1750                 $$ = ly_symbol2scm ("bracketOpenSymbol");
1751         }
1752         | ']'  {
1753                 $$ = ly_symbol2scm ("bracketCloseSymbol");
1754         }
1755         | '~'  {
1756                 $$ = ly_symbol2scm ("tildeSymbol");
1757         }
1758         | '('  {
1759                 $$ = ly_symbol2scm ("parenthesisOpenSymbol");
1760         }
1761         | ')'  {
1762                 $$ = ly_symbol2scm ("parenthesisCloseSymbol");
1763         }
1764         | E_EXCLAMATION  {
1765                 $$ = ly_symbol2scm ("escapedExclamationSymbol");
1766         }
1767         | E_OPEN  {
1768                 $$ = ly_symbol2scm ("escapedParenthesisOpenSymbol");
1769         }
1770         | E_CLOSE  {
1771                 $$ = ly_symbol2scm ("escapedParenthesisCloseSymbol");
1772         }
1773         | E_ANGLE_CLOSE  {
1774                 $$ = ly_symbol2scm ("escapedBiggerSymbol");
1775         }
1776         | E_ANGLE_OPEN  {
1777                 $$ = ly_symbol2scm ("escapedSmallerSymbol");
1778         }
1779         ;
1780
1781 direction_less_event:
1782         direction_less_char {
1783                 SCM predefd = PARSER->lexer_->lookup_identifier_symbol ($1);
1784                 Music *m = 0;
1785                 if (unsmob_music (predefd))
1786                 {
1787                         m = unsmob_music (predefd)->clone ();
1788                         m->set_spot (@$);
1789                 }
1790                 else
1791                 {
1792                         m = MY_MAKE_MUSIC ("Music", @$);
1793                 }
1794                 $$ = m->unprotect ();
1795         }
1796         | EVENT_IDENTIFIER      {
1797                 $$ = $1;
1798         }
1799         | tremolo_type  {
1800                Music *a = MY_MAKE_MUSIC ("TremoloEvent", @$);
1801                a->set_property ("tremolo-type", scm_from_int ($1));
1802                $$ = a->unprotect ();
1803         }
1804         ;
1805
1806 direction_reqd_event:
1807         gen_text_def {
1808                 $$ = $1;
1809         }
1810         | script_abbreviation {
1811                 SCM s = PARSER->lexer_->lookup_identifier ("dash" + ly_scm2string ($1));
1812                 Music *a = MY_MAKE_MUSIC ("ArticulationEvent", @$);
1813                 if (scm_is_string (s))
1814                         a->set_property ("articulation-type", s);
1815                 else PARSER->parser_error (@1, _ ("expecting string as script definition"));
1816                 $$ = a->unprotect ();
1817         }
1818         ;
1819
1820 octave_check:
1821         /**/ { $$ = SCM_EOL; }
1822         | '='  { $$ = scm_from_int (0); }
1823         | '=' sub_quotes { $$ = scm_from_int (-$2); }
1824         | '=' sup_quotes { $$ = scm_from_int ($2); }
1825         ;
1826
1827 sup_quotes:
1828         '\'' {
1829                 $$ = 1;
1830         }
1831         | sup_quotes '\'' {
1832                 $$ ++;
1833         }
1834         ;
1835
1836 sub_quotes:
1837         ',' {
1838                 $$ = 1;
1839         }
1840         | sub_quotes ',' {
1841                 $$++;
1842         }
1843         ;
1844
1845 steno_pitch:
1846         NOTENAME_PITCH  {
1847                 $$ = $1;
1848         }
1849         | NOTENAME_PITCH sup_quotes     {
1850                 Pitch p = *unsmob_pitch ($1);
1851                 p = p.transposed (Pitch ($2,0,0));
1852                 $$ = p.smobbed_copy ();
1853         }
1854         | NOTENAME_PITCH sub_quotes      {
1855                 Pitch p =* unsmob_pitch ($1);
1856                 p = p.transposed (Pitch (-$2,0,0));
1857                 $$ = p.smobbed_copy ();
1858         }
1859         ;
1860
1861 /*
1862 ugh. duplication
1863 */
1864
1865 steno_tonic_pitch:
1866         TONICNAME_PITCH {
1867                 $$ = $1;
1868         }
1869         | TONICNAME_PITCH sup_quotes    {
1870                 Pitch p = *unsmob_pitch ($1);
1871                 p = p.transposed (Pitch ($2,0,0));
1872                 $$ = p.smobbed_copy ();
1873         }
1874         | TONICNAME_PITCH sub_quotes     {
1875                 Pitch p = *unsmob_pitch ($1);
1876
1877                 p = p.transposed (Pitch (-$2,0,0));
1878                 $$ = p.smobbed_copy ();
1879         }
1880         ;
1881
1882 pitch:
1883         steno_pitch {
1884                 $$ = $1;
1885         }
1886         ;
1887
1888 pitch_also_in_chords:
1889         pitch
1890         | steno_tonic_pitch
1891         ;
1892
1893 gen_text_def:
1894         full_markup {
1895                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
1896                 t->set_property ("text", $1);
1897                 $$ = t->unprotect ();
1898         }
1899         | string {
1900                 Music *t = MY_MAKE_MUSIC ("TextScriptEvent", @$);
1901                 t->set_property ("text",
1902                         make_simple_markup ($1));
1903                 $$ = t->unprotect ();
1904         }
1905         | DIGIT {
1906                 Music *t = MY_MAKE_MUSIC ("FingeringEvent", @$);
1907                 t->set_property ("digit", scm_from_int ($1));
1908                 $$ = t->unprotect ();
1909         }
1910         ;
1911
1912 script_abbreviation:
1913         '^'             {
1914                 $$ = scm_from_locale_string ("Hat");
1915         }
1916         | '+'           {
1917                 $$ = scm_from_locale_string ("Plus");
1918         }
1919         | '-'           {
1920                 $$ = scm_from_locale_string ("Dash");
1921         }
1922         | '|'           {
1923                 $$ = scm_from_locale_string ("Bar");
1924         }
1925         | ANGLE_CLOSE   {
1926                 $$ = scm_from_locale_string ("Larger");
1927         }
1928         | '.'           {
1929                 $$ = scm_from_locale_string ("Dot");
1930         }
1931         | '_' {
1932                 $$ = scm_from_locale_string ("Underscore");
1933         }
1934         ;
1935
1936 script_dir:
1937         '_'     { $$ = DOWN; }
1938         | '^'   { $$ = UP; }
1939         | '-'   { $$ = CENTER; }
1940         ;
1941
1942
1943 absolute_pitch:
1944         steno_pitch     {
1945                 $$ = $1;
1946         }
1947         ;
1948
1949 duration_length:
1950         multiplied_duration {
1951                 $$ = $1;
1952         }
1953         ;
1954
1955 optional_notemode_duration:
1956         {
1957                 Duration dd = PARSER->default_duration_;
1958                 $$ = dd.smobbed_copy ();
1959         }
1960         | multiplied_duration   {
1961                 $$ = $1;
1962                 PARSER->default_duration_ = *unsmob_duration ($$);
1963         }
1964         ;
1965
1966 steno_duration:
1967         bare_unsigned dots              {
1968                 int len = 0;
1969                 if (!is_duration ($1))
1970                         PARSER->parser_error (@1, _f ("not a duration: %d", $1));
1971                 else
1972                         len = intlog2 ($1);
1973
1974                 $$ = Duration (len, $2).smobbed_copy ();
1975         }
1976         | DURATION_IDENTIFIER dots      {
1977                 Duration *d = unsmob_duration ($1);
1978                 Duration k (d->duration_log (), d->dot_count () + $2);
1979                 k = k.compressed (d->factor ());
1980                 *d = k;
1981                 $$ = $1;
1982         }
1983         ;
1984
1985 multiplied_duration:
1986         steno_duration {
1987                 $$ = $1;
1988         }
1989         | multiplied_duration '*' bare_unsigned {
1990                 $$ = unsmob_duration ($$)->compressed ( $3) .smobbed_copy ();
1991         }
1992         | multiplied_duration '*' FRACTION {
1993                 Rational  m (scm_to_int (scm_car ($3)), scm_to_int (scm_cdr ($3)));
1994
1995                 $$ = unsmob_duration ($$)->compressed (m).smobbed_copy ();
1996         }
1997         ;
1998
1999 fraction:
2000         FRACTION { $$ = $1; }
2001         | UNSIGNED '/' UNSIGNED {
2002                 $$ = scm_cons (scm_from_int ($1), scm_from_int ($3));
2003         }
2004         ;
2005
2006 dots:
2007         /* empty */     {
2008                 $$ = 0;
2009         }
2010         | dots '.' {
2011                 $$ ++;
2012         }
2013         ;
2014
2015 tremolo_type:
2016         ':'     {
2017                 $$ = 0;
2018         }
2019         | ':' bare_unsigned {
2020                 if (!is_duration ($2))
2021                         PARSER->parser_error (@2, _f ("not a duration: %d", $2));
2022                 $$ = $2;
2023         }
2024         ;
2025
2026 bass_number:
2027         DIGIT   {
2028                 $$ = scm_from_int ($1);
2029         }
2030         | UNSIGNED {
2031                 $$ = scm_from_int ($1);
2032         }
2033         | STRING { $$ = $1; }
2034         | full_markup { $$ = $1; }
2035         ;
2036
2037 figured_bass_alteration:
2038         '-'     { $$ = ly_rational2scm (FLAT_ALTERATION); }
2039         | '+'   { $$ = ly_rational2scm (SHARP_ALTERATION); }
2040         | '!'   { $$ = scm_from_int (0); }
2041         ;
2042
2043 bass_figure:
2044         FIGURE_SPACE {
2045                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
2046                 $$ = bfr->unprotect ();
2047         }
2048         | bass_number  {
2049                 Music *bfr = MY_MAKE_MUSIC ("BassFigureEvent", @$);
2050                 $$ = bfr->self_scm ();
2051
2052                 if (scm_is_number ($1))
2053                         bfr->set_property ("figure", $1);
2054                 else if (Text_interface::is_markup ($1))
2055                         bfr->set_property ("text", $1);
2056
2057                 bfr->unprotect ();
2058         }
2059         | bass_figure ']' {
2060                 $$ = $1;
2061                 unsmob_music ($1)->set_property ("bracket-stop", SCM_BOOL_T);
2062         }
2063         | bass_figure figured_bass_alteration {
2064                 Music *m = unsmob_music ($1);
2065                 if (scm_to_double ($2)) {
2066                         SCM salter = m->get_property ("alteration");
2067                         SCM alter = scm_is_number (salter) ? salter : scm_from_int (0);
2068                         m->set_property ("alteration",
2069                                          scm_sum (alter, $2));
2070                 } else {
2071                         m->set_property ("alteration", scm_from_int (0));
2072                 }
2073         }
2074         | bass_figure figured_bass_modification  {
2075                 Music *m = unsmob_music ($1);
2076                 if ($2 == ly_symbol2scm ("plus"))
2077                         {
2078                         m->set_property ("augmented", SCM_BOOL_T);
2079                         }
2080                 else if ($2 == ly_symbol2scm ("slash"))
2081                         {
2082                         m->set_property ("diminished", SCM_BOOL_T);
2083                         }
2084                 else if ($2 == ly_symbol2scm ("exclamation"))
2085                         {
2086                         m->set_property ("no-continuation", SCM_BOOL_T);
2087                         }
2088                 else if ($2 == ly_symbol2scm ("backslash"))
2089                         {
2090                         m->set_property ("augmented-slash", SCM_BOOL_T);
2091                         }
2092         }
2093         ;
2094
2095
2096 figured_bass_modification:
2097         E_PLUS          {
2098                 $$ = ly_symbol2scm ("plus");
2099         }
2100         | E_EXCLAMATION {
2101                 $$ = ly_symbol2scm ("exclamation");
2102         }
2103         | '/'           {
2104                 $$ = ly_symbol2scm ("slash");
2105         }
2106         | E_BACKSLASH {
2107                 $$ = ly_symbol2scm ("backslash");
2108         }
2109         ;
2110
2111 br_bass_figure:
2112         bass_figure {
2113                 $$ = $1;
2114         }
2115         | '[' bass_figure {
2116                 $$ = $2;
2117                 unsmob_music ($$)->set_property ("bracket-start", SCM_BOOL_T);
2118         }
2119         ;
2120
2121 figure_list:
2122         /**/            {
2123                 $$ = SCM_EOL;
2124         }
2125         | figure_list br_bass_figure {
2126                 $$ = scm_cons ($2, $1);
2127         }
2128         ;
2129
2130 figure_spec:
2131         FIGURE_OPEN figure_list FIGURE_CLOSE {
2132                 $$ = scm_reverse_x ($2, SCM_EOL);
2133         }
2134         ;
2135
2136
2137 optional_rest:
2138         /**/   { $$ = 0; }
2139         | REST { $$ = 1; }
2140         ;
2141
2142 simple_element:
2143         pitch exclamations questions octave_check optional_notemode_duration optional_rest {
2144                 if (!PARSER->lexer_->is_note_state ())
2145                         PARSER->parser_error (@1, _ ("have to be in Note mode for notes"));
2146
2147                 Music *n = 0;
2148                 if ($6)
2149                         n = MY_MAKE_MUSIC ("RestEvent", @$);
2150                 else
2151                         n = MY_MAKE_MUSIC ("NoteEvent", @$);
2152
2153                 n->set_property ("pitch", $1);
2154                 n->set_property ("duration", $5);
2155
2156                 if (scm_is_number ($4))
2157                 {
2158                         int q = scm_to_int ($4);
2159                         n->set_property ("absolute-octave", scm_from_int (q-1));
2160                 }
2161
2162                 if ($3 % 2)
2163                         n->set_property ("cautionary", SCM_BOOL_T);
2164                 if ($2 % 2 || $3 % 2)
2165                         n->set_property ("force-accidental", SCM_BOOL_T);
2166
2167                 $$ = n->unprotect ();
2168         }
2169         | DRUM_PITCH optional_notemode_duration {
2170                 Music *n = MY_MAKE_MUSIC ("NoteEvent", @$);
2171                 n->set_property ("duration", $2);
2172                 n->set_property ("drum-type", $1);
2173
2174                 $$ = n->unprotect ();
2175         }
2176         | RESTNAME optional_notemode_duration           {
2177                 Music *ev = 0;
2178                 if (ly_scm2string ($1) == "s") {
2179                         /* Space */
2180                         ev = MY_MAKE_MUSIC ("SkipEvent", @$);
2181                   }
2182                 else {
2183                         ev = MY_MAKE_MUSIC ("RestEvent", @$);
2184
2185                     }
2186                 ev->set_property ("duration", $2);
2187                 $$ = ev->unprotect ();
2188         }
2189         | lyric_element optional_notemode_duration      {
2190                 if (!PARSER->lexer_->is_lyric_state ())
2191                         PARSER->parser_error (@1, _ ("have to be in Lyric mode for lyrics"));
2192
2193                 Music *levent = MY_MAKE_MUSIC ("LyricEvent", @$);
2194                 levent->set_property ("text", $1);
2195                 levent->set_property ("duration",$2);
2196                 $$= levent->unprotect ();
2197         }
2198         ;
2199
2200 simple_chord_elements:
2201         simple_element  {
2202                 $$ = scm_list_1 ($1);
2203         }
2204         | new_chord {
2205                 if (!PARSER->lexer_->is_chord_state ())
2206                         PARSER->parser_error (@1, _ ("have to be in Chord mode for chords"));
2207                 $$ = $1;
2208         }
2209         | figure_spec optional_notemode_duration {
2210                 for (SCM s = $1; scm_is_pair (s); s = scm_cdr (s))
2211                 {
2212                         unsmob_music (scm_car (s))->set_property ("duration", $2);
2213                 }
2214                 $$ = $1;
2215         }
2216         ;
2217
2218 lyric_element:
2219         lyric_markup {
2220                 $$ = $1;
2221         }
2222         | LYRICS_STRING {
2223                 $$ = $1;
2224         }
2225         ;
2226
2227 new_chord:
2228         steno_tonic_pitch optional_notemode_duration   {
2229                 $$ = make_chord_elements ($1, $2, SCM_EOL);
2230         }
2231         | steno_tonic_pitch optional_notemode_duration chord_separator chord_items {
2232                 SCM its = scm_reverse_x ($4, SCM_EOL);
2233                 $$ = make_chord_elements ($1, $2, scm_cons ($3, its));
2234         }
2235         ;
2236
2237 chord_items:
2238         /**/ {
2239                 $$ = SCM_EOL;
2240         }
2241         | chord_items chord_item {
2242                 $$ = scm_cons ($2, $$);
2243         }
2244         ;
2245
2246 chord_separator:
2247         CHORD_COLON {
2248                 $$ = ly_symbol2scm ("chord-colon");
2249         }
2250         | CHORD_CARET {
2251                 $$ = ly_symbol2scm ("chord-caret");
2252         }
2253         | CHORD_SLASH steno_tonic_pitch {
2254                 $$ = scm_list_2 (ly_symbol2scm ("chord-slash"), $2);
2255         }
2256         | CHORD_BASS steno_tonic_pitch {
2257                 $$ = scm_list_2 (ly_symbol2scm ("chord-bass"), $2);
2258         }
2259         ;
2260
2261 chord_item:
2262         chord_separator {
2263                 $$ = $1;
2264         }
2265         | step_numbers {
2266                 $$ = scm_reverse_x ($1, SCM_EOL);
2267         }
2268         | CHORD_MODIFIER  {
2269                 $$ = $1;
2270         }
2271         ;
2272
2273 step_numbers:
2274         step_number { $$ = scm_cons ($1, SCM_EOL); }
2275         | step_numbers '.' step_number {
2276                 $$ = scm_cons ($3, $$);
2277         }
2278         ;
2279
2280 step_number:
2281         bare_unsigned {
2282                 $$ = make_chord_step ($1, 0);
2283         }
2284         | bare_unsigned '+' {
2285                 $$ = make_chord_step ($1, SHARP_ALTERATION);
2286         }
2287         | bare_unsigned CHORD_MINUS {
2288                 $$ = make_chord_step ($1, FLAT_ALTERATION);
2289         }
2290         ;
2291
2292 /*
2293         UTILITIES
2294
2295 TODO: should deprecate in favor of Scheme?
2296
2297  */
2298 number_expression:
2299         number_expression '+' number_term {
2300                 $$ = scm_sum ($1, $3);
2301         }
2302         | number_expression '-' number_term {
2303                 $$ = scm_difference ($1, $3);
2304         }
2305         | number_term
2306         ;
2307
2308 number_term:
2309         number_factor {
2310                 $$ = $1;
2311         }
2312         | number_factor '*' number_factor {
2313                 $$ = scm_product ($1, $3);
2314         }
2315         | number_factor '/' number_factor {
2316                 $$ = scm_divide ($1, $3);
2317         }
2318         ;
2319
2320 number_factor:
2321         '-'  number_factor { /* %prec UNARY_MINUS */
2322                 $$ = scm_difference ($2, SCM_UNDEFINED);
2323         }
2324         | bare_number
2325         ;
2326
2327
2328 bare_number:
2329         UNSIGNED        {
2330                 $$ = scm_from_int ($1);
2331         }
2332         | REAL          {
2333                 $$ = $1;
2334         }
2335         | NUMBER_IDENTIFIER             {
2336                 $$ = $1;
2337         }
2338         | REAL NUMBER_IDENTIFIER        {
2339                 $$ = scm_from_double (scm_to_double ($1) *scm_to_double ($2));
2340         }
2341         | UNSIGNED NUMBER_IDENTIFIER    {
2342                 $$ = scm_from_double ($1 *scm_to_double ($2));
2343         }
2344         ;
2345
2346
2347 bare_unsigned:
2348         UNSIGNED {
2349                         $$ = $1;
2350         }
2351         | DIGIT {
2352                 $$ = $1;
2353         }
2354         ;
2355
2356 unsigned_number:
2357         bare_unsigned  { $$ = scm_from_int ($1); }
2358         | NUMBER_IDENTIFIER {
2359                 $$ = $1;
2360         }
2361         ;
2362
2363
2364 exclamations:
2365                 { $$ = 0; }
2366         | exclamations '!'      { $$ ++; }
2367         ;
2368
2369 questions:
2370                 { $$ = 0; }
2371         | questions '?' { $$ ++; }
2372         ;
2373
2374 /*
2375 This should be done more dynamically if possible.
2376 */
2377
2378 lyric_markup:
2379         LYRIC_MARKUP_IDENTIFIER {
2380                 $$ = $1;
2381         }
2382         | LYRIC_MARKUP
2383                 { PARSER->lexer_->push_markup_state (); }
2384         markup_top {
2385                 $$ = $3;
2386                 PARSER->lexer_->pop_state ();
2387         }
2388         ;
2389
2390 full_markup_list:
2391         MARKUPLINES
2392                 { PARSER->lexer_->push_markup_state (); }
2393         markup_list {
2394                 $$ = $3;
2395                 PARSER->lexer_->pop_state ();
2396         }
2397         ;
2398
2399 full_markup:
2400         MARKUP_IDENTIFIER {
2401                 $$ = $1;
2402         }
2403         | MARKUP
2404                 { PARSER->lexer_->push_markup_state (); }
2405         markup_top {
2406                 $$ = $3;
2407                 PARSER->lexer_->pop_state ();
2408         }
2409         ;
2410
2411 markup_top:
2412         markup_list {
2413                 $$ = scm_list_2 (ly_lily_module_constant ("line-markup"),  $1);
2414         }
2415         | markup_head_1_list simple_markup      {
2416                 $$ = scm_car (scm_call_2 (ly_lily_module_constant ("map-markup-command-list"), $1, scm_list_1 ($2)));
2417         }
2418         | simple_markup {
2419                 $$ = $1;
2420         }
2421         ;
2422
2423 markup_list:
2424         markup_composed_list {
2425                 $$ = $1;
2426         }
2427         | markup_braced_list {
2428                 $$ = $1;
2429         }
2430         | markup_command_list {
2431                 $$ = scm_list_1 ($1);
2432         }
2433         ;
2434
2435 markup_composed_list:
2436         markup_head_1_list markup_braced_list {
2437                 $$ = scm_call_2 (ly_lily_module_constant ("map-markup-command-list"), $1, $2);
2438
2439         }
2440         ;
2441
2442 markup_braced_list:
2443         '{' markup_braced_list_body '}' {
2444                 $$ = scm_reverse_x ($2, SCM_EOL);
2445         }
2446         ;
2447
2448 markup_braced_list_body:
2449         /* empty */     {  $$ = SCM_EOL; }
2450         | markup_braced_list_body markup {
2451                 $$ = scm_cons ($2, $1);
2452         }
2453         | markup_braced_list_body markup_list {
2454                 $$ = scm_append_x (scm_list_2 (scm_reverse_x ($2, SCM_EOL), $1));
2455         }
2456         ;
2457
2458 markup_command_list:
2459         MARKUP_LIST_HEAD_EMPTY  {
2460                 $$ = scm_list_1 ($1);
2461         }
2462         | MARKUP_LIST_HEAD_LIST0 markup_list    {
2463                 $$ = scm_list_2 ($1, $2);
2464         }
2465         | MARKUP_LIST_HEAD_SCM0 embedded_scm    {
2466                 $$ = scm_list_2 ($1, $2);
2467         }
2468         | MARKUP_LIST_HEAD_SCM0_LIST1 embedded_scm markup_list  {
2469                 $$ = scm_list_3 ($1, $2, $3);
2470         }
2471         | MARKUP_LIST_HEAD_SCM0_SCM1_LIST2 embedded_scm embedded_scm markup_list        {
2472                 $$ = scm_list_4 ($1, $2, $3, $4);
2473         }
2474         ;
2475
2476 markup_head_1_item:
2477         MARKUP_HEAD_MARKUP0     {
2478                 $$ = scm_list_1 ($1);
2479         }
2480         | MARKUP_HEAD_SCM0_MARKUP1 embedded_scm {
2481                 $$ = scm_list_2 ($1, $2);
2482         }
2483         | MARKUP_HEAD_SCM0_SCM1_MARKUP2 embedded_scm embedded_scm       {
2484                 $$ = scm_list_3 ($1, $2, $3);
2485         }
2486         ;
2487
2488 markup_head_1_list:
2489         markup_head_1_item      {
2490                 $$ = scm_list_1 ($1);
2491         }
2492         | markup_head_1_list markup_head_1_item {
2493                 $$ = scm_cons ($2, $1);
2494         }
2495         ;
2496
2497 simple_markup:
2498         STRING {
2499                 $$ = make_simple_markup ($1);
2500         }
2501         | MARKUP_IDENTIFIER {
2502                 $$ = $1;
2503         }
2504         | LYRIC_MARKUP_IDENTIFIER {
2505                 $$ = $1;
2506         }
2507         | STRING_IDENTIFIER {
2508                 $$ = $1;
2509         }
2510         | SCORE {
2511                 SCM nn = PARSER->lexer_->lookup_identifier ("pitchnames");
2512                 PARSER->lexer_->push_note_state (alist_to_hashq (nn));
2513         } '{' score_body '}' {
2514                 Score * sc = $4;
2515                 $$ = scm_list_2 (ly_lily_module_constant ("score-markup"), sc->self_scm ());
2516                 sc->unprotect ();
2517                 PARSER->lexer_->pop_state ();
2518         }
2519         | MARKUP_HEAD_SCM0 embedded_scm {
2520                 $$ = scm_list_2 ($1, $2);
2521         }
2522         | MARKUP_HEAD_SCM0_SCM1_SCM2 embedded_scm embedded_scm embedded_scm {
2523                 $$ = scm_list_4 ($1, $2, $3, $4);
2524         }
2525         | MARKUP_HEAD_SCM0_SCM1 embedded_scm embedded_scm {
2526                 $$ = scm_list_3 ($1, $2, $3);
2527         }
2528         | MARKUP_HEAD_SCM0_MARKUP1_MARKUP2 embedded_scm markup markup {
2529                 $$ = scm_list_4 ($1, $2, $3, $4);
2530         }
2531         | MARKUP_HEAD_SCM0_SCM1_MARKUP2_MARKUP3 embedded_scm embedded_scm markup markup {
2532                 $$ = scm_list_5 ($1, $2, $3, $4, $5);
2533         }
2534         | MARKUP_HEAD_EMPTY {
2535                 $$ = scm_list_1 ($1);
2536         }
2537         | MARKUP_HEAD_LIST0 markup_list {
2538                 $$ = scm_list_2 ($1,$2);
2539         }
2540         | MARKUP_HEAD_MARKUP0_MARKUP1 markup markup {
2541                 $$ = scm_list_3 ($1, $2, $3);
2542         }
2543         ;
2544
2545 markup:
2546         markup_head_1_list simple_markup        {
2547                 SCM mapper = ly_lily_module_constant ("map-markup-command-list");
2548                 $$ = scm_car (scm_call_2 (mapper, $1, scm_list_1 ($2)));
2549         }
2550         | simple_markup {
2551                 $$ = $1;
2552         }
2553         ;
2554
2555 %%
2556
2557 void
2558 Lily_parser::set_yydebug (bool x)
2559 {
2560         yydebug = x;
2561 }
2562
2563 void
2564 Lily_parser::do_yyparse ()
2565 {
2566         yyparse ((void*)this);
2567 }
2568
2569
2570
2571
2572
2573 /*
2574
2575 It is a little strange to have this function in this file, but
2576 otherwise, we have to import music classes into the lexer.
2577
2578 */
2579 int
2580 Lily_lexer::try_special_identifiers (SCM *destination, SCM sid)
2581 {
2582         if (scm_is_string (sid)) {
2583                 *destination = sid;
2584                 return STRING_IDENTIFIER;
2585         } else if (unsmob_book (sid)) {
2586                 Book *book =  unsmob_book (sid)->clone ();
2587                 *destination = book->self_scm ();
2588                 book->unprotect ();
2589
2590                 return BOOK_IDENTIFIER;
2591         } else if (scm_is_number (sid)) {
2592                 *destination = sid;
2593                 return NUMBER_IDENTIFIER;
2594         } else if (unsmob_context_def (sid)) {
2595                 Context_def *def= unsmob_context_def (sid)->clone ();
2596
2597                 *destination = def->self_scm ();
2598                 def->unprotect ();
2599
2600                 return CONTEXT_DEF_IDENTIFIER;
2601         } else if (unsmob_context_mod (sid)) {
2602                 *destination = unsmob_context_mod (sid)->smobbed_copy ();
2603
2604                 return CONTEXT_MOD_IDENTIFIER;
2605         } else if (unsmob_score (sid)) {
2606                 Score *score = new Score (*unsmob_score (sid));
2607                 *destination = score->self_scm ();
2608
2609                 score->unprotect ();
2610                 return SCORE_IDENTIFIER;
2611         } else if (Music *mus = unsmob_music (sid)) {
2612                 mus = mus->clone ();
2613                 *destination = mus->self_scm ();
2614                 unsmob_music (*destination)->
2615                         set_property ("origin", make_input (last_input_));
2616
2617                 bool is_event = scm_memq (ly_symbol2scm ("event"), mus->get_property ("types"))
2618                         != SCM_BOOL_F;
2619
2620                 mus->unprotect ();
2621                 return is_event ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
2622         } else if (unsmob_duration (sid)) {
2623                 *destination = unsmob_duration (sid)->smobbed_copy ();
2624                 return DURATION_IDENTIFIER;
2625         } else if (unsmob_output_def (sid)) {
2626                 Output_def *p = unsmob_output_def (sid);
2627                 p = p->clone ();
2628
2629                 *destination = p->self_scm ();
2630                 p->unprotect ();
2631                 return OUTPUT_DEF_IDENTIFIER;
2632         } else if (Text_interface::is_markup (sid)) {
2633                 *destination = sid;
2634                 if (is_lyric_state ())
2635                         return LYRIC_MARKUP_IDENTIFIER;
2636                 return MARKUP_IDENTIFIER;
2637         }
2638
2639         return -1;
2640 }
2641
2642 SCM
2643 get_next_unique_context_id ()
2644 {
2645         return scm_from_locale_string ("$uniqueContextId");
2646 }
2647
2648
2649 SCM
2650 get_next_unique_lyrics_context_id ()
2651 {
2652         static int new_context_count;
2653         char s[128];
2654         snprintf (s, sizeof (s)-1, "uniqueContext%d", new_context_count++);
2655         return scm_from_locale_string (s);
2656 }
2657
2658
2659 SCM
2660 run_music_function (Lily_parser *parser, SCM expr)
2661 {
2662         SCM func = scm_car (expr);
2663         Input *loc = unsmob_input (scm_cadr (expr));
2664         SCM args = scm_cddr (expr);
2665         SCM sig = scm_object_property (func, ly_symbol2scm ("music-function-signature"));
2666
2667         SCM type_check_proc = ly_lily_module_constant ("type-check-list");
2668
2669         if (!to_boolean (scm_call_3  (type_check_proc, scm_cadr (expr), sig, args)))
2670         {
2671                 parser->error_level_ = 1;
2672                 return LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("void-music"), scm_list_2 (parser->self_scm (), make_input (*loc)));
2673         }
2674
2675         SCM syntax_args = scm_list_4 (parser->self_scm (), make_input (*loc), func, args);
2676         return LOWLEVEL_MAKE_SYNTAX (ly_lily_module_constant ("music-function"), syntax_args);
2677 }
2678
2679 bool
2680 is_regular_identifier (SCM id)
2681 {
2682   string str = ly_scm2string (id);
2683   char const *s = str.c_str ();
2684
2685   bool v = true;
2686 #if 0
2687   isalpha (*s);
2688   s++;
2689 #endif
2690   while (*s && v)
2691    {
2692         v = v && isalnum (*s);
2693         s++;
2694    }
2695   return v;
2696 }
2697
2698 Music *
2699 make_music_with_input (SCM name, Input where)
2700 {
2701        Music *m = make_music_by_name (name);
2702        m->set_spot (where);
2703        return m;
2704 }
2705
2706 SCM
2707 get_first_context_id (SCM type, Music *m)
2708 {
2709         SCM id = m->get_property ("context-id");
2710         if (SCM_BOOL_T == scm_equal_p (m->get_property ("context-type"), type)
2711             && scm_is_string (m->get_property ("context-id"))
2712             && scm_c_string_length (id) > 0)
2713         {
2714                 return id;
2715         }
2716         return SCM_EOL;
2717 }
2718
2719 SCM
2720 make_simple_markup (SCM a)
2721 {
2722         return a;
2723 }
2724
2725 bool
2726 is_duration (int t)
2727 {
2728   return t && t == 1 << intlog2 (t);
2729 }
2730
2731 void
2732 set_music_properties (Music *p, SCM a)
2733 {
2734   for (SCM k = a; scm_is_pair (k); k = scm_cdr (k))
2735         p->set_property (scm_caar (k), scm_cdar (k));
2736 }
2737
2738
2739 SCM
2740 make_chord_step (int step, Rational alter)
2741 {
2742         if (step == 7)
2743                 alter += FLAT_ALTERATION;
2744
2745         while (step < 0)
2746                 step += 7;
2747         Pitch m ((step -1) / 7, (step - 1) % 7, alter);
2748         return m.smobbed_copy ();
2749 }
2750
2751
2752 SCM
2753 make_chord_elements (SCM pitch, SCM dur, SCM modification_list)
2754 {
2755         SCM chord_ctor = ly_lily_module_constant ("construct-chord-elements");
2756         return scm_call_3 (chord_ctor, pitch, dur, modification_list);
2757 }
2758
2759
2760 /* Todo: actually also use apply iso. call too ...  */
2761 bool
2762 ly_input_procedure_p (SCM x)
2763 {
2764         return ly_is_procedure (x)
2765                 || (scm_is_pair (x) && ly_is_procedure (scm_car (x)));
2766 }
2767
2768 SCM
2769 make_music_relative (Pitch start, SCM music, Input loc)
2770 {
2771         Music *relative = MY_MAKE_MUSIC ("RelativeOctaveMusic", loc);
2772         relative->set_property ("element", music);
2773
2774         Music *m = unsmob_music (music);
2775         Pitch last = m->to_relative_octave (start);
2776         if (lily_1_8_relative)
2777                 m->set_property ("last-pitch", last.smobbed_copy ());
2778         return relative->unprotect ();
2779 }
2780
2781 int
2782 yylex (YYSTYPE *s, YYLTYPE *loc, void *v)
2783 {
2784         Lily_parser *pars = (Lily_parser*) v;
2785         Lily_lexer *lex = pars->lexer_;
2786
2787         lex->lexval_ = (void*) s;
2788         lex->lexloc_ = loc;
2789         lex->prepare_for_next_token ();
2790         return lex->yylex ();
2791 }