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